@cosmicdrift/kumiko-bundled-features 0.340.0 → 0.342.0

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 (174) hide show
  1. package/dist/audit/constants.d.ts +3 -0
  2. package/dist/audit/constants.js +3 -0
  3. package/dist/audit/feature.js +34 -7
  4. package/dist/audit/run-escape-hatch-retention.d.ts +10 -0
  5. package/dist/audit/run-escape-hatch-retention.js +27 -0
  6. package/dist/auth-email-password/bootstrap.d.ts +80 -0
  7. package/dist/auth-email-password/bootstrap.js +170 -0
  8. package/dist/auth-email-password/constants.d.ts +2 -0
  9. package/dist/auth-email-password/constants.js +5 -0
  10. package/dist/auth-email-password/errors.d.ts +1 -0
  11. package/dist/auth-email-password/errors.js +6 -0
  12. package/dist/auth-email-password/feature.js +2 -1
  13. package/dist/auth-email-password/handlers/invite-accept-with-login.write.js +9 -1
  14. package/dist/auth-email-password/handlers/invite-accept.write.js +8 -0
  15. package/dist/auth-email-password/handlers/invite-create.write.d.ts +11 -0
  16. package/dist/auth-email-password/handlers/invite-create.write.js +134 -87
  17. package/dist/auth-email-password/handlers/invite-signup-complete.write.js +5 -1
  18. package/dist/auth-email-password/handlers/signup-request.write.js +1 -0
  19. package/dist/auth-email-password/handlers/token-request-handler.js +1 -0
  20. package/dist/auth-email-password/index.d.ts +2 -0
  21. package/dist/auth-email-password/index.js +1 -0
  22. package/dist/auth-email-password/invite-token-store.d.ts +3 -0
  23. package/dist/auth-email-password/invite-token-store.js +5 -0
  24. package/dist/auth-email-password/magic-link-mail.js +1 -0
  25. package/dist/auth-email-password/web/auth-form-primitives.d.ts +7 -1
  26. package/dist/auth-email-password/web/auth-form-primitives.js +5 -5
  27. package/dist/auth-email-password/web/confirm-account-unlock-screen.js +3 -3
  28. package/dist/auth-email-password/web/forgot-password-screen.js +2 -2
  29. package/dist/auth-email-password/web/invite-accept-screen.js +9 -9
  30. package/dist/auth-email-password/web/login-screen.js +8 -8
  31. package/dist/auth-email-password/web/request-account-unlock-screen.js +2 -2
  32. package/dist/auth-email-password/web/reset-password-screen.js +3 -3
  33. package/dist/auth-email-password/web/session-bootstrap-error.js +3 -3
  34. package/dist/auth-email-password/web/signup-complete-screen.js +3 -3
  35. package/dist/auth-email-password/web/signup-screen.js +2 -2
  36. package/dist/auth-email-password/web/verify-email-screen.js +3 -3
  37. package/dist/auth-mfa/config.d.ts +1 -1
  38. package/dist/auth-mfa/config.js +8 -3
  39. package/dist/auth-mfa/feature.d.ts +2 -0
  40. package/dist/auth-mfa/feature.js +2 -1
  41. package/dist/auth-mfa/mfa-status-checker.d.ts +2 -0
  42. package/dist/auth-mfa/mfa-status-checker.js +1 -1
  43. package/dist/auth-mfa/web/mfa-setup-preauth-screen.js +5 -5
  44. package/dist/auth-mfa/web/mfa-verify-screen.js +3 -3
  45. package/dist/billing-foundation/consumer-protection/issue-confirmation.js +1 -0
  46. package/dist/billing-foundation/consumer-protection/termination-mail.js +1 -0
  47. package/dist/billing-foundation/consumer-protection/termination-notify.js +1 -0
  48. package/dist/billing-foundation/consumer-protection/termination-pages.d.ts +8 -1
  49. package/dist/billing-foundation/consumer-protection/termination-pages.js +59 -32
  50. package/dist/billing-foundation/consumer-protection/termination-request.js +2 -0
  51. package/dist/billing-foundation/consumer-protection/termination-texts.d.ts +1 -0
  52. package/dist/billing-foundation/consumer-protection/termination-texts.js +3 -1
  53. package/dist/channel-discord/discord-channel.d.ts +4 -0
  54. package/dist/channel-discord/discord-channel.js +4 -0
  55. package/dist/channel-discord/feature.js +7 -2
  56. package/dist/channel-discord/index.d.ts +1 -1
  57. package/dist/channel-discord/index.js +1 -1
  58. package/dist/channel-email/email-channel.js +8 -1
  59. package/dist/channel-email/pii-guard.js +6 -5
  60. package/dist/channel-email/smtp-transport.js +22 -1
  61. package/dist/channel-email/types.d.ts +1 -0
  62. package/dist/channel-slack/feature.js +4 -2
  63. package/dist/channel-slack/index.d.ts +1 -1
  64. package/dist/channel-slack/index.js +1 -1
  65. package/dist/channel-slack/slack-channel.d.ts +4 -0
  66. package/dist/channel-slack/slack-channel.js +4 -0
  67. package/dist/channel-teams/feature.js +4 -2
  68. package/dist/channel-teams/index.d.ts +1 -1
  69. package/dist/channel-teams/index.js +1 -1
  70. package/dist/channel-teams/teams-channel.d.ts +4 -0
  71. package/dist/channel-teams/teams-channel.js +4 -0
  72. package/dist/channel-telegram/feature.js +4 -2
  73. package/dist/channel-telegram/index.d.ts +1 -1
  74. package/dist/channel-telegram/index.js +1 -1
  75. package/dist/channel-telegram/telegram-channel.d.ts +8 -0
  76. package/dist/channel-telegram/telegram-channel.js +28 -12
  77. package/dist/delivery/chat-webhook-channel.d.ts +5 -1
  78. package/dist/delivery/chat-webhook-channel.js +10 -5
  79. package/dist/delivery/chat-webhook-sender.d.ts +6 -2
  80. package/dist/delivery/chat-webhook-sender.js +13 -8
  81. package/dist/delivery/delivery-service.d.ts +4 -3
  82. package/dist/delivery/delivery-service.js +53 -33
  83. package/dist/delivery/index.d.ts +2 -2
  84. package/dist/delivery/index.js +2 -2
  85. package/dist/delivery/jobs.js +3 -1
  86. package/dist/delivery/types.d.ts +5 -3
  87. package/dist/legal-pages/feature.d.ts +3 -15
  88. package/dist/legal-pages/feature.js +20 -50
  89. package/dist/legal-pages/lib/boot-check.d.ts +10 -0
  90. package/dist/legal-pages/lib/boot-check.js +33 -0
  91. package/dist/legal-pages/lib/validate-routes.d.ts +2 -0
  92. package/dist/legal-pages/lib/validate-routes.js +17 -0
  93. package/dist/page-render/index.d.ts +2 -1
  94. package/dist/page-render/index.js +1 -0
  95. package/dist/page-render/layout.d.ts +7 -0
  96. package/dist/page-render/layout.js +5 -0
  97. package/dist/page-render/trailing-slash.d.ts +2 -0
  98. package/dist/page-render/trailing-slash.js +15 -0
  99. package/dist/renderer-simple/feature.js +1 -0
  100. package/dist/renderer-simple/index.d.ts +1 -1
  101. package/dist/renderer-simple/index.js +1 -1
  102. package/dist/renderer-simple/simple-renderer.d.ts +12 -5
  103. package/dist/renderer-simple/simple-renderer.js +66 -12
  104. package/dist/secrets/announce-secret-change.d.ts +8 -0
  105. package/dist/secrets/announce-secret-change.js +18 -0
  106. package/dist/secrets/feature.js +1 -1
  107. package/dist/secrets/handlers/delete.write.js +2 -0
  108. package/dist/secrets/handlers/set.write.js +6 -1
  109. package/dist/secrets/i18n.d.ts +4 -0
  110. package/dist/secrets/i18n.js +4 -0
  111. package/dist/secrets/secrets-context.d.ts +2 -0
  112. package/dist/secrets/secrets-context.js +3 -0
  113. package/dist/secrets/write-gate.d.ts +5 -1
  114. package/dist/secrets/write-gate.js +24 -1
  115. package/dist/tenant/constants.d.ts +1 -0
  116. package/dist/tenant/constants.js +1 -0
  117. package/dist/tenant/handlers/add-member.write.js +14 -3
  118. package/dist/tenant/handlers/invitations.query.d.ts +8 -0
  119. package/dist/tenant/handlers/invitations.query.js +15 -1
  120. package/dist/tenant/index.d.ts +1 -0
  121. package/dist/tenant/index.js +1 -0
  122. package/dist/tenant/invitation-table.d.ts +2 -0
  123. package/dist/tenant/invitation-table.js +10 -1
  124. package/dist/tenant-lifecycle/lifecycle-gate.d.ts +1 -0
  125. package/dist/tenant-lifecycle/lifecycle-gate.js +2 -0
  126. package/dist/tenant-lifecycle/run-tenant-destroy.js +2 -2
  127. package/dist/tier-engine/feature.js +83 -14
  128. package/dist/tier-engine/handlers/set-tenant-tier.write.d.ts +3 -3
  129. package/dist/tier-engine/handlers/set-tenant-tier.write.js +17 -10
  130. package/dist/user/invited-global-roles.d.ts +14 -0
  131. package/dist/user/invited-global-roles.js +35 -0
  132. package/dist/user-data-rights/feature.js +3 -0
  133. package/dist/waitlist/constants.d.ts +33 -0
  134. package/dist/waitlist/constants.js +35 -0
  135. package/dist/waitlist/entity.d.ts +433 -0
  136. package/dist/waitlist/entity.js +64 -0
  137. package/dist/waitlist/feature.d.ts +3 -0
  138. package/dist/waitlist/feature.js +38 -0
  139. package/dist/waitlist/handlers/invite.write.d.ts +9 -0
  140. package/dist/waitlist/handlers/invite.write.js +119 -0
  141. package/dist/waitlist/handlers/list.query.d.ts +1 -0
  142. package/dist/waitlist/handlers/list.query.js +10 -0
  143. package/dist/waitlist/handlers/reject.write.d.ts +7 -0
  144. package/dist/waitlist/handlers/reject.write.js +80 -0
  145. package/dist/waitlist/handlers/submit.write.d.ts +13 -0
  146. package/dist/waitlist/handlers/submit.write.js +112 -0
  147. package/dist/waitlist/i18n.d.ts +6 -0
  148. package/dist/waitlist/i18n.js +31 -0
  149. package/dist/waitlist/index.d.ts +5 -0
  150. package/dist/waitlist/index.js +4 -0
  151. package/dist/waitlist/lib.d.ts +10 -0
  152. package/dist/waitlist/lib.js +24 -0
  153. package/dist/waitlist/mail.d.ts +15 -0
  154. package/dist/waitlist/mail.js +73 -0
  155. package/dist/waitlist/options.d.ts +30 -0
  156. package/dist/waitlist/options.js +8 -0
  157. package/dist/waitlist/payloads.d.ts +14 -0
  158. package/dist/waitlist/payloads.js +47 -0
  159. package/dist/waitlist/screens.d.ts +2 -0
  160. package/dist/waitlist/screens.js +42 -0
  161. package/dist/waitlist/user-data-hooks.d.ts +3 -0
  162. package/dist/waitlist/user-data-hooks.js +71 -0
  163. package/package.json +17 -9
  164. package/src/auth-email-password/changes.json +30 -0
  165. package/src/auth-mfa/changes.json +6 -0
  166. package/src/billing-foundation/changes.json +6 -0
  167. package/src/channel-email/changes.json +6 -0
  168. package/src/channel-telegram/changes.json +9 -0
  169. package/src/delivery/changes.json +7 -0
  170. package/src/files/changes.json +13 -1
  171. package/src/renderer-simple/changes.json +6 -0
  172. package/src/secrets/changes.json +12 -0
  173. package/src/tenant/changes.json +6 -0
  174. package/src/waitlist/changes.json +23 -0
@@ -44,6 +44,8 @@ import { fetchOne, selectMany } from "@cosmicdrift/kumiko-framework/bun-db";
44
44
  import { buildEntityTable, createEventStoreExecutor, createTenantDb, } from "@cosmicdrift/kumiko-framework/db";
45
45
  import { defineEntityCreateHandler, defineEntityListHandler, defineEntityUpdateHandler, defineFeature, defineQueryHandler, HookPhases, SYSTEM_TENANT_ID, TENANT_TIER_RESOLVER_EXT, } from "@cosmicdrift/kumiko-framework/engine";
46
46
  import { getUnscopedAggregateStreamMaxVersion } from "@cosmicdrift/kumiko-framework/event-store";
47
+ import { createFallbackLogger } from "@cosmicdrift/kumiko-framework/logging";
48
+ import { CACHE_SYNC_TOPICS, isTierAssignmentSyncMessage, } from "@cosmicdrift/kumiko-framework/redis";
47
49
  import { getTemporal } from "@cosmicdrift/kumiko-framework/time";
48
50
  import * as z from "zod";
49
51
  import { tenantTable } from "../tenant/index.js";
@@ -59,6 +61,7 @@ import { isTrialActive } from "./trial.js";
59
61
  // from the entity definition — same shape buildEntityTable would produce
60
62
  // in the App's drizzle/schema.generated.ts.
61
63
  const tierAssignmentTable = buildEntityTable("tier-assignment", tierAssignmentEntity);
64
+ const log = createFallbackLogger("tier-engine");
62
65
  // Event-store-executor für direct-write aus dem auto-default-tier-hook.
63
66
  // Pattern wie tenant/seeding.ts: hook sieht AppContext (kein ctx.write),
64
67
  // muss aber atomisch mit tenant-create im selben TX schreiben → executor
@@ -146,10 +149,9 @@ export function createTierEngineFeature(opts = {}) {
146
149
  // Cross-tenant set + read f\u00fcr den tier-admin-Screen. tier-options liefert
147
150
  // dem Client die App-Tier-Namen aus der tierMap-Closure (sonst hartkodiert).
148
151
  //
149
- // onAssigned h\u00e4lt den Resolver-Cache nach einem direkten Executor-Write
150
- // warm (der den postSave-Hook NICHT feuert). Late-bind via Holder: ohne
151
- // tierMap bleibt es no-op (kein Resolver), im tierMap-Block unten wird
152
- // die echte Cache-Update-Funktion eingeh\u00e4ngt \u2014 analog alwaysOnHolder.
152
+ // onAssigned runs after commit (the direct executor write fires no postSave
153
+ // hook). Late-bound: without tierMap it stays a no-op, the tierMap block
154
+ // below installs the real cache update + publish.
153
155
  const onTierAssigned = { fn: () => { } };
154
156
  r.writeHandler(createSetTenantTierWrite({
155
157
  onAssigned: (tenantId, tier) => onTierAssigned.fn(tenantId, tier),
@@ -223,6 +225,21 @@ export function createTierEngineFeature(opts = {}) {
223
225
  // mergeAlwaysOn-calls — Late-bind via mutable holder, gefüllt vor allen
224
226
  // Requests (build läuft pre-listen via runDevApp/runProdApp-pickup).
225
227
  const alwaysOnHolder = { set: new Set() };
228
+ // Bus handed to build(); hooks fire outside build and publish through it.
229
+ const busHolder = { bus: undefined };
230
+ // Out-of-order guard: every cache mutation or reload start takes a ticket; a
231
+ // reload result is applied only while its ticket is still the tenant's latest.
232
+ let ticketCounter = 0;
233
+ const latestTicket = new Map();
234
+ const takeTicket = (tenantId) => {
235
+ ticketCounter += 1;
236
+ latestTicket.set(tenantId, ticketCounter);
237
+ return ticketCounter;
238
+ };
239
+ const publishAssignmentChange = (tenantId) => {
240
+ const message = { tenantId };
241
+ busHolder.bus?.publish(CACHE_SYNC_TOPICS.tierAssignment, message);
242
+ };
226
243
  // Trial: zeit-abgeleitet aus tenant.inserted_at (≈ Signup), live am
227
244
  // Feature-Gate geprüft — NICHT im Resolver-Cache. Der Resolver ist
228
245
  // boot-cached + synchron und sieht weder frische Signups noch den
@@ -237,15 +254,15 @@ export function createTierEngineFeature(opts = {}) {
237
254
  ? featuresForTier(tierMap, opts.trial.tier)
238
255
  : new Set();
239
256
  const nowMs = () => getTemporal().Now.instant().epochMilliseconds;
240
- // set-tenant-tier schreibt direkt über den Executor → der postSave-Hook
241
- // unten feuert dabei NICHT. Diese Funktion repliziert den Cache-Update
242
- // des Hooks, damit ein manueller Grant das effektive Feature-Set sofort
243
- // ändert (nicht nur die Projektion). Selber Cache, selbe mergeAlwaysOn-
244
- // Semantik wie der Hook.
245
- onTierAssigned.fn = (tenantId, tier) => {
257
+ // Immediate local visibility on the writing pod; other pods learn via the
258
+ // bus and re-read the row.
259
+ const applyAssignment = (tenantId, tier) => {
260
+ takeTicket(tenantId);
246
261
  cache.set(tenantId, mergeAlwaysOn(alwaysOnHolder.set, featuresForTier(tierMap, tier)));
262
+ publishAssignmentChange(tenantId);
247
263
  };
248
- // Invalidation: tier-assignment events update the cache.
264
+ onTierAssigned.fn = applyAssignment;
265
+ // Invalidation: tier-assignment CRUD events (e.g. the billing webhook) update the cache.
249
266
  r.hook("postSave", { allOf: "tier-assignment" }, async (result) => {
250
267
  // result.data has tenantId + tier (after entity-update merge)
251
268
  const data = result.data; // @cast-boundary engine-payload
@@ -256,15 +273,17 @@ export function createTierEngineFeature(opts = {}) {
256
273
  // blocken (afterCommit-pattern, side-effect-best-effort).
257
274
  if (typeof data.tenantId !== "string" || typeof data.tier !== "string")
258
275
  return;
259
- const tenantId = data.tenantId;
260
- cache.set(tenantId, mergeAlwaysOn(alwaysOnHolder.set, featuresForTier(tierMap, data.tier)));
276
+ applyAssignment(data.tenantId, data.tier);
261
277
  });
262
278
  r.hook("postDelete", { allOf: "tier-assignment" }, async (payload) => {
263
279
  const data = payload.data; // @cast-boundary engine-payload
264
280
  // skip: gleiche type-guard semantik wie postSave-hook oben.
265
281
  if (typeof data.tenantId !== "string")
266
282
  return;
267
- cache.delete(data.tenantId);
283
+ const tenantId = data.tenantId;
284
+ takeTicket(tenantId);
285
+ cache.delete(tenantId);
286
+ publishAssignmentChange(tenantId);
268
287
  });
269
288
  // Auto-default-tier-on-tenant-signup: hook fires inTransaction (atomic
270
289
  // mit tenant-create rollback), schreibt tier-assignment via Direct-
@@ -359,6 +378,56 @@ export function createTierEngineFeature(opts = {}) {
359
378
  for (const row of rows) {
360
379
  cache.set(row.tenantId, mergeAlwaysOn(computedAlwaysOn, featuresForTier(tierMap, row.tier)));
361
380
  }
381
+ // Read first, then set — never clear-then-refill: a cache miss falls back
382
+ // to the default tier, which would lock paying tenants out meanwhile.
383
+ const reloadTenant = async (tenantId) => {
384
+ const ticket = takeTicket(tenantId);
385
+ const row = await fetchOne(deps.db, tierAssignmentTable, { tenantId });
386
+ // skip: a newer write or reload for this tenant started meanwhile and owns the entry
387
+ if (latestTicket.get(tenantId) !== ticket)
388
+ return;
389
+ if (row) {
390
+ cache.set(tenantId, mergeAlwaysOn(computedAlwaysOn, featuresForTier(tierMap, row.tier)));
391
+ }
392
+ else {
393
+ cache.delete(tenantId);
394
+ }
395
+ };
396
+ const reloadAllTenants = async () => {
397
+ ticketCounter += 1;
398
+ const startedAtTicket = ticketCounter;
399
+ const current = await selectMany(deps.db, tierAssignmentTable);
400
+ const loaded = new Map();
401
+ for (const row of current) {
402
+ loaded.set(row.tenantId, mergeAlwaysOn(computedAlwaysOn, featuresForTier(tierMap, row.tier)));
403
+ }
404
+ const isNewerThanSnapshot = (tenantId) => (latestTicket.get(tenantId) ?? 0) > startedAtTicket;
405
+ for (const tenantId of [...cache.keys()]) {
406
+ if (!loaded.has(tenantId) && !isNewerThanSnapshot(tenantId))
407
+ cache.delete(tenantId);
408
+ }
409
+ for (const [tenantId, features] of loaded) {
410
+ if (!isNewerThanSnapshot(tenantId))
411
+ cache.set(tenantId, features);
412
+ }
413
+ };
414
+ busHolder.bus = deps.cacheSync;
415
+ deps.cacheSync?.subscribe(CACHE_SYNC_TOPICS.tierAssignment, (message) => {
416
+ // skip: malformed message from another process
417
+ if (!isTierAssignmentSyncMessage(message))
418
+ return;
419
+ reloadTenant(message.tenantId).catch((err) => {
420
+ log.error("tier reload failed, keeping cached set", {
421
+ tenantId: message.tenantId,
422
+ err,
423
+ });
424
+ });
425
+ });
426
+ deps.cacheSync?.onResync(() => {
427
+ reloadAllTenants().catch((err) => {
428
+ log.error("tier resync failed, keeping cached sets", { err });
429
+ });
430
+ });
362
431
  // Synchronous resolver-callback for dispatcher hot-path.
363
432
  const resolver = (tenantId) => {
364
433
  // Operator-tooling + async-event-dispatch convention: SYSTEM_TENANT_ID
@@ -1,9 +1,9 @@
1
1
  import { type TenantId } from "@cosmicdrift/kumiko-framework/engine";
2
2
  import * as z from "zod";
3
3
  export type SetTenantTierOptions = {
4
- /** Nach erfolgreichem Write aufgerufen, damit feature.ts den Resolver-
5
- * Cache aktualisieren kann (der Executor-Write feuert den postSave-Hook
6
- * nicht). Ohne tierMap kein Resolver → no-op. */
4
+ /** Runs after the write commits so feature.ts can update the resolver cache
5
+ * and notify the other processes. Without a tierMap there is no resolver
6
+ * and this is a no-op. */
7
7
  readonly onAssigned?: (tenantId: TenantId, tier: string) => void;
8
8
  /** Tier-Namen aus der tierMap-Closure — ohne sie (storage-only-Mode) bleibt
9
9
  * `tier` unvalidiert außer Length/Non-Empty. Mit ihr rejected der Handler
@@ -19,14 +19,13 @@ import { tierAssignmentEntity } from "../entity.js";
19
19
  // `source: "manual"` markiert den Grant, damit ein späterer Stripe→Tier-Sync ihn
20
20
  // nicht plättet. Upsert: ein Aggregat pro Tenant (deterministische aggregate-id).
21
21
  //
22
- // **Effective-Set-Invalidation (kritisch):** der Executor-Write feuert NICHT
23
- // den `tier-assignment:postSave`-entityHook (Hooks laufen nur im Entity-
24
- // Handler-Pfad, nicht bei direktem executor.create/update). Ohne Cache-Update
25
- // bliebe das Feature-Gate auf dem alten Tier hängen — die Projektion zeigt
26
- // "pro", das Gate verhält sich weiter wie "free", bis der Prozess neu startet.
27
- // Daher ruft der Handler nach erfolgreichem Write `opts.onAssigned(tenantId,
28
- // tier)`; feature.ts verdrahtet das auf denselben Cache-Update wie der Hook
29
- // (storage-only ohne tierMap = no-op).
22
+ // Effective-set invalidation: the executor write does not fire the
23
+ // `tier-assignment:postSave` entity hook, and a per-handler postSave would not
24
+ // fire either because this handler returns no lifecycle result (kind "save").
25
+ // Without an explicit update the feature gate would stay on the old tier, so
26
+ // `opts.onAssigned(tenantId, tier)` runs after commit (ctx.scheduleAfterCommit).
27
+ // feature.ts uses it to update its cache and notify the other processes; in
28
+ // storage-only mode without a tierMap it is a no-op.
30
29
  const tierAssignmentTable = buildEntityTable("tier-assignment", tierAssignmentEntity);
31
30
  const executor = createEventStoreExecutor(tierAssignmentTable, tierAssignmentEntity, {
32
31
  entityName: "tier-assignment",
@@ -50,6 +49,14 @@ export function createSetTenantTierWrite(opts = {}) {
50
49
  reason: SET_TENANT_TIER_REASON,
51
50
  },
52
51
  handler: async (event, ctx) => {
52
+ // Without a commit sink (unit harness) there is no transaction to wait for.
53
+ const afterCommit = (tenantId, tier) => {
54
+ const notify = async () => opts.onAssigned?.(tenantId, tier);
55
+ if (ctx.scheduleAfterCommit)
56
+ ctx.scheduleAfterCommit(notify);
57
+ else
58
+ opts.onAssigned?.(tenantId, tier);
59
+ };
53
60
  const tenantId = event.payload.tenantId; // @cast-boundary engine-bridge
54
61
  const rawDb = ctx.db.unsafeRaw();
55
62
  const tdb = createTenantDb(rawDb, tenantId, "system");
@@ -64,13 +71,13 @@ export function createSetTenantTierWrite(opts = {}) {
64
71
  }, systemUser, tdb);
65
72
  if (!result.isSuccess)
66
73
  return result;
67
- opts.onAssigned?.(tenantId, tier);
74
+ afterCommit(tenantId, tier);
68
75
  return { isSuccess: true, data: { tenantId, tier, isNew: false } };
69
76
  }
70
77
  const result = await executor.create({ id: tierAssignmentAggregateId(tenantId), tier, source: "manual", tenantId }, systemUser, tdb);
71
78
  if (!result.isSuccess)
72
79
  return result;
73
- opts.onAssigned?.(tenantId, tier);
80
+ afterCommit(tenantId, tier);
74
81
  return { isSuccess: true, data: { tenantId, tier, isNew: true } };
75
82
  },
76
83
  });
@@ -0,0 +1,14 @@
1
+ import { type DbRunner } from "@cosmicdrift/kumiko-framework/db";
2
+ import { GLOBAL_ROLE_OPTIONS } from "./constants.js";
3
+ type GlobalRole = (typeof GLOBAL_ROLE_OPTIONS)[number];
4
+ export declare function parseInvitedGlobalRoles(raw: unknown): readonly GlobalRole[];
5
+ export declare function grantInvitedGlobalRoles(db: DbRunner, options: {
6
+ readonly userId: string;
7
+ readonly globalRoles: readonly string[];
8
+ }): Promise<import("@cosmicdrift/kumiko-types/write-error-info-types").WriteFailure | {
9
+ readonly isSuccess: true;
10
+ readonly data: {
11
+ readonly roles: readonly string[];
12
+ };
13
+ }>;
14
+ export {};
@@ -0,0 +1,35 @@
1
+ import { fetchOne } from "@cosmicdrift/kumiko-framework/bun-db";
2
+ import { createEventStoreExecutor, createTenantDb, } from "@cosmicdrift/kumiko-framework/db";
3
+ import { createSystemUser, SYSTEM_TENANT_ID } from "@cosmicdrift/kumiko-framework/engine";
4
+ import { NotFoundError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
5
+ import { parseRoles } from "@cosmicdrift/kumiko-framework/utils";
6
+ import { GLOBAL_ROLE_OPTIONS } from "./constants.js";
7
+ import { userEntity, userTable } from "./schema/user.js";
8
+ const userExecutor = createEventStoreExecutor(userTable, userEntity, { entityName: "user" });
9
+ function isGlobalRole(role) {
10
+ return GLOBAL_ROLE_OPTIONS.some((option) => option === role);
11
+ }
12
+ // Reads the invitation's stored globalRoles column, keeping only assignable
13
+ // global roles — a hand-edited row cannot smuggle "system" into users.roles.
14
+ export function parseInvitedGlobalRoles(raw) {
15
+ return parseRoles(raw).filter(isGlobalRole);
16
+ }
17
+ // Add-only: an accepted invitation never removes a global role the user
18
+ // already holds. Users live on SYSTEM_TENANT_ID (tenancy "global").
19
+ export async function grantInvitedGlobalRoles(db, options) {
20
+ const existing = await fetchOne(db, userTable, { id: options.userId });
21
+ if (!existing)
22
+ return writeFailure(new NotFoundError("user", options.userId));
23
+ const currentRoles = parseRoles(existing.roles);
24
+ const missingRoles = options.globalRoles.filter((role) => !currentRoles.includes(role));
25
+ if (missingRoles.length === 0)
26
+ return grantedGlobalRoles(currentRoles);
27
+ const roles = [...currentRoles, ...missingRoles];
28
+ const updateResult = await userExecutor.update({ id: existing.id, version: existing.version, changes: { roles } }, createSystemUser(SYSTEM_TENANT_ID), createTenantDb(db, SYSTEM_TENANT_ID, "system"));
29
+ if (!updateResult.isSuccess)
30
+ return updateResult;
31
+ return grantedGlobalRoles(roles);
32
+ }
33
+ function grantedGlobalRoles(roles) {
34
+ return { isSuccess: true, data: { roles } };
35
+ }
@@ -308,6 +308,9 @@ export function createUserDataRightsFeature(opts = {}) {
308
308
  method: "GET",
309
309
  path: "/user-export/by-token",
310
310
  anonymous: true,
311
+ // Entry point of the emailed download link; cheap static page, but
312
+ // anonymous and token-bearing, so bound per-IP probing.
313
+ rateLimit: { per: "ip+handler", limit: 30, windowSeconds: 60 },
311
314
  handler: async (c) => {
312
315
  return c.body(TOKEN_EXCHANGE_PAGE_HTML, 200, {
313
316
  "content-type": "text/html; charset=utf-8",
@@ -0,0 +1,33 @@
1
+ export declare const WAITLIST_FEATURE: "waitlist";
2
+ export declare const WAITLIST_STATUS: {
3
+ readonly Pending: "pending";
4
+ readonly Invited: "invited";
5
+ readonly Rejected: "rejected";
6
+ };
7
+ export type WaitlistStatus = (typeof WAITLIST_STATUS)[keyof typeof WAITLIST_STATUS];
8
+ export declare const WaitlistHandlers: {
9
+ readonly submit: "waitlist:write:submit";
10
+ readonly invite: "waitlist:write:invite";
11
+ readonly reject: "waitlist:write:reject";
12
+ };
13
+ export declare const WaitlistQueries: {
14
+ readonly list: "waitlist:query:waitlist-entry:list";
15
+ };
16
+ export declare const WaitlistErrors: {
17
+ readonly notInvitable: "waitlist_not_invitable";
18
+ readonly notRejectable: "waitlist_not_rejectable";
19
+ };
20
+ export declare const WAITLIST_NOTIFICATION_TYPES: {
21
+ readonly confirmation: "waitlist:notify:confirmation";
22
+ readonly adminNotice: "waitlist:notify:admin-notice";
23
+ };
24
+ export declare const DEFAULT_OWN_TENANT_INVITE_ROLE = "TenantAdmin";
25
+ export declare const WAITLIST_FIELD_LIMITS: {
26
+ readonly name: 120;
27
+ readonly email: 254;
28
+ readonly company: 200;
29
+ readonly portfolio: 200;
30
+ readonly message: 2000;
31
+ readonly locale: 35;
32
+ readonly honeypot: 500;
33
+ };
@@ -0,0 +1,35 @@
1
+ // @runtime client
2
+ // Pure string constants, safe for browser code (see auth-email-password/constants.ts).
3
+ export const WAITLIST_FEATURE = "waitlist";
4
+ export const WAITLIST_STATUS = {
5
+ Pending: "pending",
6
+ Invited: "invited",
7
+ Rejected: "rejected",
8
+ };
9
+ export const WaitlistHandlers = {
10
+ submit: "waitlist:write:submit",
11
+ invite: "waitlist:write:invite",
12
+ reject: "waitlist:write:reject",
13
+ };
14
+ // Entity-convention list QN: entityList screens resolve their query by this shape.
15
+ export const WaitlistQueries = {
16
+ list: "waitlist:query:waitlist-entry:list",
17
+ };
18
+ export const WaitlistErrors = {
19
+ notInvitable: "waitlist_not_invitable",
20
+ notRejectable: "waitlist_not_rejectable",
21
+ };
22
+ export const WAITLIST_NOTIFICATION_TYPES = {
23
+ confirmation: "waitlist:notify:confirmation",
24
+ adminNotice: "waitlist:notify:admin-notice",
25
+ };
26
+ export const DEFAULT_OWN_TENANT_INVITE_ROLE = "TenantAdmin";
27
+ export const WAITLIST_FIELD_LIMITS = {
28
+ name: 120,
29
+ email: 254,
30
+ company: 200,
31
+ portfolio: 200,
32
+ message: 2000,
33
+ locale: 35,
34
+ honeypot: 500,
35
+ };