/** * Money on v2 (1.51.11): payments and refunds, payment links and providers, cash-ups, provider * transactions, the waitlist, sponsorship, consent, attendee form answers and feedback. Each * route runs the v1 handler (viaV1.ts) behind the same access and campus checks as v1. */ import { z } from 'zod'; import { id, idParams } from '../schemas'; import { listData, v1Route } from '../viaV1'; /* eslint-disable @typescript-eslint/no-explicit-any -- v1 handlers and middleware are JavaScript */ const rateLimit = require('express-rate-limit') as (opts: object) => any; const cs = require('../../utils/campusScope') as { scopeEvent(opts?: { need?: 'view' | 'manage'; resolve?: string; from?: (req: any) => unknown; ownerOk?: boolean }): any; scopeBody(targets: [string, string][], opts?: { need?: 'view' | 'manage'; orphan?: 'pass' | 'deny' }): any; scopeList: any; unscopedOnly: any; }; const pay = require('../../controllers/paymentController'); const providers = require('../../controllers/paymentProvidersController'); const cashup = require('../../controllers/cashupController'); const ptx = require('../../controllers/providerTransactionsController'); const waitlist = require('../../controllers/waitlistController'); const sponsor = require('../../controllers/sponsorshipController'); const consent = require('../../controllers/consentController'); const forms = require('../../controllers/formController'); const feedback = require('../../controllers/feedbackController'); /** Optional text filters, passed to v1 as they come. */ const filters = (...names: string[]) => z.looseObject(Object.fromEntries(names.map(n => [n, z.string().max(4000).optional()]))); const paymentFilters = filters('page', 'limit', 'userId', 'from', 'donations', 'method', 'kind', 'startDate', 'endDate'); const tokenParams = z.object({ token: z.string().min(1).max(200) }); const asEvent = (ctx: any) => ({ params: { ...ctx.req.params, eventId: ctx.params.id } }); // Links in messages are credentials, so guessing stays slow (as on v1). const linkLimiter = () => rateLimit({ windowMs: 60 * 1000, max: 30, message: { message: 'Too many attempts. Please try again in a minute.' } }); const SCOPE_LIST = 'scopeList (the handler filters by listWhere)'; const UNSCOPED = 'unscopedOnly (admins and unscoped supervisors)'; // ─── Payments ──────────────────────────────────────────────────────────────── v1Route({ method: 'get', path: '/payments/providers', tag: 'Payments', summary: 'The payment providers switched on', access: 'public', v1: providers.listPaymentProviders, }); v1Route({ method: 'get', path: '/payments/providers/settings', tag: 'Payments', summary: 'Payment provider settings', description: 'Admins. Keys are masked.', access: 'admin', v1: providers.getPaymentProviderSettings, }); v1Route({ method: 'put', path: '/payments/providers/settings', tag: 'Payments', summary: 'Change payment provider settings', access: 'admin', body: z.looseObject({}), v1: providers.updatePaymentProviderSettings, }); v1Route({ method: 'get', path: '/payments/open-checkouts', tag: 'Payments', summary: 'Checkouts started but not finished', access: 'supervisor', campusScope: UNSCOPED, middleware: [cs.unscopedOnly], v1: providers.listOpenCheckouts, }); v1Route({ method: 'post', path: '/payments/open-checkouts/check', tag: 'Payments', summary: 'Ask the provider about open checkouts', description: 'Records any that were paid (for when a webhook was missed).', access: 'supervisor', campusScope: UNSCOPED, middleware: [cs.unscopedOnly], body: z.looseObject({}), v1: providers.checkCheckoutStatus, }); v1Route({ method: 'get', path: '/payments/stats', tag: 'Payments', summary: 'Payment totals', access: 'admin', query: paymentFilters, v1: pay.getPaymentStats, }); v1Route({ method: 'get', path: '/payments', tag: 'Payments', summary: 'Every payment', description: 'For the events you may see. `page` and `limit` page it (otherwise everything); see v1 for the filters it takes.', access: 'supervisor', campusScope: SCOPE_LIST, middleware: [cs.scopeList], query: paymentFilters, v1: pay.getPayments, }); v1Route({ method: 'post', path: '/payments', tag: 'Payments', summary: 'Record a payment', description: 'Cash, card, EFT or other, against a registration or (a donation) an event. Paid in full issues and sends the tickets.', access: 'supervisor', campusScope: 'scopeBody(registrationId, eventId; deny orphans)', status: 201, middleware: [cs.scopeBody([['registrationId', 'registration'], ['eventId', 'event']], { orphan: 'deny' })], body: z.looseObject({ registrationId: id.nullish(), eventId: id.nullish(), amount: z.union([z.number(), z.string()]), method: z.string().max(20).optional() }), v1: pay.createPayment, }); v1Route({ method: 'post', path: '/payments/refunds', tag: 'Payments', summary: 'Refund a payment', access: 'supervisor', campusScope: 'scopeBody(paymentId, registrationId; manage, deny orphans)', status: 201, middleware: [cs.scopeBody([['paymentId', 'payment'], ['registrationId', 'registration']], { need: 'manage', orphan: 'deny' })], body: z.looseObject({ paymentId: id.optional(), registrationId: id.optional() }), v1: pay.createRefund, }); v1Route({ method: 'post', path: '/payments/donations/assign', tag: 'Payments', summary: 'Put a donation towards a registration', access: 'supervisor', campusScope: 'scopeBody(paymentId deny orphans; registrationId)', middleware: [cs.scopeBody([['paymentId', 'payment']], { orphan: 'deny' }), cs.scopeBody([['registrationId', 'registration']])], body: z.looseObject({ paymentId: id, registrationId: id }), v1: pay.assignDonationToRegistration, }); v1Route({ method: 'post', path: '/payments/donations/unassign', tag: 'Payments', summary: 'Take a donation back off a registration', access: 'supervisor', campusScope: 'scopeBody(legId; deny orphans)', middleware: [cs.scopeBody([['legId', 'payment']], { orphan: 'deny' })], body: z.looseObject({ legId: id }), v1: pay.unassignDonationFromRegistration, }); v1Route({ method: 'post', path: '/donations/checkout', tag: 'Payments', summary: 'Give to an event', description: 'Starts a checkout for a donation of `amount` to `eventId`; answers where to send the person (`redirectUrl`).', access: 'user', body: z.looseObject({ eventId: id, amount: z.union([z.number(), z.string()]) }), v1: pay.createCheckout, }); v1Route({ method: 'get', path: '/me/payments', tag: 'Me', summary: 'My payments', access: 'user', query: paymentFilters, v1: pay.getUserPayments, }); v1Route({ method: 'get', path: '/payments/:id', tag: 'Payments', summary: 'One payment', access: 'user', campusScope: 'scopeEvent(payment, owner-ok)', middleware: [cs.scopeEvent({ resolve: 'payment', ownerOk: true })], params: idParams, v1: pay.getPaymentById, }); v1Route({ method: 'post', path: '/payments/:id/receipt', tag: 'Payments', summary: 'Send the receipt again', access: 'user', campusScope: 'scopeEvent(payment, owner-ok)', middleware: [cs.scopeEvent({ resolve: 'payment', ownerOk: true })], params: idParams, body: z.looseObject({ channel: z.string().max(20).optional() }), v1: pay.sendReceipt, }); v1Route({ method: 'get', path: '/registrations/:id/payments', tag: 'Payments', summary: 'A registration\'s payments', access: 'user', campusScope: 'scopeEvent(registration, owner-ok)', middleware: [cs.scopeEvent({ resolve: 'registration', ownerOk: true })], params: idParams, response: listData, map: ctx => ({ params: { registrationId: ctx.params.id } }), v1: pay.getPaymentsByRegistration, }); v1Route({ method: 'post', path: '/registrations/:id/payment-link', tag: 'Payments', summary: 'Make a link to pay', description: 'Supervisors: a checkout for `amount` (part of what\'s owed) for someone else to pay. Answers `redirectUrl` (the link) — or `priceUpdated` when an early-bird price ran out.', access: 'supervisor', campusScope: 'scopeEvent(registration)', middleware: [cs.scopeEvent({ resolve: 'registration' })], params: idParams, body: z.looseObject({ amount: z.union([z.number(), z.string()]).optional() }), map: ctx => ({ body: { ...(ctx.req.body || {}), registrationId: ctx.params.id } }), v1: pay.createCheckout, }); v1Route({ method: 'post', path: '/registrations/:id/payment-link/send', tag: 'Payments', summary: 'Send someone a link to pay', description: 'By email and/or WhatsApp, for what\'s still owing.', access: 'supervisor', campusScope: 'scopeEvent(registration)', middleware: [cs.scopeEvent({ resolve: 'registration' })], params: idParams, body: z.looseObject({}), map: ctx => ({ body: { ...(ctx.req.body || {}), registrationId: ctx.params.id } }), v1: pay.sendPaymentLink, }); v1Route({ method: 'get', path: '/events/:id/payments', tag: 'Payments', summary: 'An event\'s payments', access: 'staff', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, map: asEvent, v1: pay.getPaymentsByEvent, }); v1Route({ method: 'get', path: '/payment-links/:id', tag: 'Payments', summary: 'A pay page', description: 'What a payment link shows: what\'s owed and the providers to pay with. The id in the link is the credential.', access: 'public', params: idParams, v1: providers.getPayPage, }); v1Route({ method: 'post', path: '/payment-links/:id', tag: 'Payments', summary: 'Pay from a pay page', description: 'Starts a checkout with the chosen provider; answers where to send the person.', access: 'public', params: idParams, body: z.looseObject({ provider: z.string().max(40).optional() }), v1: providers.choosePayProvider, }); // ─── Cash-ups ──────────────────────────────────────────────────────────────── const cashupBase = '/events/:id/cashup'; v1Route({ method: 'get', path: '/cashups/audit', tag: 'Cash-ups', summary: 'Cash-up history', access: 'supervisor', campusScope: SCOPE_LIST, middleware: [cs.scopeList], query: filters('eventId', 'from', 'to'), v1: cashup.getCashupAudit, }); v1Route({ method: 'get', path: cashupBase, tag: 'Cash-ups', summary: 'An event\'s cash-up', description: 'Income by method, costs, what each drawer should hold, and the saved draft.', access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [cs.scopeEvent({ need: 'manage' })], params: idParams, map: asEvent, v1: cashup.getEventCashup, }); v1Route({ method: 'get', path: `${cashupBase}/cash-by-user`, tag: 'Cash-ups', summary: 'Cash taken by each person', access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [cs.scopeEvent({ need: 'manage' })], params: idParams, map: asEvent, v1: cashup.getCashByRecordedUser, }); v1Route({ method: 'put', path: `${cashupBase}/person-cash/:userId`, tag: 'Cash-ups', summary: 'Count one person\'s cash', access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [cs.scopeEvent({ need: 'manage' })], params: z.object({ id, userId: id }), body: z.looseObject({}), map: ctx => ({ params: { eventId: ctx.params.id, userId: ctx.params.userId } }), v1: cashup.savePersonCash, }); v1Route({ method: 'put', path: `${cashupBase}/draft`, tag: 'Cash-ups', summary: 'Save the cash-up as a draft', access: 'admin', params: idParams, body: z.looseObject({}), map: asEvent, v1: cashup.saveEventCashupDraft, }); v1Route({ method: 'post', path: `${cashupBase}/close`, tag: 'Cash-ups', summary: 'Close the event', description: 'Admins. Nothing about the event\'s money can change until it\'s reopened.', access: 'admin', params: idParams, body: z.looseObject({}), map: asEvent, v1: cashup.closeEvent, }); v1Route({ method: 'post', path: `${cashupBase}/reopen`, tag: 'Cash-ups', summary: 'Reopen a closed event', access: 'admin', params: idParams, body: z.looseObject({}), map: asEvent, v1: cashup.reopenEvent, }); v1Route({ method: 'post', path: '/cashup-lines/:id/pending', tag: 'Cash-ups', summary: 'Settle a pending cash-up line', access: 'admin', params: idParams, body: z.looseObject({}), map: ctx => ({ params: { lineId: ctx.params.id } }), v1: cashup.resolvePending, }); // ─── Provider transactions ─────────────────────────────────────────────────── v1Route({ method: 'get', path: '/provider-transactions', tag: 'Payments', summary: 'Transactions from Yoco and PayFast', access: 'supervisor', campusScope: UNSCOPED, middleware: [cs.unscopedOnly], query: filters('provider'), v1: ptx.getAllProviderTransactions, }); v1Route({ method: 'get', path: '/provider-transactions/unreconciled', tag: 'Payments', summary: 'Provider transactions not matched yet', access: 'supervisor', campusScope: UNSCOPED, middleware: [cs.unscopedOnly], v1: ptx.getUnreconciledProviderTransactions, }); v1Route({ method: 'post', path: '/provider-transactions/:id/reconcile', tag: 'Payments', summary: 'Match a provider transaction to a registration', access: 'supervisor', campusScope: UNSCOPED, middleware: [cs.unscopedOnly], params: idParams, body: z.looseObject({}), v1: ptx.reconcileProviderTransaction, }); v1Route({ method: 'post', path: '/provider-transactions/:id/ignore', tag: 'Payments', summary: 'Ignore a provider transaction', access: 'supervisor', campusScope: UNSCOPED, middleware: [cs.unscopedOnly], params: idParams, body: z.looseObject({}), v1: ptx.ignoreProviderTransaction, }); // ─── Waitlist ──────────────────────────────────────────────────────────────── v1Route({ method: 'post', path: '/waitlist', tag: 'Waitlist', summary: 'Join a sold-out event\'s waitlist', description: 'Signed in, or with a name and email or phone. When places free up, people are offered them in order.', access: 'optional', status: 201, body: z.looseObject({ eventId: id }), v1: waitlist.joinWaitlist, }); v1Route({ method: 'get', path: '/me/waitlist', tag: 'Me', summary: 'Waitlists I\'m on', access: 'user', v1: waitlist.myWaitlist }); v1Route({ method: 'get', path: '/waitlist/claims/:token', tag: 'Waitlist', summary: 'A waitlist offer', description: 'What an offer link shows (the token is the credential). Register with `waitlistToken` to take it.', access: 'public', middleware: [linkLimiter()], params: tokenParams, v1: waitlist.claimInfo, }); v1Route({ method: 'get', path: '/events/:id/waitlist', tag: 'Waitlist', summary: 'An event\'s waitlist', access: 'staff', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, query: filters('all'), map: asEvent, v1: waitlist.eventWaitlist, }); for (const [action, summary, handler] of [['offer', 'Offer someone their places now', waitlist.offerManually], ['move', 'Move someone up or down the waitlist', waitlist.moveEntry], ['remove', 'Take someone off the waitlist', waitlist.removeEntry]] as const) { v1Route({ method: 'post', path: `/waitlist/:id/${action}`, tag: 'Waitlist', summary, access: 'supervisor', campusScope: 'scopeEvent(waitlistEntry)', middleware: [cs.scopeEvent({ resolve: 'waitlistEntry' })], params: idParams, body: z.looseObject({}), v1: handler, }); } v1Route({ method: 'delete', path: '/waitlist/:id', tag: 'Waitlist', summary: 'Leave a waitlist', access: 'user', params: idParams, v1: waitlist.leaveWaitlist }); // ─── Sponsorship ───────────────────────────────────────────────────────────── v1Route({ method: 'get', path: '/events/:id/sponsorship', tag: 'Sponsorship', summary: 'Sponsoring places at an event', description: 'Whether it\'s on, and the suggested amounts.', access: 'public', params: idParams, map: asEvent, v1: sponsor.getEventSponsorship, }); v1Route({ method: 'get', path: '/events/:id/sponsorship/pool', tag: 'Sponsorship', summary: 'The sponsorship pool', description: 'What\'s been given and what\'s been used.', access: 'supervisor', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, map: asEvent, v1: sponsor.getPool, }); v1Route({ method: 'post', path: '/sponsorships/checkout', tag: 'Sponsorship', summary: 'Give towards places', description: 'Starts a checkout; answers where to send the person.', access: 'optional', body: z.looseObject({ eventId: id }), v1: sponsor.createSponsorCheckout, }); // ─── Consent ───────────────────────────────────────────────────────────────── v1Route({ method: 'get', path: '/events/:id/consent', tag: 'Consent', summary: 'Parental consent for an event', access: 'staff', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, map: asEvent, v1: consent.listEventConsent, }); v1Route({ method: 'get', path: '/events/:id/consent/medical', tag: 'Consent', summary: 'Medical notes from consent forms', access: 'supervisor', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, map: asEvent, v1: consent.medicalList, }); v1Route({ method: 'post', path: '/events/:id/consent/resend-pending', tag: 'Consent', summary: 'Remind everyone who hasn\'t answered', access: 'supervisor', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, body: z.looseObject({}), map: asEvent, v1: consent.resendPending, }); v1Route({ method: 'post', path: '/events/:id/consent/rerequest', tag: 'Consent', summary: 'Ask for consent again', description: 'After the consent wording changed.', access: 'supervisor', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, body: z.looseObject({}), map: ctx => ({ params: { eventId: ctx.params.id }, body: ctx.req.body }), v1: consent.rerequestConsent, }); v1Route({ method: 'post', path: '/consent-requests/:id/resend', tag: 'Consent', summary: 'Send a consent request again', access: 'user', campusScope: 'scopeEvent(consentRequest, owner-ok)', middleware: [cs.scopeEvent({ resolve: 'consentRequest', ownerOk: true })], params: idParams, body: z.looseObject({}), v1: consent.resendRequest, }); v1Route({ method: 'get', path: '/consent/:token', tag: 'Consent', summary: 'A consent form', description: 'What the link in a consent request shows (the token is the parent\'s credential).', access: 'public', middleware: [linkLimiter()], params: tokenParams, v1: consent.getConsent, }); v1Route({ method: 'post', path: '/consent/:token', tag: 'Consent', summary: 'Answer a consent form', access: 'public', middleware: [linkLimiter()], params: tokenParams, body: z.looseObject({}), v1: consent.answerConsent, }); // ─── Attendee form answers ─────────────────────────────────────────────────── v1Route({ method: 'get', path: '/form-responses', tag: 'Registrations', summary: 'Attendee form answers', description: 'For an event (`eventId`) or a registration (`registrationId`), for events you may see.', access: 'staff', campusScope: SCOPE_LIST, middleware: [cs.scopeList], query: filters('eventId', 'registrationId', 'userId', 'cursor'), v1: forms.listFormResponses, }); // ─── Feedback ──────────────────────────────────────────────────────────────── v1Route({ method: 'get', path: '/feedback/summary', tag: 'Feedback', summary: 'Feedback across events', access: 'supervisor', campusScope: SCOPE_LIST, middleware: [cs.scopeList], query: filters('eventIds'), v1: feedback.getSummary, }); v1Route({ method: 'get', path: '/feedback-forms/:token', tag: 'Feedback', summary: 'A feedback form', description: 'What the link sent after an event shows (the token is the credential).', access: 'public', params: tokenParams, v1: feedback.getPublicForm, }); v1Route({ method: 'post', path: '/feedback-forms/:token', tag: 'Feedback', summary: 'Answer a feedback form', access: 'public', params: tokenParams, body: z.looseObject({}), v1: feedback.submitPublicForm, }); v1Route({ method: 'get', path: '/events/:id/feedback', tag: 'Feedback', summary: 'An event\'s feedback set-up and answers', access: 'supervisor', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams, map: asEvent, v1: feedback.getEventFeedback, }); v1Route({ method: 'put', path: '/events/:id/feedback', tag: 'Feedback', summary: 'Set up an event\'s feedback form', access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [cs.scopeEvent({ need: 'manage' })], params: idParams, body: z.looseObject({}), map: asEvent, v1: feedback.saveEventFeedback, }); v1Route({ method: 'delete', path: '/events/:id/feedback', tag: 'Feedback', summary: 'Remove an event\'s feedback form', access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [cs.scopeEvent({ need: 'manage' })], params: idParams, map: asEvent, v1: feedback.deleteEventFeedback, }); v1Route({ method: 'post', path: '/events/:id/feedback/send-now', tag: 'Feedback', summary: 'Send the feedback form now', access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [cs.scopeEvent({ need: 'manage' })], params: idParams, body: z.looseObject({}), map: asEvent, v1: feedback.sendNow, });