Context
A student who paid points to join a course (ADR-0043) lost them if the course was then taken down by an admin or closed by its instructor before they could use it. CLOSED is terminal: neither close can be undone and a closed course cannot be joined.
Decision
- What is refunded: a student's _open payment_, the newest
SPEND / COURSE_UNLOCKrow for the course with no matching refund, for exactly its amount, whatever the price is now. The refund isREFUND / COURSE_UNLOCKwith keycourse-refund:<courseId>:<userId>, in one transaction with the student's notification, and the enrollment becomesWITHDRAWN: money back means the seat back, so a refunded student no longer reads the closed course (an instructor close otherwise keeps enrolled students reading). Enrollment status does not decide eligibility: a payer who withdrew paid too. - Taken down =
POST /admin/courses/:id/close(auditcourse.force_closed): every open payment is refunded. NotificationCOURSE_TAKEN_DOWN, with the amount in the title and body. - Closed early = the instructor's
POST /courses/:id/close(PUBLISHED → CLOSED, auditcourse.closed) withclosedAt − paidAt ≤ COURSE_REFUND_WINDOW_DAYSdays (default 7; 0 = off), wherepaidAtis the SPEND row'screatedAt, not the enrollment'senrolledAt(a free re-join resets that). Exactly N days is still refunded; a millisecond later is not. NotificationPOINTS_REFUNDED(new enum value). - Paid once means paid and not refunded. Payment
niscourse-unlock:<c>:<u>for n = 1 (the ADR-0043 key, unchanged) and…:<n>after; its refund iscourse-refund:<c>:<u>[:<n>]. A refunded student who joins again pays again, under generation n + 1. A student with an open payment still re-joins free. Today no closed course can be reopened, so this only matters once a reopen exists; the integration suite proves it by flipping the row back to PUBLISHED. - A price change refunds and charges nobody: the refund reads the SPEND row, the join reads the ledger, never the current price, for anyone who already paid.
- Never charged without a refund: a paid join locks the course row
FOR SHAREand re-checksPUBLISHEDinside its transaction. Both closes are anUPDATEof that row, so either the close waits for the join to commit (and its sweep, queued after the close commits, sees the payment) or the close commits first and the join answers409 COURSE_NOT_ENROLLABLEwith nothing charged. - Off the request, replay-safe: the close queues a BullMQ job on
course-refund({ courseId, reason }), 8 attempts with backoff. The sweep reads enrollments in pages of 100, finds payers with one indexed key lookup per page, and refunds each in its own short transaction. The refund key is checked under the wallet lock (ADR-0041), so a replayed, retried or concurrent sweep credits and notifies nobody twice. - The consumer is in the api, not the worker app: the refund is a points write and
PointsServiceis the only writer. It is one smallWorker(concurrency 1) in the Nest process. Without Redis, or when the enqueue fails within 5 s, the sweep runs in-process instead.
Alternatives considered
- Refund inside the close request: a course with thousands of payers would time out the admin.
- Consume in
apps/worker: it would need a second writer of points, breaking ADR-0041. - Refund every payer on an instructor close: an instructor could sell a course and close it months later; the window protects students who could not use what they paid for.
Consequences
Proven on real Postgres by apps/api/test/integration/course-points-refund.int-spec.ts: one refund per payer on a replayed job, early vs late close, a price change, pay-again after a refund, and a take-down racing joins (every student ends uncharged or charged-and-refunded).