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,47 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildConsent, phoneChanged, resetConsent } from "./consent";
|
||||
|
||||
describe("phoneChanged (B2 consent reset on number change)", () => {
|
||||
it("is true when the number changes", () => {
|
||||
expect(phoneChanged("+15551230001", "+15551230002")).toBe(true);
|
||||
});
|
||||
|
||||
it("is false when the number is unchanged", () => {
|
||||
expect(phoneChanged("+15551230001", "+15551230001")).toBe(false);
|
||||
});
|
||||
|
||||
it("detects a first-time add (null -> number)", () => {
|
||||
expect(phoneChanged(null, "+15551230001")).toBe(true);
|
||||
expect(phoneChanged("+15551230001", null)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildConsent (B2 opt-in populates consent fields)", () => {
|
||||
const at = new Date("2026-11-18T12:00:00.000Z");
|
||||
|
||||
it("enables SMS and records timestamp, source, and IP", () => {
|
||||
expect(buildConsent("web_signup", "+15550000000", at)).toEqual({
|
||||
smsEnabled: true,
|
||||
smsConsentAt: at,
|
||||
smsConsentSource: "web_signup",
|
||||
smsConsentIp: "+15550000000",
|
||||
});
|
||||
});
|
||||
|
||||
it("records admin_import with a null IP when none is available", () => {
|
||||
expect(buildConsent("admin_import", null, at).smsConsentSource).toBe(
|
||||
"admin_import",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resetConsent (B2 clears consent)", () => {
|
||||
it("disables SMS and clears all evidence", () => {
|
||||
expect(resetConsent()).toEqual({
|
||||
smsEnabled: false,
|
||||
smsConsentAt: null,
|
||||
smsConsentSource: null,
|
||||
smsConsentIp: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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