/** * 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, }; }