Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0046

Refunding a paid course: taken down, or closed early by its instructor

Trạng thái
accepted
Ngày
2026-11-21
Người quyết định
Tín (#141)

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_UNLOCK row for the course with no matching refund, for exactly its amount, whatever the price is now. The refund is REFUND / COURSE_UNLOCK with key course-refund:<courseId>:<userId>, in one transaction with the student's notification, and the enrollment becomes WITHDRAWN: 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 (audit course.force_closed): every open payment is refunded. Notification COURSE_TAKEN_DOWN, with the amount in the title and body.
  • Closed early = the instructor's POST /courses/:id/close (PUBLISHED → CLOSED, audit course.closed) with closedAt − paidAt ≤ COURSE_REFUND_WINDOW_DAYS days (default 7; 0 = off), where paidAt is the SPEND row's createdAt, not the enrollment's enrolledAt (a free re-join resets that). Exactly N days is still refunded; a millisecond later is not. Notification POINTS_REFUNDED (new enum value).
  • Paid once means paid and not refunded. Payment n is course-unlock:<c>:<u> for n = 1 (the ADR-0043 key, unchanged) and …:<n> after; its refund is course-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 SHARE and re-checks PUBLISHED inside its transaction. Both closes are an UPDATE of 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 answers 409 COURSE_NOT_ENROLLABLE with 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 PointsService is the only writer. It is one small Worker (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).