@cosmicdrift/kumiko-bundled-features 0.418.0 → 0.420.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.
- package/dist/shared/index.d.ts +1 -0
- package/dist/shared/index.js +1 -0
- package/dist/shared/tenant-scoped-subject.d.ts +11 -0
- package/dist/shared/tenant-scoped-subject.js +18 -0
- package/dist/signature/feature.js +1 -1
- package/dist/signature/grant.d.ts +1 -1
- package/dist/signature/grant.js +7 -12
- package/dist/signature/png.js +35 -10
- package/dist/template-resolver/api.d.ts +2 -0
- package/dist/template-resolver/api.js +11 -7
- package/dist/template-resolver/compile-document-body.js +9 -11
- package/dist/template-resolver/compile-document-styles.d.ts +3 -0
- package/dist/template-resolver/compile-document-styles.js +122 -0
- package/dist/template-resolver/document-frames.d.ts +4 -0
- package/dist/template-resolver/document-frames.js +56 -0
- package/dist/template-resolver/document-op-blocks.d.ts +729 -0
- package/dist/template-resolver/document-op-blocks.js +521 -0
- package/dist/template-resolver/document-ops.d.ts +2743 -37
- package/dist/template-resolver/document-ops.js +128 -106
- package/dist/template-resolver/document-print-only.d.ts +11 -0
- package/dist/template-resolver/document-print-only.js +47 -0
- package/dist/template-resolver/document-sanitizer.d.ts +1 -1
- package/dist/template-resolver/document-sanitizer.js +29 -13
- package/dist/template-resolver/feature.d.ts +3 -0
- package/dist/template-resolver/feature.js +14 -1
- package/dist/template-resolver/handlers/apply-layout.write.d.ts +29 -0
- package/dist/template-resolver/handlers/apply-layout.write.js +118 -0
- package/dist/template-resolver/handlers/create-layout.write.d.ts +15 -0
- package/dist/template-resolver/handlers/create-layout.write.js +78 -0
- package/dist/template-resolver/handlers/document-base.d.ts +2 -2
- package/dist/template-resolver/handlers/document-blocks.query.d.ts +10 -3
- package/dist/template-resolver/handlers/document-blocks.query.js +13 -8
- package/dist/template-resolver/handlers/document-region.d.ts +19 -0
- package/dist/template-resolver/handlers/document-region.js +27 -0
- package/dist/template-resolver/handlers/failure-reason.d.ts +2 -0
- package/dist/template-resolver/handlers/failure-reason.js +12 -0
- package/dist/template-resolver/handlers/import-bundle-system.write.js +4 -0
- package/dist/template-resolver/handlers/layout-source.query.d.ts +3 -3
- package/dist/template-resolver/handlers/layout-source.query.js +6 -2
- package/dist/template-resolver/handlers/overview.query.d.ts +8 -0
- package/dist/template-resolver/handlers/overview.query.js +12 -6
- package/dist/template-resolver/handlers/patch-draft.write.d.ts +1369 -4
- package/dist/template-resolver/handlers/patch-draft.write.js +11 -6
- package/dist/template-resolver/handlers/publish-templates.write.d.ts +40 -0
- package/dist/template-resolver/handlers/publish-templates.write.js +61 -0
- package/dist/template-resolver/handlers/set.write.js +4 -1
- package/dist/template-resolver/handlers/template-write.js +7 -2
- package/dist/template-resolver/handlers/tenant-settings.d.ts +23 -0
- package/dist/template-resolver/handlers/tenant-settings.js +101 -0
- package/dist/template-resolver/handlers/toggle-status.write.d.ts +14 -9
- package/dist/template-resolver/handlers/toggle-status.write.js +11 -7
- package/dist/template-resolver/handlers/upsert-system.write.js +15 -9
- package/dist/template-resolver/handlers/usage.query.d.ts +14 -0
- package/dist/template-resolver/handlers/usage.query.js +80 -12
- package/dist/template-resolver/handlers/validate.query.d.ts +2 -0
- package/dist/template-resolver/handlers/validate.query.js +10 -0
- package/dist/template-resolver/i18n.js +3 -0
- package/dist/template-resolver/index.d.ts +7 -2
- package/dist/template-resolver/index.js +6 -2
- package/dist/template-resolver/layout-regions.d.ts +4 -5
- package/dist/template-resolver/layout-regions.js +38 -11
- package/dist/template-resolver/print-document.js +3 -1
- package/dist/template-resolver/seeding.js +4 -0
- package/dist/template-resolver/template-delivery-policy.d.ts +1 -0
- package/dist/template-resolver/template-delivery-policy.js +9 -1
- package/dist/template-resolver/template-edit-policy.d.ts +5 -1
- package/dist/template-resolver/template-edit-policy.js +29 -1
- package/dist/template-resolver/template-meta.d.ts +37 -2
- package/dist/template-resolver/template-meta.js +27 -5
- package/dist/template-resolver/template-preview.js +22 -10
- package/dist/template-resolver/template-validation.js +10 -3
- package/dist/template-resolver/web/constants.d.ts +7 -1
- package/dist/template-resolver/web/constants.js +21 -1
- package/dist/template-resolver/web/document-blocks.d.ts +1 -0
- package/dist/template-resolver/web/document-blocks.js +2 -0
- package/dist/template-resolver/web/document-editor-extensions.d.ts +9 -1
- package/dist/template-resolver/web/document-editor-extensions.js +69 -19
- package/dist/template-resolver/web/document-editor-fonts.d.ts +10 -0
- package/dist/template-resolver/web/document-editor-fonts.js +13 -0
- package/dist/template-resolver/web/document-editor.js +3 -1
- package/dist/template-resolver/web/document-fonts.d.ts +4 -0
- package/dist/template-resolver/web/document-fonts.js +18 -0
- package/dist/template-resolver/web/document-frame-control.d.ts +61 -0
- package/dist/template-resolver/web/document-frame-control.js +329 -0
- package/dist/template-resolver/web/document-frame-node-view.d.ts +4 -0
- package/dist/template-resolver/web/document-frame-node-view.js +173 -0
- package/dist/template-resolver/web/document-print-decorations.d.ts +12 -0
- package/dist/template-resolver/web/document-print-decorations.js +194 -0
- package/dist/template-resolver/web/document-print-defaults.d.ts +14 -0
- package/dist/template-resolver/web/document-print-defaults.js +25 -0
- package/dist/template-resolver/web/document-print-style.d.ts +150 -0
- package/dist/template-resolver/web/document-print-style.js +311 -0
- package/dist/template-resolver/web/document-repeat-strip.d.ts +8 -0
- package/dist/template-resolver/web/document-repeat-strip.js +54 -0
- package/dist/template-resolver/web/document-schema.d.ts +5 -0
- package/dist/template-resolver/web/document-schema.js +70 -19
- package/dist/template-resolver/web/document-style-css.d.ts +62 -0
- package/dist/template-resolver/web/document-style-css.js +140 -0
- package/dist/template-resolver/web/document-tiptap-bubble.js +83 -10
- package/dist/template-resolver/web/document-tiptap-content.d.ts +2 -1
- package/dist/template-resolver/web/document-tiptap-content.js +39 -4
- package/dist/template-resolver/web/document-tiptap-context.d.ts +6 -2
- package/dist/template-resolver/web/document-tiptap-context.js +2 -1
- package/dist/template-resolver/web/document-tiptap-editor.d.ts +1 -1
- package/dist/template-resolver/web/document-tiptap-editor.js +13 -5
- package/dist/template-resolver/web/document-tiptap-helpers.d.ts +62 -3
- package/dist/template-resolver/web/document-tiptap-helpers.js +199 -23
- package/dist/template-resolver/web/document-tiptap-toolbar-groups.d.ts +16 -0
- package/dist/template-resolver/web/document-tiptap-toolbar-groups.js +127 -6
- package/dist/template-resolver/web/document-tiptap-toolbar.js +57 -15
- package/dist/template-resolver/web/document-toolbar-blocks.d.ts +17 -0
- package/dist/template-resolver/web/i18n.js +21 -0
- package/dist/template-resolver/web/index.d.ts +8 -2
- package/dist/template-resolver/web/index.js +6 -1
- package/dist/template-resolver/web/pdf-constants.d.ts +8 -0
- package/dist/template-resolver/web/pdf-constants.js +9 -0
- package/dist/template-resolver/web/placeholder-text.d.ts +2 -1
- package/dist/template-resolver/web/placeholder-text.js +5 -2
- package/dist/template-resolver/web/qualified-names.d.ts +7 -0
- package/dist/template-resolver/web/qualified-names.js +6 -0
- package/dist/template-resolver/web/use-document-tiptap-model.d.ts +6 -2
- package/dist/template-resolver/web/use-document-tiptap-model.js +38 -3
- package/dist/tenant/company-config.d.ts +6 -0
- package/dist/tenant/company-config.js +41 -0
- package/dist/tenant/feature.js +11 -0
- package/dist/tenant/i18n.js +14 -0
- package/package.json +24 -11
- package/src/signature/changes.json +6 -0
- package/src/template-resolver/changes.json +62 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { ValidationError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
import { createTemplateResolverApi, TemplateNotFoundError } from "../api.js";
|
|
5
|
+
import { refuseTemplateEdit, templateEditAccess, } from "../template-edit-policy.js";
|
|
6
|
+
import { resolveTemplateVariables } from "../template-validation.js";
|
|
7
|
+
import { DEFAULT_PDF_LAYOUT_SLUG_CONFIG_KEY, FALLBACK_LOCALE, MAX_SELECTED_TEMPLATES, } from "../web/constants.js";
|
|
8
|
+
import { ConfigHandlers, TemplateResolverErrors } from "../web/qualified-names.js";
|
|
9
|
+
import { loadDocumentBase } from "./document-base.js";
|
|
10
|
+
import { failureReason } from "./failure-reason.js";
|
|
11
|
+
import { localeSchema, templateSlugSchema } from "./shared.js";
|
|
12
|
+
import { writeTemplateRow } from "./template-write.js";
|
|
13
|
+
const templateRefSchema = z.object({ slug: templateSlugSchema, locale: localeSchema });
|
|
14
|
+
async function applyLayoutToDraft(ctx, user, layoutSlug, { slug, locale }) {
|
|
15
|
+
const loaded = await loadDocumentBase(ctx, user, { slug, kind: "document-pdf", locale });
|
|
16
|
+
if (loaded === null)
|
|
17
|
+
return { slug, locale, reason: TemplateResolverErrors.notFound };
|
|
18
|
+
const written = await writeTemplateRow({
|
|
19
|
+
ctx,
|
|
20
|
+
user,
|
|
21
|
+
tenantId: user.tenantId,
|
|
22
|
+
scope: "tenant",
|
|
23
|
+
status: "draft",
|
|
24
|
+
variables: resolveTemplateVariables(ctx.registry, slug, loaded.variableSchema),
|
|
25
|
+
payload: {
|
|
26
|
+
slug,
|
|
27
|
+
kind: "document-pdf",
|
|
28
|
+
locale,
|
|
29
|
+
content: loaded.content,
|
|
30
|
+
contentFormat: "html",
|
|
31
|
+
variableSchema: loaded.variableSchema,
|
|
32
|
+
meta: { ...loaded.meta, layoutSlug },
|
|
33
|
+
title: loaded.title,
|
|
34
|
+
folder: loaded.folder,
|
|
35
|
+
parentTemplateId: loaded.parentTemplateId,
|
|
36
|
+
},
|
|
37
|
+
expectedVersion: loaded.base.scope === "tenant" ? loaded.base.version : null,
|
|
38
|
+
});
|
|
39
|
+
return written.isSuccess ? null : { slug, locale, reason: failureReason(written) };
|
|
40
|
+
}
|
|
41
|
+
export function makeApplyLayoutWrite(policy) {
|
|
42
|
+
return defineWriteHandler({
|
|
43
|
+
name: "apply-layout",
|
|
44
|
+
schema: z.object({
|
|
45
|
+
layoutSlug: templateSlugSchema,
|
|
46
|
+
target: z.discriminatedUnion("mode", [
|
|
47
|
+
z.object({ mode: z.literal("all") }),
|
|
48
|
+
z.object({
|
|
49
|
+
mode: z.literal("selected"),
|
|
50
|
+
templates: z.array(templateRefSchema).min(1).max(MAX_SELECTED_TEMPLATES),
|
|
51
|
+
}),
|
|
52
|
+
]),
|
|
53
|
+
}),
|
|
54
|
+
access: templateEditAccess(policy.tenantEditable),
|
|
55
|
+
description: "Applies a PDF layout to the caller's PDF templates without copying it. Mode all sets the tenant's default PDF layout, which every PDF template that names no layout of its own follows; mode selected saves a tenant draft per listed template and locale that names the layout (text and placeholders stay as they are, a system template gets its own tenant version first) and returns the ones it refused or could not write in skipped with the reason. Nothing is published; use publish-templates for that. The caller needs the right to edit the layout, and for selected also each template. Returns applied and skipped (both empty for mode all).",
|
|
56
|
+
handler: async (event, ctx) => {
|
|
57
|
+
const { layoutSlug, target } = event.payload;
|
|
58
|
+
const refused = refuseTemplateEdit(policy, event.user, {
|
|
59
|
+
slug: layoutSlug,
|
|
60
|
+
kind: "pdf-layout",
|
|
61
|
+
});
|
|
62
|
+
if (refused !== null)
|
|
63
|
+
return refused;
|
|
64
|
+
try {
|
|
65
|
+
await createTemplateResolverApi(ctx.db, ctx.registry).resolveLayout({
|
|
66
|
+
tenantId: event.user.tenantId,
|
|
67
|
+
slug: layoutSlug,
|
|
68
|
+
kind: "pdf-layout",
|
|
69
|
+
locale: FALLBACK_LOCALE,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
if (!(e instanceof TemplateNotFoundError))
|
|
74
|
+
throw e;
|
|
75
|
+
return writeFailure(new ValidationError({
|
|
76
|
+
fields: [
|
|
77
|
+
{
|
|
78
|
+
path: "layoutSlug",
|
|
79
|
+
code: TemplateResolverErrors.layoutNotPublished,
|
|
80
|
+
i18nKey: "templateResolver.errors.layout_not_published",
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
}, { i18nKey: "templateResolver.errors.layout_not_published" }));
|
|
84
|
+
}
|
|
85
|
+
const applied = [];
|
|
86
|
+
const skipped = [];
|
|
87
|
+
if (target.mode === "all") {
|
|
88
|
+
const pointer = await ctx.dispatch.write(ConfigHandlers.set, {
|
|
89
|
+
key: DEFAULT_PDF_LAYOUT_SLUG_CONFIG_KEY,
|
|
90
|
+
value: layoutSlug,
|
|
91
|
+
});
|
|
92
|
+
if (!pointer.isSuccess)
|
|
93
|
+
return pointer;
|
|
94
|
+
return {
|
|
95
|
+
isSuccess: true,
|
|
96
|
+
data: { applied, skipped },
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const seen = new Set();
|
|
100
|
+
for (const ref of target.templates) {
|
|
101
|
+
const key = `${ref.slug}\u0000${ref.locale}`;
|
|
102
|
+
if (seen.has(key))
|
|
103
|
+
continue;
|
|
104
|
+
seen.add(key);
|
|
105
|
+
if (refuseTemplateEdit(policy, event.user, { slug: ref.slug, kind: "document-pdf" })) {
|
|
106
|
+
skipped.push({ ...ref, reason: TemplateResolverErrors.templateNotTenantEditable });
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const skip = await applyLayoutToDraft(ctx, event.user, layoutSlug, ref);
|
|
110
|
+
if (skip === null)
|
|
111
|
+
applied.push(ref);
|
|
112
|
+
else
|
|
113
|
+
skipped.push(skip);
|
|
114
|
+
}
|
|
115
|
+
return { isSuccess: true, data: { applied, skipped } };
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { type TemplateEditPolicy } from "../template-edit-policy.ts";
|
|
3
|
+
export declare function makeCreateLayoutWrite(policy: TemplateEditPolicy): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"create-layout", z.ZodObject<{
|
|
4
|
+
name: z.ZodString;
|
|
5
|
+
title: z.ZodString;
|
|
6
|
+
locale: z.ZodString;
|
|
7
|
+
}, z.core.$strip>, {
|
|
8
|
+
slug: string;
|
|
9
|
+
locale: string;
|
|
10
|
+
kind: "pdf-layout";
|
|
11
|
+
base: {
|
|
12
|
+
scope: "tenant";
|
|
13
|
+
version: number;
|
|
14
|
+
};
|
|
15
|
+
}, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
@@ -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 {top,right,bottom,left in mm}, fontFamily, fontSizePt, textColor, tableStyle, tableHeaderBackground, tableLineColor (#rrggbb, line color of the table style, default #bbb), foldMarks (none|din-a|din-b: DIN 5008 fold and punch marks at the left paper edge on every page, form A folds at 87 and 192 mm, form B at 105 and 210 mm), cornerMarks (boolean, default false; angles of 5 mm legs, 0.5 pt, 10 mm from the paper edge in all four corners of every page), paperColor (#rrggbb when the whole page has a tinted paper background, margins, header and footer included; default white), headerOnFirstPage and footerOnFirstPage (booleans, default true; false prints the header or footer only from page 2, margins stay the same)), 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 {
|
|
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:
|
|
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
|
-
|
|
16
|
+
...documentTargetShape,
|
|
17
17
|
locale: localeSchema,
|
|
18
18
|
}),
|
|
19
19
|
access: { roles: ["TenantAdmin", "SystemAdmin"] },
|
|
20
|
-
description: "Reads a mail
|
|
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:
|
|
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,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
|
|
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
|
|
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
|
|
32
|
+
export function ownLayoutSlugOf(row) {
|
|
32
33
|
const layoutSlug = storedRecord(row.meta)["layoutSlug"];
|
|
33
|
-
return typeof layoutSlug === "string" ? layoutSlug :
|
|
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
|
|
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)
|