import { z } from 'zod'; import type { Request } from 'express'; import { defineRoute } from '../route'; import { ApiError } from '../errors'; import { single } from '../paging'; import { id } from '../schemas'; import { previewScan, scanTicket, type ScanTicket } from '../../services/checkin'; const OUTCOMES = ['checked_in', 'already_used', 'consent_required', 'wrong_event', 'wrong_section', 'section_not_found', 'no_session', 'locked', 'transferred'] as const; const qrParams = z.object({ qrCode: z.string().min(1).max(200).describe('The code in the ticket\'s QR') }); /** * Campus-scoped staff may only check in tickets for their campuses' events (and church-wide * ones) — the same rule as v1's scopeEvent({ resolve: 'ticketQr', nameCampus: true }). */ async function assertTicketCampus(req: Request, qrCode: string) { const cs = require('../../utils/campusScope') as { scopeFor(req: unknown): Promise; eventOf(resolve: string, value: string): Promise<{ campusId: string | null; campusName?: string | null } | null>; canView(scope: object, campusId: string | null): boolean; }; const scope = await cs.scopeFor(req); if (!scope) return; const ev = await cs.eventOf('ticketQr', qrCode); if (ev && !cs.canView(scope, ev.campusId)) throw new ApiError('FORBIDDEN', `This ticket is for an event at ${ev.campusName || 'another campus'}.`); } const ticketInfo = z.object({ id, eventId: id, eventTitle: z.string().nullable(), ticketName: z.string().nullable().describe('The ticket option, e.g. "Adult"'), quantity: z.number().int().describe('People on this ticket'), }); const toTicketInfo = (t: ScanTicket) => ({ id: t.id, eventId: t.eventId, eventTitle: t.event.title ?? null, ticketName: t.registrationOption.eventOption.name ?? null, quantity: t.quantity }); const sessionRef = z.object({ id, label: z.string() }); export const preview = defineRoute({ method: 'get', path: '/checkin/:qrCode', tag: 'Check-in', summary: 'Look a ticket up before checking it in', description: 'Who it is for, how many are left to check in (for today\'s session on multi-day events, or `sessionId`), and parental consent. Nothing is changed.', access: 'staff', campusScope: 'event:ticketQr:view (assertTicketCampus)', params: qrParams, query: z.object({ sessionId: id.optional().describe('Multi-day events: the session (default: today\'s)') }), response: single(z.object({ outcome: z.enum(['found', 'transferred']), message: z.string().nullable().describe('For a transferred ticket: who has it now'), ticket: z.object({ id, eventId: id, eventTitle: z.string().nullable(), ticketOptionId: id.nullable().describe('For section checks'), ticketName: z.string().nullable(), variant: z.string().nullable(), attendee: z.string().nullable().describe('The attendee named on it, if any'), bookedBy: z.string().nullable().describe('Who registered'), quantity: z.number().int(), remaining: z.number().int().describe('Still to check in (this session, on multi-day events)'), session: sessionRef.nullable(), sessionError: z.string().nullable().describe('Why no session is open for check-in now'), consentStatus: z.string().describe('not_required, given, pending, declined or missing'), }).nullable(), })), async handler({ params, query, req }) { await assertTicketCampus(req, params.qrCode); const result = await previewScan(params.qrCode, { sessionId: query.sessionId ?? null }); if (result.outcome === 'transferred') return { data: { outcome: 'transferred' as const, message: result.message, ticket: null } }; const t = result.ticket; return { data: { outcome: 'found' as const, message: null, ticket: { id: t.id, eventId: t.eventId, eventTitle: t.event.title ?? null, ticketName: t.registrationOption.eventOption.name ?? null, ticketOptionId: t.registrationOption.eventOption.id ?? null, variant: t.registrationOption.variant?.name ?? null, attendee: t.attendee?.name ?? null, bookedBy: t.user.name ?? null, quantity: t.quantity, remaining: t.remaining, session: t.session, sessionError: t.sessionError, consentStatus: t.consentStatus, }, }, }; }, }); export const scan = defineRoute({ method: 'post', path: '/checkin/:qrCode', tag: 'Check-in', summary: 'Check a ticket in', description: [ 'Checks in everyone left on the ticket, or `people`. Every normal result is a 200 with an `outcome`:', '`checked_in`, `already_used`, `consent_required` (a supervisor can resend with `overrideConsent`, which is logged), `wrong_event`, `wrong_section`, `section_not_found`, `no_session` (multi-day: nothing open now), `locked` (offline-locked to another device) or `transferred` (handed on; `message` says to whom).', 'An unknown code is `NOT_FOUND`.', ].join('\n\n'), access: 'staff', campusScope: 'event:ticketQr:view (assertTicketCampus)', params: qrParams, body: z.object({ eventId: id.optional().describe('Refuse tickets for any other event'), sectionId: id.optional().describe('Only tickets valid for this section'), sessionId: id.optional().describe('Multi-day events: the session (default: today\'s)'), people: z.number().int().min(1).max(10000).optional().describe('How many to check in (default: everyone left)'), overrideConsent: z.boolean().optional(), deviceId: z.string().max(200).optional().describe('This scanner, so its own offline lock doesn\'t block it'), }), response: single(z.object({ outcome: z.enum(OUTCOMES), message: z.string(), ticket: ticketInfo.nullable(), checkedIn: z.number().int().describe('People checked in by this request (0 unless checked_in)'), remaining: z.number().int().nullable().describe('Still to check in afterwards'), session: sessionRef.nullable(), consentStatus: z.string().nullable(), canOverride: z.boolean().describe('consent_required: whether you may override'), })), async handler({ params, body, user, req }) { await assertTicketCampus(req, params.qrCode); const { getClientIp } = require('../../utils/requestUtils') as { getClientIp(req: unknown): string }; const r = await scanTicket(params.qrCode, user!, { eventId: body.eventId, sectionId: body.sectionId, sessionId: body.sessionId, deviceId: body.deviceId, qty: body.people ?? null, overrideConsent: body.overrideConsent, }, getClientIp(req)); const base = { outcome: r.outcome, message: r.message, ticket: 'ticket' in r ? toTicketInfo(r.ticket) : null, checkedIn: 0, remaining: null as number | null, session: null as { id: string; label: string } | null, consentStatus: null as string | null, canOverride: false }; if (r.outcome === 'checked_in') return { data: { ...base, checkedIn: r.qtyRedeemed, remaining: r.remaining, session: r.session } }; if (r.outcome === 'already_used') return { data: { ...base, remaining: 0 } }; if (r.outcome === 'consent_required') return { data: { ...base, consentStatus: r.consentStatus, canOverride: r.canOverride } }; return { data: base }; }, });