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

Kiến trúc · ADR 0060

Showcase content pack, applied by the API at boot

Trạng thái
accepted
Ngày
2026-11-24
Người quyết định
SmartLearn team (#176)

Context

Production needs content a visitor can browse (courses now, blog posts later). The dev seed cannot provide it: ADR-0031 locks its demo accounts and demo catalog out of Supabase because they share a password. Nothing ran a seed against production at all, and CI cannot be relied on to deploy (ADR-0053).

Decision

  • A content pack, declared as data. packages/database/src/showcase/: pack/ holds plain TypeScript data, one module per concern (instructors.ts, courses/<course>.ts); steps/ holds one insert step per concern; runner.ts runs the steps in order. No SQL, no Prisma calls in pack/. Later concerns (chapter quizzes, projects and rubrics, blog posts, RAG indexing) add a data module, a field on ShowcasePack and a step.
  • Stable keys. Instructors by key (email <key>@showcase.smartlearn.invalid), courses by slug (and owner + code, so an admin's slug rename is still found), chapters and lessons by a UUID derived from course slug + key. A shipped key never changes.
  • Insert-only. A step inserts what is missing and never updates an existing row, so a second run creates and changes nothing and an admin's edit to a showcase record survives. A row outside the pack that holds a pack key (another owner's slug, an owner account that gained a password or was deleted) is a conflict: logged and skipped, never touched. A course an admin soft-deleted is not refilled.
  • Passwordless owners. Showcase instructors have no password and no Google link, on a reserved .invalid domain nobody can receive mail at: login, password reset and Google linking all have nothing to open. They are never shared demo logins, so ADR-0031's gate does not apply to them; the demo seed keeps its own gate unchanged.
  • Run by the API after boot. ShowcaseSeedService starts the runner from onApplicationBootstrap when SHOWCASE_SEED_ON_BOOT=true (image default; Compose sets false; unset means off). The container has already run migrate deploy (ADR-0053). The API does not wait for it, and a failure is logged, never thrown: the API serves and /health/ready stays 200.
  • One transaction, one lock. The whole pack is applied in one interactive transaction behind pg_try_advisory_xact_lock; a second instance booting at the same time skips.
  • Posts never pay their authors. Showcase blog posts (#180) are written already PUBLISHED, with their comments and likes, by the posts step after the courses step. They never go through admin approval, the only path that credits a post reward (ADR-0040), so no ledger row is created and pointsAwardedAt stays null (nothing for a hide to reverse, ADR-0047). A post is inserted together with its comments and likes and counters set to match; an existing post is never revisited. A post links its showcase course only when this run's pack owns that course; otherwise it is written without the link.
  • Free and paid. A free showcase course has allowance NONE; a priced one is priced in points (ADR-0057) at no less than its AI allowance tier's minimum (ADR-0052). The pack test enforces both.
  • Majors in a showcase organization (#177). A major must belong to an organization, so the pack inserts one COMMUNITY organization (smartlearn-showcase, invite-only, no members) holding its majors. The courses stay community courses (no organizationId), so their points price is honoured (ADR-0034); only majorId points into it, which is all the catalog filter and the landing page's "browse by major" read. A deactivated showcase organization or major is not refilled, and courses created after that get no major.
  • Topics and covers. Each course carries its own topics (beside the default GEN) and every lesson links to at least one, so mastery and recommendations have targets. Covers are root-relative paths to images the web app ships (/img/stock/...); the catalog accepts a root-relative path besides http(s).
  • Chapter quizzes (#178). Each chapter gets one OFFICIAL quiz, inserted already published and signed by the course owner (questions APPROVED, explanations stored as data), so the database's publish check holds and a student's submission feeds mastery. Only auto-graded types (single, multiple choice, true/false): taking one never calls an AI provider. Ids derive from course slug + chapter key (+ question position); an existing quiz is never touched, so a removed question does not come back.
  • RAG indexing (#181). After the seed transaction commits, ShowcaseSeedService lists the published showcase lessons (courses owned by a showcase instructor) that are not READY under the hash of their current content, and LessonIndexQueueService adds them in one addBulk as index-lesson jobs on the existing ingest queue, job id = lesson id. The worker's ingest consumer embeds them (processLessonIndex, same chunks and hash as the api's LessonRagService, shared from @smartlearn/ai). No new queue or consumer, so no new idle polling (ADR-0059); an unchanged boot opens no queue and sends Redis nothing. The instance that lost the seed lock does not enqueue. A rate-limited embedding is retried with backoff (6 attempts from 30 s); a lesson still FAILED after that is queued again on the next boot.

Consequences

  • Every deploy converges production to the pack without CI and without a manual seed.
  • Changing an already-shipped record in the pack does not reach production; ship a new key, or edit it in the admin console. A hard-deleted showcase row comes back on the next boot.
  • pnpm --filter @smartlearn/database db:seed:showcase applies the pack to a local database by hand; it refuses any non-local host.