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:
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* SMS consent rule (BUILD-PLAN unit B2). Pure — no DB, no Next.js — so the
|
||||
* TCPA-relevant rule is auditable and unit-tested in isolation. The DB wrappers
|
||||
* in `src/db/profile.ts` call these.
|
||||
*
|
||||
* Two rules:
|
||||
* 1. Consent is bound to a specific phone number (TCPA). If the number
|
||||
* changes, the old consent no longer applies and must be cleared
|
||||
* (`phoneChanged` drives the reset in `updateProfile`).
|
||||
* 2. An opt-in records explicit evidence: timestamp, source, and IP
|
||||
* (`buildConsent`).
|
||||
*/
|
||||
|
||||
import type { SmsConsentSource } from "@/db/schema";
|
||||
|
||||
/** The consent fields on a `notification_preferences` row, in both states. */
|
||||
export type ConsentFields = {
|
||||
smsEnabled: boolean;
|
||||
smsConsentAt: Date | null;
|
||||
smsConsentSource: SmsConsentSource | null;
|
||||
smsConsentIp: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Did the phone number change? Compares the current stored number against the
|
||||
* proposed one. `null` (no number) and `""` are treated as distinct so a
|
||||
* first-time add is detected. This is the predicate `updateProfile` uses to
|
||||
* decide whether to reset consent.
|
||||
*/
|
||||
export function phoneChanged(current: string | null, next: string | null): boolean {
|
||||
return (current ?? "") !== (next ?? "");
|
||||
}
|
||||
|
||||
/**
|
||||
* The consent fields written on opt-in: SMS enabled, with the timestamp,
|
||||
* source, and IP that constitute TCPA evidence. `at` is injectable for tests.
|
||||
*/
|
||||
export function buildConsent(
|
||||
source: SmsConsentSource,
|
||||
ip: string | null,
|
||||
at: Date = new Date(),
|
||||
): ConsentFields {
|
||||
return {
|
||||
smsEnabled: true,
|
||||
smsConsentAt: at,
|
||||
smsConsentSource: source,
|
||||
smsConsentIp: ip,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The consent fields in the pre-opt-in state: SMS disabled, no evidence. Used
|
||||
* when the phone number changes (the prior consent no longer covers it).
|
||||
*/
|
||||
export function resetConsent(): ConsentFields {
|
||||
return {
|
||||
smsEnabled: false,
|
||||
smsConsentAt: null,
|
||||
smsConsentSource: null,
|
||||
smsConsentIp: null,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user