@cosmicdrift/kumiko-bundled-features 0.417.0 → 0.419.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 (154) hide show
  1. package/dist/agent-tools/api-docs/schema-fields.js +9 -0
  2. package/dist/auth-email-password/index.d.ts +2 -1
  3. package/dist/auth-email-password/index.js +2 -1
  4. package/dist/auth-email-password/signup-activation.js +2 -1
  5. package/dist/auth-email-password/signup-token-store.d.ts +1 -6
  6. package/dist/auth-email-password/signup-token-store.js +0 -5
  7. package/dist/cap-counter/stock-cap-guard.d.ts +9 -2
  8. package/dist/cap-counter/stock-cap-guard.js +9 -11
  9. package/dist/document-ingest-foundation/feature.js +5 -1
  10. package/dist/document-ingest-foundation/report-ingest-progress.js +19 -6
  11. package/dist/signature/entity.d.ts +8 -6
  12. package/dist/signature/entity.js +3 -1
  13. package/dist/signature/feature.js +11 -4
  14. package/dist/signature/handlers/lifecycle.write.d.ts +9 -0
  15. package/dist/signature/handlers/lifecycle.write.js +23 -1
  16. package/dist/signature/handlers/request.write.d.ts +1 -1
  17. package/dist/signature/i18n.js +1 -0
  18. package/dist/signature/list-cursor-seal.d.ts +3 -0
  19. package/dist/signature/list-cursor-seal.js +44 -0
  20. package/dist/signature/options.d.ts +2 -2
  21. package/dist/signature/payloads.d.ts +17 -1
  22. package/dist/signature/payloads.js +11 -2
  23. package/dist/signature/png.js +23 -3
  24. package/dist/signature/subject-queries.d.ts +1 -1
  25. package/dist/signature/subject-queries.js +30 -8
  26. package/dist/signature/sweep.js +26 -9
  27. package/dist/signature/web/constants.d.ts +9 -0
  28. package/dist/signature/web/constants.js +8 -0
  29. package/dist/subscription-stripe/plugin-methods.js +2 -0
  30. package/dist/subscription-stripe/verify-webhook.js +17 -16
  31. package/dist/template-resolver/compile-document-body.js +3 -6
  32. package/dist/template-resolver/compile-document-styles.d.ts +3 -0
  33. package/dist/template-resolver/compile-document-styles.js +84 -0
  34. package/dist/template-resolver/document-frames.d.ts +3 -0
  35. package/dist/template-resolver/document-frames.js +19 -0
  36. package/dist/template-resolver/document-op-blocks.d.ts +495 -0
  37. package/dist/template-resolver/document-op-blocks.js +430 -0
  38. package/dist/template-resolver/document-ops.d.ts +1809 -37
  39. package/dist/template-resolver/document-ops.js +97 -104
  40. package/dist/template-resolver/document-print-only.d.ts +10 -0
  41. package/dist/template-resolver/document-print-only.js +24 -0
  42. package/dist/template-resolver/document-sanitizer.d.ts +1 -1
  43. package/dist/template-resolver/document-sanitizer.js +24 -13
  44. package/dist/template-resolver/feature.d.ts +3 -0
  45. package/dist/template-resolver/feature.js +14 -1
  46. package/dist/template-resolver/handlers/apply-layout.write.d.ts +29 -0
  47. package/dist/template-resolver/handlers/apply-layout.write.js +118 -0
  48. package/dist/template-resolver/handlers/create-layout.write.d.ts +15 -0
  49. package/dist/template-resolver/handlers/create-layout.write.js +78 -0
  50. package/dist/template-resolver/handlers/document-base.d.ts +2 -2
  51. package/dist/template-resolver/handlers/document-blocks.query.d.ts +10 -3
  52. package/dist/template-resolver/handlers/document-blocks.query.js +13 -8
  53. package/dist/template-resolver/handlers/document-region.d.ts +19 -0
  54. package/dist/template-resolver/handlers/document-region.js +27 -0
  55. package/dist/template-resolver/handlers/failure-reason.d.ts +2 -0
  56. package/dist/template-resolver/handlers/failure-reason.js +12 -0
  57. package/dist/template-resolver/handlers/import-bundle-system.write.js +4 -0
  58. package/dist/template-resolver/handlers/layout-source.query.d.ts +3 -3
  59. package/dist/template-resolver/handlers/layout-source.query.js +6 -2
  60. package/dist/template-resolver/handlers/overview.query.d.ts +8 -0
  61. package/dist/template-resolver/handlers/overview.query.js +12 -6
  62. package/dist/template-resolver/handlers/patch-draft.write.d.ts +905 -4
  63. package/dist/template-resolver/handlers/patch-draft.write.js +10 -6
  64. package/dist/template-resolver/handlers/publish-templates.write.d.ts +40 -0
  65. package/dist/template-resolver/handlers/publish-templates.write.js +61 -0
  66. package/dist/template-resolver/handlers/set.write.js +4 -1
  67. package/dist/template-resolver/handlers/template-write.js +7 -2
  68. package/dist/template-resolver/handlers/tenant-settings.d.ts +17 -0
  69. package/dist/template-resolver/handlers/tenant-settings.js +91 -0
  70. package/dist/template-resolver/handlers/toggle-status.write.d.ts +14 -9
  71. package/dist/template-resolver/handlers/toggle-status.write.js +11 -7
  72. package/dist/template-resolver/handlers/upsert-system.write.js +15 -9
  73. package/dist/template-resolver/handlers/usage.query.d.ts +14 -0
  74. package/dist/template-resolver/handlers/usage.query.js +80 -12
  75. package/dist/template-resolver/handlers/validate.query.d.ts +2 -0
  76. package/dist/template-resolver/handlers/validate.query.js +10 -0
  77. package/dist/template-resolver/i18n.js +3 -0
  78. package/dist/template-resolver/index.d.ts +7 -2
  79. package/dist/template-resolver/index.js +6 -2
  80. package/dist/template-resolver/layout-regions.d.ts +4 -5
  81. package/dist/template-resolver/layout-regions.js +38 -11
  82. package/dist/template-resolver/print-document.js +3 -1
  83. package/dist/template-resolver/seeding.js +4 -0
  84. package/dist/template-resolver/template-delivery-policy.d.ts +1 -0
  85. package/dist/template-resolver/template-delivery-policy.js +9 -1
  86. package/dist/template-resolver/template-edit-policy.d.ts +5 -1
  87. package/dist/template-resolver/template-edit-policy.js +29 -1
  88. package/dist/template-resolver/template-meta.d.ts +27 -2
  89. package/dist/template-resolver/template-meta.js +21 -5
  90. package/dist/template-resolver/template-preview.js +8 -8
  91. package/dist/template-resolver/template-validation.js +10 -3
  92. package/dist/template-resolver/web/constants.d.ts +7 -1
  93. package/dist/template-resolver/web/constants.js +21 -1
  94. package/dist/template-resolver/web/document-blocks.d.ts +1 -0
  95. package/dist/template-resolver/web/document-blocks.js +2 -0
  96. package/dist/template-resolver/web/document-editor-extensions.d.ts +9 -1
  97. package/dist/template-resolver/web/document-editor-extensions.js +27 -2
  98. package/dist/template-resolver/web/document-editor-fonts.d.ts +10 -0
  99. package/dist/template-resolver/web/document-editor-fonts.js +13 -0
  100. package/dist/template-resolver/web/document-fonts.d.ts +4 -0
  101. package/dist/template-resolver/web/document-fonts.js +18 -0
  102. package/dist/template-resolver/web/document-frame-control.d.ts +61 -0
  103. package/dist/template-resolver/web/document-frame-control.js +329 -0
  104. package/dist/template-resolver/web/document-frame-node-view.d.ts +4 -0
  105. package/dist/template-resolver/web/document-frame-node-view.js +173 -0
  106. package/dist/template-resolver/web/document-print-decorations.d.ts +12 -0
  107. package/dist/template-resolver/web/document-print-decorations.js +147 -0
  108. package/dist/template-resolver/web/document-print-defaults.d.ts +14 -0
  109. package/dist/template-resolver/web/document-print-defaults.js +25 -0
  110. package/dist/template-resolver/web/document-print-style.d.ts +71 -0
  111. package/dist/template-resolver/web/document-print-style.js +199 -0
  112. package/dist/template-resolver/web/document-repeat-strip.d.ts +8 -0
  113. package/dist/template-resolver/web/document-repeat-strip.js +78 -0
  114. package/dist/template-resolver/web/document-schema.js +23 -9
  115. package/dist/template-resolver/web/document-style-css.d.ts +29 -0
  116. package/dist/template-resolver/web/document-style-css.js +76 -0
  117. package/dist/template-resolver/web/document-tiptap-bubble.js +13 -3
  118. package/dist/template-resolver/web/document-tiptap-content.d.ts +2 -1
  119. package/dist/template-resolver/web/document-tiptap-content.js +36 -4
  120. package/dist/template-resolver/web/document-tiptap-context.d.ts +6 -2
  121. package/dist/template-resolver/web/document-tiptap-context.js +2 -1
  122. package/dist/template-resolver/web/document-tiptap-editor.d.ts +1 -1
  123. package/dist/template-resolver/web/document-tiptap-editor.js +13 -5
  124. package/dist/template-resolver/web/document-tiptap-helpers.d.ts +30 -1
  125. package/dist/template-resolver/web/document-tiptap-helpers.js +103 -2
  126. package/dist/template-resolver/web/document-tiptap-toolbar-groups.d.ts +5 -0
  127. package/dist/template-resolver/web/document-tiptap-toolbar-groups.js +89 -6
  128. package/dist/template-resolver/web/document-tiptap-toolbar.js +45 -8
  129. package/dist/template-resolver/web/document-toolbar-blocks.d.ts +17 -0
  130. package/dist/template-resolver/web/i18n.js +19 -0
  131. package/dist/template-resolver/web/index.d.ts +8 -2
  132. package/dist/template-resolver/web/index.js +6 -1
  133. package/dist/template-resolver/web/pdf-constants.d.ts +3 -0
  134. package/dist/template-resolver/web/pdf-constants.js +3 -0
  135. package/dist/template-resolver/web/qualified-names.d.ts +7 -0
  136. package/dist/template-resolver/web/qualified-names.js +6 -0
  137. package/dist/template-resolver/web/use-document-tiptap-model.d.ts +6 -2
  138. package/dist/template-resolver/web/use-document-tiptap-model.js +38 -3
  139. package/dist/tenant/company-config.d.ts +6 -0
  140. package/dist/tenant/company-config.js +41 -0
  141. package/dist/tenant/feature.js +11 -0
  142. package/dist/tenant/i18n.js +14 -0
  143. package/dist/tenant-inactivity/handlers/keep-account.write.js +5 -2
  144. package/dist/tenant-lifecycle/run-tenant-destroy.js +65 -33
  145. package/package.json +16 -11
  146. package/src/agent-tools/changes.json +6 -0
  147. package/src/cap-counter/changes.json +6 -0
  148. package/src/channel-whatsapp/changes.json +1 -1
  149. package/src/document-ingest-foundation/changes.json +6 -0
  150. package/src/signature/changes.json +18 -0
  151. package/src/subscription-paddle/changes.json +1 -1
  152. package/src/subscription-stripe/changes.json +6 -0
  153. package/src/template-resolver/changes.json +38 -0
  154. package/src/tenant-lifecycle/changes.json +6 -0
@@ -0,0 +1,78 @@
1
+ import { fetchOne } from "@cosmicdrift/kumiko-framework/db";
2
+ import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
3
+ import { ValidationError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
4
+ import * as z from "zod";
5
+ import { patchIssue } from "../document-op-blocks.js";
6
+ import { composeLayoutHtml } from "../layout-regions.js";
7
+ import { templateResourcesTable } from "../table.js";
8
+ import { refuseTemplateEdit, templateEditAccess, } from "../template-edit-policy.js";
9
+ import { resolveTemplateVariables } from "../template-validation.js";
10
+ import { PDF_DEFAULT_FONT_SIZE_PT, TENANT_LAYOUT_SLUG_PREFIX } from "../web/constants.js";
11
+ import { localeSchema, slugSchema, titleSchema } from "./shared.js";
12
+ import { writeTemplateRow } from "./template-write.js";
13
+ const PDF_LAYOUT_KIND = "pdf-layout";
14
+ const STANDARD_LAYOUT_META = {
15
+ fontFamily: "arial",
16
+ fontSizePt: PDF_DEFAULT_FONT_SIZE_PT,
17
+ tableStyle: "plain",
18
+ };
19
+ export function makeCreateLayoutWrite(policy) {
20
+ return defineWriteHandler({
21
+ name: "create-layout",
22
+ schema: z.object({ name: slugSchema, title: titleSchema, locale: localeSchema }),
23
+ access: templateEditAccess(policy.tenantEditable),
24
+ agent: { risk: "low", effect: "proposal" },
25
+ description: "Creates a new PDF layout of the caller's tenant as a draft under the slug tenant-layout-<name> (name is kebab-case) with empty header, signature and footer regions and the standard look (font, size, table style); it fails with layout_exists when the tenant already has that layout. A TenantAdmin may create layouts as soon as they may edit any PDF template; every other caller is refused. Returns the full slug, locale, kind and base ({scope, version}) for the next call. Next steps: fill the regions with patch-draft (kind pdf-layout, the returned slug, region header, signature or footer; setMeta changes margins, fontFamily, fontSizePt, textColor, tableStyle, tableHeaderBackground), then let a person publish the layout and point a PDF template at it with patch-draft setMeta {layoutSlug: <returned slug>}.",
26
+ handler: async (event, ctx) => {
27
+ const { name, title, locale } = event.payload;
28
+ const slug = `${TENANT_LAYOUT_SLUG_PREFIX}${name}`;
29
+ const refused = refuseTemplateEdit(policy, event.user, { slug, kind: PDF_LAYOUT_KIND });
30
+ if (refused !== null)
31
+ return refused;
32
+ const existing = await fetchOne(ctx.db, templateResourcesTable, {
33
+ tenantId: event.user.tenantId,
34
+ slug,
35
+ kind: PDF_LAYOUT_KIND,
36
+ });
37
+ if (existing !== undefined) {
38
+ return writeFailure(new ValidationError({ fields: [patchIssue("name", "layout_exists", { slug })] }));
39
+ }
40
+ const written = await writeTemplateRow({
41
+ ctx,
42
+ user: event.user,
43
+ tenantId: event.user.tenantId,
44
+ scope: "tenant",
45
+ status: "draft",
46
+ variables: resolveTemplateVariables(ctx.registry, slug, {}),
47
+ payload: {
48
+ slug,
49
+ kind: PDF_LAYOUT_KIND,
50
+ locale,
51
+ content: composeLayoutHtml({ header: "", signature: "", footer: "" }),
52
+ contentFormat: "html",
53
+ variableSchema: {},
54
+ meta: STANDARD_LAYOUT_META,
55
+ title,
56
+ },
57
+ expectedVersion: null,
58
+ });
59
+ if (!written.isSuccess)
60
+ return written;
61
+ const created = await fetchOne(ctx.db, templateResourcesTable, {
62
+ tenantId: event.user.tenantId,
63
+ id: written.data.id,
64
+ });
65
+ if (created === undefined)
66
+ throw new Error(`create-layout: row ${slug} missing after write`);
67
+ return {
68
+ isSuccess: true,
69
+ data: {
70
+ slug,
71
+ locale,
72
+ kind: PDF_LAYOUT_KIND,
73
+ base: { scope: "tenant", version: created.version },
74
+ },
75
+ };
76
+ },
77
+ });
78
+ }
@@ -1,5 +1,5 @@
1
1
  import { type HandlerContext, type SessionUser } from "@cosmicdrift/kumiko-framework/engine";
2
- import type { DocumentHtmlKind } from "../web/constants.ts";
2
+ import type { DocumentOpsKind } from "../web/constants.ts";
3
3
  export type DocumentBase = {
4
4
  readonly scope: "tenant" | "system";
5
5
  /** The stored row's own version, which counts every draft save. */
@@ -18,6 +18,6 @@ export type LoadedDocument = {
18
18
  };
19
19
  export declare function loadDocumentBase(ctx: HandlerContext, user: SessionUser, where: {
20
20
  readonly slug: string;
21
- readonly kind: DocumentHtmlKind;
21
+ readonly kind: DocumentOpsKind;
22
22
  readonly locale: string;
23
23
  }): Promise<LoadedDocument | null>;
@@ -1,14 +1,21 @@
1
1
  import * as z from "zod";
2
2
  export declare const documentBlocksQuery: import("@cosmicdrift/kumiko-types/define-handler").QueryHandlerDefinition<"document-blocks", z.ZodObject<{
3
- slug: z.ZodString;
4
3
  kind: z.ZodEnum<{
5
4
  "document-pdf": "document-pdf";
6
5
  mail: "mail";
6
+ "pdf-layout": "pdf-layout";
7
7
  }>;
8
+ region: z.ZodOptional<z.ZodEnum<{
9
+ footer: "footer";
10
+ header: "header";
11
+ signature: "signature";
12
+ }>>;
13
+ slug: z.ZodString;
8
14
  locale: z.ZodString;
9
15
  }, z.core.$strip>, {
10
16
  slug: string;
11
- kind: "document-pdf" | "mail";
17
+ kind: "document-pdf" | "mail" | "pdf-layout";
18
+ region?: "footer" | "header" | "signature" | undefined;
12
19
  locale: string;
13
20
  base: import("./document-base.ts").DocumentBase;
14
21
  status: string;
@@ -17,6 +24,6 @@ export declare const documentBlocksQuery: import("@cosmicdrift/kumiko-types/defi
17
24
  variableSchema: Readonly<Record<string, unknown>>;
18
25
  variables: Readonly<Record<string, import("@cosmicdrift/kumiko-types/config").TemplateVariableDef>>;
19
26
  variablesDeclaredByFeature: boolean;
20
- allowedBlocks: readonly import("../web/document-blocks.ts").DocumentToolbarBlock[];
27
+ allowedBlocks: readonly ("frame" | import("../web/document-blocks.ts").DocumentToolbarBlock)[];
21
28
  resourceKeys: string[];
22
29
  }, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap>;
@@ -1,40 +1,45 @@
1
1
  import { defineQueryHandler } from "@cosmicdrift/kumiko-framework/engine";
2
- import { NotFoundError } from "@cosmicdrift/kumiko-framework/errors";
2
+ import { NotFoundError, ValidationError } from "@cosmicdrift/kumiko-framework/errors";
3
3
  import * as z from "zod";
4
+ import { allowedBlocksFor } from "../document-op-blocks.js";
4
5
  import { documentBlocksOf } from "../document-ops.js";
5
6
  import { parseDocumentJson } from "../document-sanitizer.js";
6
7
  import { resolveTemplateVariables } from "../template-validation.js";
7
- import { DOCUMENT_HTML_KINDS } from "../web/constants.js";
8
- import { documentBlocksForKind } from "../web/document-blocks.js";
9
8
  import { loadDocumentBase } from "./document-base.js";
9
+ import { documentTargetShape, editableHtmlOf, regionIssue } from "./document-region.js";
10
10
  import { localeSchema, templateSlugSchema } from "./shared.js";
11
11
  import { knownResourceKeysFor } from "./validate.query.js";
12
12
  export const documentBlocksQuery = defineQueryHandler({
13
13
  name: "document-blocks",
14
14
  schema: z.object({
15
15
  slug: templateSlugSchema,
16
- kind: z.enum(DOCUMENT_HTML_KINDS),
16
+ ...documentTargetShape,
17
17
  locale: localeSchema,
18
18
  }),
19
19
  access: { roles: ["TenantAdmin", "SystemAdmin"] },
20
- description: "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.",
20
+ description: "Reads a mail template, a PDF document template or, with kind pdf-layout and a region (header, signature, footer), one region of a PDF layout as a list of blocks with stable ids (b0, b1, ...) for structured editing with patch-draft: each block has its type (paragraph, heading, list, table, repeat, frame with its position in mm, 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 (a frame only in document-pdf) 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.",
21
21
  handler: async (query, ctx) => {
22
- const { slug, kind, locale } = query.payload;
22
+ const { slug, kind, region, locale } = query.payload;
23
+ const misplacedRegion = regionIssue(kind, region);
24
+ if (misplacedRegion !== null) {
25
+ throw new ValidationError({ fields: [misplacedRegion] });
26
+ }
23
27
  const loaded = await loadDocumentBase(ctx, query.user, { slug, kind, locale });
24
28
  if (loaded === null)
25
29
  throw new NotFoundError("template-resource", slug);
26
30
  return {
27
31
  slug,
28
32
  kind,
33
+ ...(region === undefined ? {} : { region }),
29
34
  locale,
30
35
  base: loaded.base,
31
36
  status: loaded.status,
32
- blocks: documentBlocksOf(parseDocumentJson(loaded.content)),
37
+ blocks: documentBlocksOf(parseDocumentJson(editableHtmlOf(kind, region, loaded.content))),
33
38
  meta: loaded.meta,
34
39
  variableSchema: loaded.variableSchema,
35
40
  variables: resolveTemplateVariables(ctx.registry, slug, loaded.variableSchema),
36
41
  variablesDeclaredByFeature: ctx.registry.getAllNotifications().get(slug)?.variables !== undefined,
37
- allowedBlocks: documentBlocksForKind(kind),
42
+ allowedBlocks: allowedBlocksFor(kind),
38
43
  resourceKeys: await knownResourceKeysFor(ctx, query.user, {
39
44
  slug,
40
45
  kind,
@@ -0,0 +1,19 @@
1
+ import type { FieldIssue } from "@cosmicdrift/kumiko-framework/errors";
2
+ import * as z from "zod";
3
+ import { type LayoutRegionName } from "../layout-regions.ts";
4
+ import { type DocumentOpsKind } from "../web/constants.ts";
5
+ export declare const documentTargetShape: {
6
+ kind: z.ZodEnum<{
7
+ "document-pdf": "document-pdf";
8
+ mail: "mail";
9
+ "pdf-layout": "pdf-layout";
10
+ }>;
11
+ region: z.ZodOptional<z.ZodEnum<{
12
+ footer: "footer";
13
+ header: "header";
14
+ signature: "signature";
15
+ }>>;
16
+ };
17
+ export declare function regionIssue(kind: DocumentOpsKind, region: LayoutRegionName | undefined): FieldIssue | null;
18
+ export declare function editableHtmlOf(kind: DocumentOpsKind, region: LayoutRegionName | undefined, content: string): string;
19
+ export declare function contentWithEditedHtml(kind: DocumentOpsKind, region: LayoutRegionName | undefined, content: string, editedHtml: string): string;
@@ -0,0 +1,27 @@
1
+ import * as z from "zod";
2
+ import { patchIssue } from "../document-op-blocks.js";
3
+ import { composeLayoutHtml, LAYOUT_REGION_NAMES, readLayoutRegions, } from "../layout-regions.js";
4
+ import { DOCUMENT_OPS_KINDS } from "../web/constants.js";
5
+ export const documentTargetShape = {
6
+ kind: z.enum(DOCUMENT_OPS_KINDS),
7
+ region: z.enum(LAYOUT_REGION_NAMES).optional(),
8
+ };
9
+ // A pdf-layout is edited one region at a time; every other kind is the whole body.
10
+ export function regionIssue(kind, region) {
11
+ if (kind === "pdf-layout" && region === undefined)
12
+ return patchIssue("region", "region_required");
13
+ if (kind !== "pdf-layout" && region !== undefined) {
14
+ return patchIssue("region", "region_not_allowed", { kind });
15
+ }
16
+ return null;
17
+ }
18
+ export function editableHtmlOf(kind, region, content) {
19
+ return kind === "pdf-layout" && region !== undefined
20
+ ? readLayoutRegions(content)[region]
21
+ : content;
22
+ }
23
+ export function contentWithEditedHtml(kind, region, content, editedHtml) {
24
+ if (kind !== "pdf-layout" || region === undefined)
25
+ return editedHtml;
26
+ return composeLayoutHtml({ ...readLayoutRegions(content), [region]: editedHtml });
27
+ }
@@ -0,0 +1,2 @@
1
+ import type { WriteFailure } from "@cosmicdrift/kumiko-framework/errors";
2
+ export declare function failureReason(failure: WriteFailure): string;
@@ -0,0 +1,12 @@
1
+ import { isPlainObject } from "@cosmicdrift/kumiko-types/type-guards";
2
+ // A batch reports what went wrong per template instead of aborting: the first field issue's code
3
+ // for a validation failure, else the error's own reason or code.
4
+ export function failureReason(failure) {
5
+ const { details } = failure.error;
6
+ if (!isPlainObject(details))
7
+ return failure.error.code;
8
+ const firstField = Array.isArray(details["fields"]) ? details["fields"][0] : undefined;
9
+ if (isPlainObject(firstField) && typeof firstField["code"] === "string")
10
+ return firstField["code"];
11
+ return typeof details["reason"] === "string" ? details["reason"] : failure.error.code;
12
+ }
@@ -2,6 +2,7 @@ import { defineWriteHandler, SYSTEM_TENANT_ID } from "@cosmicdrift/kumiko-framew
2
2
  import { ValidationError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
3
3
  import * as z from "zod";
4
4
  import { templateBundleSchema } from "../template-bundle.js";
5
+ import { refuseReservedLayoutSlug } from "../template-edit-policy.js";
5
6
  import { importTemplateBundle, prepareTemplateBundleBeforeTransaction } from "./bundle-import.js";
6
7
  export const importBundleSystemWrite = defineWriteHandler({
7
8
  name: "import-bundle-system",
@@ -12,6 +13,9 @@ export const importBundleSystemWrite = defineWriteHandler({
12
13
  description: "Imports a template bundle as system-default templates, active straight away and overwriting existing system rows; SystemAdmin only. Bundles with resource files need a caller in the system tenant, because the files are stored in the caller's file space.",
13
14
  beforeTransaction: async (event, ctx) => {
14
15
  const { bundle } = event.payload;
16
+ const reserved = refuseReservedLayoutSlug(bundle, "bundle.slug");
17
+ if (reserved !== null)
18
+ return reserved;
15
19
  // Must run before any upload.
16
20
  if (Object.keys(bundle.resources).length > 0 && event.user.tenantId !== SYSTEM_TENANT_ID) {
17
21
  return writeFailure(new ValidationError({
@@ -1,7 +1,7 @@
1
1
  import * as z from "zod";
2
2
  import { type LayoutRegions } from "../layout-regions.ts";
3
3
  import { type ActiveInfo } from "../template-draft.ts";
4
- import { type MailLayoutMeta } from "../template-meta.ts";
4
+ import { type MailLayoutMeta, type PdfLayoutMeta } from "../template-meta.ts";
5
5
  export type LayoutSourceRow = {
6
6
  readonly id: string;
7
7
  readonly version: number;
@@ -17,8 +17,8 @@ export type LayoutSourceResponse = {
17
17
  readonly regions: LayoutRegions;
18
18
  readonly effectiveFormat: "html" | "mjml" | null;
19
19
  readonly mjmlSource: string | null;
20
- /** Appearance of the effective mail layout; invalid stored meta reads as defaults (empty). */
21
- readonly meta: MailLayoutMeta;
20
+ /** Appearance of the effective layout; invalid stored meta reads as defaults (empty). */
21
+ readonly meta: MailLayoutMeta | PdfLayoutMeta;
22
22
  readonly locale: string;
23
23
  };
24
24
  export declare const layoutSourceQuery: import("@cosmicdrift/kumiko-types/define-handler").QueryHandlerDefinition<"layout-source", z.ZodObject<{
@@ -7,7 +7,7 @@ import { readLayoutRegions } from "../layout-regions.js";
7
7
  import { templateResourcesTable } from "../table.js";
8
8
  import { deliveryPolicyOf } from "../template-delivery-policy.js";
9
9
  import { draftViewerTenantId, isHiddenSystemDraft, toWorkingRow, } from "../template-draft.js";
10
- import { readMailLayoutMeta } from "../template-meta.js";
10
+ import { readMailLayoutMeta, readPdfLayoutMeta, } from "../template-meta.js";
11
11
  import { FALLBACK_LOCALE } from "../web/constants.js";
12
12
  import { TemplateResolverErrors } from "../web/qualified-names.js";
13
13
  import { localeSchema, templateSlugSchema } from "./shared.js";
@@ -57,6 +57,10 @@ function readMjmlSource(systemRow, isSystemAdmin) {
57
57
  const exposesMjml = isSystemAdmin && systemRow !== null && layoutFormat(systemRow) === "mjml";
58
58
  return exposesMjml ? (systemRow.content ?? "") : null;
59
59
  }
60
+ function readLayoutMeta(kind, effective) {
61
+ const stored = parseStoredJson(effective?.meta ?? null);
62
+ return kind === "mail-layout" ? readMailLayoutMeta(stored) : readPdfLayoutMeta(stored);
63
+ }
60
64
  function buildLayoutSource(input) {
61
65
  const { kind, locale, isSystemAdmin, tenantRow, systemRow } = input;
62
66
  const effective = tenantRow ?? systemRow;
@@ -69,7 +73,7 @@ function buildLayoutSource(input) {
69
73
  regions: readEffectiveRegions(effective, effectiveFormat),
70
74
  effectiveFormat,
71
75
  mjmlSource: readMjmlSource(systemRow, isSystemAdmin),
72
- meta: kind === "mail-layout" ? readMailLayoutMeta(parseStoredJson(effective?.meta ?? null)) : {},
76
+ meta: readLayoutMeta(kind, effective),
73
77
  locale: effective?.locale ?? locale ?? FALLBACK_LOCALE,
74
78
  };
75
79
  }
@@ -53,6 +53,14 @@ export type OverviewResponse = {
53
53
  /** True when the tenant has more template rows than the overview reads; rows and total are then incomplete. */
54
54
  readonly truncated: boolean;
55
55
  };
56
+ export declare function ownLayoutSlugOf(row: TemplateResourceRow): string | null;
57
+ type EffectiveRows = {
58
+ readonly origin: OverviewOrigin;
59
+ readonly rows: TemplateResourceRow[];
60
+ readonly lockedOverride: boolean;
61
+ readonly lockedOverrideId: string | null;
62
+ };
63
+ export declare function effectiveRowsOf(rows: readonly TemplateResourceRow[], policy: TemplateDeliveryPolicy): EffectiveRows | null;
56
64
  export declare function isRowVisibleToCaller(policy: TemplateEditPolicy, user: Parameters<typeof isHiddenSystemDraft>[0], registry: Parameters<typeof isSystemOnlyTemplateHiddenFrom>[1], row: TemplateResourceRow): boolean;
57
65
  export declare function effectiveMailLayoutSlugs(rows: readonly TemplateResourceRow[], policy: TemplateDeliveryPolicy): EffectiveMailLayoutSlugs;
58
66
  export declare function makeOverviewQuery(policy: TemplateEditPolicy): import("@cosmicdrift/kumiko-types/define-handler").QueryHandlerDefinition<"overview", z.ZodObject<{
@@ -12,6 +12,7 @@ import { DEFAULT_MAIL_LAYOUT_SLUG, FALLBACK_LOCALE, } from "../web/constants.js"
12
12
  import { TemplateResolverQueries } from "../web/qualified-names.js";
13
13
  import { storedRecord } from "./template-gates.js";
14
14
  import { tenantOverrideCountsResultSchema } from "./tenant-override-counts.query.js";
15
+ import { defaultPdfLayoutSlug } from "./tenant-settings.js";
15
16
  const OVERVIEW_KINDS = ["mail", "document-pdf", "mail-layout", "pdf-layout"];
16
17
  const OVERVIEW_ROW_LIMIT = 5000;
17
18
  const LOCKED_OVERRIDE_I18N_KEY = "templateResolver.overview.lockedOverride";
@@ -28,16 +29,19 @@ function isOverviewKind(kind) {
28
29
  function isTemplateStatus(status) {
29
30
  return status === "draft" || status === "active" || status === "archived";
30
31
  }
31
- function layoutSlugOf(row) {
32
+ export function ownLayoutSlugOf(row) {
32
33
  const layoutSlug = storedRecord(row.meta)["layoutSlug"];
33
- return typeof layoutSlug === "string" ? layoutSlug : DEFAULT_MAIL_LAYOUT_SLUG;
34
+ return typeof layoutSlug === "string" ? layoutSlug : null;
35
+ }
36
+ function layoutSlugOf(row, fallback = DEFAULT_MAIL_LAYOUT_SLUG) {
37
+ return ownLayoutSlugOf(row) ?? fallback;
34
38
  }
35
39
  function preferredRow(rows) {
36
40
  return (rows.find((row) => row.locale === FALLBACK_LOCALE) ??
37
41
  [...rows].sort((a, b) => a.locale.localeCompare(b.locale))[0]);
38
42
  }
39
43
  // A tenant row of a template that is not unlocked is not delivered, so the system version is the effective one.
40
- function effectiveRowsOf(rows, policy) {
44
+ export function effectiveRowsOf(rows, policy) {
41
45
  const tenantRows = rows.filter((row) => row.scope === "tenant");
42
46
  const systemRows = rows.filter((row) => row.scope === "system");
43
47
  const live = (list) => list.filter((row) => row.status !== "archived");
@@ -143,7 +147,7 @@ function versionFieldsOf(user, preferred, groupRows) {
143
147
  systemVersion: systemSameLocale === undefined ? null : activeVersionOf(systemSameLocale),
144
148
  };
145
149
  }
146
- function buildDrafts(groups, user, policy) {
150
+ function buildDrafts(groups, user, policy, defaultPdfLayoutSlug) {
147
151
  const layoutSlugByDocument = new Map();
148
152
  const drafts = [];
149
153
  for (const [id, groupRows] of groups) {
@@ -154,7 +158,9 @@ function buildDrafts(groups, user, policy) {
154
158
  if (!isOverviewKind(preferred.kind) || !isTemplateStatus(preferred.status))
155
159
  continue;
156
160
  const isDocument = preferred.kind === "mail" || preferred.kind === "document-pdf";
157
- const layoutSlug = isDocument ? layoutSlugOf(preferred) : null;
161
+ const layoutSlug = isDocument
162
+ ? layoutSlugOf(preferred, preferred.kind === "document-pdf" ? defaultPdfLayoutSlug : DEFAULT_MAIL_LAYOUT_SLUG)
163
+ : null;
158
164
  if (layoutSlug !== null && preferred.kind === "document-pdf") {
159
165
  layoutSlugByDocument.set(preferred.slug, layoutSlug);
160
166
  }
@@ -257,7 +263,7 @@ export function makeOverviewQuery(policy) {
257
263
  }
258
264
  const visibleRows = rows.filter((row) => isRowVisibleToCaller(policy, query.user, ctx.registry, row));
259
265
  const deliveryPolicy = deliveryPolicyOf(ctx.registry);
260
- const { drafts, layoutSlugByDocument } = buildDrafts(groupBySlugAndKind(visibleRows), query.user, deliveryPolicy);
266
+ const { drafts, layoutSlugByDocument } = buildDrafts(groupBySlugAndKind(visibleRows), query.user, deliveryPolicy, await defaultPdfLayoutSlug(ctx, query.user.tenantId));
261
267
  const effectiveLayouts = effectiveMailLayoutSlugs(visibleRows, deliveryPolicy);
262
268
  const rowsWithUsage = drafts.map((draft) => withUsage(draft, layoutSlugByDocument, ctx.registry, effectiveLayouts));
263
269
  const overviewRows = isSystemAdmin(query.user)