const mockDb = { registration: { findMany: jest.fn(), findUnique: jest.fn() }, eventOption: { findMany: jest.fn() }, attendee: { findMany: jest.fn(), updateMany: jest.fn() }, registrationOption: { findMany: jest.fn(), update: jest.fn() }, registrationOptionTranche: { deleteMany: jest.fn(), create: jest.fn() }, user: { findUnique: jest.fn().mockResolvedValue({ role: "user" }) }, $transaction: jest.fn(fn => fn(mockDb)), }; const mockLeft = jest.fn().mockResolvedValue(null); jest.mock("../src/utils/stock", () => ({ remainingFor: (...a) => mockLeft(...a) })); jest.mock("../src/utils/pricing", () => ({ resolveOptionPrice: jest.fn().mockResolvedValue({ price: 150, tierId: null }) })); const mockStatus = jest.fn(); jest.mock("../src/payments/webhook", () => ({ updateRegistrationStatus: (...a) => mockStatus(...a) })); jest.mock('../src/config/db', () => mockDb); jest.mock('../src/utils/adminAudit', () => ({ logAdminAction: jest.fn() })); const mockLicence = jest.fn().mockResolvedValue({ readOnly: false }); jest.mock('../src/hub/licence', () => ({ licenceStatus: (...a) => mockLicence(...a) })); const mockCancel = jest.fn(); const mockUpdate = jest.fn(); jest.mock('../src/controllers/registrationController', () => ({ cancelRegistration: (req, res) => mockCancel(req, res), updateRegistrationOptions: (req, res) => mockUpdate(req, res), })); const mockAssign = jest.fn(); jest.mock('../src/controllers/allocationController', () => ({ assign: (req, res) => mockAssign(req, res) })); const mockEmail = jest.fn(); jest.mock('../src/controllers/eventController', () => ({ emailEventAttendees: (req, res) => mockEmail(req, res), whatsappEventAttendees: jest.fn() })); const bulk = require('../src/utils/bulkRegistrations'); const { isBlocked } = require('../src/hub/readOnly'); const req = { user: { id: 'sup', role: 'supervisor' } }; const reg = (id, extra = {}) => ({ id, userId: `u-${id}`, eventId: 'ev1', status: 'pending', seasonPassPurchaseId: null, registrationOptions: [{ eventOptionId: 'adult', variantId: null, quantity: 1, sessionId: null }], ...extra }); beforeEach(() => jest.clearAllMocks()); test('the input is checked before anything runs', async () => { await expect(bulk.runBulk(req, { action: 'explode', registrationIds: ['a'] })).rejects.toThrow('Unknown'); await expect(bulk.runBulk(req, { action: 'cancel', registrationIds: [] })).rejects.toThrow('Tick at least one'); await expect(bulk.runBulk(req, { action: 'cancel', registrationIds: Array.from({ length: 501 }, (_, i) => `r${i}`) })).rejects.toThrow('At most 500'); }); test('cancelling runs the normal cancel for each; already-cancelled ones are skipped; a refusal is reported', async () => { mockDb.registration.findMany.mockResolvedValue([reg('a'), reg('b', { status: 'cancelled' }), reg('c')]); mockCancel.mockImplementation((r, res) => (r.params.id === 'c' ? res.status(403).json({ message: 'has payments' }) : res.json({ message: 'Registration cancelled' }))); const out = await bulk.runBulk(req, { action: 'cancel', registrationIds: ['a', 'b', 'c', 'gone'] }); expect(out).toMatchObject({ done: 1, skipped: 1, failed: 2 }); expect(out.results.find(r => r.id === 'c')).toMatchObject({ ok: false, message: 'has payments' }); expect(out.results.find(r => r.id === 'gone')).toMatchObject({ ok: false, message: 'Not found' }); expect(mockCancel).toHaveBeenCalledTimes(2); expect(mockCancel.mock.calls[0][0].user).toBe(req.user); }); test('moving keeps the same lines and tickets, repriced; checks places; not other events or season passes', async () => { mockDb.eventOption.findMany.mockResolvedValue([ { id: 'adult', eventId: 'ev1', name: 'Adult', event: { id: 'ev1', cashupStatus: 'open' } }, { id: 'vip', eventId: 'ev1', name: 'VIP', event: { id: 'ev1', cashupStatus: 'open' } }, ]); mockDb.registration.findMany.mockResolvedValue([reg('a'), reg('b', { eventId: 'ev2' }), reg('c', { seasonPassPurchaseId: 'p1' }), reg('d')]); mockDb.registrationOption.findMany.mockResolvedValue([{ id: 'line1', quantity: 1 }]); mockLeft.mockResolvedValueOnce(null).mockResolvedValueOnce(0); mockStatus.mockResolvedValue({ totalDue: 150, totalPaid: 100 }); const out = await bulk.runBulk(req, { action: 'moveOption', registrationIds: ['a', 'b', 'c', 'd'], fromOptionId: 'adult', toOptionId: 'vip' }); expect(out.results.find(r => r.id === 'a')).toMatchObject({ ok: true, message: 'Moved — R50.00 now owing' }); expect(mockDb.registrationOption.update).toHaveBeenCalledWith({ where: { id: 'line1' }, data: { eventOptionId: 'vip', variantId: null, priceSnapshot: 150, appliedTierId: null } }); expect(mockDb.attendee.updateMany).toHaveBeenCalledWith({ where: { registrationOptionId: 'line1' }, data: { eventOptionId: 'vip', variantId: null } }); expect(out.results.find(r => r.id === 'b').message).toBe('Not for this event'); expect(out.results.find(r => r.id === 'c').message).toMatch(/season pass/); expect(out.results.find(r => r.id === 'd')).toMatchObject({ ok: false, message: 'Only 0 places left on VIP' }); }); test('payments and ticket changes are refused in licence read-only mode; cancelling is not', async () => { mockLicence.mockResolvedValueOnce({ readOnly: true }); await expect(bulk.runBulk(req, { action: 'markPaid', registrationIds: ['a'], method: 'cash' })).rejects.toMatchObject({ statusCode: 403 }); expect(isBlocked('POST', '/api/v2/registrations/bulk')).toBe(false); }); test('allocating places each named person; a registration without names says so', async () => { mockDb.registration.findMany.mockResolvedValue([reg('a'), reg('b')]); mockDb.attendee.findMany.mockImplementation(({ where }) => Promise.resolve(where.registrationId === 'a' ? [{ id: 'p1', name: 'Amy' }, { id: 'p2', name: 'Ben' }] : [])); mockAssign.mockImplementation((r, res) => (r.body.attendeeId === 'p2' ? res.status(400).json({ message: 'Cabin 3 is full' }) : res.json({}))); const out = await bulk.runBulk(req, { action: 'allocate', registrationIds: ['a', 'b'], setId: 's1', groupId: 'g1' }); expect(out.results.find(r => r.id === 'a')).toMatchObject({ ok: false, message: 'Ben: Cabin 3 is full' }); expect(out.results.find(r => r.id === 'b').message).toMatch(/No named people/); }); test('messages go per event as an event update to exactly the ticked registrations', async () => { mockDb.registration.findMany.mockResolvedValue([reg('a'), reg('b'), reg('c', { eventId: 'ev2' })]); mockEmail.mockImplementation((r, res) => res.json({ sent: r.body.filter.registrationIds.length })); const out = await bulk.runBulk(req, { action: 'message', registrationIds: ['a', 'b', 'c'], channel: 'email', subject: 'Bus times', text: 'The bus leaves at 7:30.' }); expect(out.done).toBe(3); expect(mockEmail.mock.calls.map(c => [c[0].params.id, c[0].body.filter.registrationIds, c[0].body.kind])).toEqual([['ev1', ['a', 'b'], 'update'], ['ev2', ['c'], 'update']]); await expect(bulk.runBulk(req, { action: 'message', registrationIds: ['a'], channel: 'email', text: 'x' })).rejects.toThrow('subject'); });