B2: profile + notification_preferences + SMS consent capture (TCPA)

- notification_preferences table (user_id PK->users, email/sms toggles,
  sms_consent_at/source/ip evidence, quiet hours, digest) + migration 0002
- src/domain/consent.ts: pure TCPA rule (phoneChanged/buildConsent/resetConsent)
- src/db/profile.ts: updateProfile (partial update; resets consent on phone
  change) + setSmsConsent (opt-in) + getProfile
- PATCH/POST /api/dev/profile routes
- seed: per-user prefs row (email on, SMS off)
- 6 unit tests named after the rules
This commit is contained in:
Tiel Coder
2026-11-18 00:09:05 +00:00
parent 5f0b94af6a
commit 26b31978e2
10 changed files with 1398 additions and 0 deletions
+31
View File
@@ -164,3 +164,34 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e
- 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.**
---
## B2 — Profile + notification prefs + SMS consent capture — DONE 2026-11-18
**Scope (from BUILD-PLAN.md):** profile update, `notification_preferences` table, SMS consent capture (timestamp, source, IP).
**Delivered:**
- `src/db/schema.ts` — `notification_preferences` table (ARCHITECTURE §4.76): `user_id` PK→`users.id` (ON DELETE cascade), `email_enabled` (default true), `sms_enabled` (default false), `sms_consent_at`/`sms_consent_source`/`sms_consent_ip` (TCPA evidence), `reminder_lead_days` (default 7), `quiet_hours_start/end` ("21:00"/"08:00"), `digest_only` (default false). `SMS_CONSENT_SOURCES` enum (`web_signup` | `paper_form` | `admin_import`).
- `src/domain/consent.ts` — the pure TCPA rule. `phoneChanged(current, next)` (consent is bound to a number — drives the reset), `buildConsent(source, ip, at)` (opt-in evidence), `resetConsent()` (pre-opt-in state).
- `src/db/profile.ts` — DB wrappers: `updateProfile` (updates provided fields only; resets consent when the phone changes), `setSmsConsent` (opt-in populates consent fields), `getProfile`. `upsertPrefs` handles the one-to-one row.
- `src/app/api/dev/profile/route.ts` — PATCH `/api/dev/profile` (update) + POST `/api/dev/profile` (consent).
- `src/domain/consent.test.ts` — 6 tests named after the rules.
- `src/db/seed.ts` — per-user prefs row (email on, SMS off) for every seeded user.
- Migration `drizzle/0002_fearless_doctor_spectrum.sql`.
**Verification (all exit 0):**
- `pnpm typecheck` ✓ · `pnpm lint` ✓ · `pnpm test` ✓ (**30** total, +6 for B2) · `pnpm build` ✓
- `pnpm db:migrate` ✓ → `pnpm db:seed` ✓ (31 prefs rows, idempotent)
- Runtime smoke test against live app + Postgres on :5433:
- opt-in `web_signup` from `192.168.1.100` → `sms_enabled=true`, `sms_consent_at/source/ip` populated ✓
- change phone → consent reset (`sms_enabled=false`, evidence cleared) ✓
- change a non-phone field → consent UNCHANGED ✓
- invalid consent source → 400 ✓
**Decisions:**
- **Partial update, not replace.** The PATCH handler builds the patch from only the fields present in the body. A naive `{ phoneE164: body.phoneE164 ?? null }` would null the phone on every PATCH that omits it (and, since `null !== undefined`, `updateProfile` would treat that as a phone change and reset consent). This is the subtle trap this unit guards against.
- **No `one()` relation for the PK-FK column.** Drizzle 0.45.4's `one()` helper types `fields`/`references` as a tuple of non-PK columns; a column that is *both* PK and FK (`notification_preferences.user_id`) fails the typecheck. The table, FK, and all B2 code work without the relation (profile.ts queries both tables directly). Add the eager-load relation later once a working pattern exists.
- **Seed re-fetches canonical ids.** Same idempotency lesson as A2: `userRows` get fresh UUIDs each run, so prefs are seeded from the re-fetched `userRows2` ids (the assignments code already did this).
**Next: B3 — catalog taxonomy.**