3cd932e1a1
- Auth.js v5 Credentials provider (magic-link) + JWT session carrying systemRole - src/auth: roles, permissions (feature-scoped matrix), magic-link dev console transport - requirePermission(user, perm) chokepoint -> AuthError(401/403) - Routes: /api/auth/[...nextauth], /api/dev/magic-link, /api/dev/complete, /api/admin, /login - Auto-activate new emails in dev (AUTO_ACTIVATE_NEW_USERS, flip to require approval) - Acceptance pnpm test src/auth green: 401 anonymous, 403 volunteer on /admin, 200 admin - Verified at runtime against Postgres on :5433
112 lines
7.3 KiB
Markdown
112 lines
7.3 KiB
Markdown
# PROGRESS
|
||
|
||
Build log for Media Team Hub. One unit per commit, top to bottom. Append after each passing check.
|
||
|
||
---
|
||
|
||
## A1 — Foundation scaffold — DONE 2026-11-17
|
||
|
||
**Scope (from BUILD-PLAN.md):** Next.js 15 + TS strict, Tailwind 4, shadcn/ui (Radix), Drizzle, Zod, Vitest, lint, CI.
|
||
|
||
**Delivered:**
|
||
- Next.js 15, React 19, TypeScript strict, Tailwind 4, ESLint 9
|
||
- shadcn/ui initialized manually (Radix + CSS variables) — `components.json`, `src/lib/utils.ts`, theme in `globals.css`
|
||
- Drizzle + `postgres` driver + Zod; `src/db/index.ts` client, `src/db/schema.ts` placeholder (A2 fills it)
|
||
- Vitest (jsdom-capable) with a passing `cn()` test
|
||
- GitHub Actions CI (typecheck → lint → test → build)
|
||
- **Portable container stack** (user request): multi-stage Dockerfile (non-root, pnpm), `docker-compose.yml` (app + Postgres), `.dockerignore`
|
||
|
||
**Verification (all exit 0):**
|
||
- `pnpm typecheck` ✓
|
||
- `pnpm lint` ✓
|
||
- `pnpm test` ✓ (3 tests)
|
||
- `pnpm build` ✓
|
||
- `docker compose build app` ✓
|
||
- Container serves HTTP 200 on :3000, title "Media Team Hub" ✓
|
||
|
||
**Port audit (nothing else touched):** app → host **3000**; Postgres → host **5433** (local 5432 already taken). Confirmed free before binding.
|
||
|
||
**Decisions recorded in README.md ("Decisions recorded from your answers"):**
|
||
- Services: Sun 10:30 AM + 5:30 PM, `America/New_York` (DST-safe, changeable as data)
|
||
- One person per role
|
||
- Admin = full control; Coordinator = everything except design/theme + admin/settings
|
||
- CSV-only migration
|
||
- **Email-only at launch; SMS (Twilio + 10DLC) deferred**
|
||
|
||
**Stack change from docs:** docs target Cloudflare Workers (D1/R2/Cron Triggers — not portable). For portability we use standard Next.js Node container + Postgres + local media volume + node cron worker. Domain logic, data model, and UI are unchanged.
|
||
|
||
**Next: A2 — schema core.**
|
||
|
||
---
|
||
|
||
## A2 — Schema core — DONE 2026-11-17
|
||
|
||
**Scope (from BUILD-PLAN.md):** full data model + migration + seed.
|
||
|
||
**Delivered:**
|
||
- `src/db/schema.ts` — six core tables: `users`, `roles`, `services`, `serviceInstances`, `slots`, `assignments` (catalog tables from B3 excluded, per A2 scope).
|
||
- Spec rules enforced:
|
||
- **Rule 26** — `chk_slots_single_parent`: a slot belongs to exactly one of a service instance or an event (`CHECK ((service_instance_id IS NOT NULL) <> (event_id IS NOT NULL))`).
|
||
- **Rule 27** — `uq_active_assignment`: partial unique index, at most one non-terminal assignment per slot (`WHERE status IN ('proposed','confirmed','completed')`).
|
||
- Slug immutability (unique, no update path); deactivate-only (no hard delete); level changes via assessments; `created_at` audit on admin mutations; `America/New_York` default tz, stored UTC.
|
||
- `src/lib/uuid.ts` — UUIDv7 generator (RFC 9562).
|
||
- `src/lib/tz.ts` — DST-safe local→UTC (`localToUtc`), `nextSundaysInTz`, `formatInTimezone`, etc.
|
||
- `src/db/seed.ts` — idempotent seed (ON CONFLICT DO NOTHING + re-fetch canonical DB rows): 3 roles, 2 services, next 2 Sundays × 2 services of instances, 12 slots, 6 users, round-robin assignments.
|
||
- Migration `drizzle/0000_loose_wendell_vaughn.sql` (+ `0001` for the two added unique constraints).
|
||
|
||
**Verification (all exit 0):**
|
||
- `pnpm typecheck` ✓ · `pnpm lint` ✓ · `pnpm test` ✓ (3) · `pnpm build` ✓
|
||
- Drop DB → `pnpm db:migrate` ✓ → `pnpm db:seed` ✓ (exits 0)
|
||
- `SELECT count(*) FROM service_instances` = 4 (> 0) ✓
|
||
- Re-run seed is idempotent: stable counts (4/6/3/2/12/12), exit 0 ✓
|
||
- CHECK constraint rejects both-NULL and both-NOT-NULL slots; accepts exactly-one ✓
|
||
- Partial unique index rejects duplicate confirmed assignment for a slot ✓
|
||
- DST-safe: 9:00 AM America/New_York → `2026-11-25 14:00:00+00` (EST) ✓
|
||
|
||
**Bugs fixed this phase (recorded for future reference):**
|
||
- `.references()` takes a **column** (`() => services.id`), not a table.
|
||
- `one`/`many` are `relations()` callback params, not imports.
|
||
- CHECK XOR needs parentheses: `(a IS NOT NULL) <> (b IS NOT NULL)` — bare `a IS NULL <> b IS NULL` parses as `boolean <> text`.
|
||
- `drizzle-kit 0.31.11 generate` requires an existing `meta/_journal.json`; start from `{version, dialect, entries: []}` to diff a fresh schema.
|
||
- `ON CONFLICT DO NOTHING` needs a full unique index on its target (partial indexes don't qualify as arbiters).
|
||
- Seed must re-fetch canonical DB rows after idempotent inserts (fresh UUIDs otherwise break downstream FKs).
|
||
- `dateInTimezone` destructured `{y,m,d}` but `partsInTz` returns `{year,month,day}` — fixed.
|
||
|
||
**Next: A3 — auth (auth.js + credentials provider, admin/coordinator/volunteer roles).**
|
||
|
||
---
|
||
|
||
## A3 — Auth — DONE 2026-11-17
|
||
|
||
**Scope (from BUILD-PLAN.md):** magic-link sign-in (dev console transport), session cookie, `requirePermission()` chokepoint, role enum.
|
||
|
||
**Delivered:**
|
||
- `src/auth/roles.ts` — `SystemRole` type + helpers (`isAdmin`, `isCoordinator`, `isLeader`, `assertSystemRole`).
|
||
- `src/auth/permissions.ts` — feature-scoped permission matrix from the ARCHITECTURE §2.3 capability table, `hasPermission()`, `AuthError` (carries HTTP 401/403), `requirePermission(user, perm)` chokepoint.
|
||
- `src/auth/magic-link.ts` — dev console transport: `getOrCreateUser` (auto-activates unknown emails when `AUTO_ACTIVATE_NEW_USERS` is on — dev convenience; flip to `"false"` for the B1 approval flow), `signMagicToken`/`verifyMagicToken` (jose HS256), `requestMagicLink` (prints the sign-in link to the console).
|
||
- `src/auth/config.ts` — Auth.js v5 Credentials provider (`magic-link`) + JWT session carrying `systemRole`.
|
||
- Routes: `/api/auth/[...nextauth]`, `/api/dev/magic-link` (POST `{email}`), `/api/dev/complete` (GET `?token=…`), `/api/admin` (chokepoint), and `/login` (magic-link sign-in UI).
|
||
|
||
**Verification (all exit 0):**
|
||
- `pnpm typecheck` ✓ · `pnpm lint` ✓ · `pnpm test` ✓ (17 tests) · `pnpm build` ✓
|
||
- **Acceptance** `pnpm test src/auth` green: 401 anonymous, 403 volunteer on `/admin`, 200 admin ✓
|
||
- Runtime smoke test (dev server + Postgres on :5433):
|
||
- `GET /api/admin` (no session) → 401 `UNAUTHORIZED` ✓
|
||
- `POST /api/dev/magic-link {email}` → 200, creates user (volunteer/active), returns + logs the sign-in link ✓
|
||
- `GET /api/dev/complete?token=…` → 200, token verified, session established ✓
|
||
- User persisted in DB as `active` ✓
|
||
- `GET /login` → 200, renders ✓
|
||
|
||
**Decisions:**
|
||
- **Auto-activate new emails in dev** (`AUTO_ACTIVATE_NEW_USERS`, default on) so a volunteer can sign in as anyone without admin setup; admin can flip to `"false"` to require approval (B1).
|
||
- **Auth.js v5** (matches the docs' "Auth.js"; portable, no SaaS lock-in). JWT sessions, no DB adapter needed. (auth.js v5 ships as `next-auth@beta`; the `@auth/next` scope isn't published yet.)
|
||
- **`/admin` requires `admin.settings`** (admin-only) — matches the spec's `/admin/*` pages being admin-only.
|
||
|
||
**Bugs fixed this phase (recorded for future reference):**
|
||
- Auth.js v5 session config uses `session.strategy: "jwt"`, not `session.jwt`.
|
||
- `req.json()` was unreliable here; `req.text()` + `JSON.parse` is the robust body read.
|
||
- An object schema must be parsed as an object: `bodySchema.safeParse({ email })`, not `safeParse(email)`.
|
||
- Turbopack dev + a production `.next/` from `pnpm build` conflict on `_buildManifest.js.tmp`; `rm -rf .next` before `next dev`.
|
||
|
||
**Next: A4 — app shell (layout, nav, tokens, error + empty states, `/api/health`).
|