create-ailk 0.23.0 → 0.25.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 (604) hide show
  1. package/README.md +68 -33
  2. package/component-catalog.json +3 -3
  3. package/component-catalog.md +3 -3
  4. package/dist/add-module.js +10 -3
  5. package/dist/add-route.d.ts +12 -1
  6. package/dist/add-route.js +38 -4
  7. package/dist/cli.js +12 -7
  8. package/dist/derive-architecture.d.ts +3 -4
  9. package/dist/derive-architecture.js +75 -22
  10. package/dist/lib/design-checks.d.ts +9 -4
  11. package/dist/lib/design-checks.js +13 -7
  12. package/dist/lib/extract-module-bundle.js +29 -2
  13. package/dist/lib/fetch-template-snapshot.d.ts +1 -1
  14. package/dist/lib/fetch-template-snapshot.js +7 -3
  15. package/dist/lib/import-graph.d.ts +1 -1
  16. package/dist/lib/import-graph.js +4 -4
  17. package/dist/lib/module-license-gate.d.ts +12 -1
  18. package/dist/lib/module-license-gate.js +34 -12
  19. package/dist/lib/scaffold-render-sweep.d.ts +33 -0
  20. package/dist/lib/scaffold-render-sweep.js +45 -0
  21. package/dist/module-architecture.d.ts +20 -3
  22. package/dist/module-architecture.js +122 -538
  23. package/dist/paid-sections.d.ts +25 -1
  24. package/dist/paid-sections.js +88 -3
  25. package/dist/surfaces.js +10 -2
  26. package/dist/sync-routes.d.ts +30 -4
  27. package/dist/sync-routes.js +271 -18
  28. package/dist/template-record.d.ts +14 -4
  29. package/dist/template-record.js +23 -5
  30. package/dist/topology.d.ts +16 -3
  31. package/dist/topology.js +31 -19
  32. package/dist/update.d.ts +10 -1
  33. package/dist/update.js +540 -16
  34. package/package.json +3 -3
  35. package/release-scope.json +1 -1
  36. package/templates/.claude/cloudparity-repo-hooks.json +5 -0
  37. package/templates/.claude/hooks/template-guard.sh +137 -0
  38. package/templates/.claude/rules/extending.md +106 -0
  39. package/templates/.claude/rules/loop-contract.md +2 -2
  40. package/templates/.claude/settings.json +11 -0
  41. package/templates/.env.example +9 -5
  42. package/templates/.github/workflows/ci.yml +7 -4
  43. package/templates/CLAUDE.md +15 -3
  44. package/templates/CONVENTIONS.md +8 -10
  45. package/templates/README.md +14 -13
  46. package/templates/apps/api/.env.example +9 -2
  47. package/templates/apps/api/CLAUDE.md +122 -82
  48. package/templates/apps/api/jest.config.cjs +15 -5
  49. package/templates/apps/api/package.json +16 -20
  50. package/templates/apps/api/src/__tests__/trusted-origins.server.test.ts +2 -2
  51. package/templates/apps/api/src/__tests__/vercel-handler.test.ts +4 -4
  52. package/templates/apps/api/src/index.ts +1 -1
  53. package/templates/apps/api/src/lib/__mocks__/prisma.ts +7 -4
  54. package/templates/apps/api/src/modules/commerce.ts +10 -0
  55. package/templates/apps/api/src/modules/content-write.ts +10 -0
  56. package/templates/apps/api/src/modules/multi-tenant.ts +10 -0
  57. package/templates/apps/api/src/modules/scheduling.ts +10 -0
  58. package/templates/apps/api/src/modules/signal-collectors.ts +10 -0
  59. package/templates/apps/api/src/modules/workspace.ts +10 -0
  60. package/templates/apps/api/src/server.ts +117 -1857
  61. package/templates/apps/api/vercel.json +2 -2
  62. package/templates/apps/mcp/CLAUDE.md +77 -24
  63. package/templates/apps/mcp/__tests__/app-entry.test.ts +4 -254
  64. package/templates/apps/mcp/__tests__/boot-file.test.ts +92 -0
  65. package/templates/apps/mcp/jest.config.cjs +6 -1
  66. package/templates/apps/mcp/package.json +5 -12
  67. package/templates/apps/mcp/src/app.ts +8 -7
  68. package/templates/apps/mcp/src/index.ts +6 -34
  69. package/templates/apps/mcp/src/lib/paid-tools.generated.ts +10 -0
  70. package/templates/apps/mcp/src/lib/topology.generated.ts +2 -2
  71. package/templates/apps/mcp/src/site.ts +12 -0
  72. package/templates/apps/web/.env.example +1 -1
  73. package/templates/apps/web/CLAUDE.md +21 -5
  74. package/templates/apps/web/app/.well-known/mcp.json/route.ts +9 -3
  75. package/templates/apps/web/app/[locale]/contact/__tests__/ConversationalIntakeWrapper.test.tsx +1 -1
  76. package/templates/apps/web/app/[locale]/contact/contact.actions.ts +14 -4
  77. package/templates/apps/web/app/[locale]/contact/intake-analytics.ts +1 -1
  78. package/templates/apps/web/app/[locale]/contact/intake-script.ts +1 -1
  79. package/templates/apps/web/app/[locale]/flows/[slug]/__tests__/magnet-page.test.tsx +22 -1
  80. package/templates/apps/web/app/[locale]/flows/[slug]/magnet-page.tsx +28 -7
  81. package/templates/apps/web/app/[locale]/pricing/__tests__/unconnected.test.tsx +43 -0
  82. package/templates/apps/web/app/[locale]/pricing/page.tsx +7 -3
  83. package/templates/apps/web/app/api/offerings/checkout/[lookupKey]/__tests__/route.test.ts +222 -0
  84. package/templates/apps/web/app/api/offerings/checkout/[lookupKey]/route.ts +10 -3
  85. package/templates/apps/web/app/api/revalidate/route.ts +3 -3
  86. package/templates/apps/web/auth.ts +2 -2
  87. package/templates/apps/web/jest.config.cjs +1 -0
  88. package/templates/apps/web/lib/__tests__/magnet-routes.test.ts +7 -0
  89. package/templates/apps/web/lib/auth-email.ts +1 -1
  90. package/templates/apps/web/lib/magnet-routes.ts +3 -1
  91. package/templates/apps/web/lib/newsletter.actions.ts +9 -6
  92. package/templates/apps/web/lib/relay.ts +2 -2
  93. package/templates/apps/web/lib/release-scope.ts +47 -0
  94. package/templates/apps/web/lib/server-api.ts +1 -1
  95. package/templates/apps/web/src/lib/commerce-claim.ts +1 -1
  96. package/templates/customizations.yaml +37 -0
  97. package/templates/database/prisma.config.ts +10 -14
  98. package/templates/database/seed-test.ts +2 -2
  99. package/templates/database/seed.ts +32 -5
  100. package/templates/package.json +12 -6
  101. package/templates/pnpm-workspace.yaml +0 -1
  102. package/templates/project.yaml +16 -8
  103. package/templates/tools/jest/reporters.cjs +65 -0
  104. package/templates/tools/lint-architecture.ts +757 -7
  105. package/templates/tsconfig.json +1 -2
  106. package/versions.json +48 -35
  107. package/templates/apps/api/src/__tests__/commerce-claim-registration.test.ts +0 -409
  108. package/templates/apps/api/src/__tests__/factories/agent-caller.ts +0 -102
  109. package/templates/apps/api/src/__tests__/factories/content-fixture.ts +0 -138
  110. package/templates/apps/api/src/__tests__/factories/in-memory-prisma.ts +0 -441
  111. package/templates/apps/api/src/__tests__/factories/index.ts +0 -70
  112. package/templates/apps/api/src/__tests__/factories/lead-fixture.ts +0 -82
  113. package/templates/apps/api/src/__tests__/leads-forwarded-rate-limit.test.ts +0 -135
  114. package/templates/apps/api/src/__tests__/services/enrichment-shim.test.ts +0 -126
  115. package/templates/apps/api/src/__tests__/stripe-boot.test.ts +0 -132
  116. package/templates/apps/api/src/__tests__/support/hub-sign-in-fake-db.ts +0 -362
  117. package/templates/apps/api/src/bin/__tests__/rate-limit-probe.test.ts +0 -184
  118. package/templates/apps/api/src/bin/abandon-sweep.ts +0 -42
  119. package/templates/apps/api/src/bin/rate-limit-probe.ts +0 -53
  120. package/templates/apps/api/src/bin/spokes.ts +0 -198
  121. package/templates/apps/api/src/bin/stripe-connections-rekey.ts +0 -45
  122. package/templates/apps/api/src/config/__tests__/hub-require-spoke-key.test.ts +0 -196
  123. package/templates/apps/api/src/config/__tests__/index.test.ts +0 -233
  124. package/templates/apps/api/src/config/__tests__/modules.test.ts +0 -76
  125. package/templates/apps/api/src/config/__tests__/stripe-connection-key.test.ts +0 -185
  126. package/templates/apps/api/src/config/__tests__/trusted-origins.test.ts +0 -238
  127. package/templates/apps/api/src/config/index.ts +0 -596
  128. package/templates/apps/api/src/config/modules.ts +0 -83
  129. package/templates/apps/api/src/lib/__tests__/agent-store.test.ts +0 -581
  130. package/templates/apps/api/src/lib/__tests__/auth-email-budget.test.ts +0 -143
  131. package/templates/apps/api/src/lib/__tests__/consent-ledger-double.ts +0 -237
  132. package/templates/apps/api/src/lib/__tests__/consent-store.test.ts +0 -353
  133. package/templates/apps/api/src/lib/__tests__/content-adapter.test.ts +0 -201
  134. package/templates/apps/api/src/lib/__tests__/deliverable-asset-store.test.ts +0 -224
  135. package/templates/apps/api/src/lib/__tests__/error-serializer.test.ts +0 -120
  136. package/templates/apps/api/src/lib/__tests__/errors.test.ts +0 -94
  137. package/templates/apps/api/src/lib/__tests__/file-fallback.test.ts +0 -144
  138. package/templates/apps/api/src/lib/__tests__/fixtures/HomePage/home.mdx +0 -8
  139. package/templates/apps/api/src/lib/__tests__/flow-completion-store.test.ts +0 -149
  140. package/templates/apps/api/src/lib/__tests__/flow-rename-race.db.test.ts +0 -116
  141. package/templates/apps/api/src/lib/__tests__/hub-sign-in-store.test.ts +0 -209
  142. package/templates/apps/api/src/lib/__tests__/lead-capture-store.test.ts +0 -257
  143. package/templates/apps/api/src/lib/__tests__/lead-delivery.test.ts +0 -96
  144. package/templates/apps/api/src/lib/__tests__/outbound-http.test.ts +0 -125
  145. package/templates/apps/api/src/lib/__tests__/prisma.test.ts +0 -165
  146. package/templates/apps/api/src/lib/__tests__/require-commerce-claim.test.ts +0 -317
  147. package/templates/apps/api/src/lib/__tests__/require-paid-tier.test.ts +0 -129
  148. package/templates/apps/api/src/lib/__tests__/site.test.ts +0 -292
  149. package/templates/apps/api/src/lib/__tests__/slice-load.test.ts +0 -89
  150. package/templates/apps/api/src/lib/__tests__/spoke-store.test.ts +0 -202
  151. package/templates/apps/api/src/lib/__tests__/stripe-admission.test.ts +0 -217
  152. package/templates/apps/api/src/lib/__tests__/stripe.test.ts +0 -82
  153. package/templates/apps/api/src/lib/agent-store.ts +0 -613
  154. package/templates/apps/api/src/lib/auth-email-budget.ts +0 -161
  155. package/templates/apps/api/src/lib/consent-store.ts +0 -363
  156. package/templates/apps/api/src/lib/content-adapter.ts +0 -112
  157. package/templates/apps/api/src/lib/deliverable-asset-store.ts +0 -96
  158. package/templates/apps/api/src/lib/error-serializer.ts +0 -164
  159. package/templates/apps/api/src/lib/errors.ts +0 -95
  160. package/templates/apps/api/src/lib/file-fallback.ts +0 -87
  161. package/templates/apps/api/src/lib/flow-completion-store.ts +0 -136
  162. package/templates/apps/api/src/lib/hub-sign-in-store.ts +0 -455
  163. package/templates/apps/api/src/lib/lead-capture-store.ts +0 -122
  164. package/templates/apps/api/src/lib/lead-delivery.ts +0 -52
  165. package/templates/apps/api/src/lib/license-key-store.ts +0 -45
  166. package/templates/apps/api/src/lib/locales.ts +0 -201
  167. package/templates/apps/api/src/lib/outbound-http.ts +0 -127
  168. package/templates/apps/api/src/lib/prisma.ts +0 -21
  169. package/templates/apps/api/src/lib/proposal-paid-store.ts +0 -66
  170. package/templates/apps/api/src/lib/rate-limit-probe.ts +0 -165
  171. package/templates/apps/api/src/lib/rate-limit-store.ts +0 -142
  172. package/templates/apps/api/src/lib/require-commerce-claim.ts +0 -250
  173. package/templates/apps/api/src/lib/require-paid-tier.ts +0 -53
  174. package/templates/apps/api/src/lib/site.ts +0 -254
  175. package/templates/apps/api/src/lib/slice-load.ts +0 -90
  176. package/templates/apps/api/src/lib/spoke-store.ts +0 -180
  177. package/templates/apps/api/src/lib/stripe.ts +0 -318
  178. package/templates/apps/api/src/lib/webhook-idempotency.ts +0 -156
  179. package/templates/apps/api/src/middleware/__tests__/adversarial/bearer-token-format.test.ts +0 -111
  180. package/templates/apps/api/src/middleware/__tests__/adversarial/cookie-or-bearer-precedence.test.ts +0 -172
  181. package/templates/apps/api/src/middleware/__tests__/adversarial/csrf-bearer-skip.test.ts +0 -119
  182. package/templates/apps/api/src/middleware/__tests__/adversarial/csrf-cookie-path.test.ts +0 -138
  183. package/templates/apps/api/src/middleware/__tests__/adversarial/csrf-trusted-origins.test.ts +0 -142
  184. package/templates/apps/api/src/middleware/__tests__/adversarial/session-fixation.test.ts +0 -155
  185. package/templates/apps/api/src/middleware/__tests__/adversarial/session-timeout.test.ts +0 -139
  186. package/templates/apps/api/src/middleware/__tests__/auth.bearer-path.test.ts +0 -562
  187. package/templates/apps/api/src/middleware/__tests__/auth.both-present-precedence.test.ts +0 -76
  188. package/templates/apps/api/src/middleware/__tests__/auth.cookie-path.test.ts +0 -220
  189. package/templates/apps/api/src/middleware/__tests__/auth.database-not-configured.test.ts +0 -97
  190. package/templates/apps/api/src/middleware/__tests__/auth.hub-session-path.test.ts +0 -557
  191. package/templates/apps/api/src/middleware/__tests__/auth.neither-present.test.ts +0 -72
  192. package/templates/apps/api/src/middleware/__tests__/auth.optional-cookie.test.ts +0 -140
  193. package/templates/apps/api/src/middleware/__tests__/default-tenant.test.ts +0 -721
  194. package/templates/apps/api/src/middleware/__tests__/rate-limit-cross-isolate.db.test.ts +0 -220
  195. package/templates/apps/api/src/middleware/__tests__/rate-limit-store.test.ts +0 -430
  196. package/templates/apps/api/src/middleware/__tests__/rate-limit.test.ts +0 -1409
  197. package/templates/apps/api/src/middleware/__tests__/write-budget.test.ts +0 -174
  198. package/templates/apps/api/src/middleware/auth.ts +0 -864
  199. package/templates/apps/api/src/middleware/default-tenant.ts +0 -166
  200. package/templates/apps/api/src/middleware/rate-limit.ts +0 -983
  201. package/templates/apps/api/src/openapi/__tests__/openapi.test.ts +0 -264
  202. package/templates/apps/api/src/openapi/__tests__/spec-identity.test.ts +0 -182
  203. package/templates/apps/api/src/openapi/index.ts +0 -18
  204. package/templates/apps/api/src/openapi/spec.ts +0 -1815
  205. package/templates/apps/api/src/openapi/surface-drift.ts +0 -195
  206. package/templates/apps/api/src/routes/aeo/__tests__/index.test.ts +0 -175
  207. package/templates/apps/api/src/routes/aeo/index.ts +0 -107
  208. package/templates/apps/api/src/routes/auth/__tests__/auth-tokens.functional.test.ts +0 -196
  209. package/templates/apps/api/src/routes/auth/__tests__/auth-user.functional.test.ts +0 -249
  210. package/templates/apps/api/src/routes/auth/__tests__/hub-sign-in.test.ts +0 -532
  211. package/templates/apps/api/src/routes/auth/__tests__/tokens.test.ts +0 -313
  212. package/templates/apps/api/src/routes/auth/__tests__/transactional-email.test.ts +0 -413
  213. package/templates/apps/api/src/routes/auth/__tests__/user.test.ts +0 -609
  214. package/templates/apps/api/src/routes/auth/hub-sign-in.ts +0 -242
  215. package/templates/apps/api/src/routes/auth/tokens.ts +0 -211
  216. package/templates/apps/api/src/routes/auth/transactional-email.ts +0 -301
  217. package/templates/apps/api/src/routes/auth/user.ts +0 -305
  218. package/templates/apps/api/src/routes/billing/plan-checkout.ts +0 -325
  219. package/templates/apps/api/src/routes/checkout/__tests__/live-key.test.ts +0 -178
  220. package/templates/apps/api/src/routes/checkout/__tests__/offerings-authorization.test.ts +0 -1300
  221. package/templates/apps/api/src/routes/checkout/__tests__/session.test.ts +0 -448
  222. package/templates/apps/api/src/routes/checkout/__tests__/sessions.test.ts +0 -1742
  223. package/templates/apps/api/src/routes/checkout/index.ts +0 -34
  224. package/templates/apps/api/src/routes/checkout/session.ts +0 -171
  225. package/templates/apps/api/src/routes/checkout/sessions.ts +0 -540
  226. package/templates/apps/api/src/routes/consent/__tests__/account.test.ts +0 -386
  227. package/templates/apps/api/src/routes/consent/__tests__/sign-up-rate-limit.test.ts +0 -160
  228. package/templates/apps/api/src/routes/consent/account.ts +0 -268
  229. package/templates/apps/api/src/routes/consent/cookie.ts +0 -90
  230. package/templates/apps/api/src/routes/consent/index.ts +0 -26
  231. package/templates/apps/api/src/routes/consent/unsubscribe.ts +0 -127
  232. package/templates/apps/api/src/routes/content/__tests__/get-page.test.ts +0 -853
  233. package/templates/apps/api/src/routes/content/__tests__/list-pages.test.ts +0 -230
  234. package/templates/apps/api/src/routes/content/__tests__/list-types.test.ts +0 -44
  235. package/templates/apps/api/src/routes/content/__tests__/locale.test.ts +0 -118
  236. package/templates/apps/api/src/routes/content/__tests__/offerings.test.ts +0 -731
  237. package/templates/apps/api/src/routes/content/__tests__/pricing.test.ts +0 -753
  238. package/templates/apps/api/src/routes/content/__tests__/rate-limit.test.ts +0 -51
  239. package/templates/apps/api/src/routes/content/__tests__/search.test.ts +0 -450
  240. package/templates/apps/api/src/routes/content/__tests__/shown-offerings.test.ts +0 -762
  241. package/templates/apps/api/src/routes/content/get-page.ts +0 -268
  242. package/templates/apps/api/src/routes/content/index.ts +0 -49
  243. package/templates/apps/api/src/routes/content/list-pages.ts +0 -95
  244. package/templates/apps/api/src/routes/content/list-types.ts +0 -19
  245. package/templates/apps/api/src/routes/content/offerings.ts +0 -184
  246. package/templates/apps/api/src/routes/content/pricing.ts +0 -176
  247. package/templates/apps/api/src/routes/content/rate-limit.ts +0 -22
  248. package/templates/apps/api/src/routes/content/search.ts +0 -129
  249. package/templates/apps/api/src/routes/content/shown-offerings.ts +0 -229
  250. package/templates/apps/api/src/routes/content/slug.ts +0 -9
  251. package/templates/apps/api/src/routes/leads/__tests__/capture.test.ts +0 -88
  252. package/templates/apps/api/src/routes/leads/__tests__/list.test.ts +0 -171
  253. package/templates/apps/api/src/routes/leads/__tests__/magnet-completion.test.ts +0 -515
  254. package/templates/apps/api/src/routes/leads/__tests__/spoke.test.ts +0 -392
  255. package/templates/apps/api/src/routes/leads/__tests__/track.test.ts +0 -165
  256. package/templates/apps/api/src/routes/leads/index.ts +0 -573
  257. package/templates/apps/api/src/routes/leads/list.ts +0 -104
  258. package/templates/apps/api/src/routes/project-listings/__tests__/configured-application.test.ts +0 -626
  259. package/templates/apps/api/src/routes/project-listings/__tests__/drafts.test.ts +0 -1268
  260. package/templates/apps/api/src/routes/project-listings/__tests__/founder-identity.test.ts +0 -847
  261. package/templates/apps/api/src/routes/project-listings/__tests__/me.test.ts +0 -480
  262. package/templates/apps/api/src/routes/project-listings/__tests__/public.test.ts +0 -465
  263. package/templates/apps/api/src/routes/project-listings/__tests__/resume-email.test.ts +0 -872
  264. package/templates/apps/api/src/routes/project-listings/__tests__/site-answers.test.ts +0 -1022
  265. package/templates/apps/api/src/routes/project-listings/__tests__/site-key.test.ts +0 -484
  266. package/templates/apps/api/src/routes/project-listings/__tests__/structured-address.test.ts +0 -535
  267. package/templates/apps/api/src/routes/project-listings/index.ts +0 -164
  268. package/templates/apps/api/src/routes/project-listings/me.ts +0 -87
  269. package/templates/apps/api/src/routes/project-listings/patch-copy.ts +0 -224
  270. package/templates/apps/api/src/routes/project-listings/patch-draft.ts +0 -102
  271. package/templates/apps/api/src/routes/project-listings/public.ts +0 -98
  272. package/templates/apps/api/src/routes/project-listings/respond.ts +0 -48
  273. package/templates/apps/api/src/routes/project-listings/resume-token.ts +0 -27
  274. package/templates/apps/api/src/routes/project-listings/start.ts +0 -140
  275. package/templates/apps/api/src/routes/project-listings/submit.ts +0 -130
  276. package/templates/apps/api/src/routes/project-listings/verify-email.ts +0 -150
  277. package/templates/apps/api/src/routes/requests/__tests__/confirm.test.ts +0 -686
  278. package/templates/apps/api/src/routes/requests/confirm.ts +0 -601
  279. package/templates/apps/api/src/routes/requests/index.ts +0 -54
  280. package/templates/apps/api/src/routes/test/auth/__tests__/session.test.ts +0 -306
  281. package/templates/apps/api/src/routes/test/auth/session.ts +0 -143
  282. package/templates/apps/api/src/routes/webhooks/README.md +0 -398
  283. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-checkout-consent.test.ts +0 -649
  284. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-dunning.test.ts +0 -749
  285. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-flow-checkout.test.ts +0 -448
  286. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-idempotency.test.ts +0 -264
  287. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-livemode-live.test.ts +0 -159
  288. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-livemode.test.ts +0 -158
  289. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-offerings.test.ts +0 -814
  290. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-org-billing.test.ts +0 -301
  291. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-proposal-paid.test.ts +0 -369
  292. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-rate-limit.test.ts +0 -215
  293. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-request-paid.test.ts +0 -301
  294. package/templates/apps/api/src/routes/webhooks/__tests__/stripe-site-filter.test.ts +0 -808
  295. package/templates/apps/api/src/routes/webhooks/__tests__/stripe.test.ts +0 -1155
  296. package/templates/apps/api/src/routes/webhooks/index.ts +0 -141
  297. package/templates/apps/api/src/routes/webhooks/stripe-offerings.ts +0 -384
  298. package/templates/apps/api/src/routes/webhooks/stripe.ts +0 -2026
  299. package/templates/apps/api/src/services/__tests__/abandon-email.test.ts +0 -528
  300. package/templates/apps/api/src/services/__tests__/answer-distributions.test.ts +0 -132
  301. package/templates/apps/api/src/services/__tests__/audit-log.test.ts +0 -1028
  302. package/templates/apps/api/src/services/__tests__/checkout-plan.test.ts +0 -313
  303. package/templates/apps/api/src/services/__tests__/consent-ledger.test.ts +0 -503
  304. package/templates/apps/api/src/services/__tests__/consent-migration.test.ts +0 -65
  305. package/templates/apps/api/src/services/__tests__/consent.test.ts +0 -299
  306. package/templates/apps/api/src/services/__tests__/default-tenant.test.ts +0 -147
  307. package/templates/apps/api/src/services/__tests__/deliverable-fulfillment.test.ts +0 -867
  308. package/templates/apps/api/src/services/__tests__/deliverable-resend.test.ts +0 -379
  309. package/templates/apps/api/src/services/__tests__/dunning-policy.test.ts +0 -368
  310. package/templates/apps/api/src/services/__tests__/entitlements.test.ts +0 -142
  311. package/templates/apps/api/src/services/__tests__/hub-sign-in.test.ts +0 -854
  312. package/templates/apps/api/src/services/__tests__/keyless-workspace-predicate.test.ts +0 -234
  313. package/templates/apps/api/src/services/__tests__/lead-accept-user.test.ts +0 -251
  314. package/templates/apps/api/src/services/__tests__/lead-routing.test.ts +0 -2660
  315. package/templates/apps/api/src/services/__tests__/listing-config.test.ts +0 -166
  316. package/templates/apps/api/src/services/__tests__/magnet-completion.test.ts +0 -389
  317. package/templates/apps/api/src/services/__tests__/person.test.ts +0 -1176
  318. package/templates/apps/api/src/services/__tests__/plan-offerings.test.ts +0 -444
  319. package/templates/apps/api/src/services/__tests__/project-listings.fence.test.ts +0 -154
  320. package/templates/apps/api/src/services/__tests__/public-site-resolver.test.ts +0 -137
  321. package/templates/apps/api/src/services/__tests__/request-confirm.test.ts +0 -531
  322. package/templates/apps/api/src/services/__tests__/revalidation.test.ts +0 -372
  323. package/templates/apps/api/src/services/__tests__/slack-notify.test.ts +0 -348
  324. package/templates/apps/api/src/services/__tests__/spoke-resolution.test.ts +0 -660
  325. package/templates/apps/api/src/services/__tests__/usage-aggregation.test.ts +0 -941
  326. package/templates/apps/api/src/services/__tests__/usage-credits.test.ts +0 -588
  327. package/templates/apps/api/src/services/__tests__/verified-email.test.ts +0 -78
  328. package/templates/apps/api/src/services/abandon-email.ts +0 -313
  329. package/templates/apps/api/src/services/aeo-score.ts +0 -325
  330. package/templates/apps/api/src/services/answer-distributions.ts +0 -138
  331. package/templates/apps/api/src/services/audit-log.ts +0 -961
  332. package/templates/apps/api/src/services/checkout-buyer.ts +0 -168
  333. package/templates/apps/api/src/services/consent.ts +0 -834
  334. package/templates/apps/api/src/services/default-tenant.ts +0 -416
  335. package/templates/apps/api/src/services/deliverable-fulfillment.ts +0 -755
  336. package/templates/apps/api/src/services/dunning-policy.ts +0 -294
  337. package/templates/apps/api/src/services/enrichment-shim.ts +0 -53
  338. package/templates/apps/api/src/services/entitlements.ts +0 -338
  339. package/templates/apps/api/src/services/hub-sign-in.ts +0 -472
  340. package/templates/apps/api/src/services/lead-accept-user.ts +0 -126
  341. package/templates/apps/api/src/services/lead-promotion.ts +0 -160
  342. package/templates/apps/api/src/services/lead-routing.ts +0 -1372
  343. package/templates/apps/api/src/services/leads-inbox.ts +0 -312
  344. package/templates/apps/api/src/services/listing-config.ts +0 -178
  345. package/templates/apps/api/src/services/magnet-completion.ts +0 -242
  346. package/templates/apps/api/src/services/offerings/__tests__/catalog.test.ts +0 -295
  347. package/templates/apps/api/src/services/offerings/__tests__/checkout-session-record.test.ts +0 -268
  348. package/templates/apps/api/src/services/offerings/__tests__/connection.test.ts +0 -868
  349. package/templates/apps/api/src/services/offerings/__tests__/fixtures.ts +0 -416
  350. package/templates/apps/api/src/services/offerings/__tests__/org-products.test.ts +0 -396
  351. package/templates/apps/api/src/services/offerings/__tests__/readers.test.ts +0 -681
  352. package/templates/apps/api/src/services/offerings/__tests__/source.test.ts +0 -231
  353. package/templates/apps/api/src/services/offerings/catalog.ts +0 -308
  354. package/templates/apps/api/src/services/offerings/checkout-authorization.ts +0 -385
  355. package/templates/apps/api/src/services/offerings/checkout-session-record.ts +0 -192
  356. package/templates/apps/api/src/services/offerings/connection.ts +0 -1003
  357. package/templates/apps/api/src/services/offerings/index.ts +0 -614
  358. package/templates/apps/api/src/services/offerings/source.ts +0 -140
  359. package/templates/apps/api/src/services/offerings/store.ts +0 -150
  360. package/templates/apps/api/src/services/owner-workspaces.ts +0 -132
  361. package/templates/apps/api/src/services/person.ts +0 -1188
  362. package/templates/apps/api/src/services/plan-offerings.ts +0 -125
  363. package/templates/apps/api/src/services/project-listing-resume-email.ts +0 -326
  364. package/templates/apps/api/src/services/project-listings.ts +0 -2073
  365. package/templates/apps/api/src/services/public-site-resolver.ts +0 -396
  366. package/templates/apps/api/src/services/request-confirm.ts +0 -698
  367. package/templates/apps/api/src/services/revalidation.ts +0 -235
  368. package/templates/apps/api/src/services/slack-notify.ts +0 -291
  369. package/templates/apps/api/src/services/usage-aggregation.ts +0 -516
  370. package/templates/apps/api/src/services/usage-credits.ts +0 -340
  371. package/templates/apps/api/src/services/verified-email.ts +0 -71
  372. package/templates/apps/mcp/__tests__/abuse/daily-cap.test.ts +0 -52
  373. package/templates/apps/mcp/__tests__/abuse/email-validator.test.ts +0 -46
  374. package/templates/apps/mcp/__tests__/abuse/guard.test.ts +0 -198
  375. package/templates/apps/mcp/__tests__/abuse/honeypot.test.ts +0 -36
  376. package/templates/apps/mcp/__tests__/abuse/user-agent-deny.test.ts +0 -54
  377. package/templates/apps/mcp/__tests__/aeo-cli-parity.test.ts +0 -116
  378. package/templates/apps/mcp/__tests__/audit-single-page.test.ts +0 -299
  379. package/templates/apps/mcp/__tests__/boot.test.ts +0 -280
  380. package/templates/apps/mcp/__tests__/catalog-drift.test.ts +0 -359
  381. package/templates/apps/mcp/__tests__/config/modules.test.ts +0 -78
  382. package/templates/apps/mcp/__tests__/get_business_hours.test.ts +0 -82
  383. package/templates/apps/mcp/__tests__/get_faq.test.ts +0 -80
  384. package/templates/apps/mcp/__tests__/get_locations.test.ts +0 -65
  385. package/templates/apps/mcp/__tests__/get_offerings.test.ts +0 -557
  386. package/templates/apps/mcp/__tests__/get_pricing.test.ts +0 -524
  387. package/templates/apps/mcp/__tests__/get_site_info.test.ts +0 -81
  388. package/templates/apps/mcp/__tests__/http-transport.test.ts +0 -1151
  389. package/templates/apps/mcp/__tests__/index.test.ts +0 -195
  390. package/templates/apps/mcp/__tests__/lib/audit-single-page-readiness-scan.test.ts +0 -191
  391. package/templates/apps/mcp/__tests__/lib/commerce-claim.test.ts +0 -531
  392. package/templates/apps/mcp/__tests__/lib/content-adapter-parity.test.ts +0 -65
  393. package/templates/apps/mcp/__tests__/lib/content-adapter.test.ts +0 -187
  394. package/templates/apps/mcp/__tests__/lib/content-root.test.ts +0 -93
  395. package/templates/apps/mcp/__tests__/lib/errors.test.ts +0 -75
  396. package/templates/apps/mcp/__tests__/lib/fail-closed-tools.test.ts +0 -165
  397. package/templates/apps/mcp/__tests__/lib/public-workspace.test.ts +0 -124
  398. package/templates/apps/mcp/__tests__/list_pages.test.ts +0 -228
  399. package/templates/apps/mcp/__tests__/locale.test.ts +0 -135
  400. package/templates/apps/mcp/__tests__/module-exclusion.test.ts +0 -175
  401. package/templates/apps/mcp/__tests__/purchase-disabled.test.ts +0 -101
  402. package/templates/apps/mcp/__tests__/read_content.test.ts +0 -474
  403. package/templates/apps/mcp/__tests__/request_callback.test.ts +0 -225
  404. package/templates/apps/mcp/__tests__/search_content.test.ts +0 -256
  405. package/templates/apps/mcp/__tests__/server.test.ts +0 -388
  406. package/templates/apps/mcp/__tests__/start_purchase.test.ts +0 -448
  407. package/templates/apps/mcp/__tests__/submit_lead.test.ts +0 -307
  408. package/templates/apps/mcp/__tests__/subscribe_newsletter.test.ts +0 -223
  409. package/templates/apps/mcp/__tests__/tools/create_project.test.ts +0 -243
  410. package/templates/apps/mcp/__tests__/tools/purchase.test.ts +0 -451
  411. package/templates/apps/mcp/src/abuse/daily-cap.ts +0 -47
  412. package/templates/apps/mcp/src/abuse/email-validator.ts +0 -25
  413. package/templates/apps/mcp/src/abuse/honeypot.ts +0 -21
  414. package/templates/apps/mcp/src/abuse/index.ts +0 -342
  415. package/templates/apps/mcp/src/abuse/user-agent-deny.ts +0 -58
  416. package/templates/apps/mcp/src/boot.ts +0 -167
  417. package/templates/apps/mcp/src/config/modules.ts +0 -43
  418. package/templates/apps/mcp/src/http-transport.ts +0 -526
  419. package/templates/apps/mcp/src/lib/audit-single-page.ts +0 -308
  420. package/templates/apps/mcp/src/lib/commerce-claim.ts +0 -263
  421. package/templates/apps/mcp/src/lib/content-adapter.ts +0 -142
  422. package/templates/apps/mcp/src/lib/empty-content-adapter.ts +0 -139
  423. package/templates/apps/mcp/src/lib/errors.ts +0 -61
  424. package/templates/apps/mcp/src/lib/license-key-store.ts +0 -45
  425. package/templates/apps/mcp/src/lib/locales.ts +0 -171
  426. package/templates/apps/mcp/src/lib/prisma.ts +0 -39
  427. package/templates/apps/mcp/src/lib/public-workspace.ts +0 -133
  428. package/templates/apps/mcp/src/lib/resolve-locale.ts +0 -30
  429. package/templates/apps/mcp/src/lib/shown-offerings.ts +0 -105
  430. package/templates/apps/mcp/src/lib/stripe-tax-signal.ts +0 -59
  431. package/templates/apps/mcp/src/request-context.ts +0 -52
  432. package/templates/apps/mcp/src/server.ts +0 -343
  433. package/templates/apps/mcp/src/serverless-app.ts +0 -88
  434. package/templates/apps/mcp/src/tools/audit.ts +0 -127
  435. package/templates/apps/mcp/src/tools/capture_lead.ts +0 -64
  436. package/templates/apps/mcp/src/tools/create_project.ts +0 -134
  437. package/templates/apps/mcp/src/tools/get_business_hours.ts +0 -68
  438. package/templates/apps/mcp/src/tools/get_faq.ts +0 -71
  439. package/templates/apps/mcp/src/tools/get_locations.ts +0 -88
  440. package/templates/apps/mcp/src/tools/get_offerings.ts +0 -96
  441. package/templates/apps/mcp/src/tools/get_pricing.ts +0 -94
  442. package/templates/apps/mcp/src/tools/get_site_info.ts +0 -86
  443. package/templates/apps/mcp/src/tools/index.ts +0 -137
  444. package/templates/apps/mcp/src/tools/list_pages.ts +0 -74
  445. package/templates/apps/mcp/src/tools/person_notes.ts +0 -25
  446. package/templates/apps/mcp/src/tools/proposals.ts +0 -27
  447. package/templates/apps/mcp/src/tools/purchase.ts +0 -205
  448. package/templates/apps/mcp/src/tools/read_content.ts +0 -75
  449. package/templates/apps/mcp/src/tools/request_callback.ts +0 -59
  450. package/templates/apps/mcp/src/tools/search_content.ts +0 -67
  451. package/templates/apps/mcp/src/tools/subscribe_newsletter.ts +0 -57
  452. package/templates/apps/mcp/src/workspace-server.ts +0 -224
  453. package/templates/database/CHANGELOG.md +0 -961
  454. package/templates/database/LICENSE +0 -202
  455. package/templates/database/NOTICE +0 -2
  456. package/templates/database/README.md +0 -243
  457. package/templates/database/__tests__/agents.db.test.ts +0 -320
  458. package/templates/database/__tests__/appointment.db.test.ts +0 -106
  459. package/templates/database/__tests__/artifact-content-store.db.test.ts +0 -316
  460. package/templates/database/__tests__/composite-fence.db.test.ts +0 -206
  461. package/templates/database/__tests__/consent-ledger.db.test.ts +0 -345
  462. package/templates/database/__tests__/consent-source-checkout-migration.test.ts +0 -79
  463. package/templates/database/__tests__/fence-fan-out.db.test.ts +0 -715
  464. package/templates/database/__tests__/fixtures/appointment-create-probe.ts +0 -79
  465. package/templates/database/__tests__/fixtures/person-backfill.json +0 -264
  466. package/templates/database/__tests__/fixtures/person-contact-backfill.json +0 -182
  467. package/templates/database/__tests__/fixtures/project-listing-consent-probe.ts +0 -100
  468. package/templates/database/__tests__/flow-completion-migration.test.ts +0 -115
  469. package/templates/database/__tests__/flow-completion.db.test.ts +0 -136
  470. package/templates/database/__tests__/flow-workspace-key.db.test.ts +0 -263
  471. package/templates/database/__tests__/flows-listing-diagnostic.test.ts +0 -392
  472. package/templates/database/__tests__/flows-presets.test.ts +0 -173
  473. package/templates/database/__tests__/in-page-magnets-migration.test.ts +0 -161
  474. package/templates/database/__tests__/lead-idempotency-key.db.test.ts +0 -92
  475. package/templates/database/__tests__/member-area.db.test.ts +0 -257
  476. package/templates/database/__tests__/member-contract-migration.test.ts +0 -85
  477. package/templates/database/__tests__/member-contract.db.test.ts +0 -413
  478. package/templates/database/__tests__/offerings-checkout-session.db.test.ts +0 -270
  479. package/templates/database/__tests__/offerings-migration.test.ts +0 -197
  480. package/templates/database/__tests__/offerings-shown-price-ids-migration.test.ts +0 -86
  481. package/templates/database/__tests__/offerings.db.test.ts +0 -303
  482. package/templates/database/__tests__/outbound-message-action.db.test.ts +0 -80
  483. package/templates/database/__tests__/person-contact.db.test.ts +0 -563
  484. package/templates/database/__tests__/person-notes.db.test.ts +0 -249
  485. package/templates/database/__tests__/person.db.test.ts +0 -586
  486. package/templates/database/__tests__/piece-kind-migration.test.ts +0 -67
  487. package/templates/database/__tests__/piece-kind.db.test.ts +0 -208
  488. package/templates/database/__tests__/prefixed-ids.db.test.ts +0 -222
  489. package/templates/database/__tests__/project-listing-consent-at.db.test.ts +0 -111
  490. package/templates/database/__tests__/seed.test.ts +0 -240
  491. package/templates/database/__tests__/spoke-registry.db.test.ts +0 -191
  492. package/templates/database/__tests__/tenancy.test.ts +0 -141
  493. package/templates/database/__tests__/user-cascade.test.ts +0 -140
  494. package/templates/database/_gitignore +0 -8
  495. package/templates/database/auth/.gitkeep +0 -0
  496. package/templates/database/auth/schema.prisma +0 -641
  497. package/templates/database/config/.gitkeep +0 -0
  498. package/templates/database/config/schema.prisma +0 -441
  499. package/templates/database/content/.gitkeep +0 -0
  500. package/templates/database/content/schema.prisma +0 -339
  501. package/templates/database/eslint.config.mjs +0 -69
  502. package/templates/database/flows/listing-diagnostic.ts +0 -182
  503. package/templates/database/flows/presets.ts +0 -315
  504. package/templates/database/inbox/.gitkeep +0 -0
  505. package/templates/database/inbox/README.md +0 -3
  506. package/templates/database/inbox/__tests__/schema.test.ts +0 -44
  507. package/templates/database/inbox/schema.prisma +0 -1447
  508. package/templates/database/jest.config.cjs +0 -36
  509. package/templates/database/migrations/20260508000000_add_inbox_namespace/migration.sql +0 -328
  510. package/templates/database/migrations/20260513000000_better_auth_migration/migration.sql +0 -100
  511. package/templates/database/migrations/20260515000000_add_user_billing_fields/migration.sql +0 -28
  512. package/templates/database/migrations/20260516000000_add_user_tier_care_plan_seat/migration.sql +0 -25
  513. package/templates/database/migrations/20260518000000_add_telemetry_records/migration.sql +0 -42
  514. package/templates/database/migrations/20260525000000_theme_wiring/migration.sql +0 -67
  515. package/templates/database/migrations/20260710000000_retire_commercial_tier/migration.sql +0 -21
  516. package/templates/database/migrations/20260712215647_add_multi_tenant_tenancy/migration.sql +0 -201
  517. package/templates/database/migrations/20260713143320_reanchor_site_config_to_workspace/migration.sql +0 -67
  518. package/templates/database/migrations/20260713174000_add_audit_logs/migration.sql +0 -46
  519. package/templates/database/migrations/20260713190000_drop_telemetry_records/migration.sql +0 -25
  520. package/templates/database/migrations/20260713192000_add_uuidv7_function/migration.sql +0 -49
  521. package/templates/database/migrations/20260713192429_prefixed_uuid_entity_ids/migration.sql +0 -197
  522. package/templates/database/migrations/20260713200000_add_org_billing/migration.sql +0 -32
  523. package/templates/database/migrations/20260714000000_add_lead_promotion_fields/migration.sql +0 -22
  524. package/templates/database/migrations/20260714010000_add_usage_billing_content_models/migration.sql +0 -151
  525. package/templates/database/migrations/20260714020000_add_usage_billing_auth_config_models/migration.sql +0 -223
  526. package/templates/database/migrations/20260714030000_credit_balance_non_negative/migration.sql +0 -22
  527. package/templates/database/migrations/20260714030000_widen_usage_billing_report_aggregates/migration.sql +0 -32
  528. package/templates/database/migrations/20260810233516_add_flow_engine_models/migration.sql +0 -40
  529. package/templates/database/migrations/20260811020000_add_flow_session_abandon_email_columns/migration.sql +0 -4
  530. package/templates/database/migrations/20260811030000_add_flow_session_abandon_email_index/migration.sql +0 -2
  531. package/templates/database/migrations/20260811040000_add_flow_session_segment/migration.sql +0 -2
  532. package/templates/database/migrations/20260811050000_add_deliverable_models/migration.sql +0 -29
  533. package/templates/database/migrations/20260812000000_add_deliverable_grant_send_columns/migration.sql +0 -6
  534. package/templates/database/migrations/20260902222948_add_waitlist_experiment_models/migration.sql +0 -39
  535. package/templates/database/migrations/20260902224500_add_waitlist_id_format_checks/migration.sql +0 -24
  536. package/templates/database/migrations/20260903004112_add_flow_session_flags_aeo_score/migration.sql +0 -3
  537. package/templates/database/migrations/20260903010000_add_playbook_instance/migration.sql +0 -33
  538. package/templates/database/migrations/20260904000000_add_waitlist_followup_send/migration.sql +0 -30
  539. package/templates/database/migrations/20260905230000_add_flow_checkout/migration.sql +0 -47
  540. package/templates/database/migrations/20260906000000_add_flow_checkout_idempotency/migration.sql +0 -16
  541. package/templates/database/migrations/20260907120000_add_project_listing/migration.sql +0 -47
  542. package/templates/database/migrations/20260907130000_add_project_listing_slack_columns/migration.sql +0 -10
  543. package/templates/database/migrations/20260907180000_add_listing_csv_send/migration.sql +0 -26
  544. package/templates/database/migrations/20260909120000_project_listings_platform/migration.sql +0 -90
  545. package/templates/database/migrations/20260909180000_listing_draft_and_fields/migration.sql +0 -174
  546. package/templates/database/migrations/20260910120000_listing_public_site_key/migration.sql +0 -14
  547. package/templates/database/migrations/20260911120000_listing_copy_edit_audit/migration.sql +0 -25
  548. package/templates/database/migrations/20260911140000_listing_structured_address/migration.sql +0 -32
  549. package/templates/database/migrations/20260911180000_consent_grants/migration.sql +0 -71
  550. package/templates/database/migrations/20260911200000_listing_site_answers/migration.sql +0 -30
  551. package/templates/database/migrations/20260912120000_listing_owner_link/migration.sql +0 -49
  552. package/templates/database/migrations/20260914120000_listing_founder_identity/migration.sql +0 -123
  553. package/templates/database/migrations/20260915120000_project_listing_solution/migration.sql +0 -15
  554. package/templates/database/migrations/20260916120000_artifact_content_store/migration.sql +0 -158
  555. package/templates/database/migrations/20260917120000_callback_request_confirm/migration.sql +0 -40
  556. package/templates/database/migrations/20260918120000_add_appointment/migration.sql +0 -67
  557. package/templates/database/migrations/20260918120000_asset_metadata_columns/migration.sql +0 -70
  558. package/templates/database/migrations/20260919120000_flow_workspace_key/migration.sql +0 -181
  559. package/templates/database/migrations/20260919130000_flow_definition_admin_columns/migration.sql +0 -172
  560. package/templates/database/migrations/20260919130000_workspace_meetings/migration.sql +0 -41
  561. package/templates/database/migrations/20260919140000_inbox_workspace_key/migration.sql +0 -185
  562. package/templates/database/migrations/20260920120000_lead_handled_at/migration.sql +0 -70
  563. package/templates/database/migrations/20260920130000_lead_workspace_source_index/migration.sql +0 -57
  564. package/templates/database/migrations/20260921120000_deliverable_grant_workspace_key/migration.sql +0 -68
  565. package/templates/database/migrations/20260922120000_tenancy_fence/migration.sql +0 -1067
  566. package/templates/database/migrations/20260923120000_listing_capturing_magnet/migration.sql +0 -210
  567. package/templates/database/migrations/20261003120000_spoke_registry/migration.sql +0 -80
  568. package/templates/database/migrations/20261003130000_scheduling_connection/migration.sql +0 -49
  569. package/templates/database/migrations/20261005120000_agents/migration.sql +0 -84
  570. package/templates/database/migrations/20261008120000_user_last_workspace/migration.sql +0 -35
  571. package/templates/database/migrations/20261008130000_lead_idempotency_key/migration.sql +0 -20
  572. package/templates/database/migrations/20261009120000_agent_grants/migration.sql +0 -33
  573. package/templates/database/migrations/20261010120000_person/migration.sql +0 -106
  574. package/templates/database/migrations/20261010121000_person_backfill/migration.sql +0 -132
  575. package/templates/database/migrations/20261010130000_agent_grants_people_read/migration.sql +0 -20
  576. package/templates/database/migrations/20261011120000_member_area/migration.sql +0 -158
  577. package/templates/database/migrations/20261011130000_agent_grants_member_pieces_manage/migration.sql +0 -21
  578. package/templates/database/migrations/20261012120000_member_contract/migration.sql +0 -192
  579. package/templates/database/migrations/20261013120000_lead_spoke_checkout/migration.sql +0 -20
  580. package/templates/database/migrations/20261014120000_consent_ledger/migration.sql +0 -122
  581. package/templates/database/migrations/20261014120000_person_contact/migration.sql +0 -231
  582. package/templates/database/migrations/20261015120000_person_notes/migration.sql +0 -77
  583. package/templates/database/migrations/20261015120000_rate_limit_counters/migration.sql +0 -43
  584. package/templates/database/migrations/20261016120000_piece_kind/migration.sql +0 -95
  585. package/templates/database/migrations/20261017120000_outbound_message_action/migration.sql +0 -30
  586. package/templates/database/migrations/20261018120000_flow_completion/migration.sql +0 -52
  587. package/templates/database/migrations/20261019120000_consent_source_checkout/migration.sql +0 -27
  588. package/templates/database/migrations/20261020120000_offerings_data_model/migration.sql +0 -146
  589. package/templates/database/migrations/20261021120000_offerings_checkout_session/migration.sql +0 -77
  590. package/templates/database/migrations/20261022120000_in_page_magnets/migration.sql +0 -45
  591. package/templates/database/migrations/20261023120000_offerings_shown_price_ids/migration.sql +0 -47
  592. package/templates/database/migrations/20261024120000_organization_plan_checkout_session/migration.sql +0 -16
  593. package/templates/database/migrations/migration_lock.toml +0 -3
  594. package/templates/database/ops/.gitkeep +0 -0
  595. package/templates/database/ops/schema.prisma +0 -43
  596. package/templates/database/package.json +0 -89
  597. package/templates/database/schema.prisma +0 -71
  598. package/templates/database/scripts/db-generate-locked.sh +0 -80
  599. package/templates/database/scripts/person-backfill.sql +0 -132
  600. package/templates/database/tenancy.ts +0 -183
  601. package/templates/database/tsconfig.build.json +0 -16
  602. package/templates/database/tsconfig.flows.json +0 -15
  603. package/templates/database/tsconfig.json +0 -16
  604. package/templates/database/tsconfig.tenancy.json +0 -14
@@ -1,1188 +0,0 @@
1
- /**
2
- * @file person.ts
3
- * @description Person — one client of one workspace (#852, spec
4
- * docs/specs/852-person-the-per-workspace-client-spec.md D3, D5, D6). This
5
- * file is the ONLY writer of `Person`:
6
- *
7
- * - `findOrCreatePerson` — the one creator, called by lead capture
8
- * (`lead-routing.ts`), the hub's own Stripe checkout webhook (through
9
- * `recordCheckoutPerson` below), appointment creation
10
- * (`scheduling/appointments.ts`) and the Admin inbox accept
11
- * (`inbox-messages.ts`).
12
- * - `stampPersonUser` — links a Person to a User, only on a verified-email
13
- * match, never overwriting a link.
14
- * - `recordCheckoutPerson` — the webhook's step: the Person of a completed
15
- * hub checkout, with its live-mode customer id.
16
- * `resolveCheckoutPerson` is the same step answering that Person too, for
17
- * the checkout's terms grant (#8765 D14); the webhook calls it, and
18
- * `recordCheckoutPerson` stays as its outcome-only form for the #852
19
- * suites that pin that outcome.
20
- * - `resolveMemberPersonId` — the member reads' Person (#7877 D8): the one
21
- * linked to the caller, else the one whose email is the caller's
22
- * verified email, linked on first read by `stampPersonUser`'s rule. That
23
- * first link — and no other — releases the Person's held checkout plan
24
- * (#8793 D2).
25
- * - `resolveCallerBillingCustomerId` — the customer id the hub vouches for
26
- * in `GET /v1/hub-sign-in/me` (#7961, spec 7894 D13): that same Person's,
27
- * only while its `userId` is the caller.
28
- * - `setPersonPlanOffering` — the plan's only writer (#7877 D8), called by
29
- * the Admin's plan route and `releaseHeldCheckoutPlan`.
30
- * - `releaseHeldCheckoutPlan` and `checkoutPlanKey` — the checkout plan
31
- * rule (#7877 D10, #8793 D1–D4). A spoke's checkout lead never writes a
32
- * plan: it holds it (`lead-routing.ts`), because its email is whatever
33
- * the buyer typed at Stripe Checkout. The hold is released once, at the
34
- * Person's first member-path link to the user whose VERIFIED email is the
35
- * Person's own, into a blank plan only, and only when every held purchase
36
- * under that email names the same plan key.
37
- * - the contact details (#8435): `findOrCreatePerson` fills BLANK contact
38
- * fields of an uncurated Person (D4); `updatePersonContact` (the
39
- * operator's save), `ensureMemberPerson` and `setOwnPersonContact` (the
40
- * client's save) overwrite and stamp who confirmed them (D9, D11).
41
- *
42
- * ## Match order (D3) — fixed, and there is no merge
43
- *
44
- * 1. Normalize the email (trim ASCII whitespace, lowercase ASCII letters);
45
- * not storable → `invalid_email`, before any read.
46
- * 2. A customer id given → the oldest Person of this workspace carrying it.
47
- * A hit is returned with NO write — its email is never rewritten.
48
- * 3. The Person of this workspace with this email. A hit gains the customer
49
- * id only when it has none AND is linked to no user (first writer wins,
50
- * else `billing: "kept"`), and a name only when it has none. The email is
51
- * whatever the caller typed, never verified here, so it never pins a
52
- * customer on a Person whose `userId` the verified-email stamp set
53
- * (SEC-7894-6, decided on #7961 2026-10-09).
54
- * 4. No hit → `createMany({ skipDuplicates })` — Postgres INSERT … ON
55
- * CONFLICT DO NOTHING — then a re-read that finds the winner of any
56
- * concurrent create. The duplicate is absorbed IN THE DATABASE, never as
57
- * a caught P2002: the appointment path calls this inside an interactive
58
- * transaction, where a raised unique violation aborts the transaction
59
- * (C4). One implementation serves both.
60
- *
61
- * ## Why public, and what it may import (D3)
62
- *
63
- * The base-tree `lead-routing.ts` imports this file, so it ships in every
64
- * build and must never reach the paid import closure: it imports
65
- * `lib/prisma.js`, `verified-email.js`, `planOfferingSchema` from the
66
- * base-tree kernel `@working-theory/validation` and types only — never
67
- * `lead-promotion.ts`, `owner-workspaces.ts`, `lead-accept-user.ts` (whose
68
- * find-or-create shape it mirrors) or `lib/tenant-db.ts`.
69
- * `packages/create-ailk/__tests__/paid-module-exclusions.test.ts` holds this
70
- * file and `lead-routing.ts` in `MUST_SHIP`.
71
- *
72
- * ## Tenancy
73
- *
74
- * Every `Person` statement is scoped by a server-resolved `workspaceId`;
75
- * there is no `Person` list read here (the reads are
76
- * `tenantDb(tenant).people`). The one list read is `releaseHeldCheckoutPlan`'s
77
- * (#8793 D3): the newest 51 `spokeCheckout` leads of a Person just linked to
78
- * the caller, by `(workspaceId, personId)`, the workspace the member read
79
- * resolved.
80
- * `recordCheckoutPerson`'s two other reads are by a server-minted
81
- * `FlowCheckout` id from a signature-verified webhook, matched on that
82
- * session's own `stripeSessionId`, then that row's lead scoped by the row's
83
- * own `workspaceId`. `resolveMemberPersonId` reads by `(workspaceId, userId)`
84
- * and by `(workspaceId, email)`, and `resolveCallerBillingCustomerId` by
85
- * `(id, workspaceId, userId)`, the workspace server-resolved and the user
86
- * the authenticated caller. `architecture.yaml` names this file as an
87
- * exclusion with that reason.
88
- *
89
- * ## Errors and logs
90
- *
91
- * A store error is caught and returned as a bare kind; no Prisma message is
92
- * returned or logged (SEC-006). These functions log nothing: each caller
93
- * logs the outcome kind, never an email, a name or a customer id. The
94
- * exceptions are `resolveMemberPersonId`, whose `null` hides a refused stamp,
95
- * and `resolveCallerBillingCustomerId`, whose `null` hides a failed read: each
96
- * logs that outcome's kind through the logger its caller passes.
97
- */
98
-
99
- import type { Prisma } from "@working-theory/database";
100
- import type { Id } from "@working-theory/ids";
101
- import { planOfferingSchema, type PersonContactPatch } from "@working-theory/validation";
102
-
103
- import { prisma } from "../lib/prisma.js";
104
-
105
- import { findVerifiedEmail } from "./verified-email.js";
106
-
107
- // ─── Normalization ────────────────────────────────────────────────────────────
108
-
109
- /** The ASCII whitespace the backfill's btrim set names: SP, HT, LF, CR, FF, VT. */
110
- const EDGE_WHITESPACE = /^[ \t\n\r\f\v]+|[ \t\n\r\f\v]+$/g;
111
- /** Printable ASCII, one `@`, a non-empty side each — the `people_email_normalized` CHECK. */
112
- const STORABLE_EMAIL = /^[!-?A-~]+@[!-?A-~]+$/;
113
- const MAX_EMAIL_LENGTH = 254;
114
- const MAX_NAME_CODE_POINTS = 200;
115
- const MAX_CUSTOMER_ID_LENGTH = 255;
116
- /** C0 and C1 controls and the bidi controls, removed from a name (SEC-852-I). */
117
- // eslint-disable-next-line no-control-regex -- the control-character range is exactly what a stored name must not carry.
118
- const NAME_STRIP = /[\u0000-\u001F\u007F-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/g;
119
-
120
- /**
121
- * Trim ASCII whitespace, then lowercase ASCII letters only — the same value
122
- * Postgres computes as `lower(btrim(x, E' \t\n\r\f\x0B') COLLATE "C")`.
123
- */
124
- export function normalizePersonEmail(raw: string): string {
125
- return raw
126
- .replace(EDGE_WHITESPACE, "")
127
- .replace(/[A-Z]/g, (c) => c.toLowerCase());
128
- }
129
-
130
- /**
131
- * Printable ASCII, one `@` with a non-empty side each, at most 254
132
- * characters — the `people_email_normalized` CHECK's regex. ASCII only, so
133
- * the TypeScript length and the Postgres length agree.
134
- */
135
- export function isStorablePersonEmail(email: string): boolean {
136
- return email.length <= MAX_EMAIL_LENGTH && STORABLE_EMAIL.test(email);
137
- }
138
-
139
- /**
140
- * A name as stored: controls and bidi controls removed, trimmed of spaces, cut
141
- * to 200 code points; empty → `null`.
142
- */
143
- export function normalizePersonName(raw: unknown): string | null {
144
- if (typeof raw !== "string") return null;
145
- // Spaces only, as the backfill's `btrim(x)` trims, so a live-written
146
- // name and a backfilled one agree.
147
- const cleaned = Array.from(raw.replace(NAME_STRIP, "").replace(/^ +| +$/g, ""))
148
- .slice(0, MAX_NAME_CODE_POINTS)
149
- .join("");
150
- return cleaned === "" ? null : cleaned;
151
- }
152
-
153
- // ─── Contact normalization (#8435 D3) ────────────────────────────────────────
154
-
155
- export type PersonAddress = {
156
- addressLine1: string;
157
- addressLine2: string | null;
158
- city: string;
159
- region: string;
160
- postalCode: string;
161
- country: string;
162
- };
163
-
164
- export type PersonContact = {
165
- givenName: string | null;
166
- familyName: string | null;
167
- telephone: string | null;
168
- address: PersonAddress | null;
169
- companyName: string | null;
170
- jobTitle: string | null;
171
- };
172
-
173
- /**
174
- * What a capture offers, as untrusted `unknown` values: the magnet's tagged
175
- * answers or a caller's explicit contact (`RouteLeadOptions.contact`).
176
- */
177
- export type LeadContact = {
178
- name?: unknown;
179
- givenName?: unknown;
180
- familyName?: unknown;
181
- phone?: unknown;
182
- address?: unknown;
183
- company?: unknown;
184
- jobTitle?: unknown;
185
- };
186
-
187
- /** The widths of the person-level text columns, in code points. */
188
- const CONTACT_MAX = {
189
- givenName: 100,
190
- familyName: 100,
191
- companyName: 200,
192
- jobTitle: 100,
193
- } as const;
194
- /** The address parts' widths — `ADDRESS_PART_MAX_LENGTHS` in `packages/validation`. */
195
- const ADDRESS_MAX = {
196
- addressLine1: 100,
197
- addressLine2: 60,
198
- city: 60,
199
- region: 40,
200
- postalCode: 20,
201
- } as const;
202
- const E164 = /^\+[1-9]\d{6,14}$/;
203
- const SHORT_REGION = /^[A-Za-z0-9]{1,3}$/;
204
- const ALPHA2 = /^[A-Z]{2}$/;
205
- /**
206
- * The code-point ranges `CONTACT_STRIP` removes (SEC-8435-L): `NAME_STRIP`'s
207
- * C0/C1 and bidi controls plus the zero-width characters U+200B–U+200D and
208
- * U+FEFF and the separators U+2028/U+2029. Numbers, not a regex literal, so no
209
- * tool can rewrite a separator into a line break inside it. The same set
210
- * `packages/validation`'s `people-contact.ts` strips on the wire.
211
- */
212
- const CONTACT_STRIPPED: ReadonlyArray<readonly [number, number]> = [
213
- [0x0000, 0x001f],
214
- [0x007f, 0x009f],
215
- [0x200b, 0x200f],
216
- [0x2028, 0x2029],
217
- [0x202a, 0x202e],
218
- [0x2066, 0x2069],
219
- [0xfeff, 0xfeff],
220
- ];
221
-
222
- function stripContactChars(raw: string): string {
223
- let out = "";
224
- for (const ch of raw) {
225
- const cp = ch.codePointAt(0) ?? 0;
226
- if (!CONTACT_STRIPPED.some(([lo, hi]) => cp >= lo && cp <= hi)) out += ch;
227
- }
228
- return out;
229
- }
230
-
231
- const cutToCodePoints = (value: string, max: number): string =>
232
- Array.from(value).slice(0, max).join("");
233
-
234
- /** Stripped, trimmed, cut to `max` code points; empty or not a string → `null`. */
235
- function normalizeContactText(raw: unknown, max: number): string | null {
236
- if (typeof raw !== "string") return null;
237
- const cleaned = cutToCodePoints(stripContactChars(raw).trim(), max);
238
- return cleaned === "" ? null : cleaned;
239
- }
240
-
241
- /**
242
- * Strip spaces, `( ) . -` (and the invisible characters), then the result
243
- * must be an E.164 number — else `null`.
244
- */
245
- export function normalizeTelephone(raw: unknown): string | null {
246
- if (typeof raw !== "string") return null;
247
- const compact = stripContactChars(raw)
248
- .replace(/[ ().-]/g, "")
249
- .trim();
250
- return E164.test(compact) ? compact : null;
251
- }
252
-
253
- /**
254
- * An address as stored: whole or not at all. Every required part must be a
255
- * non-empty string within its width after stripping; a short region is
256
- * upper-cased (C3); the country must be an upper-case alpha-2 code.
257
- */
258
- function normalizeAddress(raw: unknown): PersonAddress | null {
259
- if (typeof raw !== "object" || raw === null || Array.isArray(raw))
260
- return null;
261
- const value = raw as Record<string, unknown>;
262
- const part = (key: keyof typeof ADDRESS_MAX): string | null => {
263
- const field = value[key];
264
- if (typeof field !== "string") return null;
265
- const cleaned = stripContactChars(field).trim();
266
- return cleaned === "" || Array.from(cleaned).length > ADDRESS_MAX[key]
267
- ? null
268
- : cleaned;
269
- };
270
- const addressLine1 = part("addressLine1");
271
- const city = part("city");
272
- const region = part("region");
273
- const postalCode = part("postalCode");
274
- const country = value["country"];
275
- if (
276
- addressLine1 === null ||
277
- city === null ||
278
- region === null ||
279
- postalCode === null ||
280
- typeof country !== "string" ||
281
- !ALPHA2.test(country)
282
- ) {
283
- return null;
284
- }
285
- const line2Raw = value["addressLine2"];
286
- const line2 = part("addressLine2");
287
- // An over-long or non-string second line makes the address unusable; an
288
- // absent or blank one is simply null.
289
- if (line2 === null && line2Raw !== undefined && line2Raw !== null) {
290
- if (
291
- typeof line2Raw !== "string" ||
292
- stripContactChars(line2Raw).trim() !== ""
293
- )
294
- return null;
295
- }
296
- return {
297
- addressLine1,
298
- addressLine2: line2,
299
- city,
300
- region: SHORT_REGION.test(region) ? region.toUpperCase() : region,
301
- postalCode,
302
- country,
303
- };
304
- }
305
-
306
- /** Capture-side: every usable field, each normalized; unusable ones dropped. */
307
- export function normalizeCapturedContact(
308
- raw: LeadContact,
309
- ): Partial<PersonContact> {
310
- const out: Partial<PersonContact> = {};
311
- const givenName = normalizeContactText(raw.givenName, CONTACT_MAX.givenName);
312
- if (givenName !== null) out.givenName = givenName;
313
- const familyName = normalizeContactText(
314
- raw.familyName,
315
- CONTACT_MAX.familyName,
316
- );
317
- if (familyName !== null) out.familyName = familyName;
318
- const telephone = normalizeTelephone(raw.phone);
319
- if (telephone !== null) out.telephone = telephone;
320
- const address = normalizeAddress(raw.address);
321
- if (address !== null) out.address = address;
322
- const companyName = normalizeContactText(
323
- raw.company,
324
- CONTACT_MAX.companyName,
325
- );
326
- if (companyName !== null) out.companyName = companyName;
327
- const jobTitle = normalizeContactText(raw.jobTitle, CONTACT_MAX.jobTitle);
328
- if (jobTitle !== null) out.jobTitle = jobTitle;
329
- return out;
330
- }
331
-
332
- // ─── The contact columns, as rows and as the wire's shape ───────────────────
333
-
334
- /** The thirteen contact columns a read names (`select`). */
335
- export const PERSON_CONTACT_SELECT = {
336
- givenName: true,
337
- familyName: true,
338
- telephone: true,
339
- addressLine1: true,
340
- addressLine2: true,
341
- addressCity: true,
342
- addressRegion: true,
343
- addressPostalCode: true,
344
- addressCountry: true,
345
- companyName: true,
346
- jobTitle: true,
347
- contactConfirmedAt: true,
348
- contactConfirmedBy: true,
349
- } as const;
350
-
351
- export type PersonContactRow = {
352
- givenName: string | null;
353
- familyName: string | null;
354
- telephone: string | null;
355
- addressLine1: string | null;
356
- addressLine2: string | null;
357
- addressCity: string | null;
358
- addressRegion: string | null;
359
- addressPostalCode: string | null;
360
- addressCountry: string | null;
361
- companyName: string | null;
362
- jobTitle: string | null;
363
- contactConfirmedAt: Date | null;
364
- contactConfirmedBy: string | null;
365
- };
366
-
367
- /** The wire's contact: every key present, `null` where blank, the address whole or `null`. */
368
- export function personContactFromRow(row: PersonContactRow): PersonContact {
369
- // `?? null` so a row that does not carry a column reads as blank, never as
370
- // `undefined` on the wire.
371
- const line1 = row.addressLine1 ?? null;
372
- const city = row.addressCity ?? null;
373
- const region = row.addressRegion ?? null;
374
- const postalCode = row.addressPostalCode ?? null;
375
- const country = row.addressCountry ?? null;
376
- const address: PersonAddress | null =
377
- line1 !== null && city !== null && region !== null && postalCode !== null && country !== null
378
- ? {
379
- addressLine1: line1,
380
- addressLine2: row.addressLine2 ?? null,
381
- city,
382
- region,
383
- postalCode,
384
- country,
385
- }
386
- : null;
387
- return {
388
- givenName: row.givenName ?? null,
389
- familyName: row.familyName ?? null,
390
- telephone: row.telephone ?? null,
391
- address,
392
- companyName: row.companyName ?? null,
393
- jobTitle: row.jobTitle ?? null,
394
- };
395
- }
396
-
397
- /** Who last saved the contact by hand, and when — `null` while only captures have written it. */
398
- export function contactConfirmedFromRow(
399
- row: Pick<PersonContactRow, "contactConfirmedAt" | "contactConfirmedBy">,
400
- ): { by: "operator" | "client"; at: string } | null {
401
- if (row.contactConfirmedAt === null || row.contactConfirmedAt === undefined)
402
- return null;
403
- if (
404
- row.contactConfirmedBy !== "operator" &&
405
- row.contactConfirmedBy !== "client"
406
- )
407
- return null;
408
- return {
409
- by: row.contactConfirmedBy,
410
- at: row.contactConfirmedAt.toISOString(),
411
- };
412
- }
413
-
414
- type AddressColumns = {
415
- addressLine1: string | null;
416
- addressLine2: string | null;
417
- addressCity: string | null;
418
- addressRegion: string | null;
419
- addressPostalCode: string | null;
420
- addressCountry: string | null;
421
- };
422
-
423
- /** The six address columns for an address — all six `null` for `null`. */
424
- function addressColumns(address: PersonAddress | null): AddressColumns {
425
- return {
426
- addressLine1: address?.addressLine1 ?? null,
427
- addressLine2: address?.addressLine2 ?? null,
428
- addressCity: address?.city ?? null,
429
- addressRegion: address?.region ?? null,
430
- addressPostalCode: address?.postalCode ?? null,
431
- addressCountry: address?.country ?? null,
432
- };
433
- }
434
-
435
- /** The scalar contact fields, each its own column of the same name. */
436
- const SCALAR_CONTACT_FIELDS = [
437
- "givenName",
438
- "familyName",
439
- "telephone",
440
- "companyName",
441
- "jobTitle",
442
- ] as const;
443
-
444
- /** Every supplied contact field as insert columns. */
445
- function contactInsertColumns(
446
- contact: Partial<PersonContact>,
447
- ): Record<string, string | null> {
448
- const columns: Record<string, string | null> = {};
449
- for (const field of SCALAR_CONTACT_FIELDS) {
450
- const value = contact[field];
451
- if (value !== undefined && value !== null) columns[field] = value;
452
- }
453
- if (contact.address) Object.assign(columns, addressColumns(contact.address));
454
- return columns;
455
- }
456
-
457
- /** `"Ada" + "Lovelace"` → `"Ada Lovelace"`, `"Ada"` alone → `"Ada"`, neither → `null`. */
458
- function joinedName(contact: Partial<PersonContact>): string | null {
459
- return normalizePersonName(
460
- [contact.givenName, contact.familyName].filter((part) => part).join(" "),
461
- );
462
- }
463
-
464
- function usableCustomerId(raw: string | null | undefined): string | null {
465
- return typeof raw === "string" &&
466
- raw.length > 0 &&
467
- raw.length <= MAX_CUSTOMER_ID_LENGTH
468
- ? raw
469
- : null;
470
- }
471
-
472
- // ─── findOrCreatePerson ───────────────────────────────────────────────────────
473
-
474
- /** The one delegate this file writes through — the shared client or a transaction's. */
475
- export type PersonDb = Pick<Prisma.TransactionClient, "person">;
476
-
477
- export type FindOrCreatePersonResult =
478
- | {
479
- ok: true;
480
- value: {
481
- personId: Id<"person">;
482
- created: boolean;
483
- billing: "written" | "kept" | "unchanged";
484
- };
485
- }
486
- | { ok: false; error: { kind: "invalid_email" | "internal_error" } };
487
-
488
- type MatchedPerson = {
489
- id: string;
490
- name: string | null;
491
- userId: string | null;
492
- billingCustomerId: string | null;
493
- // #8435 D4.4 — the blank-sentinels the contact fill reads. A value reading
494
- // `undefined` (a test double that does not carry the key) is "not blank",
495
- // and an undefined `contactConfirmedAt` is "confirmed": no fill runs.
496
- givenName?: string | null;
497
- familyName?: string | null;
498
- telephone?: string | null;
499
- addressLine1?: string | null;
500
- companyName?: string | null;
501
- jobTitle?: string | null;
502
- contactConfirmedAt?: Date | null;
503
- };
504
-
505
- const MATCH_SELECT = {
506
- id: true,
507
- name: true,
508
- userId: true,
509
- billingCustomerId: true,
510
- givenName: true,
511
- familyName: true,
512
- telephone: true,
513
- addressLine1: true,
514
- companyName: true,
515
- jobTitle: true,
516
- contactConfirmedAt: true,
517
- } as const;
518
-
519
- /**
520
- * Step 3's writes on an email match: the customer id only when the Person
521
- * has none and is linked to no user, the name only when it has none — each
522
- * one guarded `updateMany`, so a concurrent writer's value, or a stamp that
523
- * lands between the read and the write, is never overwritten or bypassed.
524
- */
525
- async function enrichMatched(
526
- db: PersonDb,
527
- workspaceId: string,
528
- person: MatchedPerson,
529
- name: string | null,
530
- customerId: string | null,
531
- ): Promise<"written" | "kept" | "unchanged"> {
532
- let billing: "written" | "kept" | "unchanged" = "unchanged";
533
- if (customerId !== null) {
534
- if (person.billingCustomerId !== null || person.userId !== null) {
535
- billing = "kept";
536
- } else {
537
- const written = await db.person.updateMany({
538
- where: { id: person.id, workspaceId, userId: null, billingCustomerId: null },
539
- data: { billingProvider: "stripe", billingCustomerId: customerId },
540
- });
541
- billing = written.count === 1 ? "written" : "kept";
542
- }
543
- }
544
- if (name !== null && person.name === null) {
545
- await db.person.updateMany({
546
- where: { id: person.id, workspaceId, name: null },
547
- data: { name },
548
- });
549
- }
550
- return billing;
551
- }
552
-
553
- /**
554
- * The contact fill on an email match (#8435 D4.3): for each supplied field
555
- * whose stored value is `null`, one guarded `updateMany`; the address is one
556
- * statement guarded on `addressLine1: null` writing all six. It runs ONLY
557
- * while the Person is uncurated — linked to no user and never saved by hand
558
- * (`contactConfirmedAt` null) — and every guard repeats that condition, so a
559
- * link or a save that lands between the read and the write still wins
560
- * (SEC-8435-A). A stored value, whoever wrote it, is never overwritten.
561
- * Best-effort: a failed write is swallowed, never refusing the capture.
562
- */
563
- async function fillBlankContact(
564
- db: PersonDb,
565
- workspaceId: string,
566
- person: MatchedPerson,
567
- contact: Partial<PersonContact>,
568
- plainName: string | null,
569
- ): Promise<void> {
570
- if (person.userId !== null || person.contactConfirmedAt !== null) return;
571
- const uncurated = { id: person.id, workspaceId, userId: null, contactConfirmedAt: null };
572
- try {
573
- for (const field of SCALAR_CONTACT_FIELDS) {
574
- const value = contact[field];
575
- if (value !== undefined && value !== null && person[field] === null) {
576
- await db.person.updateMany({
577
- where: { ...uncurated, [field]: null } as Prisma.PersonWhereInput,
578
- data: { [field]: value } as Prisma.PersonUpdateManyMutationInput,
579
- });
580
- }
581
- }
582
- if (contact.address && person.addressLine1 === null) {
583
- await db.person.updateMany({
584
- where: { ...uncurated, addressLine1: null },
585
- data: addressColumns(contact.address),
586
- });
587
- }
588
- // The display name from given + family, only when nothing named it.
589
- if (plainName === null && person.name === null) {
590
- const derived = joinedName(contact);
591
- if (derived !== null) {
592
- await db.person.updateMany({ where: { ...uncurated, name: null }, data: { name: derived } });
593
- }
594
- }
595
- } catch {
596
- // A contact fill never changes the capture's outcome.
597
- }
598
- }
599
-
600
- /**
601
- * Find this workspace's Person for an email, or create it.
602
- *
603
- * @param workspaceId the SERVER-RESOLVED workspace id — never a request value.
604
- * @param input.email the raw address; normalized here.
605
- * @param input.name an optional display name, from anonymous text.
606
- * @param input.billingCustomerId an optional provider customer id.
607
- * @param input.contact optional, already-normalized contact fields
608
- * (`normalizeCapturedContact`): a created Person carries them all; a matched
609
- * uncurated Person gains only the ones it has blank (#8435 D4).
610
- * @param db the shared client by default; the appointment path passes its
611
- * transaction client so the step runs on the lock's own connection.
612
- */
613
- export async function findOrCreatePerson(
614
- workspaceId: string,
615
- input: {
616
- email: string;
617
- name?: string | null;
618
- billingCustomerId?: string | null;
619
- contact?: Partial<PersonContact>;
620
- },
621
- db: PersonDb = prisma,
622
- ): Promise<FindOrCreatePersonResult> {
623
- const email =
624
- typeof input.email === "string" ? normalizePersonEmail(input.email) : "";
625
- if (!isStorablePersonEmail(email)) {
626
- return { ok: false, error: { kind: "invalid_email" } };
627
- }
628
- const name = normalizePersonName(input.name);
629
- const customerId = usableCustomerId(input.billingCustomerId);
630
- const contact = input.contact ?? {};
631
- const hasContact = Object.keys(contact).length > 0;
632
-
633
- try {
634
- // Step 2 — a customer id names its Person; nothing is written.
635
- if (customerId !== null) {
636
- const byCustomer = await db.person.findFirst({
637
- where: { workspaceId, billingCustomerId: customerId },
638
- orderBy: [{ createdAt: "asc" }, { id: "asc" }],
639
- select: { id: true },
640
- });
641
- if (byCustomer) {
642
- return {
643
- ok: true,
644
- value: {
645
- personId: byCustomer.id as Id<"person">,
646
- created: false,
647
- billing: "unchanged",
648
- },
649
- };
650
- }
651
- }
652
-
653
- // Step 3 — the email's Person.
654
- const byEmail = await db.person.findFirst({
655
- where: { workspaceId, email },
656
- select: MATCH_SELECT,
657
- });
658
- if (byEmail) {
659
- const billing = await enrichMatched(db, workspaceId, byEmail, name, customerId);
660
- if (hasContact) await fillBlankContact(db, workspaceId, byEmail, contact, name);
661
- return {
662
- ok: true,
663
- value: { personId: byEmail.id as Id<"person">, created: false, billing },
664
- };
665
- }
666
-
667
- // Step 4 — insert, absorbing a concurrent duplicate in the database.
668
- const inserted = await db.person.createMany({
669
- data: [
670
- {
671
- workspaceId,
672
- email,
673
- // A supplied name wins; else given + family joined (#8435 D4.2).
674
- name: name ?? joinedName(contact),
675
- ...(customerId !== null && {
676
- billingProvider: "stripe",
677
- billingCustomerId: customerId,
678
- }),
679
- ...(hasContact && contactInsertColumns(contact)),
680
- },
681
- ],
682
- skipDuplicates: true,
683
- });
684
- const winner = await db.person.findFirst({
685
- where: { workspaceId, email },
686
- select: MATCH_SELECT,
687
- });
688
- if (!winner) return { ok: false, error: { kind: "internal_error" } };
689
- if (inserted.count === 1) {
690
- return {
691
- ok: true,
692
- value: {
693
- personId: winner.id as Id<"person">,
694
- created: true,
695
- billing: customerId !== null ? "written" : "unchanged",
696
- },
697
- };
698
- }
699
- // A concurrent create won the race: treat its row as an email match.
700
- const billing = await enrichMatched(db, workspaceId, winner, name, customerId);
701
- if (hasContact) await fillBlankContact(db, workspaceId, winner, contact, name);
702
- return {
703
- ok: true,
704
- value: { personId: winner.id as Id<"person">, created: false, billing },
705
- };
706
- } catch {
707
- return { ok: false, error: { kind: "internal_error" } };
708
- }
709
- }
710
-
711
- // ─── stampPersonUser (D5) ─────────────────────────────────────────────────────
712
-
713
- export type StampPersonUserOutcome =
714
- | "stamped"
715
- | "already_linked"
716
- | "user_unverified"
717
- | "email_mismatch"
718
- | "linked_to_other_user"
719
- | "user_on_other_person"
720
- | "not_found"
721
- | "internal_error";
722
-
723
- function isUniqueViolation(err: unknown): boolean {
724
- return (
725
- typeof err === "object" &&
726
- err !== null &&
727
- (err as { code?: unknown }).code === "P2002"
728
- );
729
- }
730
-
731
- /**
732
- * Link a Person to a User — only when the User's VERIFIED email normalizes
733
- * to the Person's (`verified-email.ts`'s rule), and never over an existing
734
- * link. Creating a Person never calls this: no verified email is needed to
735
- * exist. Runs outside any transaction, so catching a P2002 is safe here.
736
- */
737
- export async function stampPersonUser(
738
- workspaceId: string,
739
- personId: string,
740
- userId: string,
741
- ): Promise<StampPersonUserOutcome> {
742
- try {
743
- const person = await prisma.person.findFirst({
744
- where: { id: personId, workspaceId },
745
- select: { email: true, userId: true },
746
- });
747
- if (!person) return "not_found";
748
- if (person.userId === userId) return "already_linked";
749
- if (person.userId !== null) return "linked_to_other_user";
750
-
751
- const verified = await findVerifiedEmail(userId);
752
- if (!verified.ok) return "user_unverified";
753
- if (normalizePersonEmail(verified.value.email) !== person.email) {
754
- return "email_mismatch";
755
- }
756
-
757
- let count: number;
758
- try {
759
- ({ count } = await prisma.person.updateMany({
760
- where: { id: personId, workspaceId, userId: null },
761
- data: { userId },
762
- }));
763
- } catch (err) {
764
- if (isUniqueViolation(err)) return "user_on_other_person";
765
- throw err;
766
- }
767
- if (count === 1) return "stamped";
768
-
769
- const reread = await prisma.person.findFirst({
770
- where: { id: personId, workspaceId },
771
- select: { userId: true },
772
- });
773
- if (!reread) return "not_found";
774
- return reread.userId === userId ? "already_linked" : "linked_to_other_user";
775
- } catch {
776
- return "internal_error";
777
- }
778
- }
779
-
780
- // ─── recordCheckoutPerson (D6) ────────────────────────────────────────────────
781
-
782
- export type CheckoutPersonOutcome =
783
- | "person_written"
784
- | "person_kept"
785
- | "person_unchanged"
786
- | "person_skipped"
787
- | "person_failed";
788
-
789
- /** `recordCheckoutPerson`'s outcome plus the Person it found or created (#8765 D14). */
790
- export type CheckoutPersonResult = {
791
- outcome: CheckoutPersonOutcome;
792
- /** The checkout's Person, or `null` when the step skipped or failed. */
793
- person: { workspaceId: string; personId: string } | null;
794
- };
795
-
796
- /**
797
- * The hub webhook's Person step for a paid `checkout.session.completed` of
798
- * its OWN site carrying `metadata.flowCheckoutId`.
799
- *
800
- * Reads the checkout row THIS session completed (`stripeSessionId` must be
801
- * this session's id and the row `completed`), so a session that merely names
802
- * another checkout's id matches nothing (SEC-852-H); then that row's lead,
803
- * scoped by the row's workspace. The customer id's one source is
804
- * `session.customer`, and it is passed only for a LIVE-mode event: a
805
- * test-mode checkout typed with someone else's email cannot pin a customer
806
- * id on their Person (SEC-852-A). Never throws.
807
- */
808
- export async function recordCheckoutPerson(input: {
809
- flowCheckoutId: string;
810
- stripeSessionId: string | null | undefined;
811
- customer: unknown;
812
- livemode: boolean;
813
- }): Promise<CheckoutPersonOutcome> {
814
- return (await resolveCheckoutPerson(input)).outcome;
815
- }
816
-
817
- /**
818
- * `recordCheckoutPerson`, also answering the Person it found or created —
819
- * the subject of the checkout's terms grant (#8765 D14), which reads the
820
- * flow checkout's Person from here and never creates one itself.
821
- */
822
- export async function resolveCheckoutPerson(input: {
823
- flowCheckoutId: string;
824
- stripeSessionId: string | null | undefined;
825
- customer: unknown;
826
- livemode: boolean;
827
- }): Promise<CheckoutPersonResult> {
828
- const skipped = { outcome: "person_skipped", person: null } as const;
829
- if (typeof input.stripeSessionId !== "string" || input.stripeSessionId === "") {
830
- return skipped;
831
- }
832
- try {
833
- const checkout = await prisma.flowCheckout.findFirst({
834
- where: {
835
- id: input.flowCheckoutId,
836
- stripeSessionId: input.stripeSessionId,
837
- status: "completed",
838
- },
839
- select: { leadId: true, workspaceId: true },
840
- });
841
- if (!checkout) return skipped;
842
-
843
- const lead = await prisma.lead.findFirst({
844
- where: { id: checkout.leadId, workspaceId: checkout.workspaceId },
845
- select: { email: true },
846
- });
847
- if (!lead) return skipped;
848
-
849
- const customerId =
850
- input.livemode === true && typeof input.customer === "string"
851
- ? input.customer
852
- : undefined;
853
- const result = await findOrCreatePerson(checkout.workspaceId, {
854
- email: lead.email,
855
- ...(customerId !== undefined && { billingCustomerId: customerId }),
856
- });
857
- if (!result.ok) {
858
- return result.error.kind === "invalid_email"
859
- ? skipped
860
- : { outcome: "person_failed", person: null };
861
- }
862
- const person = {
863
- workspaceId: checkout.workspaceId,
864
- personId: result.value.personId,
865
- };
866
- switch (result.value.billing) {
867
- case "written":
868
- return { outcome: "person_written", person };
869
- case "kept":
870
- return { outcome: "person_kept", person };
871
- default:
872
- return { outcome: "person_unchanged", person };
873
- }
874
- } catch {
875
- return { outcome: "person_failed", person: null };
876
- }
877
- }
878
-
879
- // ─── The member area (#7877 D8) ───────────────────────────────────────────────
880
-
881
- /**
882
- * The Person a member read answers for: this workspace's Person linked to
883
- * the caller, else the one whose email is the caller's VERIFIED email —
884
- * linked now through `stampPersonUser` (its rule, unchanged) and the answer
885
- * only on `stamped` or `already_linked`. `null` is "not a client here";
886
- * every other stamp outcome, and a store error, is `null` too, logged by its
887
- * kind only. Never throws.
888
- *
889
- * A fresh `stamped` link — and only that — releases the Person's held
890
- * checkout plan (#8793 D2, `releaseHeldCheckoutPlan`): the caller has just
891
- * proven they own the email the purchases were made under. A Person linked
892
- * earlier, a refused stamp and every other caller of `stampPersonUser` (the
893
- * Inbox accept above all) release nothing. The release never changes the
894
- * answer; an `ambiguous` or `internal_error` release is logged by its kind
895
- * only, as `{ event: "checkout-plan-release", outcome }`.
896
- *
897
- * @param workspaceId the SERVER-RESOLVED workspace (the calling spoke's).
898
- * @param userId the authenticated caller.
899
- * @param verifiedEmail the caller's verified address, read by the caller.
900
- */
901
- export async function resolveMemberPersonId(
902
- workspaceId: string,
903
- userId: string,
904
- verifiedEmail: string,
905
- log?: { warn: (obj: object, msg: string) => void },
906
- ): Promise<string | null> {
907
- let byEmail: { id: string } | null;
908
- try {
909
- const linked = await prisma.person.findFirst({
910
- where: { workspaceId, userId },
911
- select: { id: true },
912
- });
913
- if (linked) return linked.id;
914
-
915
- byEmail = await prisma.person.findFirst({
916
- where: { workspaceId, email: normalizePersonEmail(verifiedEmail) },
917
- select: { id: true },
918
- });
919
- } catch {
920
- // A store error is "not a client here" for this read: never thrown.
921
- log?.warn({ event: "member-person-stamp", outcome: "internal_error" }, "[person] member read failed");
922
- return null;
923
- }
924
- if (!byEmail) return null;
925
-
926
- const outcome = await stampPersonUser(workspaceId, byEmail.id, userId);
927
- if (outcome === "stamped") {
928
- const release = await releaseHeldCheckoutPlan(
929
- workspaceId,
930
- byEmail.id,
931
- userId,
932
- normalizePersonEmail(verifiedEmail),
933
- );
934
- if (release === "ambiguous" || release === "internal_error") {
935
- log?.warn({ event: "checkout-plan-release", outcome: release }, "[person] held checkout plan not released");
936
- }
937
- return byEmail.id;
938
- }
939
- if (outcome === "already_linked") return byEmail.id;
940
- log?.warn({ event: "member-person-stamp", outcome }, "[person] member link refused");
941
- return null;
942
- }
943
-
944
- /**
945
- * The customer id the hub vouches for to a spoke's buy route (#7961, spec
946
- * 7894 D13): the `billingCustomerId` of the Person `resolveMemberPersonId`
947
- * answers for (workspace, caller), read only while that Person's `userId` is
948
- * the caller — never a Person matched by email alone (SEC-7894-6). `null` is
949
- * "no customer to reuse"; a store error is `null` too, logged by its kind
950
- * only. Never throws.
951
- *
952
- * @param workspaceId the SERVER-RESOLVED workspace (the calling spoke's).
953
- * @param userId the authenticated caller.
954
- * @param verifiedEmail the caller's verified address, read by the caller.
955
- */
956
- export async function resolveCallerBillingCustomerId(
957
- workspaceId: string,
958
- userId: string,
959
- verifiedEmail: string,
960
- log?: { warn: (obj: object, msg: string) => void },
961
- ): Promise<string | null> {
962
- const personId = await resolveMemberPersonId(workspaceId, userId, verifiedEmail, log);
963
- if (personId === null) return null;
964
- try {
965
- const person = await prisma.person.findFirst({
966
- where: { id: personId, workspaceId, userId },
967
- select: { billingCustomerId: true },
968
- });
969
- return person?.billingCustomerId ?? null;
970
- } catch {
971
- log?.warn({ event: "buyer-customer-read", outcome: "internal_error" }, "[person] buyer read failed");
972
- return null;
973
- }
974
- }
975
-
976
- export type SetPlanOfferingOutcome = "set" | "not_found" | "internal_error";
977
-
978
- /**
979
- * Set — or, with `null`, clear — a Person's plan: the ONE writer of
980
- * `Person.planOffering`, called by the Admin's plan route and the held
981
- * checkout plan's release (#8793). One `updateMany` scoped by the
982
- * server-resolved workspace; no row → `not_found`. Never throws.
983
- *
984
- * With a `guard` (#8793 D5, the release's own write) the statement also
985
- * requires a blank plan and the Person still linked to `guard.linkedUserId`,
986
- * so a plan already set — or one the operator PUTs at the same moment — wins
987
- * and the answer is `not_found`. Without one it is the operator's overwrite.
988
- */
989
- export async function setPersonPlanOffering(
990
- workspaceId: string,
991
- personId: string,
992
- planOffering: string | null,
993
- guard?: { linkedUserId: string },
994
- ): Promise<SetPlanOfferingOutcome> {
995
- try {
996
- const { count } = await prisma.person.updateMany({
997
- where: {
998
- id: personId,
999
- workspaceId,
1000
- ...(guard !== undefined && { planOffering: null, userId: guard.linkedUserId }),
1001
- },
1002
- data: { planOffering },
1003
- });
1004
- return count === 0 ? "not_found" : "set";
1005
- } catch {
1006
- return "internal_error";
1007
- }
1008
- }
1009
-
1010
- // ─── The held checkout plan (#8793 D3, D4) ───────────────────────────────────
1011
-
1012
- /**
1013
- * A spoke checkout's plan key: the offering minus a leading `<site>_` (when
1014
- * the site is a string), answered only when `planOfferingSchema` — the key
1015
- * the operator's PUT accepts — admits it; else `null`. Shared by the
1016
- * checkout's hold (`lead-routing.ts`) and the release below.
1017
- */
1018
- export function checkoutPlanKey(offering: unknown, site: unknown): string | null {
1019
- if (typeof offering !== "string") return null;
1020
- const prefix = typeof site === "string" ? `${site}_` : null;
1021
- const key = prefix !== null && offering.startsWith(prefix) ? offering.slice(prefix.length) : offering;
1022
- return planOfferingSchema.safeParse(key).success ? key : null;
1023
- }
1024
-
1025
- export type HeldPlanOutcome = "set" | "none" | "ambiguous" | "kept" | "internal_error";
1026
-
1027
- /** The most held purchases a release weighs; one more is `ambiguous`. */
1028
- const HELD_PURCHASES_READ = 50;
1029
-
1030
- /**
1031
- * Release a Person's held checkout plan (#8793 D3) — called only by
1032
- * `resolveMemberPersonId` on a fresh `stamped` link, so `verifiedEmail` (the
1033
- * caller's, normalized) is the Person's own. The Person's marked spoke
1034
- * checkout leads (`Lead.spokeCheckout`, newest 51) under that email are taken
1035
- * as a SET: one distinct plan key is written to a blank plan through the
1036
- * guarded `setPersonPlanOffering` (`set`, or `kept` when the guard matched
1037
- * nothing — a plan was already there, or the Person is no longer linked to
1038
- * `userId`); no key is `none`; more than one key, or more than 50 leads, is
1039
- * `ambiguous` and writes nothing — the operator sets the plan. So a
1040
- * stranger's purchase under the owner's email can deny the automatic plan,
1041
- * never choose it (SEC-8793-A). A store error is `internal_error`. Never
1042
- * throws, and logs nothing: the caller logs the kind.
1043
- */
1044
- export async function releaseHeldCheckoutPlan(
1045
- workspaceId: string,
1046
- personId: string,
1047
- userId: string,
1048
- verifiedEmail: string,
1049
- ): Promise<HeldPlanOutcome> {
1050
- try {
1051
- const rows = await prisma.lead.findMany({
1052
- where: { workspaceId, personId, spokeCheckout: true },
1053
- orderBy: [{ createdAt: "desc" }, { id: "desc" }],
1054
- take: HELD_PURCHASES_READ + 1,
1055
- select: { email: true, payload: true },
1056
- });
1057
- if (rows.length > HELD_PURCHASES_READ) return "ambiguous";
1058
-
1059
- const keys = new Set<string>();
1060
- for (const row of rows) {
1061
- // The purchase read's own rule: a purchase is its Person's only under
1062
- // the Person's own email.
1063
- if (normalizePersonEmail(row.email) !== verifiedEmail) continue;
1064
- const payload = (row.payload ?? {}) as Record<string, unknown>;
1065
- const key = checkoutPlanKey(payload["offering"], payload["site"]);
1066
- if (key !== null) keys.add(key);
1067
- }
1068
- if (keys.size > 1) return "ambiguous";
1069
- const [key] = keys;
1070
- if (key === undefined) return "none";
1071
-
1072
- const written = await setPersonPlanOffering(workspaceId, personId, key, { linkedUserId: userId });
1073
- if (written === "set") return "set";
1074
- return written === "not_found" ? "kept" : "internal_error";
1075
- } catch {
1076
- return "internal_error";
1077
- }
1078
- }
1079
-
1080
- // ─── The two human edits of the contact (#8435 D9, D11) ───────────────────────
1081
-
1082
- export type SavedContact = {
1083
- contact: PersonContact;
1084
- confirmed: { by: "operator" | "client"; at: string } | null;
1085
- };
1086
-
1087
- export type SaveContactOutcome =
1088
- | { kind: "saved"; value: SavedContact }
1089
- | { kind: "not_found" | "internal_error" };
1090
-
1091
- /** The columns a patch writes: only the keys present; the address key writes (or clears) all six. */
1092
- function patchColumns(patch: PersonContactPatch): Record<string, string | null> {
1093
- const columns: Record<string, string | null> = {};
1094
- for (const field of SCALAR_CONTACT_FIELDS) {
1095
- const value = patch[field];
1096
- if (value !== undefined) columns[field] = value;
1097
- }
1098
- if (patch.address !== undefined) Object.assign(columns, addressColumns(patch.address));
1099
- return columns;
1100
- }
1101
-
1102
- async function readSavedContact(workspaceId: string, personId: string): Promise<SaveContactOutcome> {
1103
- const row = await prisma.person.findFirst({
1104
- where: { id: personId, workspaceId },
1105
- select: PERSON_CONTACT_SELECT,
1106
- });
1107
- if (!row) return { kind: "not_found" };
1108
- return { kind: "saved", value: { contact: personContactFromRow(row), confirmed: contactConfirmedFromRow(row) } };
1109
- }
1110
-
1111
- /**
1112
- * The operator's save (#8435 D9): one `updateMany` scoped by the
1113
- * server-resolved workspace, writing only the keys present plus the
1114
- * confirmation stamp (`operator`). No row → `not_found`. Never throws.
1115
- */
1116
- export async function updatePersonContact(
1117
- workspaceId: string,
1118
- personId: string,
1119
- patch: PersonContactPatch,
1120
- ): Promise<SaveContactOutcome> {
1121
- try {
1122
- const { count } = await prisma.person.updateMany({
1123
- where: { id: personId, workspaceId },
1124
- data: {
1125
- ...patchColumns(patch),
1126
- contactConfirmedAt: new Date(),
1127
- contactConfirmedBy: "operator",
1128
- } as Prisma.PersonUpdateManyMutationInput,
1129
- });
1130
- if (count === 0) return { kind: "not_found" };
1131
- return await readSavedContact(workspaceId, personId);
1132
- } catch {
1133
- return { kind: "internal_error" };
1134
- }
1135
- }
1136
-
1137
- /**
1138
- * The Person a client's first contact save belongs to (#8435 D11): the
1139
- * caller's VERIFIED email's Person in this workspace, found or created, and
1140
- * linked to the caller through `stampPersonUser`'s own rule. Anything but a
1141
- * stamped or already-linked outcome is `unavailable` — nothing is written for
1142
- * the contact. Never throws.
1143
- */
1144
- export async function ensureMemberPerson(
1145
- workspaceId: string,
1146
- userId: string,
1147
- ): Promise<{ ok: true; personId: string } | { ok: false }> {
1148
- try {
1149
- const verified = await findVerifiedEmail(userId);
1150
- if (!verified.ok) return { ok: false };
1151
- const person = await findOrCreatePerson(workspaceId, { email: verified.value.email });
1152
- if (!person.ok) return { ok: false };
1153
- const outcome = await stampPersonUser(workspaceId, person.value.personId, userId);
1154
- if (outcome !== "stamped" && outcome !== "already_linked") return { ok: false };
1155
- return { ok: true, personId: person.value.personId };
1156
- } catch {
1157
- return { ok: false };
1158
- }
1159
- }
1160
-
1161
- /**
1162
- * The client's save (#8435 D11): ONE `updateMany` guarded on `{ id,
1163
- * workspaceId, userId }` — only the caller's own linked Person is ever
1164
- * written — replacing all six contact keys with the body and stamping
1165
- * `client`, so no value the client never saw survives it (SEC-8435-B). No
1166
- * row → `not_found`. Never throws.
1167
- */
1168
- export async function setOwnPersonContact(
1169
- workspaceId: string,
1170
- personId: string,
1171
- userId: string,
1172
- contact: PersonContactPatch,
1173
- ): Promise<SaveContactOutcome> {
1174
- try {
1175
- const { count } = await prisma.person.updateMany({
1176
- where: { id: personId, workspaceId, userId },
1177
- data: {
1178
- ...patchColumns(contact),
1179
- contactConfirmedAt: new Date(),
1180
- contactConfirmedBy: "client",
1181
- } as Prisma.PersonUpdateManyMutationInput,
1182
- });
1183
- if (count === 0) return { kind: "not_found" };
1184
- return await readSavedContact(workspaceId, personId);
1185
- } catch {
1186
- return { kind: "internal_error" };
1187
- }
1188
- }