Setting events up on API v2, pg-boss 11 (1.51.7)
- v2: /events/manage, create, PATCH, deactivate, copy-from, notify-recipients. - src/services/events.ts (from eventController.js, with the schedule helpers); v1 uses it. - fetchManagedEvents() for every picker; the event wizard writes through v2. - pg-boss 11 in its own schema (pgboss_v11); old pgboss schema left in place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
This commit is contained in:
@@ -25,7 +25,7 @@ jest.mock('../src/middleware/authMiddleware', () => {
|
||||
optionalAuth: (req: { user?: unknown }, _res: unknown, next: () => void) => { if (signedIn) req.user = signedIn; next(); },
|
||||
admin: (req: { user?: { role: string } }, res: { status(n: number): void }, next: (e?: Error) => void) => (req.user?.role === 'admin' ? next() : deny(403, 'Not authorized as an admin')(req, res, next as (e: Error) => void)),
|
||||
staff: (req: { user?: { role: string } }, res: { status(n: number): void }, next: (e?: Error) => void) => (['admin', 'supervisor', 'staff'].includes(req.user?.role || '') ? next() : deny(403, 'Not authorized as staff')(req, res, next as (e: Error) => void)),
|
||||
supervisor: jest.fn(),
|
||||
supervisor: (req: { user?: { role: string } }, res: { status(n: number): void }, next: (e?: Error) => void) => (['admin', 'supervisor'].includes(req.user?.role || '') ? next() : deny(403, 'Not authorized as a supervisor')(req, res, next as (e: Error) => void)),
|
||||
loginLimiter: (_req: unknown, _res: unknown, next: () => void) => next(),
|
||||
};
|
||||
});
|
||||
@@ -41,12 +41,17 @@ jest.mock('../src/services/auth', () => ({
|
||||
}));
|
||||
const mockCheckin = { previewScan: jest.fn(), scanTicket: jest.fn() };
|
||||
jest.mock('../src/services/checkin', () => ({ previewScan: (...a: unknown[]) => mockCheckin.previewScan(...a), scanTicket: (...a: unknown[]) => mockCheckin.scanTicket(...a) }));
|
||||
const mockScope = { scope: null as object | null, campusId: 'north' as string | null };
|
||||
const mockScope = { scope: null as object | null, campusId: 'north' as string | null, blockEvent: false };
|
||||
jest.mock('../src/utils/campusScope', () => ({
|
||||
scopeFor: async () => mockScope.scope,
|
||||
eventOf: async () => ({ campusId: mockScope.campusId, campusName: 'North' }),
|
||||
canView: (scope: { campusIds: string[] }, campusId: string | null) => !campusId || scope.campusIds.includes(campusId),
|
||||
// Route-level checks: refuse like v1 does (v1 shape — the v2 wrapper isn't in this test app).
|
||||
scopeEvent: () => (_req: unknown, res: { status(n: number): { json(b: unknown): void } }, next: () => void) => (mockScope.blockEvent ? res.status(404).json({ message: 'Not found' }) : next()),
|
||||
scopeBody: () => (_req: unknown, _res: unknown, next: () => void) => next(),
|
||||
}));
|
||||
const mockEvents = { listManagedEvents: jest.fn(), createEvent: jest.fn(), updateEvent: jest.fn(), deactivateEvent: jest.fn(), getNotifyRecipients: jest.fn(), setNotifyRecipients: jest.fn(), copySetupFrom: jest.fn() };
|
||||
jest.mock('../src/services/events', () => Object.fromEntries(Object.keys(mockEvents).map(k => [k, (...a: unknown[]) => (mockEvents as Record<string, jest.Mock>)[k](...a)])));
|
||||
|
||||
import { v2Router } from '../src/v2';
|
||||
import { ServiceError } from '../src/services/errors';
|
||||
@@ -343,7 +348,8 @@ describe('v1 deprecation headers', () => {
|
||||
test('v1 endpoints with a v2 replacement point at it; the rest say nothing', () => {
|
||||
expect(successorOf('GET', '/api/events')).toBe('/api/v2/events');
|
||||
expect(successorOf('GET', '/api/events/e1')).toBe('/api/v2/events/e1');
|
||||
expect(successorOf('GET', '/api/events/all')).toBeNull();
|
||||
expect(successorOf('GET', '/api/events/all')).toBe('/api/v2/events/manage');
|
||||
expect(successorOf('PUT', '/api/events/e1')).toBe('/api/v2/events/e1');
|
||||
expect(successorOf('DELETE', '/api/registrations/r1')).toBe('/api/v2/registrations/r1/cancel');
|
||||
expect(successorOf('POST', '/api/tickets/scan/QR1')).toBe('/api/v2/checkin/QR1');
|
||||
expect(successorOf('GET', '/api/reports/summary')).toBeNull();
|
||||
@@ -367,3 +373,60 @@ describe('shared middleware on v2', () => {
|
||||
expect((await request(mini).get('/api/v2/fine')).body).toEqual({ data: { message: 'not an error' } });
|
||||
});
|
||||
});
|
||||
|
||||
describe('events (staff)', () => {
|
||||
const stored = (over: Record<string, unknown> = {}) => ({
|
||||
id: 'e1', title: 'Camp', description: null, startDate: new Date('2026-12-04T16:00:00Z'), endDate: new Date('2026-12-06T12:00:00Z'),
|
||||
isActive: true, isHidden: true, requiresRegistration: true, scheduleType: 'single', attendanceMode: 'whole', cashupStatus: 'open',
|
||||
campusId: null, seriesId: null, picture: '/uploads/events/c.jpg', waitlistEnabled: true,
|
||||
eventOptions: [{ id: 'o1', name: 'Main Ticket', price: 600, isMainTicket: true, stockLimit: 0, earlyBirdTiers: [] }], sessions: [], ...over,
|
||||
});
|
||||
afterEach(() => { mockScope.blockEvent = false; });
|
||||
|
||||
test('/events/manage lists hidden events for staff, with the rest of each record passed through', async () => {
|
||||
signedIn = { id: 's1', role: 'staff' };
|
||||
mockEvents.listManagedEvents.mockResolvedValue([stored()]);
|
||||
const res = await request(app).get('/api/v2/events/manage?past=true');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.data[0]).toMatchObject({ id: 'e1', isHidden: true, waitlistEnabled: true, startDate: '2026-12-04T16:00:00.000Z' });
|
||||
expect(res.body.data[0].picture).toMatch(/^http:\/\/.*\/uploads\/events\/c\.jpg$/);
|
||||
expect(mockEvents.listManagedEvents).toHaveBeenCalledWith({ includePast: true, includeInactive: false, excludeClosed: false, campus: undefined }, expect.objectContaining({ scope: null }));
|
||||
});
|
||||
|
||||
test('members can\'t list or create', async () => {
|
||||
signedIn = { id: 'u1', role: 'user' };
|
||||
expect((await request(app).get('/api/v2/events/manage')).body.error.code).toBe('FORBIDDEN');
|
||||
signedIn = { id: 's1', role: 'staff' };
|
||||
expect((await request(app).post('/api/v2/events').send({ title: 'X', startDate: '2026-12-01', endDate: '2026-12-02' })).body.error.code).toBe('FORBIDDEN');
|
||||
});
|
||||
|
||||
test('create needs a title and dates; update passes only what was sent', async () => {
|
||||
signedIn = { id: 'v1', role: 'supervisor' };
|
||||
const bad = await request(app).post('/api/v2/events').send({ title: '' });
|
||||
expect(Object.keys(bad.body.error.fields).sort()).toEqual(['endDate', 'startDate', 'title']);
|
||||
mockEvents.createEvent.mockResolvedValue(stored());
|
||||
const made = await request(app).post('/api/v2/events').send({ title: 'Camp', startDate: '2026-12-04T16:00', endDate: '2026-12-06T12:00', waitlistEnabled: true });
|
||||
expect(made.status).toBe(201);
|
||||
expect(mockEvents.createEvent.mock.calls[0][0]).toMatchObject({ title: 'Camp', waitlistEnabled: true });
|
||||
|
||||
mockEvents.updateEvent.mockResolvedValue({ ...stored({ title: 'New' }), sessionChanges: null });
|
||||
const upd = await request(app).patch('/api/v2/events/e1').send({ title: 'New' });
|
||||
expect(upd.body.data).toMatchObject({ title: 'New', sessionChanges: null });
|
||||
expect(mockEvents.updateEvent).toHaveBeenCalledWith('e1', { title: 'New' }, { id: 'v1', role: 'supervisor' }, expect.any(Object));
|
||||
});
|
||||
|
||||
test("a campus-scoped supervisor can't change another campus's event", async () => {
|
||||
signedIn = { id: 'v1', role: 'supervisor' };
|
||||
mockScope.blockEvent = true;
|
||||
expect((await request(app).patch('/api/v2/events/e1').send({ title: 'X' })).status).toBe(404);
|
||||
expect(mockEvents.updateEvent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('deactivating is for admins', async () => {
|
||||
signedIn = { id: 'v1', role: 'supervisor' };
|
||||
expect((await request(app).delete('/api/v2/events/e1')).status).toBe(403);
|
||||
signedIn = { id: 'a1', role: 'admin' };
|
||||
mockEvents.deactivateEvent.mockResolvedValue({ id: 'e1', isActive: false });
|
||||
expect((await request(app).delete('/api/v2/events/e1')).body.data).toEqual({ id: 'e1', isActive: false });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user