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
+38
View File
@@ -5,6 +5,7 @@
* users → roles → services → service_instances → slots → assignments
*
* Deliberately NOT here (added in later units, do not add "while I'm in the schema"):
* - notification_preferences (B2), notification_templates/rules/outbox (later)
* - Catalog taxonomy: skill_categories, vendors, technologies, functions,
* skills, position_skills, user_skills, skill_assessments, growth_goals (B3)
* - service_role_requirements, unavailability, events, notifications outbox
@@ -42,6 +43,10 @@ export type AssignmentSource = (typeof ASSIGNMENT_SOURCES)[number];
export const CONFIRMED_VIAS = ["app", "email_link", "sms_reply", "admin"] as const;
export type ConfirmedVia = (typeof CONFIRMED_VIAS)[number];
/** TCPA consent source recorded on SMS opt-in (ARCHITECTURE §4.76). */
export const SMS_CONSENT_SOURCES = ["web_signup", "paper_form", "admin_import"] as const;
export type SmsConsentSource = (typeof SMS_CONSENT_SOURCES)[number];
const DEFAULT_TIMEZONE = "America/New_York";
/* ------------------------------------------------------------------ users */
@@ -79,6 +84,33 @@ export const users = pgTable(
],
);
/* -------------------------------------------------- notification prefs */
/**
* Per-user notification preferences (ARCHITECTURE §4.76). One row per user
* (PK = user_id). SMS consent fields are TCPA evidence: an explicit opt-in
* records the timestamp, source, and IP. Consent is bound to a phone number,
* so changing the number clears it (see src/db/profile.ts).
*/
export const notificationPreferences = pgTable(
"notification_preferences",
{
userId: text("user_id")
.primaryKey()
.references(() => users.id, { onDelete: "cascade" }),
emailEnabled: boolean("email_enabled").notNull().default(true),
smsEnabled: boolean("sms_enabled").notNull().default(false),
smsConsentAt: timestamp("sms_consent_at", { mode: "date", withTimezone: true }),
smsConsentSource: text("sms_consent_source", { enum: SMS_CONSENT_SOURCES }),
smsConsentIp: text("sms_consent_ip"),
reminderLeadDays: integer("reminder_lead_days").notNull().default(7),
quietHoursStart: text("quiet_hours_start").notNull().default("21:00"),
quietHoursEnd: text("quiet_hours_end").notNull().default("08:00"),
digestOnly: boolean("digest_only").notNull().default(false),
},
() => [],
);
/* ------------------------------------------------------------------ roles */
export const roles = pgTable(
@@ -226,6 +258,11 @@ export const usersRelations = relations(users, ({ many }) => ({
assignments: many(assignments),
}));
export const notificationPreferencesRelations = relations(
notificationPreferences,
() => ({}),
);
export const serviceInstanceRelations = relations(serviceInstances, ({ one, many }) => ({
service: one(services),
slots: many(slots),
@@ -248,6 +285,7 @@ export const assignmentRelations = relations(assignments, ({ one }) => ({
*/
export const schema = {
users,
notificationPreferences,
roles,
services,
serviceInstances,