unit(B1): invite -> pending -> approve/deny with console outbox

Admin-invite lifecycle (BUILD-PLAN Phase B, depends A3):
- src/auth/invites.ts: pure nextUserStatus() rule (invite->pending_approval,
  approve->active, deny->denied) + isSignInEligible() mirroring the Auth.js
  authorize guard; thin DB wrappers inviteUser/approveUser/denyUser;
  enqueueOutbox() console transport (launch, ARCHITECTURE 3.4).
- Routes: /api/dev/invite, /api/dev/approve, /api/dev/deny.
- src/auth/invites.test.ts: 7 tests named after the rules.
- magic-link.ts exports APP_ORIGIN.
No migration: users.status already carries the enum (A2).
Verify: typecheck, lint, 24 unit tests (+7), build, 4 e2e all green; runtime
smoke test confirms invite->pending, approve->active signs in, deny->denied
gets no session.
This commit is contained in:
Tiel Coder
2026-11-17 23:46:07 +00:00
parent 3451bced82
commit 5f0b94af6a
7 changed files with 418 additions and 1 deletions
+55
View File
@@ -109,3 +109,58 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e
- 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`).
---
## A4 — App shell — DONE 2026-11-17
**Scope (from BUILD-PLAN.md):** layout, nav (5 tabs mobile / rail desktop), tokens, error + empty states, `/api/health`.
**Delivered:**
- `src/app/(app)/layout.tsx` — app shell wrapper; `src/components/app/nav.tsx` — 5 nav tabs (mobile tab bar + desktop rail); `src/components/app/app-layout.tsx`, `src/components/app/state-message.tsx` (tokens + error/empty states).
- 5 shell pages: `schedule`, `announcements`, `roster`, `training`, `settings`.
- `src/app/api/health/route.ts` — `GET /api/health -> { ok: true }`.
- Playwright e2e `e2e/shell.spec.ts` + `playwright.config.ts` (webServer builds + starts the app; `reuseExistingServer`).
**Verification (all exit 0):**
- `pnpm typecheck` ✓ · `pnpm lint` ✓ · `pnpm test` ✓ (17) · `pnpm build` ✓
- `pnpm test:e2e --grep shell` → **4/4 pass** at 360 px viewport (the A4 "Done when"):
- renders the shell with 5 nav tabs at 360 px ✓
- shows the desktop rail at desktop width ✓
- redirects unauthenticated users away from the shell ✓
- `/api/health` returns ok ✓
**A4 blocker — root cause + fix (recorded for future reference):**
- **Reported** as `/api/dev/complete` returning HTTP 000 / connection reset. That was a **curl/shell-quoting artifact** in the handoff's test script, not an app bug.
- **Real symptom:** the complete response carried **no `Set-Cookie`**, so no session was established and every `/schedule` 307-redirected to `/login`; the two shell e2e tests failed (nav tabs never rendered).
- **Root cause:** `[auth][error] UntrustedHost`. Auth.js v5's `assertConfig()` checks `config.trustHost` **before** `setEnvDefaults()` applies its env default, and that default only derives `trustHost` from `AUTH_URL`/`AUTH_TRUST_HOST`/`VERCEL`/`CF_PAGES`/`NODE_ENV !== "production"` — **not** `NEXTAUTH_URL`. In `pnpm start` (production) with no `AUTH_URL`, `trustHost` stayed `false`, so the magic-link callback was rejected and no session cookie was written.
- **Fix:** `src/auth/config.ts` sets `trustHost: true` explicitly (documented fix for `UntrustedHost`; localhost app behind a reverse proxy). Rebuild required for `pnpm start`. Verified: complete response now sets `authjs.session-token`, `/schedule` returns 200, all 4 e2e pass.
**Next: B1 — invite → pending → approve/deny, with status transitions and an outbox row (console).**
---
## B1 — Invite → pending → approve/deny — DONE 2026-11-18
**Scope (from BUILD-PLAN.md):** invite creates `pending_approval`; approve flips to `active`; denied user cannot sign in; an outbox row (console).
**Delivered:**
- `src/auth/invites.ts` — the unit. Pure transition rule `nextUserStatus(current, action)` (invite→pending_approval, approve→active, deny→denied) and `isSignInEligible(user)` (active && not deactivated, mirrors the Auth.js `authorize` guard), both exported and unit-tested. Thin DB wrappers `inviteUser`, `approveUser`, `denyUser` around the rule. `enqueueOutbox()` = launch console transport (ARCHITECTURE §3.4), a structured greppable line — swapping in Postmark later is a change here only.
- Routes: `src/app/api/dev/invite/route.ts` (POST { email, invitedBy? }), `src/app/api/dev/approve/route.ts` (POST { userId, approvedBy? }), `src/app/api/dev/deny/route.ts` (POST { userId, deniedBy? }).
- `src/auth/invites.test.ts` — 7 tests named after the rules.
- `src/auth/magic-link.ts` exports `APP_ORIGIN` (reused by invites).
**No migration needed** — `users.status` already carries the `invited / pending_approval / active / inactive / denied` enum and defaults to `pending_approval` (A2).
**Verification (all exit 0):**
- `pnpm typecheck` ✓ · `pnpm lint` ✓ · `pnpm test` ✓ (**24** total, +7 for B1) · `pnpm build` ✓
- Runtime smoke test against live app + Postgres on :5433 (full lifecycle):
- invite `b1.active@example.com` → DB `pending_approval` ✓
- approve → DB `active`; complete invite-time token → `/api/auth/session` returns the user (`systemRole: volunteer`) ✓
- invite + deny `b1.denied@example.com` → DB `denied`; complete → session `null`, `/api/admin` 401 ✓
**Decisions:**
- Invite is the *admin-invites* path, distinct from `getOrCreateUser`'s self-serve auto-activate. Re-inviting an existing (e.g. previously denied) user resets them to `pending_approval`.
- The transition rule lives in one pure, auditable place (`nextUserStatus`); the DB wrappers call it, so the rule and the code cannot drift.
**Next: B2 — profile + notification prefs + SMS consent capture.**