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
+112
View File
@@ -0,0 +1,112 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { updateProfile, setSmsConsent } from "@/db/profile";
import type { ProfilePatch } from "@/db/profile";
import type { SmsConsentSource } from "@/db/schema";
const JSON_HEADERS = { "content-type": "application/json" };
const profileSchema = z.object({
userId: z.string().min(1),
firstName: z.string().optional(),
lastName: z.string().optional(),
displayName: z.string().optional(),
phoneE164: z.string().nullable().optional(),
emergencyContact: z.string().nullable().optional(),
timezone: z.string().optional(),
});
const consentSchema = z.object({
userId: z.string().min(1),
source: z.enum(["web_signup", "paper_form", "admin_import"]),
ip: z.string().nullable().optional(),
});
/**
* PATCH /api/dev/profile — update a user's profile. If the phone number
* changes, SMS consent is reset (TCPA).
*/
export const PATCH = async (req: NextRequest) => {
let body: unknown;
try {
body = JSON.parse(await req.text());
} catch {
return new Response(JSON.stringify({ error: "invalid body" }), {
status: 400,
headers: JSON_HEADERS,
});
}
const parsed = profileSchema.safeParse(body);
if (!parsed.success) {
return new Response(JSON.stringify({ error: "a userId is required" }), {
status: 400,
headers: JSON_HEADERS,
});
}
try {
// Only update fields actually present in the body — a PATCH must not null
// out unspecified fields (e.g. omitting phoneE164 must not wipe the phone).
const patch: ProfilePatch = {};
if (parsed.data.firstName !== undefined) patch.firstName = parsed.data.firstName;
if (parsed.data.lastName !== undefined) patch.lastName = parsed.data.lastName;
if (parsed.data.displayName !== undefined) patch.displayName = parsed.data.displayName;
if (parsed.data.phoneE164 !== undefined) patch.phoneE164 = parsed.data.phoneE164;
if (parsed.data.emergencyContact !== undefined) patch.emergencyContact = parsed.data.emergencyContact;
if (parsed.data.timezone !== undefined) patch.timezone = parsed.data.timezone;
const result = await updateProfile(parsed.data.userId, patch);
return new Response(JSON.stringify(result), {
status: 200,
headers: JSON_HEADERS,
});
} catch {
return new Response(JSON.stringify({ error: "failed to update profile" }), {
status: 500,
headers: JSON_HEADERS,
});
}
};
/**
* POST /api/dev/consent — record an explicit SMS opt-in (TCPA evidence).
*/
export const POST = async (req: NextRequest) => {
let body: unknown;
try {
body = JSON.parse(await req.text());
} catch {
return new Response(JSON.stringify({ error: "invalid body" }), {
status: 400,
headers: JSON_HEADERS,
});
}
const parsed = consentSchema.safeParse(body);
if (!parsed.success) {
return new Response(JSON.stringify({ error: "userId and source are required" }), {
status: 400,
headers: JSON_HEADERS,
});
}
try {
const preferences = await setSmsConsent(
parsed.data.userId,
{
source: parsed.data.source as SmsConsentSource,
ip: parsed.data.ip ?? null,
},
);
return new Response(JSON.stringify({ ok: true, preferences }), {
status: 200,
headers: JSON_HEADERS,
});
} catch {
return new Response(JSON.stringify({ error: "failed to record consent" }), {
status: 500,
headers: JSON_HEADERS,
});
}
};