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
pointPricenull 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) is422 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(keycourse-unlock:<courseId>:<userId>,SPEND / COURSE_UNLOCK, the caller'stx), then create or reactivate the enrollment with sourcePOINTS. 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 answers409 ALREADY_ENROLLED. Short of points →409 INSUFFICIENT_POINTSwithdetails { 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).