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

Kiến trúc · ADR 0052

A points course's AI tutor allowance is a per-week bucket, charged before the personal quota

Trạng thái
accepted. The tier counts and the price rule are the first estimate of ADR-0049
Ngày
2026-11-22
Người quyết định
SmartLearn team (#127)

Context

A course a student pays points to join should carry AI tutor answers, so a heavy learner does not burn the free monthly quota on one course. ADR-0049 proposed the tiers and a price floor but the numbers are estimates: they must be tunable without a deploy, and the floor must be checked by the API, not only shown.

Decision

  • Tiers: Course.aiAllowanceTier ∈ NONE, WEEKLY_10, WEEKLY_25, DAILY_10. The counts behind them (perWeek, and perDay for a daily tier), pointsPerAnswer (3) and courseWeeks (10) are the AppSetting ai.course-allowance, edited at /admin/billing/plans; the defaults are ADR-0049's (10/week, 25/week, 10/day capped at 70/week). An admin setting, not env, so it moves with the other AI pricing and needs no restart.
  • Price floor: pointPrice ≥ pointsPerAnswer × perWeek × courseWeeks (300 / 750 / 2 100 by default), else 422 COURSE_POINT_PRICE_BELOW_MINIMUM with details.minimum. A tier on a free course is 422 COURSE_AI_ALLOWANCE_NOT_ALLOWED; an official course cannot be priced at all. Checked when the price or tier changes, so raising the rule never blocks an unrelated edit.
  • Snapshot at the join: Enrollment.aiAllowanceTier is set when a new payment is made, from the course row read FOR SHARE; re-joining on a payment already held keeps it. A tier change applies to the next join, like a price change.
  • Who: a STUDENT whose enrollment is source = POINTS, ACTIVE or COMPLETED, in a PUBLISHED, not deleted, non-UNIVERSITY course. A refund (#141) sets WITHDRAWN and closing ends it, so both end the allowance with no extra code. Free joiners, instructors and admins keep their quota.
  • Buckets: one CourseAiAllowanceBucket row per (course, student, WEEK|DAY, ICT date), opened on first use with the count snapshotted; the ICT week starts Monday 00:00 UTC+7. A new window is a new row, so nothing resets on a cron.
  • Charge order: a tutor answer asked in the course takes one answer (whatever the credit cost) from the course: the day row, then the week row, each a conditional UPDATE (unitsUsed + 1 <= unitsGranted), in that order, in the same transaction as the ledger row; a week refusal gives the day unit back. Then the free month, then purchased credits, as in ADR-0044. The ledger row has source = COURSE_ALLOWANCE, periodStart = the Monday and note = course:<id>; a refund goes back to the windows the charge names.
  • Shown: catalog card and course page ("Kèm N lượt hỏi Trợ giảng AI/tuần"), what is left above the tutor composer (and a clear notice when it is spent), the wallet's AI section, the course settings with the floor per tier. Every place says the numbers are an estimate.

Consequences

Proven on Postgres by apps/api/test/integration/course-ai-allowance.int-spec.ts: allowance before quota, the Monday 00:00 ICT and daily rollovers, 12 concurrent questions against a week of 5 and a month of 3 → exactly 8 answered and 5 allowance spends, the floor, and the take-down. The quota summary now reads the enrollments (one more query on the wallet). Existing POINTS enrollments have no tier: they get one only by paying again. Only the tutor answer uses the allowance; other priced actions in the course stay on the personal quota.