@cosmicdrift/kumiko-samples 0.374.0 → 0.378.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 (247) hide show
  1. package/package.json +1 -1
  2. package/packages/bundled-features/package.json +6 -4
  3. package/packages/bundled-features/src/account-security/changes.json +6 -0
  4. package/packages/bundled-features/src/account-security/constants.ts +1 -0
  5. package/packages/bundled-features/src/account-security/index.ts +1 -0
  6. package/packages/bundled-features/src/admin-shell/web/client-plugin.tsx +1 -1
  7. package/packages/bundled-features/src/audit/web/client-plugin.tsx +1 -1
  8. package/packages/bundled-features/src/auth-email-password/mail-notifications.ts +1 -1
  9. package/packages/bundled-features/src/auth-email-password/web/auth-client.ts +1 -1
  10. package/packages/bundled-features/src/auth-email-password/web/auth-form-primitives.tsx +1 -1
  11. package/packages/bundled-features/src/auth-email-password/web/client-plugin.ts +3 -3
  12. package/packages/bundled-features/src/auth-email-password/web/default-topbar-actions.tsx +1 -1
  13. package/packages/bundled-features/src/auth-email-password/web/index.ts +1 -1
  14. package/packages/bundled-features/src/auth-email-password/web/session.tsx +1 -1
  15. package/packages/bundled-features/src/auth-email-password/web/tenant-menu-items.tsx +1 -1
  16. package/packages/bundled-features/src/auth-email-password/web/tenant-switcher.tsx +1 -1
  17. package/packages/bundled-features/src/auth-email-password/web/user-menu.tsx +1 -1
  18. package/packages/bundled-features/src/auth-foundation/changes.json +6 -0
  19. package/packages/bundled-features/src/auth-foundation/env.ts +27 -0
  20. package/packages/bundled-features/src/auth-foundation/index.ts +1 -0
  21. package/packages/bundled-features/src/billing-foundation/changes.json +6 -0
  22. package/packages/bundled-features/src/billing-foundation/consumer-protection/confirmation-mail.ts +1 -1
  23. package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-pages.ts +1 -1
  24. package/packages/bundled-features/src/billing-foundation/contact-card.ts +14 -0
  25. package/packages/bundled-features/src/billing-foundation/handlers/billing-plans.query.ts +1 -1
  26. package/packages/bundled-features/src/billing-foundation/index.ts +1 -0
  27. package/packages/bundled-features/src/billing-foundation/plan-catalog.ts +1 -0
  28. package/packages/bundled-features/src/billing-foundation/types.ts +12 -0
  29. package/packages/bundled-features/src/billing-foundation/validate-options.ts +16 -0
  30. package/packages/bundled-features/src/billing-foundation/web/billing-contact-plan.tsx +32 -0
  31. package/packages/bundled-features/src/billing-foundation/web/billing-plans-panel.tsx +5 -15
  32. package/packages/bundled-features/src/billing-foundation/web/cancel-contract-dialog.tsx +2 -2
  33. package/packages/bundled-features/src/billing-foundation/web/checkout-consent-dialog.tsx +1 -1
  34. package/packages/bundled-features/src/billing-foundation/web/client-plugin.tsx +1 -1
  35. package/packages/bundled-features/src/billing-foundation/web/localized-params.ts +14 -0
  36. package/packages/bundled-features/src/cap-counter/changes.json +12 -0
  37. package/packages/bundled-features/src/cap-counter/stock-cap-guard.ts +9 -0
  38. package/packages/bundled-features/src/cap-overview/web/cap-cards-panel.tsx +3 -3
  39. package/packages/bundled-features/src/cap-overview/web/client-plugin.tsx +1 -1
  40. package/packages/bundled-features/src/channel-email/changes.json +6 -0
  41. package/packages/bundled-features/src/channel-email/email-channel.ts +2 -1
  42. package/packages/bundled-features/src/compliance-profiles/web/client-plugin.tsx +1 -1
  43. package/packages/bundled-features/src/config/feature.ts +2 -2
  44. package/packages/bundled-features/src/config/web/i18n.ts +1 -0
  45. package/packages/bundled-features/src/crypto-shredding/changes.json +6 -0
  46. package/packages/bundled-features/src/custom-fields/web/client-plugin.tsx +1 -1
  47. package/packages/bundled-features/src/delivery/unsubscribe-page.ts +1 -1
  48. package/packages/bundled-features/src/delivery/web/client-plugin.tsx +1 -1
  49. package/packages/bundled-features/src/delivery/web/delivery-status-cell.tsx +1 -1
  50. package/packages/bundled-features/src/feature-toggles/web/client-plugin.tsx +1 -1
  51. package/packages/bundled-features/src/file-provider-s3/changes.json +6 -0
  52. package/packages/bundled-features/src/file-provider-s3/env-named-feature.ts +45 -0
  53. package/packages/bundled-features/src/file-provider-s3/env-schema.ts +20 -13
  54. package/packages/bundled-features/src/file-provider-s3/index.ts +4 -0
  55. package/packages/bundled-features/src/folders/web/client-plugin.tsx +1 -1
  56. package/packages/bundled-features/src/folders/web/folder-section.tsx +3 -4
  57. package/packages/bundled-features/src/host-routing/README.md +7 -0
  58. package/packages/bundled-features/src/host-routing/changes.json +6 -0
  59. package/packages/bundled-features/src/host-routing/constants.ts +4 -0
  60. package/packages/bundled-features/src/host-routing/dispatch.ts +34 -26
  61. package/packages/bundled-features/src/host-routing/feature.ts +122 -41
  62. package/packages/bundled-features/src/host-routing/host.ts +28 -1
  63. package/packages/bundled-features/src/host-routing/index.ts +3 -0
  64. package/packages/bundled-features/src/host-routing/options.ts +45 -2
  65. package/packages/bundled-features/src/host-routing/redirects.ts +33 -0
  66. package/packages/bundled-features/src/host-routing/tenant-lookup.ts +65 -0
  67. package/packages/bundled-features/src/host-routing/validate-options.ts +48 -1
  68. package/packages/bundled-features/src/inbound-mail-foundation/changes.json +13 -0
  69. package/packages/bundled-features/src/inbound-mail-foundation/feature.ts +8 -0
  70. package/packages/bundled-features/src/inbound-mail-foundation/handlers/list-accounts.query.ts +11 -4
  71. package/packages/bundled-features/src/inbound-mail-foundation/i18n.ts +42 -0
  72. package/packages/bundled-features/src/inbound-mail-foundation/index.ts +4 -0
  73. package/packages/bundled-features/src/inbound-mail-foundation/screens.ts +100 -0
  74. package/packages/bundled-features/src/inbound-mail-foundation/types.ts +2 -2
  75. package/packages/bundled-features/src/jobs/web/client-plugin.tsx +1 -1
  76. package/packages/bundled-features/src/legal-pages/changes.json +6 -0
  77. package/packages/bundled-features/src/legal-pages/feature.ts +14 -1
  78. package/packages/bundled-features/src/legal-pages/layout.ts +6 -6
  79. package/packages/bundled-features/src/legal-pages/web/client-plugin.ts +1 -1
  80. package/packages/bundled-features/src/mail-foundation/changes.json +6 -0
  81. package/packages/bundled-features/src/mail-foundation/feature.ts +21 -22
  82. package/packages/bundled-features/src/mail-foundation/i18n.ts +3 -2
  83. package/packages/bundled-features/src/mail-transport-smtp/i18n.ts +1 -1
  84. package/packages/bundled-features/src/managed-pages/web/client-plugin.tsx +1 -1
  85. package/packages/bundled-features/src/notes-history/web/client-plugin.tsx +1 -1
  86. package/packages/bundled-features/src/page-render/branding.ts +1 -1
  87. package/packages/bundled-features/src/page-render/layout.ts +2 -2
  88. package/packages/bundled-features/src/page-render/markdown.ts +1 -1
  89. package/packages/bundled-features/src/personal-access-tokens/i18n.ts +1 -1
  90. package/packages/bundled-features/src/personal-access-tokens/screens.ts +1 -1
  91. package/packages/bundled-features/src/secrets/feature.ts +4 -4
  92. package/packages/bundled-features/src/seo/sitemap.ts +1 -1
  93. package/packages/bundled-features/src/shared/mail-template-body.ts +1 -1
  94. package/packages/bundled-features/src/shared/purge-files-reaching-subject.ts +127 -2
  95. package/packages/bundled-features/src/tags/web/client-plugin.tsx +1 -1
  96. package/packages/bundled-features/src/template-resolver/changes.json +38 -0
  97. package/packages/bundled-features/src/template-resolver/compile-document-body.ts +61 -3
  98. package/packages/bundled-features/src/template-resolver/constants.ts +3 -0
  99. package/packages/bundled-features/src/template-resolver/document-blocks.ts +36 -0
  100. package/packages/bundled-features/src/template-resolver/document-images.ts +29 -0
  101. package/packages/bundled-features/src/template-resolver/document-ops.ts +515 -0
  102. package/packages/bundled-features/src/template-resolver/document-sanitizer.ts +44 -0
  103. package/packages/bundled-features/src/template-resolver/document-schema.ts +5 -0
  104. package/packages/bundled-features/src/template-resolver/feature.ts +79 -34
  105. package/packages/bundled-features/src/template-resolver/handlers/bundle-import.ts +1 -0
  106. package/packages/bundled-features/src/template-resolver/handlers/collection-item.query.ts +1 -1
  107. package/packages/bundled-features/src/template-resolver/handlers/collection-list.query.ts +1 -1
  108. package/packages/bundled-features/src/template-resolver/handlers/collection-set.write.ts +2 -6
  109. package/packages/bundled-features/src/template-resolver/handlers/collection-shared.ts +0 -6
  110. package/packages/bundled-features/src/template-resolver/handlers/copy-locale.write.ts +67 -58
  111. package/packages/bundled-features/src/template-resolver/handlers/discard-draft.write.ts +36 -13
  112. package/packages/bundled-features/src/template-resolver/handlers/document-base.ts +86 -0
  113. package/packages/bundled-features/src/template-resolver/handlers/document-blocks.query.ts +48 -0
  114. package/packages/bundled-features/src/template-resolver/handlers/export-bundle.query.ts +11 -3
  115. package/packages/bundled-features/src/template-resolver/handlers/import-bundle.write.ts +37 -28
  116. package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +66 -62
  117. package/packages/bundled-features/src/template-resolver/handlers/patch-draft.write.ts +134 -0
  118. package/packages/bundled-features/src/template-resolver/handlers/preview.query.ts +2 -1
  119. package/packages/bundled-features/src/template-resolver/handlers/reset-to-system.write.ts +62 -49
  120. package/packages/bundled-features/src/template-resolver/handlers/resource-link-pdf.write.ts +44 -29
  121. package/packages/bundled-features/src/template-resolver/handlers/resource-remove.write.ts +52 -36
  122. package/packages/bundled-features/src/template-resolver/handlers/resource-upload.write.ts +126 -106
  123. package/packages/bundled-features/src/template-resolver/handlers/send-test-shared.ts +38 -0
  124. package/packages/bundled-features/src/template-resolver/handlers/send-test-status.query.ts +31 -0
  125. package/packages/bundled-features/src/template-resolver/handlers/send-test.write.ts +105 -110
  126. package/packages/bundled-features/src/template-resolver/handlers/set.write.ts +110 -103
  127. package/packages/bundled-features/src/template-resolver/handlers/template-write.ts +22 -1
  128. package/packages/bundled-features/src/template-resolver/handlers/toggle-status.write.ts +56 -28
  129. package/packages/bundled-features/src/template-resolver/handlers/upsert-tenant.write.ts +38 -29
  130. package/packages/bundled-features/src/template-resolver/handlers/validate.query.ts +24 -9
  131. package/packages/bundled-features/src/template-resolver/index.ts +8 -1
  132. package/packages/bundled-features/src/template-resolver/{web/placeholder-text.ts → placeholder-text.ts} +1 -1
  133. package/packages/bundled-features/src/template-resolver/qualified-names.ts +4 -0
  134. package/packages/bundled-features/src/template-resolver/seeding.ts +3 -1
  135. package/packages/bundled-features/src/template-resolver/template-bundle.ts +6 -1
  136. package/packages/bundled-features/src/template-resolver/template-edit-policy.ts +91 -0
  137. package/packages/bundled-features/src/template-resolver/template-preview.ts +32 -10
  138. package/packages/bundled-features/src/template-resolver/template-validation.ts +32 -11
  139. package/packages/bundled-features/src/template-resolver/web/client-plugin.tsx +3 -3
  140. package/packages/bundled-features/src/template-resolver/web/document-editor-extensions.ts +29 -0
  141. package/packages/bundled-features/src/template-resolver/web/document-tiptap-editor.tsx +43 -5
  142. package/packages/bundled-features/src/template-resolver/web/document-toolbar-blocks.ts +9 -33
  143. package/packages/bundled-features/src/template-resolver/web/i18n.ts +30 -0
  144. package/packages/bundled-features/src/template-resolver/web/placeholder-line-editor.tsx +2 -2
  145. package/packages/bundled-features/src/tenant/web/client-plugin.tsx +1 -1
  146. package/packages/bundled-features/src/tenant/web/member-status-cell.tsx +1 -1
  147. package/packages/bundled-features/src/tier-engine/web/client-plugin.tsx +1 -1
  148. package/packages/bundled-features/src/user-data-rights/changes.json +6 -0
  149. package/packages/bundled-features/src/user-data-rights/env.ts +15 -0
  150. package/packages/bundled-features/src/user-data-rights/feature.ts +4 -2
  151. package/packages/bundled-features/src/user-data-rights/index.ts +1 -0
  152. package/packages/bundled-features/src/user-data-rights/lib/mail-transport-resolver.ts +1 -1
  153. package/packages/bundled-features/src/user-data-rights/lib/storage-provider-resolver.ts +1 -1
  154. package/packages/bundled-features/src/user-data-rights/mail-notifications.ts +1 -1
  155. package/packages/bundled-features/src/user-data-rights/web/client-plugin.tsx +1 -1
  156. package/packages/bundled-features/src/user-data-rights/web/privacy-center-screen.tsx +2 -2
  157. package/packages/bundled-features/src/waitlist/changes.json +6 -0
  158. package/packages/bundled-features/src/waitlist/legacy-migrate.ts +51 -2
  159. package/samples/apps/admin-console/package.json +0 -3
  160. package/samples/apps/admin-console/src/app/client.tsx +1 -1
  161. package/samples/apps/admin-console/src/app/shell.tsx +1 -1
  162. package/samples/apps/config-demo/package.json +1 -3
  163. package/samples/apps/config-demo/src/app/client.tsx +1 -1
  164. package/samples/apps/config-demo/src/app/shell.tsx +2 -2
  165. package/samples/apps/config-demo/src/features/demo/web/index.ts +1 -1
  166. package/samples/apps/config-demo/tsconfig.json +0 -12
  167. package/samples/apps/marketing-demo/package.json +1 -3
  168. package/samples/apps/marketing-demo/src/app/client.tsx +1 -1
  169. package/samples/apps/marketing-demo/src/app/shell.tsx +1 -1
  170. package/samples/apps/marketing-demo/src/features/assets/web/index.ts +1 -1
  171. package/samples/apps/marketing-demo/src/features/helpdesk/web/index.ts +1 -1
  172. package/samples/apps/showcase/package.json +1 -3
  173. package/samples/apps/showcase/src/app/client.tsx +1 -1
  174. package/samples/apps/showcase/src/app/shell.tsx +1 -1
  175. package/samples/apps/showcase/src/features/demos/web/index.ts +1 -1
  176. package/samples/apps/showcase/src/features/demos/web/pages/sidebar-panel.tsx +1 -1
  177. package/samples/apps/showcase/src/features/demos/web/pages/toast.tsx +1 -1
  178. package/samples/apps/showcase/src/features/items/web/index.ts +1 -1
  179. package/samples/apps/styleguide/package.json +1 -3
  180. package/samples/apps/styleguide/src/app/client.tsx +1 -1
  181. package/samples/apps/styleguide/src/app/shell.tsx +2 -2
  182. package/samples/apps/styleguide/src/features/ai-settings/web.ts +1 -1
  183. package/samples/apps/styleguide/src/features/content/web.tsx +2 -2
  184. package/samples/apps/styleguide/src/features/demo/web.ts +1 -1
  185. package/samples/apps/styleguide/src/features/examples/web.ts +1 -1
  186. package/samples/apps/styleguide/src/features/gallery/web/Gallery.tsx +1 -1
  187. package/samples/apps/styleguide/src/features/gallery/web/index.ts +1 -1
  188. package/samples/apps/styleguide/src/features/widgets/demo-locale.ts +1 -1
  189. package/samples/apps/styleguide/src/features/widgets/lib/format.ts +1 -1
  190. package/samples/apps/styleguide/src/features/widgets/web/DashboardFilterEcho.tsx +1 -1
  191. package/samples/apps/styleguide/src/features/widgets/web/FormExamples.tsx +1 -1
  192. package/samples/apps/styleguide/src/features/widgets/web/Widgets.tsx +1 -1
  193. package/samples/apps/styleguide/src/features/widgets/web/index.ts +1 -1
  194. package/samples/apps/styleguide/tsconfig.json +0 -12
  195. package/samples/apps/ui-walkthrough/package.json +1 -3
  196. package/samples/apps/ui-walkthrough/src/app/client.tsx +1 -1
  197. package/samples/apps/ui-walkthrough/src/app/help-action.tsx +37 -0
  198. package/samples/apps/ui-walkthrough/src/app/seed-templates.ts +2 -0
  199. package/samples/apps/ui-walkthrough/src/app/shell.tsx +7 -1
  200. package/samples/apps/ui-walkthrough/src/app/template-seed-data.ts +20 -0
  201. package/samples/apps/ui-walkthrough/src/features/billing/web/index.ts +1 -1
  202. package/samples/apps/ui-walkthrough/src/features/rental/feature.ts +65 -1
  203. package/samples/apps/ui-walkthrough/src/features/rental/i18n.ts +70 -0
  204. package/samples/apps/ui-walkthrough/src/features/rental/invoice-receive.ts +20 -0
  205. package/samples/apps/ui-walkthrough/src/features/rental/lease-support.ts +15 -0
  206. package/samples/apps/ui-walkthrough/src/features/rental/qualified-names.ts +2 -0
  207. package/samples/apps/ui-walkthrough/src/features/rental/screens.ts +79 -0
  208. package/samples/apps/ui-walkthrough/src/features/rental/web/index.ts +3 -1
  209. package/samples/apps/ui-walkthrough/src/features/rental/web/upload-standard-layout.tsx +61 -0
  210. package/samples/apps/ui-walkthrough/src/features/tasks/i18n.ts +8 -0
  211. package/samples/apps/ui-walkthrough/src/features/tasks/web/index.ts +1 -1
  212. package/samples/apps/ui-walkthrough/src/features/vehicles/web/index.ts +1 -1
  213. package/samples/apps/ui-walkthrough/src/run-config.ts +32 -3
  214. package/samples/apps/use-all-bundled/bin/main.ts +3 -2
  215. package/samples/apps/use-all-bundled/e2e-billing-gallery/gallery-features.ts +12 -0
  216. package/samples/apps/use-all-bundled/e2e-billing-gallery/server.ts +1 -1
  217. package/samples/apps/use-all-bundled/feature-manifest.json +3 -2
  218. package/samples/apps/use-all-bundled/package.json +1 -3
  219. package/samples/apps/use-all-bundled/src/app/client.tsx +1 -1
  220. package/samples/apps/use-all-bundled/src/app/dev-app-options.ts +5 -4
  221. package/samples/apps/use-all-bundled/src/app/shell.tsx +1 -1
  222. package/samples/apps/use-all-bundled/src/run-config.ts +10 -1
  223. package/samples/apps/workspaces/package.json +1 -3
  224. package/samples/apps/workspaces/src/app/client.tsx +1 -1
  225. package/samples/apps/workspaces/src/app/shell.tsx +1 -1
  226. package/samples/apps/workspaces/src/features/demo/web/index.ts +1 -1
  227. package/samples/apps/workspaces/src/features/demo-driver/web/index.ts +1 -1
  228. package/samples/recipes/apex-landing/package.json +1 -1
  229. package/samples/recipes/apex-landing/src/feature.ts +1 -1
  230. package/samples/recipes/apex-surface-auth/README.md +1 -1
  231. package/samples/recipes/apex-surface-auth/src/feature.ts +2 -2
  232. package/samples/recipes/compose-stacks/README.md +7 -4
  233. package/samples/recipes/delivery-notifications/README.md +1 -1
  234. package/samples/recipes/embedded/README.md +8 -0
  235. package/samples/recipes/embedded/src/feature.ts +8 -0
  236. package/samples/recipes/embedded/src/handlers/invoice-receive.write.ts +38 -0
  237. package/samples/recipes/embedded/src/screens/invoice-receive.screen.ts +59 -0
  238. package/samples/recipes/embedded-entity-form/package.json +0 -1
  239. package/samples/recipes/embedded-entity-form/src/web/accept-suggestion-drawer.tsx +1 -1
  240. package/samples/recipes/notes-history-basic/README.md +1 -1
  241. package/samples/recipes/record-detail-layout/package.json +0 -1
  242. package/samples/recipes/tags-basic/README.md +1 -1
  243. package/samples/recipes/user-profile/README.md +1 -1
  244. package/samples/recipes/wizard-form/package.json +0 -1
  245. package/samples/recipes/wizard-form/src/web/index.ts +2 -2
  246. package/samples/recipes/wizard-form/src/web/listing-review-section.tsx +2 -2
  247. package/samples/recipes/writeform-section/package.json +0 -1
@@ -3,41 +3,48 @@ import {
3
3
  createSystemConfig,
4
4
  defineFeature,
5
5
  } from "@cosmicdrift/kumiko-framework/engine";
6
- import { isCollectionRefusedKind } from "./constants.ts";
6
+ import { DEFAULT_COLLECTION_ACCESS, isCollectionRefusedKind } from "./constants.ts";
7
7
  import { bySlugQuery } from "./handlers/by-slug.query.ts";
8
8
  import { byTenantQuery } from "./handlers/by-tenant.query.ts";
9
9
  import { makeCollectionItemQuery } from "./handlers/collection-item.query.ts";
10
10
  import { makeCollectionListQuery } from "./handlers/collection-list.query.ts";
11
11
  import { makeCollectionSetWrite } from "./handlers/collection-set.write.ts";
12
- import { copyLocaleWrite } from "./handlers/copy-locale.write.ts";
12
+ import { makeCopyLocaleWrite } from "./handlers/copy-locale.write.ts";
13
13
  import { diffToSystemQuery } from "./handlers/diff-to-system.query.ts";
14
- import { discardDraftSystemWrite, discardDraftWrite } from "./handlers/discard-draft.write.ts";
14
+ import {
15
+ makeDiscardDraftSystemWrite,
16
+ makeDiscardDraftWrite,
17
+ } from "./handlers/discard-draft.write.ts";
18
+ import { documentBlocksQuery } from "./handlers/document-blocks.query.ts";
15
19
  import { exportBundleQuery } from "./handlers/export-bundle.query.ts";
16
20
  import { findByIdQuery } from "./handlers/find-by-id.query.ts";
17
- import { importBundleWrite } from "./handlers/import-bundle.write.ts";
21
+ import { makeImportBundleWrite } from "./handlers/import-bundle.write.ts";
18
22
  import { importBundleSystemWrite } from "./handlers/import-bundle-system.write.ts";
19
23
  import { layoutSourceQuery } from "./handlers/layout-source.query.ts";
20
24
  import { listQuery } from "./handlers/list.query.ts";
21
- import { overviewQuery } from "./handlers/overview.query.ts";
25
+ import { makeOverviewQuery } from "./handlers/overview.query.ts";
26
+ import { makePatchDraftWrite } from "./handlers/patch-draft.write.ts";
22
27
  import { previewQuery } from "./handlers/preview.query.ts";
23
- import { resetToSystemWrite } from "./handlers/reset-to-system.write.ts";
24
- import { resourceLinkPdfWrite } from "./handlers/resource-link-pdf.write.ts";
25
- import { resourceRemoveWrite } from "./handlers/resource-remove.write.ts";
26
- import { resourceUploadWrite } from "./handlers/resource-upload.write.ts";
27
- import { sendTestWrite } from "./handlers/send-test.write.ts";
28
- import { setWrite } from "./handlers/set.write.ts";
28
+ import { makeResetToSystemWrite } from "./handlers/reset-to-system.write.ts";
29
+ import { makeResourceLinkPdfWrite } from "./handlers/resource-link-pdf.write.ts";
30
+ import { makeResourceRemoveWrite } from "./handlers/resource-remove.write.ts";
31
+ import { makeResourceUploadWrite } from "./handlers/resource-upload.write.ts";
32
+ import { makeSendTestWrite } from "./handlers/send-test.write.ts";
33
+ import { sendTestStatusQuery } from "./handlers/send-test-status.query.ts";
34
+ import { makeSetWrite } from "./handlers/set.write.ts";
29
35
  import { tenantOverridesQuery } from "./handlers/tenant-overrides.query.ts";
30
36
  import {
31
- archiveSystemWrite,
32
- archiveWrite,
33
- publishSystemWrite,
34
- publishWrite,
37
+ makeArchiveSystemWrite,
38
+ makeArchiveWrite,
39
+ makePublishSystemWrite,
40
+ makePublishWrite,
35
41
  } from "./handlers/toggle-status.write.ts";
36
42
  import { upsertSystemWrite } from "./handlers/upsert-system.write.ts";
37
- import { upsertTenantWrite } from "./handlers/upsert-tenant.write.ts";
43
+ import { makeUpsertTenantWrite } from "./handlers/upsert-tenant.write.ts";
38
44
  import { usageQuery } from "./handlers/usage.query.ts";
39
45
  import { validateQuery } from "./handlers/validate.query.ts";
40
46
  import { templateResourceEntity } from "./table.ts";
47
+ import type { TenantEditableTemplate } from "./template-edit-policy.ts";
41
48
  import { userContentEntryEntity } from "./user-content-table.ts";
42
49
 
43
50
  // template-resolver — structured template storage with tenant-override
@@ -59,6 +66,13 @@ export type TemplateResolverOptions = {
59
66
  * access rule, so the dispatcher enforces the separation instead of a
60
67
  * branch in a shared handler. */
61
68
  readonly collections?: readonly ContentCollectionDefinition[];
69
+ /** Templates a TenantAdmin may edit for the own tenant (override, draft, publish, archive,
70
+ * reset, copy-locale, import, resources). Without an entry only a SystemAdmin edits.
71
+ * A tenant-edited mail goes out under the platform's name, so the app opts in per
72
+ * template instead of the framework guessing which ones a tenant may reword.
73
+ * `access` is required: the host decides its role vocabulary here, like for
74
+ * `collections`. SystemAdmin always passes. */
75
+ readonly tenantEditable?: readonly TenantEditableTemplate[];
62
76
  };
63
77
 
64
78
  function assertCollectionKindsAllowed(collections: readonly ContentCollectionDefinition[]): void {
@@ -71,20 +85,45 @@ function assertCollectionKindsAllowed(collections: readonly ContentCollectionDef
71
85
  }
72
86
  }
73
87
 
88
+ function assertTenantEditableValid(entries: readonly TenantEditableTemplate[]): void {
89
+ const seen = new Set<string>();
90
+ for (const entry of entries) {
91
+ const key = `${entry.kind}/${entry.slug}`;
92
+ if (seen.has(key)) {
93
+ throw new Error(`template-resolver: tenantEditable lists "${key}" twice`);
94
+ }
95
+ seen.add(key);
96
+ if (entry.slug.trim() === "") {
97
+ throw new Error(
98
+ `template-resolver: tenantEditable entry of kind "${entry.kind}" has an empty slug`,
99
+ );
100
+ }
101
+ if (entry.access.roles.length === 0) {
102
+ throw new Error(
103
+ `template-resolver: tenantEditable entry "${entry.kind}/${entry.slug}" has no roles in access`,
104
+ );
105
+ }
106
+ }
107
+ }
108
+
74
109
  export function createTemplateResolverFeature(opts: TemplateResolverOptions = {}) {
75
110
  const collections = opts.collections ?? [];
76
111
  assertCollectionKindsAllowed(collections);
112
+ const tenantEditable = opts.tenantEditable ?? [];
113
+ assertTenantEditableValid(tenantEditable);
114
+ const policy = { tenantEditable };
77
115
  const hasUserOwned = collections.some((c) => c.ownership === "user");
78
116
  return defineFeature("template-resolver", (r) => {
79
117
  r.describe(
80
118
  [
81
119
  "Every piece of editable text lives here, in one entity: mail bodies, notification texts, PDF document templates, AI prompts and plain text blocks. What a record is used for is the `kind` (`notification`, `mail`, `mail-layout`, `document-pdf`, `pdf-layout`, `ai-prompt`, `text-block`, `snippet`, `image-snapshot`).",
82
120
  "Reading is one call — `ctx.templateResolver.resolveTemplate({ tenantId, slug, kind, locale })`. It walks four levels: tenant+locale, system+locale, tenant+fallback-locale, system+fallback-locale. A tenant overrides a system default by simply having its own record; no application code changes. Writing programmatically goes through the `upsertSystem`, `upsertTenant`, `publish` and `archive` write handlers; a SystemAdmin retires or restores a system default with `publishSystem` and `archiveSystem`, and a tenant drops its override with `resetToSystem`. Saving a draft over an active template keeps the active version served and stores the edit beside it (column `draft`); `publish` makes it the served version, `discardDraft` / `discardDraftSystem` throws it away.",
121
+ 'Editing templates is for SystemAdmin only. A TenantAdmin edits only the templates the app unlocks at the mount site, `createTemplateResolverFeature({ tenantEditable: [{ kind: "mail", slug: "status-update", access: { roles: ["TenantAdmin"] } }] })`, and every write handler enforces that on the server (override, draft, publish, archive, reset, copy-locale, import, resources, `set`). Each entry carries the `access` roles allowed to edit it, because a bundled feature cannot know the host\'s roles. Reading and delivery do not depend on it: a tenant override that exists is served. A collection mounted without `access` is SystemAdmin only.',
83
122
  "Text an editor should be able to change belongs in a collection, declared at mount: `createTemplateResolverFeature({ collections: [{ id, kind, access: { roles }, nav }] })`. It appears in the navigation, and `access` is part of the mount because a bundled feature cannot know the host's roles. Each collection gets its own `<id>-list` / `<id>-item` / `<id>-set` handlers, so the dispatcher enforces the separation.",
84
123
  "How a collection is edited follows from `contentFormat`: `plain` gives a text area, `rich` a small WYSIWYG (bold, italic, headings, lists, links), `markdown` a text area that stores markdown text (same insertable chips as `plain`). All three offer the collection's `variableSchema` as insertable chips and a preview rendered with sample data — an editor sees what `{{firstName}}` becomes without sending a mail. An app can register its own editor for a format; the last `clientFeature` that registers a format wins (a conflict logs a warning).",
85
124
  'A collection is tenant-wide by default. With `ownership: "user"` every user keeps their own entries — mail signatures being the obvious case. Those rows live in the separate `user-content-entry` entity and count as user data, so mounting one also requires the `template-resolver-user-data` feature and a migration on the app side.',
86
125
  "`validate` dry-runs the write pipeline for a draft (sanitizer, layout rules, placeholders) and reports what the template uses and misses. `export-bundle` / `import-bundle` (and `import-bundle-system`) move one template with all locales and resource files between tenants or environments; `copy-locale` starts a translation from an existing locale. Layouts are structured: a mail or PDF layout is `data-header` / `data-content` / `data-signature` / `data-footer` regions, and mjml layout source is system-only.",
87
- "`preview` renders a draft (or the stored template) with sample values taken from the `example` of each declared variable, through the same gates and the same mail renderer as a real send, and returns subject, html, `displayHtml` (the html with cid images as data URIs for an editor), text, attachments (inline images and files) and `checks` (images resolved, plain text present, size under 10 MiB, required placeholders used); nothing is saved or sent. `send-test` mails that rendering to the calling user's own address only, subject prefixed with `[TEST]`, at most 10 per user and hour; it needs `delivery`, `channel-email` and `renderer-mail-html` to be mounted.",
126
+ "`preview` renders a draft (or the stored template) with sample values taken from the `example` of each declared variable, through the same gates and the same mail renderer as a real send, and returns subject, html, `displayHtml` (the html with cid images as data URIs for an editor), text, attachments (inline images and files) and `checks` (images resolved, plain text present, size under 10 MiB, required placeholders used); nothing is saved or sent. `send-test` mails that rendering to the calling user's own address only, subject prefixed with `[TEST]`, at most 10 per user and hour; it needs `delivery`, `channel-email` and `renderer-mail-html` to be mounted. `send-test-status` tells an editor beforehand whether that send can work for the caller (`canSend`, else `reason` `no_channel` or `no_address`), never returning the address.",
88
127
  'Images used by a template (`<img data-resource="logo">`) are uploaded with `resourceUpload`, which re-encodes them, stores a file and links it under the key; `resourceRemove` unlinks it. `linkedResources` is written only through these two handlers. With `purpose: "attachment"` and a `fileName`, `resourceUpload` stores a PDF, PNG or JPEG as a file attachment of a mail template (original bytes, at most 10) that the mail renderer attaches after the inline images. `resourceLinkPdf` instead links a `document-pdf` template under a key: every mail sent from the template then carries that PDF, rendered at send time with the mail\'s own variables and locale, under a file name pattern such as `invoice-{{number}}.pdf`; files and linked PDFs share the limit of 10.',
89
128
  "Replaces the former `text-content` feature; its blocks now live here as kind `text-block`.",
90
129
  ].join("\n\n"),
@@ -108,22 +147,23 @@ export function createTemplateResolverFeature(opts: TemplateResolverOptions = {}
108
147
 
109
148
  const handlers = {
110
149
  upsertSystem: r.writeHandler(upsertSystemWrite),
111
- upsertTenant: r.writeHandler(upsertTenantWrite),
112
- publish: r.writeHandler(publishWrite),
113
- archive: r.writeHandler(archiveWrite),
114
- publishSystem: r.writeHandler(publishSystemWrite),
115
- archiveSystem: r.writeHandler(archiveSystemWrite),
116
- discardDraft: r.writeHandler(discardDraftWrite),
117
- discardDraftSystem: r.writeHandler(discardDraftSystemWrite),
118
- resetToSystem: r.writeHandler(resetToSystemWrite),
119
- set: r.writeHandler(setWrite),
120
- importBundle: r.writeHandler(importBundleWrite),
150
+ upsertTenant: r.writeHandler(makeUpsertTenantWrite(policy)),
151
+ publish: r.writeHandler(makePublishWrite(policy)),
152
+ archive: r.writeHandler(makeArchiveWrite(policy)),
153
+ publishSystem: r.writeHandler(makePublishSystemWrite(policy)),
154
+ archiveSystem: r.writeHandler(makeArchiveSystemWrite(policy)),
155
+ discardDraft: r.writeHandler(makeDiscardDraftWrite(policy)),
156
+ discardDraftSystem: r.writeHandler(makeDiscardDraftSystemWrite(policy)),
157
+ resetToSystem: r.writeHandler(makeResetToSystemWrite(policy)),
158
+ set: r.writeHandler(makeSetWrite(policy)),
159
+ importBundle: r.writeHandler(makeImportBundleWrite(policy)),
121
160
  importBundleSystem: r.writeHandler(importBundleSystemWrite),
122
- copyLocale: r.writeHandler(copyLocaleWrite),
123
- resourceUpload: r.writeHandler(resourceUploadWrite),
124
- resourceRemove: r.writeHandler(resourceRemoveWrite),
125
- resourceLinkPdf: r.writeHandler(resourceLinkPdfWrite),
126
- sendTest: r.writeHandler(sendTestWrite),
161
+ copyLocale: r.writeHandler(makeCopyLocaleWrite(policy)),
162
+ resourceUpload: r.writeHandler(makeResourceUploadWrite(policy)),
163
+ resourceRemove: r.writeHandler(makeResourceRemoveWrite(policy)),
164
+ resourceLinkPdf: r.writeHandler(makeResourceLinkPdfWrite(policy)),
165
+ sendTest: r.writeHandler(makeSendTestWrite(policy)),
166
+ patchDraft: r.writeHandler(makePatchDraftWrite(policy)),
127
167
  };
128
168
 
129
169
  const queries = {
@@ -132,17 +172,22 @@ export function createTemplateResolverFeature(opts: TemplateResolverOptions = {}
132
172
  bySlug: r.queryHandler(bySlugQuery),
133
173
  byTenant: r.queryHandler(byTenantQuery),
134
174
  usage: r.queryHandler(usageQuery),
135
- overview: r.queryHandler(overviewQuery),
175
+ overview: r.queryHandler(makeOverviewQuery(policy)),
136
176
  layoutSource: r.queryHandler(layoutSourceQuery),
137
177
  tenantOverrides: r.queryHandler(tenantOverridesQuery),
138
178
  diffToSystem: r.queryHandler(diffToSystemQuery),
139
179
  validate: r.queryHandler(validateQuery),
140
180
  preview: r.queryHandler(previewQuery),
181
+ documentBlocks: r.queryHandler(documentBlocksQuery),
141
182
  exportBundle: r.queryHandler(exportBundleQuery),
183
+ sendTestStatus: r.queryHandler(sendTestStatusQuery),
142
184
  };
143
185
 
144
186
  for (const collection of collections) {
145
- r.contentCollection(collection);
187
+ r.contentCollection({
188
+ ...collection,
189
+ access: collection.access ?? DEFAULT_COLLECTION_ACCESS,
190
+ });
146
191
  r.queryHandler(makeCollectionListQuery(collection));
147
192
  r.queryHandler(makeCollectionItemQuery(collection));
148
193
  r.writeHandler(makeCollectionSetWrite(collection));
@@ -214,6 +214,7 @@ export async function importTemplateBundle(
214
214
  contentFormat: args.bundle.contentFormat,
215
215
  variableSchema,
216
216
  meta: entry.meta,
217
+ ...(entry.title !== undefined && { title: entry.title }),
217
218
  };
218
219
  const written = await writeTemplateRow({
219
220
  ctx: args.ctx,
@@ -4,10 +4,10 @@ import {
4
4
  defineQueryHandler,
5
5
  } from "@cosmicdrift/kumiko-framework/engine";
6
6
  import * as z from "zod";
7
+ import { DEFAULT_COLLECTION_ACCESS } from "../constants.ts";
7
8
  import {
8
9
  type CollectionEntryRow,
9
10
  collectionStore,
10
- DEFAULT_COLLECTION_ACCESS,
11
11
  toCollectionEntry,
12
12
  } from "./collection-shared.ts";
13
13
 
@@ -4,10 +4,10 @@ import {
4
4
  defineQueryHandler,
5
5
  } from "@cosmicdrift/kumiko-framework/engine";
6
6
  import * as z from "zod";
7
+ import { DEFAULT_COLLECTION_ACCESS } from "../constants.ts";
7
8
  import {
8
9
  type CollectionEntryRow,
9
10
  collectionStore,
10
- DEFAULT_COLLECTION_ACCESS,
11
11
  toCollectionEntry,
12
12
  } from "./collection-shared.ts";
13
13
 
@@ -7,13 +7,9 @@ import {
7
7
  import { ValidationError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
8
8
  import * as z from "zod";
9
9
  import { requireForTenant } from "../../shared/index.ts";
10
- import { isCollectionRefusedKind } from "../constants.ts";
10
+ import { DEFAULT_COLLECTION_ACCESS, isCollectionRefusedKind } from "../constants.ts";
11
11
  import { documentViolationsToFieldIssues, sanitizeTemplateBody } from "../document-sanitizer.ts";
12
- import {
13
- type CollectionEntryRow,
14
- collectionStore,
15
- DEFAULT_COLLECTION_ACCESS,
16
- } from "./collection-shared.ts";
12
+ import { type CollectionEntryRow, collectionStore } from "./collection-shared.ts";
17
13
  import { contentFormatSchema, folderSchema, localeSchema, slugSchema } from "./shared.ts";
18
14
 
19
15
  // Upsert inside one collection. Mirrors set.write for text-blocks, but the
@@ -1,7 +1,6 @@
1
1
  import type { EventStoreExecutor } from "@cosmicdrift/kumiko-framework/db";
2
2
  import { createEventStoreExecutor } from "@cosmicdrift/kumiko-framework/db";
3
3
  import type {
4
- AccessRule,
5
4
  ContentCollectionDefinition,
6
5
  SessionUser,
7
6
  TenantId,
@@ -11,11 +10,6 @@ import type { Temporal } from "@cosmicdrift/kumiko-types/temporal";
11
10
  import { templateResourceEntity, templateResourcesTable } from "../table.ts";
12
11
  import { userContentEntriesTable, userContentEntryEntity } from "../user-content-table.ts";
13
12
 
14
- // Applies when an app mounts a collection without saying who may reach it.
15
- // Deliberately narrow: a collection whose access nobody decided should be
16
- // invisible to normal users rather than open by default.
17
- export const DEFAULT_COLLECTION_ACCESS: AccessRule = { roles: ["TenantAdmin", "SystemAdmin"] };
18
-
19
13
  const templateExecutor = createEventStoreExecutor(templateResourcesTable, templateResourceEntity, {
20
14
  entityName: "template-resource",
21
15
  });
@@ -25,6 +25,11 @@ import {
25
25
  } from "../store-template-resource.ts";
26
26
  import { type TemplateResourceRow, templateResourcesTable } from "../table.ts";
27
27
  import { toWorkingRow, type WorkingRow } from "../template-draft.ts";
28
+ import {
29
+ refuseTemplateEdit,
30
+ type TemplateEditPolicy,
31
+ templateEditAccess,
32
+ } from "../template-edit-policy.ts";
28
33
  import { attachTemplateResources } from "../template-resources.ts";
29
34
  import { resolveTemplateVariables } from "../template-validation.ts";
30
35
  import { targetExistsIssue } from "./bundle-import.ts";
@@ -97,6 +102,7 @@ type CopyRequest = {
97
102
  // Runs before the upload (reject early, store nothing) and again in the transaction on the fresh rows.
98
103
  async function checkCopyRequest(
99
104
  ctx: WriteCtx,
105
+ policy: TemplateEditPolicy,
100
106
  event: {
101
107
  readonly user: SessionUser;
102
108
  readonly payload: z.infer<typeof copyLocaleSchema>;
@@ -113,6 +119,7 @@ async function checkCopyRequest(
113
119
  }
114
120
  const tenantId = scope === "system" ? SYSTEM_TENANT_ID : event.user.tenantId;
115
121
  const refused =
122
+ (scope === "tenant" ? refuseTemplateEdit(policy, event.user, { slug, kind }) : null) ??
116
123
  refuseTenantScopeUnderSystemTenant(scope, tenantId) ??
117
124
  refuseSystemOnlyTenantOverride(ctx.registry, scope, slug, kind);
118
125
  if (refused !== null) return refused;
@@ -155,62 +162,64 @@ async function checkCopyRequest(
155
162
  };
156
163
  }
157
164
 
158
- export const copyLocaleWrite = defineWriteHandler({
159
- name: "copy-locale",
160
- schema: copyLocaleSchema,
161
- access: { roles: ["TenantAdmin", "SystemAdmin"] },
162
- description:
163
- "Copies the text of one locale of a template into another locale as a draft override of the caller's tenant, taking the tenant's own row or else the system default as the source; use it as the starting point for a translation. It refuses an existing target unless overwrite is set. With scope system (SystemAdmin only) both source and target are system-default rows and the target lands as a system draft; any other caller gets a 403.",
164
- beforeTransaction: async (event, ctx) => {
165
- const request = await checkCopyRequest(ctx, event);
166
- if (!request.isSuccess) return request;
167
- if (!request.data.sourceIsForeign) return { isSuccess: true, data: [] };
168
- return prepareLinkedResourceCopiesBeforeTransaction({
169
- ctx,
170
- user: event.user,
171
- tenantId: request.data.tenantId,
172
- resources: request.data.sourceResources,
173
- });
174
- },
175
- handler: async (event, ctx) => {
176
- const { slug, kind, toLocale } = event.payload;
177
- const request = await checkCopyRequest(ctx, event);
178
- if (!request.isSuccess) return request;
179
- const { scope, tenantId, source, contentFormat, sourceResources, sourceIsForeign } =
180
- request.data;
165
+ export function makeCopyLocaleWrite(policy: TemplateEditPolicy) {
166
+ return defineWriteHandler({
167
+ name: "copy-locale",
168
+ schema: copyLocaleSchema,
169
+ access: templateEditAccess(policy.tenantEditable),
170
+ description:
171
+ "Copies the text of one locale of a template into another locale as a draft override of the caller's tenant, taking the tenant's own row or else the system default as the source; use it as the starting point for a translation. It refuses an existing target unless overwrite is set. With scope system (SystemAdmin only) both source and target are system-default rows and the target lands as a system draft; any other caller gets a 403. A TenantAdmin may copy only templates the app lists in tenantEditable, otherwise SystemAdmin only.",
172
+ beforeTransaction: async (event, ctx) => {
173
+ const request = await checkCopyRequest(ctx, policy, event);
174
+ if (!request.isSuccess) return request;
175
+ if (!request.data.sourceIsForeign) return { isSuccess: true, data: [] };
176
+ return prepareLinkedResourceCopiesBeforeTransaction({
177
+ ctx,
178
+ user: event.user,
179
+ tenantId: request.data.tenantId,
180
+ resources: request.data.sourceResources,
181
+ });
182
+ },
183
+ handler: async (event, ctx) => {
184
+ const { slug, kind, toLocale } = event.payload;
185
+ const request = await checkCopyRequest(ctx, policy, event);
186
+ if (!request.isSuccess) return request;
187
+ const { scope, tenantId, source, contentFormat, sourceResources, sourceIsForeign } =
188
+ request.data;
181
189
 
182
- const variableSchema = storedRecord(source.variableSchema);
183
- const written = await writeTemplateRow({
184
- ctx,
185
- user: event.user,
186
- tenantId,
187
- scope,
188
- status: "draft",
189
- variables: resolveTemplateVariables(ctx.registry, slug, variableSchema),
190
- payload: {
191
- slug,
192
- kind,
193
- locale: toLocale,
194
- content: source.content,
195
- contentFormat,
196
- variableSchema,
197
- meta: parseStoredJson(source.meta),
198
- title: source.title,
199
- folder: source.folder,
200
- },
201
- });
202
- if (!written.isSuccess) return written;
203
- const resources = sourceIsForeign
204
- ? await createLinkedResourceCopies({ ctx, user: event.user, tenantId }, event.prepared)
205
- : { isSuccess: true as const, data: sourceResources };
206
- if (!resources.isSuccess) return resources;
207
- const attached = await attachTemplateResources(ctx, {
208
- user: event.user,
209
- tenantId,
210
- templateId: written.data.id,
211
- resources: resources.data,
212
- });
213
- if (!attached.isSuccess) return attached;
214
- return written;
215
- },
216
- });
190
+ const variableSchema = storedRecord(source.variableSchema);
191
+ const written = await writeTemplateRow({
192
+ ctx,
193
+ user: event.user,
194
+ tenantId,
195
+ scope,
196
+ status: "draft",
197
+ variables: resolveTemplateVariables(ctx.registry, slug, variableSchema),
198
+ payload: {
199
+ slug,
200
+ kind,
201
+ locale: toLocale,
202
+ content: source.content,
203
+ contentFormat,
204
+ variableSchema,
205
+ meta: parseStoredJson(source.meta),
206
+ title: source.title,
207
+ folder: source.folder,
208
+ },
209
+ });
210
+ if (!written.isSuccess) return written;
211
+ const resources = sourceIsForeign
212
+ ? await createLinkedResourceCopies({ ctx, user: event.user, tenantId }, event.prepared)
213
+ : { isSuccess: true as const, data: sourceResources };
214
+ if (!resources.isSuccess) return resources;
215
+ const attached = await attachTemplateResources(ctx, {
216
+ user: event.user,
217
+ tenantId,
218
+ templateId: written.data.id,
219
+ resources: resources.data,
220
+ });
221
+ if (!attached.isSuccess) return attached;
222
+ return written;
223
+ },
224
+ });
225
+ }
@@ -2,19 +2,36 @@ import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
2
2
  import { NotFoundError, ValidationError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
3
3
  import * as z from "zod";
4
4
  import { TemplateResolverErrors } from "../qualified-names.ts";
5
+ import {
6
+ refuseMissingTemplate,
7
+ refuseTemplateEdit,
8
+ type TemplateEditPolicy,
9
+ } from "../template-edit-policy.ts";
5
10
  import { executor } from "./shared.ts";
6
11
  import { loadTargetRow, type StatusTarget, writeAccessFor } from "./toggle-status.write.ts";
7
12
 
8
- function createDiscardDraftHandler(name: string, target: StatusTarget, description: string) {
13
+ function createDiscardDraftHandler(
14
+ policy: TemplateEditPolicy,
15
+ name: string,
16
+ target: StatusTarget,
17
+ description: string,
18
+ ) {
9
19
  return defineWriteHandler({
10
20
  name,
11
21
  schema: z.object({ id: z.string().min(1) }),
12
- access: writeAccessFor(target),
22
+ access: writeAccessFor(target, policy),
13
23
  ...(target === "system" ? { agent: { risk: "high" as const } } : {}),
14
24
  description,
15
25
  handler: async (event, ctx) => {
16
26
  const { db, scoped, row } = await loadTargetRow(ctx, target, event.user, event.payload.id);
17
- if (!row) return writeFailure(new NotFoundError("template-resource", event.payload.id));
27
+ if (!row) {
28
+ const refused = target === "own" ? refuseMissingTemplate(policy, event.user) : null;
29
+ return refused ?? writeFailure(new NotFoundError("template-resource", event.payload.id));
30
+ }
31
+ if (target === "own") {
32
+ const refused = refuseTemplateEdit(policy, event.user, row);
33
+ if (refused !== null) return refused;
34
+ }
18
35
  if (row.draft === null) {
19
36
  return writeFailure(
20
37
  new ValidationError({
@@ -40,13 +57,19 @@ function createDiscardDraftHandler(name: string, target: StatusTarget, descripti
40
57
  });
41
58
  }
42
59
 
43
- export const discardDraftWrite = createDiscardDraftHandler(
44
- "discard-draft",
45
- "own",
46
- "Throws away the open draft of one template resource of the caller's tenant so the editor shows the active version again; the served version is untouched. Refused with no_draft when there is no draft.",
47
- );
48
- export const discardDraftSystemWrite = createDiscardDraftHandler(
49
- "discard-draft-system",
50
- "system",
51
- "Throws away the open draft of one system-default template; the served version stays. SystemAdmin only, refused with no_draft when there is no draft.",
52
- );
60
+ export function makeDiscardDraftWrite(policy: TemplateEditPolicy) {
61
+ return createDiscardDraftHandler(
62
+ policy,
63
+ "discard-draft",
64
+ "own",
65
+ "Throws away the open draft of one template resource of the caller's tenant so the editor shows the active version again; the served version is untouched. Refused with no_draft when there is no draft. Outside the templates the app lists in tenantEditable only a SystemAdmin may use it.",
66
+ );
67
+ }
68
+ export function makeDiscardDraftSystemWrite(policy: TemplateEditPolicy) {
69
+ return createDiscardDraftHandler(
70
+ policy,
71
+ "discard-draft-system",
72
+ "system",
73
+ "Throws away the open draft of one system-default template; the served version stays. SystemAdmin only, refused with no_draft when there is no draft.",
74
+ );
75
+ }
@@ -0,0 +1,86 @@
1
+ import { fetchOne } from "@cosmicdrift/kumiko-framework/db";
2
+ import {
3
+ type HandlerContext,
4
+ type SessionUser,
5
+ SYSTEM_TENANT_ID,
6
+ } from "@cosmicdrift/kumiko-framework/engine";
7
+ import { isPlainObject } from "@cosmicdrift/kumiko-types/type-guards";
8
+ import type { DocumentHtmlKind } from "../constants.ts";
9
+ import { type TemplateResourceRow, templateResourcesTable } from "../table.ts";
10
+ import { isHiddenSystemDraft, toWorkingRow } from "../template-draft.ts";
11
+ import { isSystemOnlyTemplateHiddenFrom } from "../template-system-only.ts";
12
+ import { parseStoredJson } from "./template-gates.ts";
13
+
14
+ export type DocumentBase = {
15
+ readonly scope: "tenant" | "system";
16
+ /** The stored row's own version, which counts every draft save. */
17
+ readonly version: number;
18
+ };
19
+
20
+ export type LoadedDocument = {
21
+ readonly rowId: string;
22
+ readonly base: DocumentBase;
23
+ readonly status: string;
24
+ readonly content: string;
25
+ readonly meta: Readonly<Record<string, unknown>>;
26
+ readonly variableSchema: Readonly<Record<string, unknown>>;
27
+ readonly title: string | null;
28
+ readonly folder: string | null;
29
+ readonly parentTemplateId: string | null;
30
+ };
31
+
32
+ function recordOrEmpty(raw: string | null): Readonly<Record<string, unknown>> {
33
+ const parsed = parseStoredJson(raw);
34
+ return isPlainObject(parsed) ? parsed : {};
35
+ }
36
+
37
+ // The version an editor works on: the caller's own row (its open draft if there is one),
38
+ // else the served system default. A system default as base is what a patch copies into a tenant draft.
39
+ export async function loadDocumentBase(
40
+ ctx: HandlerContext,
41
+ user: SessionUser,
42
+ where: { readonly slug: string; readonly kind: DocumentHtmlKind; readonly locale: string },
43
+ ): Promise<LoadedDocument | null> {
44
+ const own = await fetchOne<TemplateResourceRow>(ctx.db, templateResourcesTable, {
45
+ tenantId: user.tenantId,
46
+ ...where,
47
+ });
48
+ if (own !== undefined && !isSystemOnlyTemplateHiddenFrom(user, ctx.registry, own)) {
49
+ const working = toWorkingRow(own, user.tenantId);
50
+ return {
51
+ rowId: String(own.id),
52
+ base: { scope: own.scope === "system" ? "system" : "tenant", version: own.version },
53
+ status: working.status,
54
+ content: working.content ?? "",
55
+ meta: recordOrEmpty(working.meta),
56
+ variableSchema: recordOrEmpty(working.variableSchema),
57
+ title: working.title,
58
+ folder: working.folder,
59
+ parentTemplateId: working.parentTemplateId,
60
+ };
61
+ }
62
+ const system = await fetchOne<TemplateResourceRow>(ctx.db, templateResourcesTable, {
63
+ tenantId: SYSTEM_TENANT_ID,
64
+ scope: "system",
65
+ status: "active",
66
+ ...where,
67
+ });
68
+ if (
69
+ system === undefined ||
70
+ isHiddenSystemDraft(user, system) ||
71
+ isSystemOnlyTemplateHiddenFrom(user, ctx.registry, system)
72
+ ) {
73
+ return null;
74
+ }
75
+ return {
76
+ rowId: String(system.id),
77
+ base: { scope: "system", version: system.version },
78
+ status: system.status,
79
+ content: system.content ?? "",
80
+ meta: recordOrEmpty(system.meta),
81
+ variableSchema: recordOrEmpty(system.variableSchema),
82
+ title: system.title,
83
+ folder: system.folder,
84
+ parentTemplateId: String(system.id),
85
+ };
86
+ }
@@ -0,0 +1,48 @@
1
+ import { defineQueryHandler } from "@cosmicdrift/kumiko-framework/engine";
2
+ import { NotFoundError } from "@cosmicdrift/kumiko-framework/errors";
3
+ import * as z from "zod";
4
+ import { DOCUMENT_HTML_KINDS } from "../constants.ts";
5
+ import { documentBlocksForKind } from "../document-blocks.ts";
6
+ import { documentBlocksOf } from "../document-ops.ts";
7
+ import { parseDocumentJson } from "../document-sanitizer.ts";
8
+ import { resolveTemplateVariables } from "../template-validation.ts";
9
+ import { loadDocumentBase } from "./document-base.ts";
10
+ import { localeSchema, templateSlugSchema } from "./shared.ts";
11
+ import { knownResourceKeysFor } from "./validate.query.ts";
12
+
13
+ export const documentBlocksQuery = defineQueryHandler({
14
+ name: "document-blocks",
15
+ schema: z.object({
16
+ slug: templateSlugSchema,
17
+ kind: z.enum(DOCUMENT_HTML_KINDS),
18
+ locale: localeSchema,
19
+ }),
20
+ access: { roles: ["TenantAdmin", "SystemAdmin"] },
21
+ description:
22
+ "Reads a mail or PDF document template as a list of blocks with stable ids (b0, b1, ...) for structured editing: each block has its type (paragraph, heading, list, table, repeat, image, button, divider, pageBreak, steps), its text with {{name}} placeholders and its settings, plus the meta, the stored and effective variables (variablesDeclaredByFeature is true when a notification declares them, so they cannot be changed), the block types allowed for the kind and the uploaded image keys. It reads the caller's own version (the open draft if there is one), else the served system default; base carries the scope and version that patch-draft expects back.",
23
+ handler: async (query, ctx) => {
24
+ const { slug, kind, locale } = query.payload;
25
+ const loaded = await loadDocumentBase(ctx, query.user, { slug, kind, locale });
26
+ if (loaded === null) throw new NotFoundError("template-resource", slug);
27
+ return {
28
+ slug,
29
+ kind,
30
+ locale,
31
+ base: loaded.base,
32
+ status: loaded.status,
33
+ blocks: documentBlocksOf(parseDocumentJson(loaded.content)),
34
+ meta: loaded.meta,
35
+ variableSchema: loaded.variableSchema,
36
+ variables: resolveTemplateVariables(ctx.registry, slug, loaded.variableSchema),
37
+ variablesDeclaredByFeature:
38
+ ctx.registry.getAllNotifications().get(slug)?.variables !== undefined,
39
+ allowedBlocks: documentBlocksForKind(kind),
40
+ resourceKeys: await knownResourceKeysFor(ctx, query.user, {
41
+ slug,
42
+ kind,
43
+ scope: "tenant",
44
+ locale,
45
+ }),
46
+ };
47
+ },
48
+ });