create-ailk 0.19.0 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/add-module.js +7 -2
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/lib/scaffold-render-sweep.d.ts +23 -0
  5. package/dist/lib/scaffold-render-sweep.js +52 -0
  6. package/dist/module-architecture.js +9 -3
  7. package/dist/programmatic.js +8 -1
  8. package/dist/site-roster.js +21 -4
  9. package/dist/surfaces.d.ts +14 -0
  10. package/dist/surfaces.js +31 -1
  11. package/dist/topology.js +29 -1
  12. package/package.json +3 -3
  13. package/templates/apps/api/.env.example +17 -1
  14. package/templates/apps/api/CLAUDE.md +10 -4
  15. package/templates/apps/api/src/__tests__/factories/in-memory-prisma.ts +19 -0
  16. package/templates/apps/api/src/__tests__/stripe-boot.test.ts +132 -0
  17. package/templates/apps/api/src/lib/__mocks__/prisma.ts +8 -0
  18. package/templates/apps/api/src/lib/__tests__/consent-ledger-double.ts +237 -0
  19. package/templates/apps/api/src/lib/__tests__/consent-store.test.ts +334 -0
  20. package/templates/apps/api/src/lib/__tests__/stripe-admission.test.ts +217 -0
  21. package/templates/apps/api/src/lib/__tests__/stripe.test.ts +7 -0
  22. package/templates/apps/api/src/lib/consent-store.ts +335 -0
  23. package/templates/apps/api/src/lib/stripe.ts +97 -5
  24. package/templates/apps/api/src/middleware/__tests__/auth.hub-session-path.test.ts +10 -4
  25. package/templates/apps/api/src/middleware/__tests__/rate-limit.test.ts +51 -0
  26. package/templates/apps/api/src/middleware/auth.ts +11 -3
  27. package/templates/apps/api/src/middleware/rate-limit.ts +5 -0
  28. package/templates/apps/api/src/routes/checkout/__tests__/live-key.test.ts +167 -0
  29. package/templates/apps/api/src/routes/checkout/session.ts +3 -3
  30. package/templates/apps/api/src/routes/checkout/sessions.ts +6 -4
  31. package/templates/apps/api/src/routes/consent/cookie.ts +90 -0
  32. package/templates/apps/api/src/routes/consent/index.ts +18 -0
  33. package/templates/apps/api/src/routes/consent/unsubscribe.ts +127 -0
  34. package/templates/apps/api/src/routes/leads/index.ts +15 -0
  35. package/templates/apps/api/src/routes/project-listings/__tests__/drafts.test.ts +5 -0
  36. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-flow-checkout.test.ts +17 -4
  37. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-idempotency.test.ts +2 -0
  38. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-livemode-live.test.ts +159 -0
  39. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-livemode.test.ts +158 -0
  40. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-rate-limit.test.ts +2 -0
  41. package/templates/apps/api/src/routes/webhooks/stripe.ts +17 -0
  42. package/templates/apps/api/src/server.ts +111 -16
  43. package/templates/apps/api/src/services/__tests__/consent-ledger.test.ts +503 -0
  44. package/templates/apps/api/src/services/__tests__/consent.test.ts +18 -3
  45. package/templates/apps/api/src/services/__tests__/lead-routing.test.ts +115 -4
  46. package/templates/apps/api/src/services/__tests__/person.test.ts +392 -0
  47. package/templates/apps/api/src/services/__tests__/request-confirm.test.ts +87 -0
  48. package/templates/apps/api/src/services/__tests__/revalidation.test.ts +57 -17
  49. package/templates/apps/api/src/services/consent.ts +488 -3
  50. package/templates/apps/api/src/services/lead-routing.ts +105 -12
  51. package/templates/apps/api/src/services/person.ts +501 -1
  52. package/templates/apps/api/src/services/project-listings.ts +7 -2
  53. package/templates/apps/api/src/services/request-confirm.ts +52 -0
  54. package/templates/apps/api/src/services/revalidation.ts +11 -1
  55. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/account-frame-sign-out.test.tsx +175 -0
  56. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/billing.test.tsx +17 -3
  57. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/general.test.tsx +156 -0
  58. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/hub-mode.test.tsx +89 -14
  59. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/layout-sign-out.test.tsx +146 -0
  60. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/layout.test.tsx +122 -18
  61. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/notifications.test.tsx +15 -1
  62. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/security.test.tsx +290 -20
  63. package/templates/apps/web/app/[locale]/(authed)/account/__tests__/settings.test.tsx +55 -0
  64. package/templates/apps/web/app/[locale]/(authed)/account/account-frame.tsx +74 -4
  65. package/templates/apps/web/app/[locale]/(authed)/account/artifacts/page.tsx +4 -7
  66. package/templates/apps/web/app/[locale]/(authed)/account/billing/page.tsx +3 -3
  67. package/templates/apps/web/app/[locale]/(authed)/account/general/contact-actions.ts +81 -0
  68. package/templates/apps/web/app/[locale]/(authed)/account/general/page.tsx +66 -4
  69. package/templates/apps/web/app/[locale]/(authed)/account/layout.tsx +26 -3
  70. package/templates/apps/web/app/[locale]/(authed)/account/notifications/page.tsx +2 -2
  71. package/templates/apps/web/app/[locale]/(authed)/account/security/page.tsx +89 -37
  72. package/templates/apps/web/app/[locale]/(authed)/account/security/security-panel.tsx +102 -0
  73. package/templates/apps/web/app/[locale]/(authed)/account/security/security-source.ts +62 -0
  74. package/templates/apps/web/app/[locale]/(authed)/account/settings-frame.tsx +41 -33
  75. package/templates/apps/web/app/[locale]/(authed)/account/sign-in-method.ts +1 -1
  76. package/templates/apps/web/auth.ts +20 -1
  77. package/templates/apps/web/lib/__tests__/auth-email.test.ts +84 -0
  78. package/templates/apps/web/lib/auth-email.ts +45 -10
  79. package/templates/apps/web/lib/topology.generated.ts +9 -0
  80. package/templates/apps/web/messages/en.json +54 -14
  81. package/templates/database/CHANGELOG.md +16 -0
  82. package/templates/database/__tests__/consent-ledger.db.test.ts +345 -0
  83. package/templates/database/__tests__/fence-fan-out.db.test.ts +6 -2
  84. package/templates/database/__tests__/fixtures/person-contact-backfill.json +182 -0
  85. package/templates/database/__tests__/person-contact.db.test.ts +563 -0
  86. package/templates/database/__tests__/person.db.test.ts +3 -1
  87. package/templates/database/inbox/schema.prisma +76 -10
  88. package/templates/database/migrations/20261014120000_consent_ledger/migration.sql +122 -0
  89. package/templates/database/migrations/20261014120000_person_contact/migration.sql +231 -0
  90. package/templates/database/package.json +1 -1
  91. package/versions.json +35 -35
@@ -127,19 +127,41 @@ model Lead {
127
127
  /// writer wins. `planOffering` is written by a later phase (Epic 2's
128
128
  /// checkout-lead rule); this slice only carries the column.
129
129
  model Person {
130
- id String @id @default(dbgenerated("('person_'::text || (uuidv7())::text)")) @db.Text
131
- workspaceId String @map("workspace_id")
132
- email String @db.VarChar(254)
133
- name String? @db.VarChar(200)
134
- userId String? @map("user_id")
135
- billingProvider String? @map("billing_provider") @db.VarChar(32)
136
- billingCustomerId String? @map("billing_customer_id") @db.VarChar(255)
137
- planOffering String? @map("plan_offering") @db.VarChar(100)
138
- createdAt DateTime @default(now()) @map("created_at")
139
- updatedAt DateTime @updatedAt @map("updated_at")
130
+ id String @id @default(dbgenerated("('person_'::text || (uuidv7())::text)")) @db.Text
131
+ workspaceId String @map("workspace_id")
132
+ email String @db.VarChar(254)
133
+ name String? @db.VarChar(200)
134
+ userId String? @map("user_id")
135
+ billingProvider String? @map("billing_provider") @db.VarChar(32)
136
+ billingCustomerId String? @map("billing_customer_id") @db.VarChar(255)
137
+ planOffering String? @map("plan_offering") @db.VarChar(100)
138
+ // #8435 D1 — contact details, to the standard (option A). Every column is
139
+ // nullable: a capture fills only a blank one (D4), an edit overwrites (D9,
140
+ // D11). The address is the magnet's six parts, stored whole or not at all
141
+ // (people_address_complete).
142
+ givenName String? @map("given_name") @db.VarChar(100)
143
+ familyName String? @map("family_name") @db.VarChar(100)
144
+ telephone String? @db.VarChar(16)
145
+ addressLine1 String? @map("address_line1") @db.VarChar(100)
146
+ addressLine2 String? @map("address_line2") @db.VarChar(60)
147
+ addressCity String? @map("address_city") @db.VarChar(60)
148
+ addressRegion String? @map("address_region") @db.VarChar(40)
149
+ addressPostalCode String? @map("address_postal_code") @db.VarChar(20)
150
+ addressCountry String? @map("address_country") @db.Char(2)
151
+ companyName String? @map("company_name") @db.VarChar(200)
152
+ jobTitle String? @map("job_title") @db.VarChar(100)
153
+ // Who last saved the contact by hand — "operator" (the Person page) or
154
+ // "client" (their account) — and when. NULL until a human saves it; while
155
+ // NULL and `userId` is NULL, a capture may fill a blank field (D4).
156
+ contactConfirmedAt DateTime? @map("contact_confirmed_at")
157
+ contactConfirmedBy String? @map("contact_confirmed_by") @db.VarChar(16)
158
+ createdAt DateTime @default(now()) @map("created_at")
159
+ updatedAt DateTime @updatedAt @map("updated_at")
140
160
 
141
161
  pieces Piece[]
142
162
  interactions Interaction[]
163
+ callbacks Callback[]
164
+ subscribers Subscriber[]
143
165
 
144
166
  @@unique([workspaceId, email])
145
167
  @@unique([workspaceId, userId])
@@ -269,9 +291,14 @@ model Subscriber {
269
291
  email String
270
292
  source String
271
293
  createdAt DateTime @default(now()) @map("created_at")
294
+ // #8435 D1/D5 — the Person this subscription belongs to; composite FK onto
295
+ // people(id, workspace_id), the #7877 shape. NULL is not checked.
296
+ personId String? @map("person_id")
297
+ person Person? @relation(fields: [personId, workspaceId], references: [id, workspaceId], onDelete: Restrict, map: "subscribers_person_fk")
272
298
 
273
299
  @@unique([siteId, email])
274
300
  @@index([siteId])
301
+ @@index([personId])
275
302
  @@index([workspaceId])
276
303
  @@map("subscribers")
277
304
  }
@@ -338,7 +365,14 @@ model Callback {
338
365
  /// that update IS the durable idempotency key.
339
366
  paidAt DateTime? @map("paid_at")
340
367
 
368
+ // #8435 D1/D5 — the Person this request belongs to (its payload.email, or
369
+ // the buyer email at confirm). Composite FK onto people(id, workspace_id):
370
+ // "a Person of the same workspace" is a database fact. NULL is not checked.
371
+ personId String? @map("person_id")
372
+ person Person? @relation(fields: [personId, workspaceId], references: [id, workspaceId], onDelete: Restrict, map: "callbacks_person_fk")
373
+
341
374
  @@index([siteId])
375
+ @@index([personId])
342
376
  @@index([createdAt])
343
377
  @@index([stripePaymentLinkId])
344
378
  @@index([stripeInvoiceId])
@@ -1243,6 +1277,18 @@ model ConsentGrant {
1243
1277
  locale String
1244
1278
  statement String @db.Text
1245
1279
  statementHash String @map("statement_hash")
1280
+ // #8438 D2 — the Person this row counts toward. A plain scalar with no
1281
+ // foreign key (this table's own D10 posture): a row about a visitor or a
1282
+ // listing has none, and a ledger row must outlive anything it names.
1283
+ personId String? @map("person_id")
1284
+ // #8438 D2 — where the answer came from (CONSENT_SOURCES). NOT NULL with no
1285
+ // default after the migration: every new row must say. Rows stored before
1286
+ // this column read `legacy`.
1287
+ source String @db.Text
1288
+ // The magnet slug when `source` is `magnet`.
1289
+ sourceRef String? @map("source_ref") @db.VarChar(200)
1290
+ // The registry's policy version the person read. NULL only for `legacy` rows.
1291
+ policyVersion Int? @map("policy_version")
1246
1292
  createdAt DateTime @default(now()) @map("created_at")
1247
1293
 
1248
1294
  // "What did this subject agree to" — every row for one subject, newest last.
@@ -1250,9 +1296,29 @@ model ConsentGrant {
1250
1296
  // "Who agreed to this purpose" — the cross-surface count the id exists for (D1/D2).
1251
1297
  @@index([siteId, purposeId])
1252
1298
  @@index([workspaceId])
1299
+ // #8438 D2 — "current state per purpose" for one Person (DISTINCT ON).
1300
+ @@index([workspaceId, personId, purposeId, createdAt])
1253
1301
  @@map("consent_grants")
1254
1302
  }
1255
1303
 
1304
+ // #8438 D2 — links a banner visitor (a random browser-held id) to the Person a
1305
+ // later form or sign-in identifies, so the Person's cookie history can include
1306
+ // the visitor's own rows without ever editing them. Append-only like the
1307
+ // ledger: a database trigger refuses any change to a stored row. No relation
1308
+ // directive (ADR-0009 invariant #1); no IP, user-agent or device column.
1309
+ model ConsentVisitorLink {
1310
+ id String @id @default(dbgenerated("('cvlink_'::text || (uuidv7())::text)")) @db.Text
1311
+ workspaceId String @map("workspace_id")
1312
+ visitorId String @map("visitor_id")
1313
+ personId String @map("person_id")
1314
+ source String @db.Text
1315
+ createdAt DateTime @default(now()) @map("created_at")
1316
+
1317
+ @@unique([workspaceId, visitorId, personId])
1318
+ @@index([workspaceId, personId])
1319
+ @@map("consent_visitor_links")
1320
+ }
1321
+
1256
1322
  /// Appointment — a Cal.com booking, normalized from the webhook receiver (#6085, brief H7).
1257
1323
  ///
1258
1324
  /// siteId-scoped with no FK, like every other inbox model (ADR 0009 D2). The
@@ -0,0 +1,122 @@
1
+ -- #8438 — the consent ledger (spec docs/specs/8438-consent-registry-and-ledger-step-spec.md D2).
2
+ --
3
+ -- consent_grants (inbox) — the existing append-only table (#5275)
4
+ -- becomes THE ledger. It gains the Person a
5
+ -- row counts toward, the source it came
6
+ -- from, the magnet slug, and the policy
7
+ -- version read. Every row stored before this
8
+ -- migration reads source = 'legacy' with no
9
+ -- policy version, honestly: those rows
10
+ -- recorded neither. Every row written after
11
+ -- it must name its source and version.
12
+ -- consent_visitor_links (inbox) — a banner visitor id linked to the Person a
13
+ -- later form or sign-in identifies. A second
14
+ -- append-only table, so a visitor's own rows
15
+ -- are never edited to point at a Person.
16
+ --
17
+ -- Both tables are APPEND-ONLY, and now the database says so: a trigger refuses
18
+ -- any change to a stored row and any truncation. The triggers bind the
19
+ -- application's database role; a table owner can still disable them. A later
20
+ -- migration that must rewrite or erase rows disables them explicitly inside its
21
+ -- own transaction, which makes the act a reviewed migration rather than
22
+ -- something application code can do.
23
+ --
24
+ -- person_id is a plain scalar with no foreign key, the table's own posture
25
+ -- (ADR 0009 invariant 1, #5275 D10): a row about a visitor or a listing has no
26
+ -- Person, and a ledger row must outlive anything it names. Neither table
27
+ -- carries any network or browser evidence column (spec section 2.3).
28
+ --
29
+ -- ADDITIVE: ADD COLUMN with a constant default, then the default dropped, so no
30
+ -- existing row fails a new CHECK. Deploy this migration BEFORE any code that
31
+ -- writes the new columns; code written before it never names them.
32
+ --
33
+ -- Rollback (safe while nothing but #8438 writes the new surface):
34
+ -- DROP TABLE "consent_visitor_links";
35
+ -- ALTER TABLE "consent_grants" DISABLE TRIGGER USER;
36
+ -- ALTER TABLE "consent_grants"
37
+ -- DROP COLUMN "person_id", DROP COLUMN "source", DROP COLUMN "source_ref", DROP COLUMN "policy_version";
38
+ -- ALTER TABLE "consent_grants" ENABLE TRIGGER USER; -- after dropping the three triggers and the function
39
+ -- DROP FUNCTION "consent_ledger_append_only"();
40
+
41
+ -- AlterTable
42
+ -- source: every row already stored reads 'legacy'; the default is dropped
43
+ -- straight after, so every new row has to say where it came from.
44
+ ALTER TABLE "consent_grants" ADD COLUMN "person_id" TEXT,
45
+ ADD COLUMN "source" TEXT NOT NULL DEFAULT 'legacy',
46
+ ADD COLUMN "source_ref" VARCHAR(200),
47
+ ADD COLUMN "policy_version" INTEGER;
48
+ ALTER TABLE "consent_grants" ALTER COLUMN "source" DROP DEFAULT;
49
+
50
+ -- CreateIndex
51
+ -- "Current state per purpose" for one Person: DISTINCT ON (purpose_id) over
52
+ -- workspace_id = $1 AND person_id = $2 ORDER BY purpose_id, created_at DESC.
53
+ CREATE INDEX "consent_grants_workspace_id_person_id_purpose_id_created_at_idx" ON "consent_grants"("workspace_id", "person_id", "purpose_id", "created_at");
54
+
55
+ -- The source vocabulary is packages/validation CONSENT_SOURCES. 'legacy' is
56
+ -- read-only in practice: only the default above ever writes it.
57
+ ALTER TABLE "consent_grants" ADD CONSTRAINT "consent_grants_source_known"
58
+ CHECK ("source" IN ('cookie_banner', 'sign_up', 'magnet', 'account_settings', 'email_unsubscribe', 'api', 'legacy'));
59
+ -- A new row records the policy version it was read at; only a legacy row may not.
60
+ ALTER TABLE "consent_grants" ADD CONSTRAINT "consent_grants_policy_version_present"
61
+ CHECK ("source" = 'legacy' OR "policy_version" IS NOT NULL);
62
+ ALTER TABLE "consent_grants" ADD CONSTRAINT "consent_grants_policy_version_positive"
63
+ CHECK ("policy_version" IS NULL OR "policy_version" >= 1);
64
+ ALTER TABLE "consent_grants" ADD CONSTRAINT "consent_grants_person_id_format"
65
+ CHECK ("person_id" IS NULL OR "person_id" ~ '^person_[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$');
66
+
67
+ -- CreateTable
68
+ CREATE TABLE "consent_visitor_links" (
69
+ "id" TEXT NOT NULL DEFAULT ('cvlink_'::text || (uuidv7())::text),
70
+ "workspace_id" TEXT NOT NULL,
71
+ "visitor_id" TEXT NOT NULL,
72
+ "person_id" TEXT NOT NULL,
73
+ "source" TEXT NOT NULL,
74
+ "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
75
+
76
+ CONSTRAINT "consent_visitor_links_pkey" PRIMARY KEY ("id")
77
+ );
78
+
79
+ -- CreateIndex
80
+ -- A repeat link is a no-op: the writer inserts and ignores a conflict here.
81
+ CREATE UNIQUE INDEX "consent_visitor_links_workspace_id_visitor_id_person_id_key" ON "consent_visitor_links"("workspace_id", "visitor_id", "person_id");
82
+
83
+ -- CreateIndex
84
+ -- A Person's linked visitors: workspace_id = $1 AND person_id = $2.
85
+ CREATE INDEX "consent_visitor_links_workspace_id_person_id_idx" ON "consent_visitor_links"("workspace_id", "person_id");
86
+
87
+ -- ADR-0031 rule 3 — consent_visitor_links: cvlink_ (registered in
88
+ -- packages/ids/src/registry.ts ENTITY_PREFIX.consentVisitorLink; this
89
+ -- migration's path is added to migration-sync.test.ts's PK_CHECK_MIGRATIONS).
90
+ -- Each reference column carries the format CHECK of the entity it names.
91
+ ALTER TABLE "consent_visitor_links" ALTER COLUMN "id" TYPE text COLLATE "C";
92
+ ALTER TABLE "consent_visitor_links" ALTER COLUMN "id" SET DEFAULT ('cvlink_'::text || (uuidv7())::text);
93
+ ALTER TABLE "consent_visitor_links" ADD CONSTRAINT "consent_visitor_links_id_format" CHECK ("id" ~ '^cvlink_[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$');
94
+ ALTER TABLE "consent_visitor_links" ADD CONSTRAINT "consent_visitor_links_workspace_id_format" CHECK ("workspace_id" ~ '^wksp_[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$');
95
+ ALTER TABLE "consent_visitor_links" ADD CONSTRAINT "consent_visitor_links_person_id_format" CHECK ("person_id" ~ '^person_[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$');
96
+ -- A visitor id is a random version-4 UUID the site's own server minted.
97
+ ALTER TABLE "consent_visitor_links" ADD CONSTRAINT "consent_visitor_links_visitor_id_format" CHECK ("visitor_id" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$');
98
+ ALTER TABLE "consent_visitor_links" ADD CONSTRAINT "consent_visitor_links_source_known"
99
+ CHECK ("source" IN ('magnet', 'api', 'account_settings'));
100
+
101
+ -- SEC-8438-7 — append-only, enforced. One function, three triggers per table.
102
+ CREATE FUNCTION "consent_ledger_append_only"() RETURNS trigger
103
+ LANGUAGE plpgsql AS $$
104
+ BEGIN
105
+ RAISE EXCEPTION '% is append-only', TG_TABLE_NAME
106
+ USING ERRCODE = 'restrict_violation';
107
+ END;
108
+ $$;
109
+
110
+ CREATE TRIGGER "consent_grants_no_update" BEFORE UPDATE ON "consent_grants"
111
+ FOR EACH ROW EXECUTE FUNCTION "consent_ledger_append_only"();
112
+ CREATE TRIGGER "consent_grants_no_delete" BEFORE DELETE ON "consent_grants"
113
+ FOR EACH ROW EXECUTE FUNCTION "consent_ledger_append_only"();
114
+ CREATE TRIGGER "consent_grants_no_truncate" BEFORE TRUNCATE ON "consent_grants"
115
+ FOR EACH STATEMENT EXECUTE FUNCTION "consent_ledger_append_only"();
116
+
117
+ CREATE TRIGGER "consent_visitor_links_no_update" BEFORE UPDATE ON "consent_visitor_links"
118
+ FOR EACH ROW EXECUTE FUNCTION "consent_ledger_append_only"();
119
+ CREATE TRIGGER "consent_visitor_links_no_delete" BEFORE DELETE ON "consent_visitor_links"
120
+ FOR EACH ROW EXECUTE FUNCTION "consent_ledger_append_only"();
121
+ CREATE TRIGGER "consent_visitor_links_no_truncate" BEFORE TRUNCATE ON "consent_visitor_links"
122
+ FOR EACH STATEMENT EXECUTE FUNCTION "consent_ledger_append_only"();
@@ -0,0 +1,231 @@
1
+ -- #8435 — contact details on the client record (spec
2
+ -- docs/specs/8435-contact-details-on-the-client-spec.md D1, D2).
3
+ --
4
+ -- people (13 columns) — given name, family name, an E.164 telephone, the
5
+ -- magnet's six-part address, a company name, a job title,
6
+ -- and the confirmation pair (who last saved the contact by
7
+ -- hand, and when). Every column is nullable and has no
8
+ -- default, so Postgres changes only the catalog.
9
+ -- callbacks.person_id, subscribers.person_id — the Person a request or a
10
+ -- subscription belongs to: a composite foreign key onto
11
+ -- people(id, workspace_id), the #7877 shape, so "a Person
12
+ -- of the row's own workspace" is a database fact. NULL is
13
+ -- not checked (MATCH SIMPLE).
14
+ --
15
+ -- PURELY ADDITIVE, DDL ONLY. No existing row's values change here: the
16
+ -- backfill is the next migration, 20261014121000_person_contact_backfill,
17
+ -- split out so its row updates never run under the exclusive locks these
18
+ -- ALTER TABLEs take. Prisma runs this file as one transaction, so each ALTER
19
+ -- TABLE's ACCESS EXCLUSIVE lock on "people", "callbacks" and "subscribers"
20
+ -- is held until the file commits. Every CHECK and foreign key below is added
21
+ -- NOT VALID and then VALIDATEd over an all-NULL column or a table with no
22
+ -- person_id yet, and none of the three is the large "leads" table; the window
23
+ -- is accepted, as #852 accepted the same on "leads" (SEC-852-E, SEC-8435-C).
24
+ --
25
+ -- Rollout: deploy this migration BEFORE the API code that writes the columns;
26
+ -- code without them fails every callback and subscription write.
27
+ --
28
+ -- Rollback: ALTER TABLE "callbacks" DROP CONSTRAINT "callbacks_person_fk",
29
+ -- DROP COLUMN "person_id"; ALTER TABLE "subscribers" DROP CONSTRAINT
30
+ -- "subscribers_person_fk", DROP COLUMN "person_id"; ALTER TABLE "people"
31
+ -- DROP COLUMN "given_name", DROP COLUMN "family_name", DROP COLUMN
32
+ -- "telephone", DROP COLUMN "address_line1", DROP COLUMN "address_line2",
33
+ -- DROP COLUMN "address_city", DROP COLUMN "address_region", DROP COLUMN
34
+ -- "address_postal_code", DROP COLUMN "address_country", DROP COLUMN
35
+ -- "company_name", DROP COLUMN "job_title", DROP COLUMN "contact_confirmed_at",
36
+ -- DROP COLUMN "contact_confirmed_by"; (the indexes and CHECKs go with them).
37
+
38
+ -- AlterTable
39
+ ALTER TABLE "people" ADD COLUMN "address_city" VARCHAR(60),
40
+ ADD COLUMN "address_country" CHAR(2),
41
+ ADD COLUMN "address_line1" VARCHAR(100),
42
+ ADD COLUMN "address_line2" VARCHAR(60),
43
+ ADD COLUMN "address_postal_code" VARCHAR(20),
44
+ ADD COLUMN "address_region" VARCHAR(40),
45
+ ADD COLUMN "company_name" VARCHAR(200),
46
+ ADD COLUMN "contact_confirmed_at" TIMESTAMP(3),
47
+ ADD COLUMN "contact_confirmed_by" VARCHAR(16),
48
+ ADD COLUMN "family_name" VARCHAR(100),
49
+ ADD COLUMN "given_name" VARCHAR(100),
50
+ ADD COLUMN "job_title" VARCHAR(100),
51
+ ADD COLUMN "telephone" VARCHAR(16);
52
+
53
+ -- AlterTable
54
+ ALTER TABLE "callbacks" ADD COLUMN "person_id" TEXT;
55
+
56
+ -- AlterTable
57
+ ALTER TABLE "subscribers" ADD COLUMN "person_id" TEXT;
58
+
59
+ -- CreateIndex
60
+ CREATE INDEX "callbacks_person_id_idx" ON "callbacks"("person_id");
61
+
62
+ -- CreateIndex
63
+ CREATE INDEX "subscribers_person_id_idx" ON "subscribers"("person_id");
64
+
65
+ -- A stored telephone is E.164 (the form the phone field posts) or NULL.
66
+ ALTER TABLE "people" ADD CONSTRAINT "people_telephone_e164"
67
+ CHECK ("telephone" IS NULL OR "telephone" ~ '^\+[1-9][0-9]{6,14}$') NOT VALID;
68
+ ALTER TABLE "people" VALIDATE CONSTRAINT "people_telephone_e164";
69
+
70
+ -- The country is the upper-case ISO 3166-1 alpha-2 code.
71
+ ALTER TABLE "people" ADD CONSTRAINT "people_address_country_format"
72
+ CHECK ("address_country" IS NULL OR "address_country" ~ '^[A-Z]{2}$') NOT VALID;
73
+ ALTER TABLE "people" VALIDATE CONSTRAINT "people_address_country_format";
74
+
75
+ -- An address is stored whole or not at all: all six parts NULL, or every
76
+ -- part but the optional second line present.
77
+ ALTER TABLE "people" ADD CONSTRAINT "people_address_complete"
78
+ CHECK (
79
+ ("address_line1" IS NULL AND "address_line2" IS NULL AND "address_city" IS NULL AND "address_region" IS NULL AND "address_postal_code" IS NULL AND "address_country" IS NULL)
80
+ OR
81
+ ("address_line1" IS NOT NULL AND "address_city" IS NOT NULL AND "address_region" IS NOT NULL AND "address_postal_code" IS NOT NULL AND "address_country" IS NOT NULL)
82
+ ) NOT VALID;
83
+ ALTER TABLE "people" VALIDATE CONSTRAINT "people_address_complete";
84
+
85
+ -- The confirmation pair is written together, and the actor is one of two.
86
+ ALTER TABLE "people" ADD CONSTRAINT "people_contact_confirmed"
87
+ CHECK (
88
+ ("contact_confirmed_at" IS NULL AND "contact_confirmed_by" IS NULL)
89
+ OR
90
+ ("contact_confirmed_at" IS NOT NULL AND "contact_confirmed_by" IS NOT NULL AND "contact_confirmed_by" IN ('operator', 'client'))
91
+ ) NOT VALID;
92
+ ALTER TABLE "people" VALIDATE CONSTRAINT "people_contact_confirmed";
93
+
94
+ -- The two new reference columns carry the person_ format CHECK (the #852
95
+ -- shape), added NOT VALID and then validated over an all-NULL column.
96
+ ALTER TABLE "callbacks" ADD CONSTRAINT "callbacks_person_id_format" CHECK ("person_id" ~ '^person_[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') NOT VALID;
97
+ ALTER TABLE "callbacks" VALIDATE CONSTRAINT "callbacks_person_id_format";
98
+ ALTER TABLE "subscribers" ADD CONSTRAINT "subscribers_person_id_format" CHECK ("person_id" ~ '^person_[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') NOT VALID;
99
+ ALTER TABLE "subscribers" VALIDATE CONSTRAINT "subscribers_person_id_format";
100
+
101
+ -- AddForeignKey
102
+ -- A callback / subscriber names a Person of ITS OWN workspace; deleting a
103
+ -- Person with one is refused (the pieces_person_fk shape, #7877).
104
+ ALTER TABLE "callbacks" ADD CONSTRAINT "callbacks_person_fk" FOREIGN KEY ("person_id", "workspace_id") REFERENCES "people"("id", "workspace_id") ON DELETE RESTRICT ON UPDATE CASCADE NOT VALID;
105
+ ALTER TABLE "callbacks" VALIDATE CONSTRAINT "callbacks_person_fk";
106
+ ALTER TABLE "subscribers" ADD CONSTRAINT "subscribers_person_fk" FOREIGN KEY ("person_id", "workspace_id") REFERENCES "people"("id", "workspace_id") ON DELETE RESTRICT ON UPDATE CASCADE NOT VALID;
107
+ ALTER TABLE "subscribers" VALIDATE CONSTRAINT "subscribers_person_fk";
108
+
109
+ -- ===== #8435 contact backfill (idempotent; runs after the DDL above) =====
110
+ -- #8435 — the contact backfill (spec
111
+ -- docs/specs/8435-contact-details-on-the-client-spec.md D2).
112
+ --
113
+ -- Gives every newsletter subscriber and every callback that carries a
114
+ -- storable email a Person, stamps subscribers.person_id and
115
+ -- callbacks.person_id, and fills a Person's blank telephone from the oldest
116
+ -- callback phone that is already E.164 once spaces and punctuation are
117
+ -- removed. Set-based and idempotent: every insert is ON CONFLICT DO NOTHING
118
+ -- and every update is guarded by IS NULL, so running this text twice changes
119
+ -- nothing the first run wrote.
120
+ --
121
+ --
122
+ -- Normalization — the SQL twin of apps/api/src/services/person.ts's
123
+ -- normalizePersonEmail / isStorablePersonEmail (#852's expressions verbatim):
124
+ -- N(x) = lower(btrim(x, E' \t\n\r\f\x0B') COLLATE "C")
125
+ -- (\x0B is vertical tab; an E-string has no \v escape — E'\v'
126
+ -- is the letter v, which would strip the v of every .dev)
127
+ -- VALID(e) = e ~ '^[!-?A-~]+@[!-?A-~]+$' AND length(e) <= 254
128
+ -- NAME(x) = C0/C1 controls and bidi controls removed, trimmed, cut to
129
+ -- 200 characters; empty → NULL
130
+ -- A row whose email is not VALID makes no Person and keeps person_id NULL.
131
+ --
132
+ -- A callback's email is its payload.email when that is a JSON string and
133
+ -- VALID, else its buyer_email when VALID — the order the live code links in
134
+ -- (capture first, the confirm step only when capture left it NULL;
135
+ -- SEC-8435-G).
136
+ --
137
+ -- Rollback: nothing to undo on its own — dropping 20261014120000_person_contact's
138
+ -- columns removes the stamps and the telephones this wrote; the Persons it
139
+ -- created are ordinary Persons and are left in place.
140
+
141
+ -- 1. Subscribers, oldest first: one Person per (workspace, normalized email).
142
+ INSERT INTO "people" ("workspace_id", "email", "created_at", "updated_at")
143
+ SELECT DISTINCT ON (s."workspace_id", lower(btrim(s."email", E' \t\n\r\f\x0B') COLLATE "C"))
144
+ s."workspace_id",
145
+ lower(btrim(s."email", E' \t\n\r\f\x0B') COLLATE "C"),
146
+ s."created_at",
147
+ now()
148
+ FROM "subscribers" s
149
+ WHERE lower(btrim(s."email", E' \t\n\r\f\x0B') COLLATE "C") ~ '^[!-?A-~]+@[!-?A-~]+$'
150
+ AND length(lower(btrim(s."email", E' \t\n\r\f\x0B') COLLATE "C")) <= 254
151
+ ORDER BY s."workspace_id", lower(btrim(s."email", E' \t\n\r\f\x0B') COLLATE "C"), s."created_at" ASC, s."id" ASC
152
+ ON CONFLICT ("workspace_id", "email") DO NOTHING;
153
+
154
+ -- 2. Callbacks, oldest first: one Person per (workspace, normalized email),
155
+ -- named from the callback's own name. A Person a lead or a subscriber
156
+ -- already made keeps its name and created_at (ON CONFLICT DO NOTHING).
157
+ INSERT INTO "people" ("workspace_id", "email", "name", "created_at", "updated_at")
158
+ SELECT DISTINCT ON (ce."workspace_id", ce."email_n")
159
+ ce."workspace_id",
160
+ ce."email_n",
161
+ NULLIF(left(btrim(regexp_replace(ce."name", '[\x01-\x1F\x7F-\x9F‎‏‪-‮⁦-⁩]', '', 'g')), 200), ''),
162
+ ce."created_at",
163
+ now()
164
+ FROM (
165
+ SELECT c."id", c."workspace_id", c."name", c."phone", c."created_at",
166
+ (
167
+ SELECT v."e"
168
+ FROM (VALUES
169
+ (1, lower(btrim(CASE WHEN jsonb_typeof(c."payload" -> 'email') = 'string' THEN c."payload" ->> 'email' END, E' \t\n\r\f\x0B') COLLATE "C")),
170
+ (2, lower(btrim(c."buyer_email", E' \t\n\r\f\x0B') COLLATE "C"))
171
+ ) v("ord", "e")
172
+ WHERE v."e" ~ '^[!-?A-~]+@[!-?A-~]+$' AND length(v."e") <= 254
173
+ ORDER BY v."ord"
174
+ LIMIT 1
175
+ ) AS "email_n"
176
+ FROM "callbacks" c
177
+ ) ce
178
+ WHERE ce."email_n" IS NOT NULL
179
+ ORDER BY ce."workspace_id", ce."email_n", ce."created_at" ASC, ce."id" ASC
180
+ ON CONFLICT ("workspace_id", "email") DO NOTHING;
181
+
182
+ -- 3. Stamps: every subscriber and callback with a storable email points at
183
+ -- its workspace's Person; a row already stamped is left alone.
184
+ UPDATE "subscribers"
185
+ SET "person_id" = p."id"
186
+ FROM "people" p
187
+ WHERE "subscribers"."person_id" IS NULL
188
+ AND p."workspace_id" = "subscribers"."workspace_id"
189
+ AND p."email" = lower(btrim("subscribers"."email", E' \t\n\r\f\x0B') COLLATE "C");
190
+
191
+ UPDATE "callbacks"
192
+ SET "person_id" = p."id"
193
+ FROM (
194
+ SELECT c."id" AS "callback_id", c."workspace_id",
195
+ (
196
+ SELECT v."e"
197
+ FROM (VALUES
198
+ (1, lower(btrim(CASE WHEN jsonb_typeof(c."payload" -> 'email') = 'string' THEN c."payload" ->> 'email' END, E' \t\n\r\f\x0B') COLLATE "C")),
199
+ (2, lower(btrim(c."buyer_email", E' \t\n\r\f\x0B') COLLATE "C"))
200
+ ) v("ord", "e")
201
+ WHERE v."e" ~ '^[!-?A-~]+@[!-?A-~]+$' AND length(v."e") <= 254
202
+ ORDER BY v."ord"
203
+ LIMIT 1
204
+ ) AS "email_n"
205
+ FROM "callbacks" c
206
+ WHERE c."person_id" IS NULL
207
+ ) ce
208
+ JOIN "people" p ON p."workspace_id" = ce."workspace_id" AND p."email" = ce."email_n"
209
+ WHERE "callbacks"."id" = ce."callback_id"
210
+ AND "callbacks"."person_id" IS NULL;
211
+
212
+ -- 4. Telephone: for an uncurated Person (no linked user, never confirmed by
213
+ -- hand — the rule a capture obeys, D4) with no telephone, the oldest
214
+ -- stamped callback's phone with spaces and punctuation removed, when that
215
+ -- is E.164. A national-format phone fills nothing.
216
+ UPDATE "people" p
217
+ SET "telephone" = c."tel",
218
+ "updated_at" = now()
219
+ FROM (
220
+ SELECT DISTINCT ON (cb."person_id")
221
+ cb."person_id",
222
+ regexp_replace(cb."phone", '[ ().-]', '', 'g') AS "tel"
223
+ FROM "callbacks" cb
224
+ WHERE cb."person_id" IS NOT NULL
225
+ AND regexp_replace(cb."phone", '[ ().-]', '', 'g') ~ '^\+[1-9][0-9]{6,14}$'
226
+ ORDER BY cb."person_id", cb."created_at" ASC, cb."id" ASC
227
+ ) c
228
+ WHERE p."id" = c."person_id"
229
+ AND p."telephone" IS NULL
230
+ AND p."user_id" IS NULL
231
+ AND p."contact_confirmed_at" IS NULL;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@working-theory/database",
3
- "version": "0.39.0",
3
+ "version": "0.40.0",
4
4
  "license": "Apache-2.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/versions.json CHANGED
@@ -1,53 +1,53 @@
1
1
  {
2
- "@working-theory/admin-ui": "0.39.0",
3
- "@working-theory/aeo": "0.39.0",
4
- "@working-theory/agent-context": "0.39.0",
2
+ "@working-theory/admin-ui": "0.40.0",
3
+ "@working-theory/aeo": "0.40.0",
4
+ "@working-theory/agent-context": "0.40.0",
5
5
  "@working-theory/agent-contract": "0.1.0",
6
6
  "@working-theory/agent-memory": "0.0.0",
7
7
  "@working-theory/ai": "0.1.0",
8
- "@working-theory/analytics": "0.39.0",
9
- "@working-theory/api-client": "0.39.0",
8
+ "@working-theory/analytics": "0.40.0",
9
+ "@working-theory/api-client": "0.40.0",
10
10
  "@working-theory/cda-filesystem": "0.0.0",
11
- "@working-theory/cli": "0.39.0",
12
- "@working-theory/common": "0.39.0",
11
+ "@working-theory/cli": "0.40.0",
12
+ "@working-theory/common": "0.40.0",
13
13
  "@working-theory/consent": "0.1.0",
14
- "@working-theory/content-adapters": "0.39.0",
15
- "@working-theory/content-projection": "0.39.0",
16
- "@working-theory/email": "0.39.0",
14
+ "@working-theory/content-adapters": "0.40.0",
15
+ "@working-theory/content-projection": "0.40.0",
16
+ "@working-theory/email": "0.40.0",
17
17
  "@working-theory/env": "0.0.0",
18
- "@working-theory/eslint-config": "0.39.0",
19
- "@working-theory/fumadocs-schema": "0.39.0",
20
- "@working-theory/ids": "0.12.0",
21
- "@working-theory/jsonld": "0.39.0",
22
- "@working-theory/license": "0.39.0",
23
- "@working-theory/metadata": "0.39.0",
24
- "@working-theory/observability": "0.39.0",
25
- "@working-theory/page-renderer": "0.39.0",
18
+ "@working-theory/eslint-config": "0.40.0",
19
+ "@working-theory/fumadocs-schema": "0.40.0",
20
+ "@working-theory/ids": "0.13.0",
21
+ "@working-theory/jsonld": "0.40.0",
22
+ "@working-theory/license": "0.40.0",
23
+ "@working-theory/metadata": "0.40.0",
24
+ "@working-theory/observability": "0.40.0",
25
+ "@working-theory/page-renderer": "0.40.0",
26
26
  "@working-theory/plugin-mcp": "0.1.3",
27
- "@working-theory/schema": "0.39.0",
28
- "@working-theory/schema-pack-registry": "0.39.0",
29
- "@working-theory/schema-validator": "0.39.0",
30
- "@working-theory/sections-registry": "0.39.0",
31
- "@working-theory/site-config": "0.39.0",
32
- "@working-theory/spoke-checkout": "0.5.0",
33
- "@working-theory/stripe-client": "0.39.0",
34
- "@working-theory/templates": "0.39.0",
35
- "@working-theory/theme": "0.39.0",
36
- "@working-theory/ui": "0.39.0",
37
- "@working-theory/validation": "0.39.0",
38
- "@working-theory/web-i18n": "0.39.0",
27
+ "@working-theory/schema": "0.40.0",
28
+ "@working-theory/schema-pack-registry": "0.40.0",
29
+ "@working-theory/schema-validator": "0.40.0",
30
+ "@working-theory/sections-registry": "0.40.0",
31
+ "@working-theory/site-config": "0.40.0",
32
+ "@working-theory/spoke-checkout": "0.5.1",
33
+ "@working-theory/stripe-client": "0.40.0",
34
+ "@working-theory/templates": "0.40.0",
35
+ "@working-theory/theme": "0.40.0",
36
+ "@working-theory/ui": "0.40.0",
37
+ "@working-theory/validation": "0.40.0",
38
+ "@working-theory/web-i18n": "0.40.0",
39
39
  "@working-theory/billing-client": "0.0.0",
40
40
  "@working-theory/branding": "0.0.0",
41
41
  "@working-theory/license-client": "0.0.0",
42
42
  "@working-theory/license-manifest": "0.0.0",
43
43
  "@working-theory/partner-session": "0.0.0",
44
44
  "@working-theory/preview-protocol": "0.0.0",
45
- "@working-theory/ui-commerce": "0.23.17",
46
- "@working-theory/ui-docs-blog": "0.23.17",
47
- "@working-theory/ui-scheduling": "0.23.17",
48
- "@working-theory/ui-signal-collectors": "0.25.6",
45
+ "@working-theory/ui-commerce": "0.23.18",
46
+ "@working-theory/ui-docs-blog": "0.23.18",
47
+ "@working-theory/ui-scheduling": "0.23.18",
48
+ "@working-theory/ui-signal-collectors": "0.25.7",
49
49
  "@working-theory/web": "0.0.0",
50
50
  "@working-theory/api": "1.0.0",
51
51
  "@working-theory/mcp": "0.0.0",
52
- "@working-theory/database": "0.39.0"
52
+ "@working-theory/database": "0.40.0"
53
53
  }