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

Kiến trúc · ADR 0043

Joining a points-priced course: one transaction, paid once per student

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

Context

The course builder already stored Course.pointPrice, but nothing enforced it. A paid join must charge once even on a double click, never charge without enrolling (or the reverse), and an official UNIVERSITY-org (FPT) course must never be points-gated.

Decision

  • Free is pointPrice null or 0; the API stores 0 as null. A price is an integer 1..100000.
  • Official = the course's organization has type UNIVERSITY. Create/update with a price on such a course (or moving a priced course into one) is 422 COURSE_POINT_PRICE_NOT_ALLOWED. A stored price on an official course is ignored when joining and shown as Free in the catalog.
  • Paid join = one Prisma transaction: debit through PointsService (key course-unlock:<courseId>:<userId>, SPEND / COURSE_UNLOCK, the caller's tx), then create or reactivate the enrollment with source POINTS. The debit holds the wallet row lock, so a second concurrent join of the same student waits, finds the key (a replay, no charge), re-reads the enrollment and answers 409 ALREADY_ENROLLED. Short of points → 409 INSUFFICIENT_POINTS with details { balance, required, topUpUrl }; the rollback leaves no charge and no enrollment.
  • Paid once per student per course. The key is per (course, student), so a student who paid, withdrew and joins again is not charged again, at the old or a new price. A price change only affects students who never paid.
  • Who never pays: the course owner, a platform ADMIN, and OWNER/ADMIN/INSTRUCTOR members of the course's organization.

Alternatives considered

  • Charge on every (re)join. Rejected: a withdraw/rejoin would charge twice for the same content, and the idempotency key would have to carry a counter.
  • Disallow a price on any org course. Rejected: ADR-0034 only protects official coursework; company and training-centre courses may be priced.

Consequences

The ledger row is the proof of purchase. Proven on real Postgres by apps/api/test/integration/course-points-join.int-spec.ts (10 concurrent joins → charged and enrolled once; balance for one of two courses → exactly one succeeds).