@cosmicdrift/kumiko-bundled-features 0.362.0 → 0.363.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 (307) hide show
  1. package/dist/admin-shell/feature.d.ts +3 -0
  2. package/dist/admin-shell/feature.js +6 -1
  3. package/dist/agent-tools/agent-manifest.js +2 -1
  4. package/dist/agent-tools/filter-ops.d.ts +1 -1
  5. package/dist/agent-tools/filter-ops.js +1 -3
  6. package/dist/agent-tools/tool-dispatch.js +2 -1
  7. package/dist/auth-email-password/email-templates.d.ts +4 -26
  8. package/dist/auth-email-password/email-templates.js +9 -101
  9. package/dist/auth-email-password/feature.js +24 -6
  10. package/dist/auth-email-password/handlers/invite-create.write.js +4 -4
  11. package/dist/auth-email-password/handlers/request-account-unlock.write.js +2 -4
  12. package/dist/auth-email-password/handlers/request-email-verification.write.js +2 -4
  13. package/dist/auth-email-password/handlers/request-password-reset.write.js +2 -4
  14. package/dist/auth-email-password/handlers/signup-confirm.write.js +3 -4
  15. package/dist/auth-email-password/handlers/token-request-handler.d.ts +1 -2
  16. package/dist/auth-email-password/handlers/token-request-handler.js +0 -1
  17. package/dist/auth-email-password/index.d.ts +2 -2
  18. package/dist/auth-email-password/index.js +1 -1
  19. package/dist/auth-email-password/magic-link-mail.d.ts +3 -2
  20. package/dist/auth-email-password/magic-link-mail.js +19 -14
  21. package/dist/auth-email-password/mail-notifications.d.ts +32 -0
  22. package/dist/auth-email-password/mail-notifications.js +116 -0
  23. package/dist/auth-email-password/signup-activation.d.ts +1 -1
  24. package/dist/auth-email-password/signup-activation.js +2 -3
  25. package/dist/auth-foundation/types.js +2 -8
  26. package/dist/auth-mfa/feature.js +4 -4
  27. package/dist/auth-mfa/hmac-token-codec.js +4 -4
  28. package/dist/auth-mfa/mfa-challenge-token.js +3 -3
  29. package/dist/auth-mfa/mfa-preauth-setup-token.js +3 -3
  30. package/dist/auth-mfa/mfa-setup-token.js +8 -8
  31. package/dist/billing-foundation/constants.d.ts +3 -3
  32. package/dist/billing-foundation/constants.js +5 -4
  33. package/dist/billing-foundation/consumer-protection/confirmation-mail.d.ts +32 -14
  34. package/dist/billing-foundation/consumer-protection/confirmation-mail.js +27 -32
  35. package/dist/billing-foundation/consumer-protection/issue-confirmation.js +3 -3
  36. package/dist/billing-foundation/consumer-protection/mail-notifications.d.ts +8 -0
  37. package/dist/billing-foundation/consumer-protection/mail-notifications.js +95 -0
  38. package/dist/billing-foundation/consumer-protection/register-consent.js +2 -0
  39. package/dist/billing-foundation/consumer-protection/termination-mail.d.ts +20 -10
  40. package/dist/billing-foundation/consumer-protection/termination-mail.js +10 -21
  41. package/dist/billing-foundation/consumer-protection/termination-notify.js +2 -2
  42. package/dist/billing-foundation/consumer-protection/termination-pages.js +1 -3
  43. package/dist/billing-foundation/consumer-protection/termination-request.js +2 -2
  44. package/dist/billing-foundation/handlers/process-event.write.js +2 -2
  45. package/dist/billing-foundation/handlers/process-payment-event.write.js +1 -1
  46. package/dist/billing-foundation/subscription-tier-sync.js +3 -4
  47. package/dist/cap-overview/handlers/tenant-caps-list.query.js +2 -1
  48. package/dist/cap-overview/web/cap-usage-cell.js +5 -4
  49. package/dist/channel-email/email-channel.d.ts +2 -2
  50. package/dist/channel-email/email-channel.js +89 -26
  51. package/dist/channel-email/feature.js +3 -3
  52. package/dist/channel-email/index.d.ts +1 -1
  53. package/dist/channel-email/index.js +1 -1
  54. package/dist/channel-email/pii-guard.js +10 -0
  55. package/dist/channel-email/smtp-transport.js +13 -0
  56. package/dist/channel-email/types.d.ts +8 -0
  57. package/dist/config/handlers/set.write.js +1 -2
  58. package/dist/custom-fields/lib/value-schema.js +10 -11
  59. package/dist/custom-fields/wire-user-data-rights.js +7 -6
  60. package/dist/data-retention/index.d.ts +1 -0
  61. package/dist/data-retention/index.js +1 -0
  62. package/dist/data-retention/run-retention-cleanup.js +6 -6
  63. package/dist/delivery/attachment-ref.d.ts +39 -0
  64. package/dist/delivery/attachment-ref.js +64 -0
  65. package/dist/delivery/channel-context.d.ts +8 -4
  66. package/dist/delivery/channel-context.js +76 -2
  67. package/dist/delivery/constants.d.ts +1 -0
  68. package/dist/delivery/constants.js +1 -0
  69. package/dist/delivery/delivery-service.d.ts +4 -1
  70. package/dist/delivery/delivery-service.js +38 -32
  71. package/dist/delivery/feature.js +1 -1
  72. package/dist/delivery/index.d.ts +5 -2
  73. package/dist/delivery/index.js +4 -1
  74. package/dist/delivery/jobs.js +25 -5
  75. package/dist/delivery/notify-factory.js +4 -1
  76. package/dist/delivery/testing.d.ts +4 -1
  77. package/dist/delivery/types.d.ts +20 -14
  78. package/dist/delivery/unsubscribe.js +4 -1
  79. package/dist/derivatives-sharp/render.js +1 -18
  80. package/dist/document-ingest-foundation/pages.js +4 -4
  81. package/dist/feature-toggles/toggle-sync-signal.js +4 -4
  82. package/dist/file-foundation/index.d.ts +1 -0
  83. package/dist/file-foundation/index.js +1 -0
  84. package/dist/file-foundation/read-file-ref-content.d.ts +27 -0
  85. package/dist/file-foundation/read-file-ref-content.js +49 -0
  86. package/dist/folders/web/folder-manager.js +3 -2
  87. package/dist/form-draft/db/queries/cleanup.js +4 -4
  88. package/dist/form-draft/db/queries/draft-count.js +2 -2
  89. package/dist/form-draft/db/queries/owned-file-refs.js +2 -2
  90. package/dist/form-draft/release-file-refs.js +3 -2
  91. package/dist/inbound-mail-foundation/handlers/connect-account.write.js +1 -1
  92. package/dist/inbound-mail-foundation/handlers/ingest-message.write.js +6 -6
  93. package/dist/inbound-mail-foundation/handlers/scope-visibility.d.ts +1 -1
  94. package/dist/inbound-mail-foundation/oauth-state.js +7 -7
  95. package/dist/inbound-mail-foundation/types.js +4 -4
  96. package/dist/inbound-mail-foundation/watch-supervisor.js +22 -13
  97. package/dist/inbound-provider-imap/imap-client.js +4 -3
  98. package/dist/ledger/recurring.js +1 -3
  99. package/dist/managed-pages/branding.d.ts +2 -0
  100. package/dist/managed-pages/branding.js +24 -2
  101. package/dist/managed-pages/feature.js +1 -0
  102. package/dist/managed-pages/i18n.js +1 -0
  103. package/dist/managed-pages/screens/branding-screen.js +8 -1
  104. package/dist/page-render/branding.d.ts +1 -0
  105. package/dist/page-render/branding.js +1 -0
  106. package/dist/page-render/markdown.js +1 -1
  107. package/dist/personal-access-tokens/feature.js +1 -1
  108. package/dist/personal-access-tokens/handlers/list.query.js +2 -1
  109. package/dist/personal-access-tokens/revoke-for-user.js +2 -1
  110. package/dist/renderer-foundation/api.d.ts +13 -13
  111. package/dist/renderer-foundation/api.js +1 -1
  112. package/dist/renderer-foundation/constants.js +2 -4
  113. package/dist/renderer-foundation/feature.js +7 -7
  114. package/dist/renderer-foundation/index.d.ts +2 -1
  115. package/dist/renderer-foundation/index.js +1 -0
  116. package/dist/renderer-foundation/types.d.ts +92 -32
  117. package/dist/renderer-pdf/feature.js +7 -1
  118. package/dist/sessions/db/queries/cleanup.js +2 -2
  119. package/dist/sessions/feature.js +1 -1
  120. package/dist/sessions/handlers/list.query.js +2 -1
  121. package/dist/shared/attachment-file-name.d.ts +3 -0
  122. package/dist/shared/attachment-file-name.js +35 -0
  123. package/dist/shared/index.d.ts +3 -0
  124. package/dist/shared/index.js +3 -0
  125. package/dist/shared/is-tenant-db.js +1 -1
  126. package/dist/shared/mail-template-body.d.ts +4 -0
  127. package/dist/shared/mail-template-body.js +17 -0
  128. package/dist/shared/sharp-input-limits.d.ts +6 -0
  129. package/dist/shared/sharp-input-limits.js +22 -0
  130. package/dist/shared/signup-handover.js +2 -2
  131. package/dist/tags/handlers/assign-tag.write.js +3 -1
  132. package/dist/tags/web/tag-chip.js +2 -0
  133. package/dist/template-resolver/api.d.ts +30 -2
  134. package/dist/template-resolver/api.js +40 -5
  135. package/dist/template-resolver/compile-document-body.d.ts +8 -0
  136. package/dist/template-resolver/compile-document-body.js +163 -0
  137. package/dist/template-resolver/constants.d.ts +13 -4
  138. package/dist/template-resolver/constants.js +30 -10
  139. package/dist/template-resolver/document-sanitizer.d.ts +32 -0
  140. package/dist/template-resolver/document-sanitizer.js +201 -0
  141. package/dist/template-resolver/document-schema.d.ts +21 -0
  142. package/dist/template-resolver/document-schema.js +296 -0
  143. package/dist/template-resolver/feature.d.ts +18 -0
  144. package/dist/template-resolver/feature.js +50 -6
  145. package/dist/template-resolver/handlers/bundle-import.d.ts +20 -0
  146. package/dist/template-resolver/handlers/bundle-import.js +147 -0
  147. package/dist/template-resolver/handlers/collection-set.write.js +25 -2
  148. package/dist/template-resolver/handlers/copy-locale.write.d.ts +19 -0
  149. package/dist/template-resolver/handlers/copy-locale.write.js +116 -0
  150. package/dist/template-resolver/handlers/diff-to-system.query.d.ts +22 -0
  151. package/dist/template-resolver/handlers/diff-to-system.query.js +64 -0
  152. package/dist/template-resolver/handlers/export-bundle.query.d.ts +35 -0
  153. package/dist/template-resolver/handlers/export-bundle.query.js +68 -0
  154. package/dist/template-resolver/handlers/find-by-id.query.d.ts +1 -0
  155. package/dist/template-resolver/handlers/find-by-id.query.js +3 -3
  156. package/dist/template-resolver/handlers/import-bundle-system.write.d.ts +39 -0
  157. package/dist/template-resolver/handlers/import-bundle-system.write.js +36 -0
  158. package/dist/template-resolver/handlers/import-bundle.write.d.ts +40 -0
  159. package/dist/template-resolver/handlers/import-bundle.write.js +19 -0
  160. package/dist/template-resolver/handlers/layout-source.query.d.ts +26 -0
  161. package/dist/template-resolver/handlers/layout-source.query.js +64 -0
  162. package/dist/template-resolver/handlers/list.query.d.ts +4 -1
  163. package/dist/template-resolver/handlers/list.query.js +5 -7
  164. package/dist/template-resolver/handlers/overview.query.d.ts +59 -0
  165. package/dist/template-resolver/handlers/overview.query.js +165 -0
  166. package/dist/template-resolver/handlers/preview.query.d.ts +12 -0
  167. package/dist/template-resolver/handlers/preview.query.js +24 -0
  168. package/dist/template-resolver/handlers/reset-to-system.write.d.ts +8 -0
  169. package/dist/template-resolver/handlers/reset-to-system.write.js +50 -0
  170. package/dist/template-resolver/handlers/resource-link-pdf.write.d.ts +8 -0
  171. package/dist/template-resolver/handlers/resource-link-pdf.write.js +32 -0
  172. package/dist/template-resolver/handlers/resource-remove.write.d.ts +9 -0
  173. package/dist/template-resolver/handlers/resource-remove.write.js +44 -0
  174. package/dist/template-resolver/handlers/resource-shared.d.ts +9 -0
  175. package/dist/template-resolver/handlers/resource-shared.js +14 -0
  176. package/dist/template-resolver/handlers/resource-upload.write.d.ts +13 -0
  177. package/dist/template-resolver/handlers/resource-upload.write.js +98 -0
  178. package/dist/template-resolver/handlers/send-test.write.d.ts +11 -0
  179. package/dist/template-resolver/handlers/send-test.write.js +81 -0
  180. package/dist/template-resolver/handlers/set.write.d.ts +4 -1
  181. package/dist/template-resolver/handlers/set.write.js +25 -3
  182. package/dist/template-resolver/handlers/shared.d.ts +9 -5
  183. package/dist/template-resolver/handlers/shared.js +6 -2
  184. package/dist/template-resolver/handlers/template-gates.d.ts +36 -0
  185. package/dist/template-resolver/handlers/template-gates.js +131 -0
  186. package/dist/template-resolver/handlers/template-write.d.ts +45 -0
  187. package/dist/template-resolver/handlers/template-write.js +109 -0
  188. package/dist/template-resolver/handlers/tenant-overrides.query.d.ts +17 -0
  189. package/dist/template-resolver/handlers/tenant-overrides.query.js +48 -0
  190. package/dist/template-resolver/handlers/toggle-status.write.d.ts +12 -0
  191. package/dist/template-resolver/handlers/toggle-status.write.js +27 -11
  192. package/dist/template-resolver/handlers/upsert-system.write.d.ts +5 -8
  193. package/dist/template-resolver/handlers/upsert-system.write.js +14 -59
  194. package/dist/template-resolver/handlers/upsert-tenant.write.d.ts +5 -8
  195. package/dist/template-resolver/handlers/upsert-tenant.write.js +10 -61
  196. package/dist/template-resolver/handlers/usage.query.d.ts +17 -0
  197. package/dist/template-resolver/handlers/usage.query.js +15 -0
  198. package/dist/template-resolver/handlers/validate.query.d.ts +74 -0
  199. package/dist/template-resolver/handlers/validate.query.js +80 -0
  200. package/dist/template-resolver/index.d.ts +18 -1
  201. package/dist/template-resolver/index.js +16 -1
  202. package/dist/template-resolver/layout-regions.d.ts +23 -0
  203. package/dist/template-resolver/layout-regions.js +125 -0
  204. package/dist/template-resolver/linked-resources.d.ts +17 -0
  205. package/dist/template-resolver/linked-resources.js +75 -0
  206. package/dist/template-resolver/preview-display-html.d.ts +7 -0
  207. package/dist/template-resolver/preview-display-html.js +27 -0
  208. package/dist/template-resolver/print-document.d.ts +1 -0
  209. package/dist/template-resolver/print-document.js +55 -0
  210. package/dist/template-resolver/qualified-names.d.ts +30 -0
  211. package/dist/template-resolver/qualified-names.js +30 -0
  212. package/dist/template-resolver/record-mapping.d.ts +2 -0
  213. package/dist/template-resolver/record-mapping.js +21 -0
  214. package/dist/template-resolver/resource-image.d.ts +15 -0
  215. package/dist/template-resolver/resource-image.js +34 -0
  216. package/dist/template-resolver/seeding.d.ts +14 -1
  217. package/dist/template-resolver/seeding.js +105 -15
  218. package/dist/template-resolver/store-template-resource.d.ts +64 -0
  219. package/dist/template-resolver/store-template-resource.js +264 -0
  220. package/dist/template-resolver/table.d.ts +47 -8
  221. package/dist/template-resolver/table.js +11 -0
  222. package/dist/template-resolver/template-bundle.d.ts +36 -0
  223. package/dist/template-resolver/template-bundle.js +60 -0
  224. package/dist/template-resolver/template-diff.d.ts +7 -0
  225. package/dist/template-resolver/template-diff.js +68 -0
  226. package/dist/template-resolver/template-meta.d.ts +60 -0
  227. package/dist/template-resolver/template-meta.js +101 -0
  228. package/dist/template-resolver/template-preview.d.ts +86 -0
  229. package/dist/template-resolver/template-preview.js +374 -0
  230. package/dist/template-resolver/template-resources.d.ts +14 -0
  231. package/dist/template-resolver/template-resources.js +42 -0
  232. package/dist/template-resolver/template-usage.d.ts +12 -0
  233. package/dist/template-resolver/template-usage.js +30 -0
  234. package/dist/template-resolver/template-validation.d.ts +22 -0
  235. package/dist/template-resolver/template-validation.js +172 -0
  236. package/dist/template-resolver/testing.js +5 -5
  237. package/dist/template-resolver/user-content-table.d.ts +14 -8
  238. package/dist/template-resolver/web/client-plugin.d.ts +1 -0
  239. package/dist/template-resolver/web/client-plugin.js +9 -6
  240. package/dist/template-resolver/web/document-editor-extensions.d.ts +15 -0
  241. package/dist/template-resolver/web/document-editor-extensions.js +107 -0
  242. package/dist/template-resolver/web/document-editor.d.ts +3 -0
  243. package/dist/template-resolver/web/document-editor.js +11 -0
  244. package/dist/template-resolver/web/document-tiptap-editor.d.ts +3 -0
  245. package/dist/template-resolver/web/document-tiptap-editor.js +376 -0
  246. package/dist/template-resolver/web/document-toolbar-blocks.d.ts +33 -0
  247. package/dist/template-resolver/web/document-toolbar-blocks.js +15 -0
  248. package/dist/template-resolver/web/editor-handle.d.ts +3 -0
  249. package/dist/template-resolver/web/editor-handle.js +24 -0
  250. package/dist/template-resolver/web/i18n.js +76 -0
  251. package/dist/template-resolver/web/index.d.ts +4 -0
  252. package/dist/template-resolver/web/index.js +4 -0
  253. package/dist/template-resolver/web/placeholder-line-editor-lazy.d.ts +4 -0
  254. package/dist/template-resolver/web/placeholder-line-editor-lazy.js +16 -0
  255. package/dist/template-resolver/web/placeholder-line-editor.d.ts +15 -0
  256. package/dist/template-resolver/web/placeholder-line-editor.js +94 -0
  257. package/dist/template-resolver/web/placeholder-text.d.ts +4 -0
  258. package/dist/template-resolver/web/placeholder-text.js +34 -0
  259. package/dist/template-resolver/web/use-stable-by-key.d.ts +1 -0
  260. package/dist/template-resolver/web/use-stable-by-key.js +13 -0
  261. package/dist/tenant/handlers/team-list.query.js +3 -2
  262. package/dist/tenant/seeding.js +31 -6
  263. package/dist/tenant-lifecycle/handlers/cancel-destruction.write.d.ts +1 -1
  264. package/dist/tenant-lifecycle/handlers/cancel-destruction.write.js +2 -1
  265. package/dist/user/db/queries/stream-tenant-backfill.js +4 -4
  266. package/dist/user/handlers/detail.query.js +2 -1
  267. package/dist/user-data-rights/db/queries/forget-cleanup.d.ts +2 -2
  268. package/dist/user-data-rights/email-templates.d.ts +1 -29
  269. package/dist/user-data-rights/email-templates.js +5 -106
  270. package/dist/user-data-rights/feature.js +53 -35
  271. package/dist/user-data-rights/handlers/cancel-deletion.write.d.ts +1 -1
  272. package/dist/user-data-rights/handlers/cancel-deletion.write.js +2 -1
  273. package/dist/user-data-rights/handlers/request-deletion.write.js +5 -7
  274. package/dist/user-data-rights/lib/default-mailers.d.ts +15 -7
  275. package/dist/user-data-rights/lib/default-mailers.js +100 -48
  276. package/dist/user-data-rights/lib/update-user-lifecycle.js +1 -1
  277. package/dist/user-data-rights/mail-notifications.d.ts +13 -0
  278. package/dist/user-data-rights/mail-notifications.js +118 -0
  279. package/dist/user-data-rights/web/privacy-center-screen.js +2 -1
  280. package/dist/user-profile/web/profile-screen.js +2 -1
  281. package/dist/waitlist/feature.js +10 -0
  282. package/dist/waitlist/handlers/submit.write.js +3 -4
  283. package/dist/waitlist/mail.d.ts +19 -6
  284. package/dist/waitlist/mail.js +50 -14
  285. package/package.json +28 -17
  286. package/src/admin-shell/changes.json +6 -0
  287. package/src/auth-email-password/changes.json +14 -0
  288. package/src/billing-foundation/changes.json +7 -0
  289. package/src/channel-email/changes.json +19 -0
  290. package/src/data-retention/changes.json +6 -0
  291. package/src/delivery/changes.json +48 -0
  292. package/src/managed-pages/changes.json +6 -0
  293. package/src/renderer-foundation/changes.json +39 -0
  294. package/src/template-resolver/changes.json +118 -0
  295. package/src/user-data-rights/changes.json +7 -0
  296. package/src/waitlist/changes.json +7 -0
  297. package/dist/renderer-simple/feature.d.ts +0 -10
  298. package/dist/renderer-simple/feature.js +0 -42
  299. package/dist/renderer-simple/index.d.ts +0 -3
  300. package/dist/renderer-simple/index.js +0 -2
  301. package/dist/renderer-simple/mail-blocks.d.ts +0 -21
  302. package/dist/renderer-simple/mail-blocks.js +0 -107
  303. package/dist/renderer-simple/resolve-variables.d.ts +0 -7
  304. package/dist/renderer-simple/resolve-variables.js +0 -47
  305. package/dist/renderer-simple/simple-renderer.d.ts +0 -24
  306. package/dist/renderer-simple/simple-renderer.js +0 -200
  307. package/src/renderer-simple/changes.json +0 -27
@@ -0,0 +1,9 @@
1
+ import type { TenantId } from "@cosmicdrift/kumiko-framework/engine";
2
+ import * as z from "zod";
3
+ export declare const RESOURCE_MAX_BYTES_CONFIG_KEY = "template-resolver:config:resource-max-bytes";
4
+ export declare const DEFAULT_RESOURCE_MAX_BYTES: number;
5
+ export declare const resourceKeySchema: z.ZodString;
6
+ export declare const RESOURCE_PURPOSES: readonly ["image", "attachment"];
7
+ export type ResourcePurpose = (typeof RESOURCE_PURPOSES)[number];
8
+ export declare const MAX_FILE_ATTACHMENTS_PER_TEMPLATE = 10;
9
+ export declare function resourceTargetTenantId(tenantIdOverride: string | undefined, sessionTenantId: TenantId): TenantId;
@@ -0,0 +1,14 @@
1
+ import * as z from "zod";
2
+ import { isDocumentResourceKey } from "../document-schema.js";
3
+ export const RESOURCE_MAX_BYTES_CONFIG_KEY = "template-resolver:config:resource-max-bytes";
4
+ export const DEFAULT_RESOURCE_MAX_BYTES = 2 * 1024 * 1024;
5
+ export const resourceKeySchema = z.string().min(1).max(64).refine(isDocumentResourceKey, {
6
+ message: "resource key must be letters, digits, dot, dash or underscore",
7
+ });
8
+ export const RESOURCE_PURPOSES = ["image", "attachment"];
9
+ export const MAX_FILE_ATTACHMENTS_PER_TEMPLATE = 10;
10
+ export function resourceTargetTenantId(tenantIdOverride, sessionTenantId) {
11
+ // @cast-boundary engine-payload — override is a zod-validated string; forTenant
12
+ // rejects it for non-SystemAdmins.
13
+ return (tenantIdOverride ?? sessionTenantId);
14
+ }
@@ -0,0 +1,13 @@
1
+ import * as z from "zod";
2
+ import { type StoredTemplateAttachment, type StoredTemplateResource } from "../store-template-resource.js";
3
+ export declare const resourceUploadWrite: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"resource-upload", z.ZodObject<{
4
+ templateId: z.ZodOptional<z.ZodString>;
5
+ key: z.ZodOptional<z.ZodString>;
6
+ sourceFileRefId: z.ZodUUID;
7
+ tenantIdOverride: z.ZodOptional<z.ZodString>;
8
+ purpose: z.ZodDefault<z.ZodEnum<{
9
+ attachment: "attachment";
10
+ image: "image";
11
+ }>>;
12
+ fileName: z.ZodOptional<z.ZodString>;
13
+ }, z.core.$strip>, StoredTemplateAttachment | StoredTemplateResource, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap>;
@@ -0,0 +1,98 @@
1
+ import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
2
+ import { NotFoundError, UnprocessableError, writeFailure, } from "@cosmicdrift/kumiko-framework/errors";
3
+ import * as z from "zod";
4
+ import { FileRefTooLargeError, fileContextForTenant, readFileRefContent, } from "../../file-foundation/index.js";
5
+ import { attachmentFileNameSchema } from "../../shared/index.js";
6
+ import { TemplateResolverErrors } from "../qualified-names.js";
7
+ import { readTemplateResourceMaxBytes, storeTemplateAttachment, storeTemplateResource, } from "../store-template-resource.js";
8
+ import { RESOURCE_PURPOSES, resourceKeySchema, resourceTargetTenantId } from "./resource-shared.js";
9
+ function attachmentTargetOf(input) {
10
+ const { templateId, key, fileName } = input;
11
+ if (templateId === undefined || key === undefined || fileName === undefined)
12
+ return null;
13
+ const parsedName = attachmentFileNameSchema.safeParse(fileName);
14
+ return parsedName.success ? { templateId, key, fileName: parsedName.data } : null;
15
+ }
16
+ // Purpose "image": stores a re-encoded image as a FileRef and, with templateId + key, links it into
17
+ // the template; without templateId it only creates the FileRef (the branding logo upload path).
18
+ // Purpose "attachment": stores the file as uploaded and links it as a mail attachment.
19
+ export const resourceUploadWrite = defineWriteHandler({
20
+ name: "resource-upload",
21
+ schema: z
22
+ .object({
23
+ templateId: z.string().min(1).optional(),
24
+ key: resourceKeySchema.optional(),
25
+ /** An unclaimed upload of the caller (POST /api/files without entityType). */
26
+ sourceFileRefId: z.uuid(),
27
+ /** Cross-tenant target — SystemAdmin only. */
28
+ tenantIdOverride: z.string().min(1).optional(),
29
+ /** "attachment" links a PDF/PNG/JPEG as a file attached to every mail of the template. */
30
+ purpose: z.enum(RESOURCE_PURPOSES).default("image"),
31
+ /** Name the recipient sees; required for purpose "attachment". */
32
+ fileName: z.string().optional(),
33
+ })
34
+ .refine((p) => p.purpose === "attachment" || (p.templateId === undefined) === (p.key === undefined), {
35
+ message: "templateId and key must be given together",
36
+ path: ["key"],
37
+ }),
38
+ access: { roles: ["TenantAdmin", "SystemAdmin"] },
39
+ description: "Turns a file uploaded via POST /api/files (multipart, no entityType) into a template resource. With purpose image (default) the upload is checked against the resource-max-bytes cap, re-encoded to a supported image format and stored as a new file; with templateId and key it is linked into that template under the key, without them only the file is created. With purpose attachment, templateId, key and fileName are required: a PDF, PNG or JPEG is stored unchanged and linked into a mail template as a file attached to every mail sent from it, at most 10 per template. A cap above the files route's maxUploadSize has no effect.",
40
+ handler: async (event, ctx) => {
41
+ const { templateId, key, sourceFileRefId, purpose, fileName } = event.payload;
42
+ const attachmentTarget = purpose === "attachment" ? attachmentTargetOf({ templateId, key, fileName }) : null;
43
+ if (purpose === "attachment" && attachmentTarget === null) {
44
+ return writeFailure(new UnprocessableError(TemplateResolverErrors.attachmentInvalid, {
45
+ i18nKey: "templateResolver.errors.attachment.invalid",
46
+ details: { code: "target_required" },
47
+ }));
48
+ }
49
+ const maxBytes = await readTemplateResourceMaxBytes(ctx.config, ctx.registry);
50
+ // The source is read from the session tenant only: with tenantIdOverride the upload
51
+ // sits in the admin's own tenant while the normalized file lands in the target tenant.
52
+ // Only the caller's own unclaimed upload qualifies, so a record-attached image of
53
+ // someone else cannot be copied into a template.
54
+ let source;
55
+ try {
56
+ source = await readFileRefContent({
57
+ db: ctx.db,
58
+ filesForTenant: (tid) => fileContextForTenant(ctx, tid),
59
+ fileRefId: sourceFileRefId,
60
+ allowedTenantIds: [event.user.tenantId],
61
+ unclaimedUploadOf: event.user.id,
62
+ maxBytes,
63
+ });
64
+ }
65
+ catch (cause) {
66
+ if (cause instanceof FileRefTooLargeError) {
67
+ return writeFailure(new UnprocessableError(TemplateResolverErrors.resourceTooLarge, {
68
+ i18nKey: "templateResolver.errors.resource.tooLarge",
69
+ details: { maxBytes, size: cause.size },
70
+ }));
71
+ }
72
+ throw cause;
73
+ }
74
+ if (source === null)
75
+ return writeFailure(new NotFoundError("file-ref", sourceFileRefId));
76
+ // The source upload stays as an unclaimed file: bundled-features cannot reach the fileRef executor to delete it.
77
+ const tenantId = resourceTargetTenantId(event.payload.tenantIdOverride, event.user.tenantId);
78
+ if (attachmentTarget !== null) {
79
+ return storeTemplateAttachment({
80
+ ctx,
81
+ user: event.user,
82
+ tenantId,
83
+ bytes: source.content,
84
+ maxBytes,
85
+ ...attachmentTarget,
86
+ });
87
+ }
88
+ return storeTemplateResource({
89
+ ctx,
90
+ user: event.user,
91
+ tenantId,
92
+ bytes: source.content,
93
+ maxBytes,
94
+ ...(templateId !== undefined && { templateId }),
95
+ ...(key !== undefined && { key }),
96
+ });
97
+ },
98
+ });
@@ -0,0 +1,11 @@
1
+ import * as z from "zod";
2
+ export declare const sendTestWrite: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"send-test", z.ZodObject<{
3
+ slug: z.ZodString;
4
+ locale: z.ZodString;
5
+ content: z.ZodOptional<z.ZodString>;
6
+ meta: z.ZodOptional<z.ZodUnknown>;
7
+ variableSchema: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
8
+ }, z.core.$strip>, {
9
+ subject: string;
10
+ checks: readonly import("../template-preview.js").PreviewCheck[];
11
+ }, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap>;
@@ -0,0 +1,81 @@
1
+ import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
2
+ import { UnprocessableError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
3
+ import * as z from "zod";
4
+ import { collectChannels, createEmailAttachmentReader, EMAIL_CHANNEL_NAME, } from "../../delivery/index.js";
5
+ import { fileContextForTenant } from "../../file-foundation/index.js";
6
+ import { TEST_MAIL_NOTIFICATION_TYPE, TEST_MAIL_SUBJECT_PREFIX } from "../constants.js";
7
+ import { TemplateResolverErrors } from "../qualified-names.js";
8
+ import { renderTemplatePreview } from "../template-preview.js";
9
+ import { localeSchema, templateSlugSchema } from "./shared.js";
10
+ const TEST_MAIL_RATE_LIMIT = { limit: 10, windowSeconds: 3600 };
11
+ function emailChannelOf(registry) {
12
+ return collectChannels(registry).find((channel) => channel.name === EMAIL_CHANNEL_NAME);
13
+ }
14
+ // Transport errors can carry provider responses or the address, so the client only gets a fixed code.
15
+ function testMailFailed(code) {
16
+ return writeFailure(new UnprocessableError(TemplateResolverErrors.testMailFailed, {
17
+ i18nKey: "templateResolver.errors.testMail.failed",
18
+ details: { code },
19
+ }));
20
+ }
21
+ // The only recipient is the caller's own address, resolved server-side, so the handler can never mail a third party.
22
+ export const sendTestWrite = defineWriteHandler({
23
+ name: "send-test",
24
+ schema: z.object({
25
+ slug: templateSlugSchema,
26
+ locale: localeSchema,
27
+ content: z.string().max(200_000).optional(),
28
+ meta: z.unknown().optional(),
29
+ variableSchema: z.record(z.string(), z.unknown()).default({}),
30
+ }),
31
+ access: { roles: ["TenantAdmin", "SystemAdmin"] },
32
+ rateLimit: { per: "user+handler", ...TEST_MAIL_RATE_LIMIT },
33
+ description: "Sends the rendered preview of a mail template draft, or of the stored template when no content is given, to the calling user's own address with the subject prefixed by [TEST], carrying the inline images and the files attached to the template; there is no recipient input, the sample values come from the declared variable examples, and a draft that fails the write rules is rejected without sending.",
34
+ handler: async (event, ctx) => {
35
+ const outcome = await renderTemplatePreview(ctx, event.user, {
36
+ ...event.payload,
37
+ kind: "mail",
38
+ });
39
+ if (!outcome.ok)
40
+ return writeFailure(outcome.error);
41
+ const { preview, mailAttachments } = outcome;
42
+ const channel = emailChannelOf(ctx.registry);
43
+ if (channel === undefined) {
44
+ return writeFailure(new UnprocessableError(TemplateResolverErrors.testMailNoChannel, {
45
+ i18nKey: "templateResolver.errors.testMail.noChannel",
46
+ }));
47
+ }
48
+ const channelContext = {
49
+ db: ctx.db,
50
+ registry: ctx.registry,
51
+ sseBroker: undefined,
52
+ tenantId: event.user.tenantId,
53
+ secrets: ctx.secrets,
54
+ readAttachment: createEmailAttachmentReader(ctx.db, ctx.registry, event.user.tenantId, ctx.config, (tenantId) => fileContextForTenant(ctx, tenantId)),
55
+ };
56
+ const address = (await channel.resolve?.(event.user.id, channelContext)) ?? null;
57
+ if (address === null) {
58
+ return writeFailure(new UnprocessableError(TemplateResolverErrors.testMailNoAddress, {
59
+ i18nKey: "templateResolver.errors.testMail.noAddress",
60
+ }));
61
+ }
62
+ const subject = `${TEST_MAIL_SUBJECT_PREFIX}${preview.subject ?? ""}`;
63
+ let sent;
64
+ try {
65
+ const result = await channel.send(address, {
66
+ notificationType: TEST_MAIL_NOTIFICATION_TYPE,
67
+ title: subject,
68
+ body: undefined,
69
+ data: undefined,
70
+ locale: event.payload.locale,
71
+ }, channelContext, { subject, html: preview.html, text: preview.text ?? "", attachments: mailAttachments });
72
+ sent = result.status === "sent";
73
+ }
74
+ catch {
75
+ return testMailFailed("channel_error");
76
+ }
77
+ if (!sent)
78
+ return testMailFailed("not_sent");
79
+ return { isSuccess: true, data: { subject, checks: preview.checks } };
80
+ },
81
+ });
@@ -6,8 +6,11 @@ export declare const setWrite: import("@cosmicdrift/kumiko-types/define-handler"
6
6
  "ai-prompt": "ai-prompt";
7
7
  "document-pdf": "document-pdf";
8
8
  "image-snapshot": "image-snapshot";
9
- "mail-html": "mail-html";
9
+ mail: "mail";
10
+ "mail-layout": "mail-layout";
10
11
  notification: "notification";
12
+ "pdf-layout": "pdf-layout";
13
+ snippet: "snippet";
11
14
  "text-block": "text-block";
12
15
  }>>;
13
16
  title: z.ZodString;
@@ -1,10 +1,14 @@
1
1
  import { fetchOne } from "@cosmicdrift/kumiko-framework/bun-db";
2
2
  import { defineWriteHandler, SYSTEM_TENANT_ID, } from "@cosmicdrift/kumiko-framework/engine";
3
+ import { isOneOf } from "@cosmicdrift/kumiko-types/type-guards";
3
4
  import * as z from "zod";
4
5
  import { requireForTenant } from "../../shared/index.js";
5
- import { TEMPLATE_KINDS, TEXT_BLOCK_KIND } from "../constants.js";
6
+ import { TEMPLATE_KINDS, TEXT_BLOCK_KIND, UPSERT_KINDS } from "../constants.js";
6
7
  import { templateResourcesTable } from "../table.js";
8
+ import { resolveTemplateVariables } from "../template-validation.js";
7
9
  import { contentFormatSchema, executor, folderSchema, localeSchema, slugSchema } from "./shared.js";
10
+ import { parseStoredJson, storedRecord } from "./template-gates.js";
11
+ import { gateTemplateWrite } from "./template-write.js";
8
12
  // Upsert of a single resource, one operation per (tenantId, slug, kind,
9
13
  // locale). This is the content-tree authoring path: unlike upsertTenant it
10
14
  // takes a title and a folder, has no draft stage (saving publishes) and it may
@@ -21,6 +25,9 @@ import { contentFormatSchema, executor, folderSchema, localeSchema, slugSchema }
21
25
  // upsertTenant → publish draft stage. That is the deliberate split between
22
26
  // the two authoring routes — use upsertSystem/upsertTenant when a template
23
27
  // needs a draft or a variable schema.
28
+ //
29
+ // Kinds that upsertSystem/upsertTenant also write run the same validation
30
+ // pipeline here, and because saving publishes, publish issues reject too.
24
31
  export const setWrite = defineWriteHandler({
25
32
  name: "set",
26
33
  schema: z.object({
@@ -55,17 +62,32 @@ export const setWrite = defineWriteHandler({
55
62
  kind,
56
63
  locale: event.payload.locale,
57
64
  });
65
+ const scope = tenantId === SYSTEM_TENANT_ID ? "system" : "tenant";
66
+ let content = event.payload.content;
67
+ if (isOneOf(UPSERT_KINDS, kind)) {
68
+ const gated = gateTemplateWrite({
69
+ kind,
70
+ scope,
71
+ contentFormat: event.payload.contentFormat,
72
+ content,
73
+ rawMeta: parseStoredJson(existing?.meta ?? null),
74
+ variables: resolveTemplateVariables(ctx.registry, event.payload.slug, storedRecord(existing?.variableSchema ?? null)),
75
+ }, "active");
76
+ if (!gated.ok)
77
+ return gated.failure;
78
+ content = gated.evaluated.content;
79
+ }
58
80
  const fields = {
59
81
  slug: event.payload.slug,
60
82
  kind,
61
83
  locale: event.payload.locale,
62
84
  title: event.payload.title,
63
- content: event.payload.content,
85
+ content,
64
86
  contentFormat: event.payload.contentFormat,
65
87
  folder: event.payload.folder ?? null,
66
88
  variableSchema: "{}",
67
89
  linkedResources: "{}",
68
- scope: tenantId === SYSTEM_TENANT_ID ? "system" : "tenant",
90
+ scope,
69
91
  parentTemplateId: null,
70
92
  // Text blocks have no draft stage — saving publishes, so resolveTemplate
71
93
  // (which only returns active rows) finds them.
@@ -1,13 +1,16 @@
1
1
  import * as z from "zod";
2
+ import { templateSlugSchema } from "../template-meta.js";
2
3
  export declare const executor: import("@cosmicdrift/kumiko-types/event-store-executor-types").EventStoreExecutor;
3
4
  export declare const slugSchema: z.ZodString;
5
+ export { templateSlugSchema };
4
6
  export declare const localeSchema: z.ZodString;
5
7
  export declare const kindSchema: z.ZodEnum<{
6
8
  "ai-prompt": "ai-prompt";
7
9
  "document-pdf": "document-pdf";
8
10
  "image-snapshot": "image-snapshot";
9
- "mail-html": "mail-html";
10
- notification: "notification";
11
+ mail: "mail";
12
+ "mail-layout": "mail-layout";
13
+ "pdf-layout": "pdf-layout";
11
14
  }>;
12
15
  export declare const folderSchema: z.ZodString;
13
16
  export declare const titleSchema: z.ZodString;
@@ -28,8 +31,9 @@ export declare const upsertPayloadSchema: z.ZodObject<{
28
31
  "ai-prompt": "ai-prompt";
29
32
  "document-pdf": "document-pdf";
30
33
  "image-snapshot": "image-snapshot";
31
- "mail-html": "mail-html";
32
- notification: "notification";
34
+ mail: "mail";
35
+ "mail-layout": "mail-layout";
36
+ "pdf-layout": "pdf-layout";
33
37
  }>;
34
38
  locale: z.ZodString;
35
39
  content: z.ZodString;
@@ -40,7 +44,7 @@ export declare const upsertPayloadSchema: z.ZodObject<{
40
44
  plain: "plain";
41
45
  }>;
42
46
  variableSchema: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
43
- linkedResources: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
47
+ meta: z.ZodOptional<z.ZodUnknown>;
44
48
  parentTemplateId: z.ZodOptional<z.ZodString>;
45
49
  title: z.ZodOptional<z.ZodNullable<z.ZodString>>;
46
50
  folder: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -2,6 +2,7 @@ import { createEventStoreExecutor } from "@cosmicdrift/kumiko-framework/db";
2
2
  import * as z from "zod";
3
3
  import { CONTENT_FORMATS, TEMPLATE_STATUSES, UPSERT_KINDS } from "../constants.js";
4
4
  import { templateResourceEntity, templateResourcesTable } from "../table.js";
5
+ import { templateSlugSchema } from "../template-meta.js";
5
6
  // One executor per bundle, shared by all four handlers for create/update via
6
7
  // event store + optimistic lock.
7
8
  export const executor = createEventStoreExecutor(templateResourcesTable, templateResourceEntity, {
@@ -13,6 +14,7 @@ export const slugSchema = z
13
14
  .min(1)
14
15
  .max(80)
15
16
  .regex(/^[a-z0-9][a-z0-9-]*$/, "slug must be kebab-case (lowercase, digits, dashes)");
17
+ export { templateSlugSchema };
16
18
  export const localeSchema = z
17
19
  .string()
18
20
  .min(2)
@@ -31,13 +33,15 @@ export const statusSchema = z.enum(TEMPLATE_STATUSES);
31
33
  // Common Upsert-Payload — geteilt zwischen upsertSystem + upsertTenant.
32
34
  // Unterschied: ACL + tenantId-Bestimmung, sonst identisch.
33
35
  export const upsertPayloadSchema = z.object({
34
- slug: slugSchema,
36
+ slug: templateSlugSchema,
35
37
  kind: kindSchema,
36
38
  locale: localeSchema,
37
39
  content: z.string().max(200_000),
38
40
  contentFormat: contentFormatSchema,
39
41
  variableSchema: z.record(z.string(), z.unknown()).default({}),
40
- linkedResources: z.record(z.string(), z.string()).default({}),
42
+ // Shape depends on kind; validated against template-meta.ts in the handler so
43
+ // problems surface as field issues on `meta.*`.
44
+ meta: z.unknown().optional(),
41
45
  parentTemplateId: z.string().min(1).optional(),
42
46
  title: titleSchema.nullable().optional(),
43
47
  folder: folderSchema.nullable().optional(),
@@ -0,0 +1,36 @@
1
+ import type { Registry, TemplateVariables } from "@cosmicdrift/kumiko-framework/engine";
2
+ import type { FieldIssue } from "@cosmicdrift/kumiko-framework/errors";
3
+ import { type ContentFormat, type TemplateKind } from "../constants.js";
4
+ import { type TemplateWriteScope } from "../layout-regions.js";
5
+ import type { TemplateResourceRow } from "../table.js";
6
+ import { type TemplateValidation } from "../template-validation.js";
7
+ export declare function placeholderIssues(validation: TemplateValidation, opts: {
8
+ readonly requireAllRequired: boolean;
9
+ }): FieldIssue[];
10
+ export type TemplateWriteInput = {
11
+ readonly kind: TemplateKind;
12
+ readonly scope: TemplateWriteScope;
13
+ readonly contentFormat: ContentFormat;
14
+ readonly content: string | null;
15
+ readonly rawMeta: unknown;
16
+ readonly variables: TemplateVariables;
17
+ /** Keys with an uploaded file besides the built-in `logo`; only feeds the unknownResources warning. */
18
+ readonly resourceKeys?: readonly string[];
19
+ };
20
+ export type TemplateWriteResult = {
21
+ readonly ok: true;
22
+ readonly content: string | null;
23
+ readonly metaJson: string | null;
24
+ readonly validation: TemplateValidation | null;
25
+ /** Problems that still allow a draft but block publishing. */
26
+ readonly publishIssues: readonly FieldIssue[];
27
+ } | {
28
+ readonly ok: false;
29
+ readonly issues: readonly FieldIssue[];
30
+ /** Set when the placeholder scan ran, so callers can still list what it found. */
31
+ readonly validation: TemplateValidation | null;
32
+ };
33
+ export declare function evaluateTemplateWrite(input: TemplateWriteInput): TemplateWriteResult;
34
+ export declare function parseStoredJson(raw: string | null): unknown;
35
+ export declare function storedRecord(raw: string | null): Readonly<Record<string, unknown>>;
36
+ export declare function publishGateIssues(registry: Registry, row: TemplateResourceRow): readonly FieldIssue[];
@@ -0,0 +1,131 @@
1
+ import { isOneOf } from "@cosmicdrift/kumiko-types/type-guards";
2
+ import { CONTENT_FORMATS, TEMPLATE_KINDS, } from "../constants.js";
3
+ import { documentViolationsToFieldIssues } from "../document-sanitizer.js";
4
+ import { sanitizeTemplateWriteBody } from "../layout-regions.js";
5
+ import { imageResourceKeysOf } from "../linked-resources.js";
6
+ import { parseTemplateMeta } from "../template-meta.js";
7
+ import { resolveTemplateVariables, validateTemplatePlaceholders, } from "../template-validation.js";
8
+ const PLACEHOLDER_VALIDATED_KINDS = new Set([
9
+ "mail",
10
+ "document-pdf",
11
+ "pdf-layout",
12
+ ]);
13
+ function metaIssueToFieldIssue(issue) {
14
+ return {
15
+ path: issue.path,
16
+ code: "invalid_meta",
17
+ i18nKey: "templateResolver.errors.template.invalidMeta",
18
+ params: { detail: issue.message },
19
+ };
20
+ }
21
+ export function placeholderIssues(validation, opts) {
22
+ const issues = [];
23
+ if (validation.unknown.length > 0) {
24
+ issues.push({
25
+ path: "content",
26
+ code: "unknown_placeholder",
27
+ i18nKey: "templateResolver.errors.template.unknownPlaceholders",
28
+ params: { detail: validation.unknown.join(", ") },
29
+ });
30
+ }
31
+ if (opts.requireAllRequired && validation.missingRequired.length > 0) {
32
+ issues.push({
33
+ path: "content",
34
+ code: "missing_required_placeholder",
35
+ i18nKey: "templateResolver.errors.template.missingRequiredPlaceholders",
36
+ params: { detail: validation.missingRequired.join(", ") },
37
+ });
38
+ }
39
+ return issues;
40
+ }
41
+ const NO_PUBLISH_ISSUES = [];
42
+ function isPlaceholderValidated(kind, contentFormat) {
43
+ if (kind === "mail-layout")
44
+ return contentFormat === "html";
45
+ return PLACEHOLDER_VALIDATED_KINDS.has(kind);
46
+ }
47
+ // The single validation pipeline of every template write: sanitize and layout
48
+ // rules, then meta, then placeholders.
49
+ export function evaluateTemplateWrite(input) {
50
+ const body = sanitizeTemplateWriteBody({
51
+ kind: input.kind,
52
+ scope: input.scope,
53
+ contentFormat: input.contentFormat,
54
+ content: input.content,
55
+ });
56
+ if (!body.ok)
57
+ return {
58
+ ok: false,
59
+ issues: documentViolationsToFieldIssues(body.violations),
60
+ validation: null,
61
+ };
62
+ const parsedMeta = parseTemplateMeta(input.kind, input.rawMeta);
63
+ if (!parsedMeta.ok) {
64
+ return { ok: false, issues: parsedMeta.issues.map(metaIssueToFieldIssue), validation: null };
65
+ }
66
+ const metaJson = parsedMeta.meta === null ? null : JSON.stringify(parsedMeta.meta);
67
+ if (!isPlaceholderValidated(input.kind, input.contentFormat)) {
68
+ return {
69
+ ok: true,
70
+ content: body.content,
71
+ metaJson,
72
+ validation: null,
73
+ publishIssues: NO_PUBLISH_ISSUES,
74
+ };
75
+ }
76
+ const validation = validateTemplatePlaceholders({
77
+ kind: input.kind,
78
+ body: body.content ?? "",
79
+ meta: parsedMeta.meta,
80
+ variables: input.variables,
81
+ resourceKeys: input.resourceKeys,
82
+ });
83
+ const issues = placeholderIssues(validation, { requireAllRequired: false });
84
+ if (issues.length > 0)
85
+ return { ok: false, issues, validation };
86
+ return {
87
+ ok: true,
88
+ content: body.content,
89
+ metaJson,
90
+ validation,
91
+ publishIssues: placeholderIssues(validation, { requireAllRequired: true }),
92
+ };
93
+ }
94
+ function isTemplateKind(kind) {
95
+ return isOneOf(TEMPLATE_KINDS, kind);
96
+ }
97
+ export function parseStoredJson(raw) {
98
+ if (!raw)
99
+ return undefined;
100
+ try {
101
+ return JSON.parse(raw);
102
+ }
103
+ catch {
104
+ return undefined;
105
+ }
106
+ }
107
+ function isGatedKind(kind) {
108
+ return isTemplateKind(kind) && (kind === "mail-layout" || PLACEHOLDER_VALIDATED_KINDS.has(kind));
109
+ }
110
+ export function storedRecord(raw) {
111
+ const parsed = parseStoredJson(raw);
112
+ return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)
113
+ ? Object.fromEntries(Object.entries(parsed))
114
+ : {};
115
+ }
116
+ // Publishing makes a row servable, so a stored row must pass the same pipeline
117
+ // as an active write would have.
118
+ export function publishGateIssues(registry, row) {
119
+ if (!isGatedKind(row.kind) || !isOneOf(CONTENT_FORMATS, row.contentFormat))
120
+ return [];
121
+ const result = evaluateTemplateWrite({
122
+ kind: row.kind,
123
+ scope: row.scope === "system" ? "system" : "tenant",
124
+ contentFormat: row.contentFormat,
125
+ content: row.content,
126
+ rawMeta: parseStoredJson(row.meta),
127
+ variables: resolveTemplateVariables(registry, row.slug, storedRecord(row.variableSchema)),
128
+ resourceKeys: imageResourceKeysOf(row.linkedResources),
129
+ });
130
+ return result.ok ? result.publishIssues : result.issues;
131
+ }
@@ -0,0 +1,45 @@
1
+ import { type HandlerContext, type SessionUser, type TemplateVariables, type TenantId, type WriteResult } from "@cosmicdrift/kumiko-framework/engine";
2
+ import { type WriteFailure } from "@cosmicdrift/kumiko-framework/errors";
3
+ import type { ContentFormat, TemplateKind } from "../constants.js";
4
+ import type { TemplateWriteScope } from "../layout-regions.js";
5
+ import type { TemplateValidation } from "../template-validation.js";
6
+ import { type TemplateWriteInput, type TemplateWriteResult } from "./template-gates.js";
7
+ export type TemplateRowPayload = {
8
+ readonly slug: string;
9
+ readonly kind: TemplateKind;
10
+ readonly locale: string;
11
+ readonly content: string | null;
12
+ readonly contentFormat: ContentFormat;
13
+ readonly variableSchema: Readonly<Record<string, unknown>>;
14
+ readonly meta?: unknown;
15
+ readonly title?: string | null | undefined;
16
+ readonly folder?: string | null | undefined;
17
+ readonly parentTemplateId?: string | null | undefined;
18
+ };
19
+ export type WrittenTemplateRow = {
20
+ readonly id: string;
21
+ readonly slug: string;
22
+ readonly isNew: boolean;
23
+ readonly validation: TemplateValidation | null;
24
+ };
25
+ export type TemplateRowWrite = {
26
+ readonly ctx: HandlerContext;
27
+ readonly user: SessionUser;
28
+ readonly tenantId: TenantId;
29
+ readonly scope: TemplateWriteScope;
30
+ readonly status: "draft" | "active";
31
+ readonly variables: TemplateVariables;
32
+ readonly payload: TemplateRowPayload;
33
+ };
34
+ export declare function refuseTenantScopeUnderSystemTenant(scope: TemplateWriteScope, tenantId: string): WriteResult<never> | null;
35
+ export type GatedTemplateWrite = {
36
+ readonly ok: true;
37
+ readonly evaluated: Extract<TemplateWriteResult, {
38
+ ok: true;
39
+ }>;
40
+ } | {
41
+ readonly ok: false;
42
+ readonly failure: WriteFailure;
43
+ };
44
+ export declare function gateTemplateWrite(input: TemplateWriteInput, status: "draft" | "active"): GatedTemplateWrite;
45
+ export declare function writeTemplateRow(args: TemplateRowWrite): Promise<WriteResult<WrittenTemplateRow>>;