Context
A student who closes the payment page leaves a PointPurchase PENDING forever, and a callback arriving days later would still grant points by surprise. Admins also had no view of top-ups: nothing listed who paid what, and nothing showed the money taken in a period.
Decision
- A new status,
PaymentStatus.EXPIRED, added by an additive migration (ALTER TYPE … ADD VALUE), not a reused one:CANCELLEDmeans the buyer pressed cancel andFAILEDmeans the gateway refused; an expiry is neither, and the admin needs to tell them apart. The enum mirror (PAYMENT_STATUSES) and the target schema follow. - Timeout:
PAYMENT_PENDING_TIMEOUT_MINUTES(5..1440, default 30), validated by both the api and the worker env. - The transition is one conditional UPDATE:
status = 'PENDING' AND createdAt < now − timeout. A callback settling the same row holdsSELECT … FOR UPDATE; Postgres makes the UPDATE wait and re-check the condition, so it can never flip a PAID purchase. - Three places run it, all the same statement: 1. a repeatable BullMQ job (queue
payments, schedulerexpire-point-purchases, every 5 minutes) registered withupsertJobSchedulerat worker boot — the worker runs inside the API container on production (ADR-0039), so this costs no extra service; 2. lazily in the api before it reads purchases (student history, admin list and detail, the gateway page, a replayed checkout), so the UI is right even when Redis is down or the free instance slept through a tick; 3. inside the callback's transaction: a callback that finds its purchase PENDING but past the timeout expires it under the lock. Late is late whether or not the sweep ran first. - A callback for an EXPIRED purchase grants nothing. It answers
granted: falsewith the EXPIRED purchase, logs a warning, and writes AuditLogpoint_purchase.late_callbackwith the reported outcome, amount and provider transaction number. No automatic revival: if the gateway really took the money, an admin grants the points with the existing ADJUST (reason quoting the order code). The admin detail lists the late callbacks and opens the wallet. - Admin view (
GET /admin/points/purchases,/summary,/:id; ADMIN only, BFF GET only): filters by status, buyer (email, name) or order code, and creation date[from, to). Totals count PAID purchases bypaidAt, the moment the money arrived. Rows show amounts and provider references only; a purchase row holds no secret.
Alternatives considered
- Lazy sweep only: no timer to run, but a purchase nobody looks at stays PENDING and the callback check alone would carry correctness. The worker job keeps the table honest.
- A
setIntervalin the api process: one more timer per api instance, outside BullMQ's locking, so two instances would both sweep (harmless but noisy). - Reviving an expired purchase on a late PAID: convenient, but it is exactly the surprise grant the issue removes, and it would bypass the admin's judgement.
Consequences
Proven on real Postgres by apps/api/test/integration/admin-point-purchases.int-spec.ts (only stale PENDING rows flip; a PAID callback racing the sweep ends PAID with one ledger row or EXPIRED with none, 12 rounds; admin filters and totals). Refunds remain out of scope.