@cosmicdrift/kumiko-bundled-features 0.421.0 → 0.424.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/document-draft/entity.d.ts +320 -0
- package/dist/document-draft/entity.js +98 -0
- package/dist/document-draft/feature.d.ts +3 -0
- package/dist/document-draft/feature.js +42 -0
- package/dist/document-draft/handlers/blocks.query.d.ts +28 -0
- package/dist/document-draft/handlers/blocks.query.js +48 -0
- package/dist/document-draft/handlers/by-subject.query.d.ts +16 -0
- package/dist/document-draft/handlers/by-subject.query.js +42 -0
- package/dist/document-draft/handlers/create.write.d.ts +17 -0
- package/dist/document-draft/handlers/create.write.js +83 -0
- package/dist/document-draft/handlers/discard.write.d.ts +8 -0
- package/dist/document-draft/handlers/discard.write.js +27 -0
- package/dist/document-draft/handlers/finalize.write.d.ts +21 -0
- package/dist/document-draft/handlers/finalize.write.js +73 -0
- package/dist/document-draft/handlers/patch.write.d.ts +1420 -0
- package/dist/document-draft/handlers/patch.write.js +65 -0
- package/dist/document-draft/handlers/revise.write.d.ts +12 -0
- package/dist/document-draft/handlers/revise.write.js +39 -0
- package/dist/document-draft/handlers/save.write.d.ts +12 -0
- package/dist/document-draft/handlers/save.write.js +40 -0
- package/dist/document-draft/i18n.d.ts +6 -0
- package/dist/document-draft/i18n.js +20 -0
- package/dist/document-draft/index.d.ts +5 -0
- package/dist/document-draft/index.js +3 -0
- package/dist/document-draft/lib.d.ts +13 -0
- package/dist/document-draft/lib.js +42 -0
- package/dist/document-draft/options.d.ts +35 -0
- package/dist/document-draft/options.js +1 -0
- package/dist/document-draft/payloads.d.ts +1440 -0
- package/dist/document-draft/payloads.js +35 -0
- package/dist/document-draft/screens/editor-screen.d.ts +3 -0
- package/dist/document-draft/screens/editor-screen.js +13 -0
- package/dist/document-draft/user-data.d.ts +13 -0
- package/dist/document-draft/user-data.js +43 -0
- package/dist/document-draft/web/client-plugin.d.ts +7 -0
- package/dist/document-draft/web/client-plugin.js +13 -0
- package/dist/document-draft/web/constants.d.ts +41 -0
- package/dist/document-draft/web/constants.js +42 -0
- package/dist/document-draft/web/editor-screen.d.ts +2 -0
- package/dist/document-draft/web/editor-screen.js +181 -0
- package/dist/document-draft/web/i18n.d.ts +99 -0
- package/dist/document-draft/web/i18n.js +52 -0
- package/dist/document-draft/web/index.d.ts +3 -0
- package/dist/document-draft/web/index.js +3 -0
- package/dist/shared/index.d.ts +1 -0
- package/dist/shared/index.js +1 -0
- package/dist/shared/subject-scope.d.ts +21 -0
- package/dist/signature/feature.js +1 -1
- package/dist/signature/handlers/cancel.write.d.ts +1 -1
- package/dist/signature/handlers/issue-link.write.d.ts +1 -1
- package/dist/signature/handlers/request.write.d.ts +1 -1
- package/dist/signature/handlers/revoke-link.write.d.ts +1 -1
- package/dist/signature/handlers/sign-in-person.write.d.ts +1 -1
- package/dist/signature/options.d.ts +2 -1
- package/dist/signature/subject-queries.d.ts +1 -1
- package/dist/template-resolver/api.d.ts +3 -1
- package/dist/template-resolver/boot-rules.d.ts +5 -0
- package/dist/template-resolver/boot-rules.js +5 -0
- package/dist/template-resolver/compile-document-body.d.ts +11 -1
- package/dist/template-resolver/compile-document-body.js +42 -5
- package/dist/template-resolver/document-print-only.d.ts +1 -1
- package/dist/template-resolver/document-print-only.js +1 -0
- package/dist/template-resolver/feature.d.ts +2 -0
- package/dist/template-resolver/feature.js +15 -1
- package/dist/template-resolver/handlers/create-variant.write.d.ts +17 -0
- package/dist/template-resolver/handlers/create-variant.write.js +112 -0
- package/dist/template-resolver/handlers/document-base.d.ts +1 -0
- package/dist/template-resolver/handlers/document-base.js +2 -0
- package/dist/template-resolver/handlers/overview.query.d.ts +6 -0
- package/dist/template-resolver/handlers/overview.query.js +29 -3
- package/dist/template-resolver/handlers/patch-draft.write.js +9 -1
- package/dist/template-resolver/handlers/template-write.d.ts +8 -2
- package/dist/template-resolver/handlers/template-write.js +87 -12
- package/dist/template-resolver/handlers/toggle-status.write.js +26 -5
- package/dist/template-resolver/handlers/upsert-tenant.write.js +1 -0
- package/dist/template-resolver/handlers/variants.query.d.ts +11 -0
- package/dist/template-resolver/handlers/variants.query.js +62 -0
- package/dist/template-resolver/i18n.js +1 -0
- package/dist/template-resolver/index.d.ts +6 -2
- package/dist/template-resolver/index.js +6 -2
- package/dist/template-resolver/seeding.d.ts +2 -0
- package/dist/template-resolver/seeding.js +12 -1
- package/dist/template-resolver/template-delivery-policy.d.ts +13 -4
- package/dist/template-resolver/template-delivery-policy.js +38 -2
- package/dist/template-resolver/template-edit-policy.d.ts +3 -0
- package/dist/template-resolver/template-edit-policy.js +2 -2
- package/dist/template-resolver/template-meta.d.ts +1 -0
- package/dist/template-resolver/template-meta.js +1 -0
- package/dist/template-resolver/template-variants.d.ts +11 -0
- package/dist/template-resolver/template-variants.js +43 -0
- package/dist/template-resolver/web/document-images.d.ts +3 -0
- package/dist/template-resolver/web/document-images.js +25 -8
- package/dist/template-resolver/web/document-tiptap-content.js +7 -2
- package/dist/template-resolver/web/document-tiptap-toolbar.js +8 -4
- package/dist/template-resolver/web/i18n.js +22 -0
- package/dist/template-resolver/web/index.d.ts +1 -0
- package/dist/template-resolver/web/index.js +1 -0
- package/dist/template-resolver/web/qualified-names.d.ts +5 -0
- package/dist/template-resolver/web/qualified-names.js +5 -0
- package/dist/template-resolver/web/system-placeholders.d.ts +4 -0
- package/dist/template-resolver/web/system-placeholders.js +13 -0
- package/dist/tenant/handlers/update-member-roles.write.js +1 -1
- package/dist/tenant/i18n.js +0 -1
- package/dist/tenant-inactivity/constants.d.ts +1 -1
- package/dist/tenant-inactivity/constants.js +1 -1
- package/dist/tenant-lifecycle/constants.d.ts +1 -1
- package/dist/tenant-lifecycle/constants.js +1 -1
- package/package.json +27 -11
- package/src/document-draft/changes.json +8 -0
- package/src/helpdesk/changes.json +6 -0
- package/src/signature/changes.json +6 -0
- package/src/template-resolver/changes.json +36 -0
- package/src/tenant/changes.json +7 -0
- package/src/tenant-inactivity/changes.json +7 -0
- package/src/tenant-lifecycle/changes.json +7 -0
- package/dist/signature/subject-scope.d.ts +0 -11
- /package/dist/{signature → shared}/subject-scope.js +0 -0
|
@@ -1,14 +1,20 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
1
2
|
import { fetchOne } from "@cosmicdrift/kumiko-framework/db";
|
|
2
3
|
import { SYSTEM_TENANT_ID, } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
4
|
import { AccessDeniedError, ValidationError, VersionConflictError, writeFailure, } from "@cosmicdrift/kumiko-framework/errors";
|
|
5
|
+
import { isPlainObject, propertyOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
4
6
|
import { requireForTenant } from "../../shared/index.js";
|
|
7
|
+
import { patchIssue } from "../document-op-blocks.js";
|
|
5
8
|
import { templateResourcesTable } from "../table.js";
|
|
6
|
-
import {
|
|
9
|
+
import { deliveryPolicyOf, findVariantsEntry } from "../template-delivery-policy.js";
|
|
10
|
+
import { activationChanges, serializeTemplateDraft, toWorkingRow } from "../template-draft.js";
|
|
7
11
|
import { refuseReservedSystemLayoutSlug } from "../template-edit-policy.js";
|
|
8
12
|
import { isSystemOnlyTemplate } from "../template-system-only.js";
|
|
13
|
+
import { resolveTemplateVariables } from "../template-validation.js";
|
|
14
|
+
import { readVariantOf, VARIANT_KIND, variantOfMatchesSlug } from "../template-variants.js";
|
|
9
15
|
import { TemplateResolverErrors } from "../web/qualified-names.js";
|
|
10
16
|
import { executor } from "./shared.js";
|
|
11
|
-
import { evaluateTemplateWrite, } from "./template-gates.js";
|
|
17
|
+
import { evaluateTemplateWrite, parseStoredJson, } from "./template-gates.js";
|
|
12
18
|
// The tenant-scope-under-system-tenant ban lives here so no write path can
|
|
13
19
|
// create the inconsistent state the resolver does not model.
|
|
14
20
|
export function refuseTenantScopeUnderSystemTenant(scope, tenantId) {
|
|
@@ -77,8 +83,74 @@ function versionConflictFor(expectedVersion, existing, slug) {
|
|
|
77
83
|
currentVersion: existing?.version ?? 0,
|
|
78
84
|
}));
|
|
79
85
|
}
|
|
86
|
+
// A tenant row under `<base>:<name>` of a base with variants must declare that base; otherwise the
|
|
87
|
+
// policy would serve it as a variant while schema sync never runs for it.
|
|
88
|
+
function refuseVariantOfSlugMismatch(registry, scope, payload) {
|
|
89
|
+
const declaredBase = readVariantOf(payload.meta);
|
|
90
|
+
const requiredBase = scope === "tenant"
|
|
91
|
+
? findVariantsEntry(deliveryPolicyOf(registry).tenantEditable, payload)?.slug
|
|
92
|
+
: undefined;
|
|
93
|
+
const missesRequiredBase = requiredBase !== undefined && declaredBase !== requiredBase;
|
|
94
|
+
if (!missesRequiredBase && variantOfMatchesSlug(payload.slug, payload.meta))
|
|
95
|
+
return null;
|
|
96
|
+
return writeFailure(new ValidationError({ fields: [patchIssue("meta.variantOf", "invalid_variant_of")] }));
|
|
97
|
+
}
|
|
98
|
+
function storedKeyOf(data) {
|
|
99
|
+
const id = propertyOf(data, "id");
|
|
100
|
+
const version = propertyOf(propertyOf(data, "data"), "version");
|
|
101
|
+
if ((typeof id !== "string" && typeof id !== "number") || typeof version !== "number") {
|
|
102
|
+
throw new Error("template write: executor result carries no id and version");
|
|
103
|
+
}
|
|
104
|
+
return { id: String(id), version };
|
|
105
|
+
}
|
|
106
|
+
// The base as the tenant sees it: its own row (open draft included), else the served system default.
|
|
107
|
+
export async function baseVariableSchemaOf(db, tenantId, baseSlug, locale) {
|
|
108
|
+
const where = { slug: baseSlug, kind: VARIANT_KIND, locale };
|
|
109
|
+
const own = await fetchOne(db, templateResourcesTable, {
|
|
110
|
+
...where,
|
|
111
|
+
tenantId,
|
|
112
|
+
});
|
|
113
|
+
if (own !== undefined)
|
|
114
|
+
return parseStoredJson(toWorkingRow(own, tenantId).variableSchema);
|
|
115
|
+
const system = await fetchOne(db, templateResourcesTable, {
|
|
116
|
+
...where,
|
|
117
|
+
tenantId: SYSTEM_TENANT_ID,
|
|
118
|
+
scope: "system",
|
|
119
|
+
status: "active",
|
|
120
|
+
});
|
|
121
|
+
return system === undefined ? undefined : parseStoredJson(system.variableSchema);
|
|
122
|
+
}
|
|
123
|
+
// A variant follows its base: a write that sends no schema of its own stores the base's current one,
|
|
124
|
+
// so a base that gains a variable does not lock its variants. A caller that sends a different
|
|
125
|
+
// schema explicitly is refused.
|
|
126
|
+
async function syncVariantSchema(args, db) {
|
|
127
|
+
const { payload } = args;
|
|
128
|
+
const baseSlug = readVariantOf(payload.meta);
|
|
129
|
+
const unchanged = { payload, variables: args.variables };
|
|
130
|
+
if (args.scope !== "tenant" || baseSlug === null)
|
|
131
|
+
return unchanged;
|
|
132
|
+
const baseSchema = await baseVariableSchemaOf(db, args.tenantId, baseSlug, payload.locale);
|
|
133
|
+
if (!isPlainObject(baseSchema))
|
|
134
|
+
return unchanged;
|
|
135
|
+
if (args.explicitVariableSchema === true) {
|
|
136
|
+
if (isDeepStrictEqual(baseSchema, payload.variableSchema))
|
|
137
|
+
return unchanged;
|
|
138
|
+
return {
|
|
139
|
+
refused: writeFailure(new ValidationError({
|
|
140
|
+
fields: [
|
|
141
|
+
patchIssue("variableSchema", "variables_inherited_from_base", { slug: baseSlug }),
|
|
142
|
+
],
|
|
143
|
+
})),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
payload: { ...payload, variableSchema: baseSchema },
|
|
148
|
+
variables: resolveTemplateVariables(args.ctx.registry, payload.slug, baseSchema),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
80
151
|
function refuseScopeViolation(registry, scope, tenantId, payload) {
|
|
81
|
-
return (
|
|
152
|
+
return (refuseVariantOfSlugMismatch(registry, scope, payload) ??
|
|
153
|
+
refuseTenantScopeUnderSystemTenant(scope, tenantId) ??
|
|
82
154
|
refuseReservedSystemLayoutSlug(scope, payload, "slug") ??
|
|
83
155
|
refuseSystemOnlyTenantOverride(registry, scope, payload.slug, payload.kind));
|
|
84
156
|
}
|
|
@@ -87,18 +159,22 @@ function refuseScopeViolation(registry, scope, tenantId, payload) {
|
|
|
87
159
|
// executor. linkedResources is never part of an update; resources are attached
|
|
88
160
|
// separately.
|
|
89
161
|
export async function writeTemplateRow(args) {
|
|
90
|
-
const { ctx, user, tenantId, scope, status
|
|
91
|
-
const refused = refuseScopeViolation(ctx.registry, scope, tenantId, payload);
|
|
162
|
+
const { ctx, user, tenantId, scope, status } = args;
|
|
163
|
+
const refused = refuseScopeViolation(ctx.registry, scope, tenantId, args.payload);
|
|
92
164
|
if (refused !== null)
|
|
93
165
|
return refused;
|
|
94
166
|
const { db, streamTenantId } = requireForTenant(ctx, tenantId);
|
|
167
|
+
const synced = await syncVariantSchema(args, db);
|
|
168
|
+
if ("refused" in synced)
|
|
169
|
+
return synced.refused;
|
|
170
|
+
const { payload, variables } = synced;
|
|
95
171
|
const gated = gateTemplateWrite({
|
|
96
172
|
kind: payload.kind,
|
|
97
173
|
scope,
|
|
98
174
|
contentFormat: payload.contentFormat,
|
|
99
175
|
content: payload.content,
|
|
100
176
|
rawMeta: payload.meta,
|
|
101
|
-
variables
|
|
177
|
+
variables,
|
|
102
178
|
}, status);
|
|
103
179
|
if (!gated.ok)
|
|
104
180
|
return gated.failure;
|
|
@@ -138,6 +214,7 @@ export async function writeTemplateRow(args) {
|
|
|
138
214
|
isSuccess: true,
|
|
139
215
|
data: {
|
|
140
216
|
id: String(existing.id),
|
|
217
|
+
version: storedKeyOf(result.data).version,
|
|
141
218
|
slug: payload.slug,
|
|
142
219
|
isNew: false,
|
|
143
220
|
validation: evaluated.validation,
|
|
@@ -146,21 +223,19 @@ export async function writeTemplateRow(args) {
|
|
|
146
223
|
}
|
|
147
224
|
const result = await executor.create({
|
|
148
225
|
...fields,
|
|
149
|
-
linkedResources: "{}",
|
|
226
|
+
linkedResources: args.linkedResources ?? "{}",
|
|
150
227
|
tenantId,
|
|
151
228
|
draft: null,
|
|
152
229
|
...(status === "active" ? activationChanges(null) : {}),
|
|
153
230
|
}, user, db, { streamTenantId });
|
|
154
231
|
if (!result.isSuccess)
|
|
155
232
|
return result;
|
|
156
|
-
|
|
157
|
-
// INSERT RETURNING; the { id } shape is guaranteed because the primary key is in the
|
|
158
|
-
// RETURNING clause.
|
|
159
|
-
const createdRow = result.data;
|
|
233
|
+
const created = storedKeyOf(result.data);
|
|
160
234
|
return {
|
|
161
235
|
isSuccess: true,
|
|
162
236
|
data: {
|
|
163
|
-
id:
|
|
237
|
+
id: created.id,
|
|
238
|
+
version: created.version,
|
|
164
239
|
slug: payload.slug,
|
|
165
240
|
isNew: true,
|
|
166
241
|
validation: evaluated.validation,
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { fetchOne } from "@cosmicdrift/kumiko-framework/db";
|
|
2
2
|
import { defineWriteHandler, SYSTEM_TENANT_ID, } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
3
|
import { NotFoundError, ValidationError, writeFailure, } from "@cosmicdrift/kumiko-framework/errors";
|
|
4
|
+
import { isPlainObject } from "@cosmicdrift/kumiko-types/type-guards";
|
|
4
5
|
import * as z from "zod";
|
|
5
6
|
import { requireForTenant } from "../../shared/index.js";
|
|
6
7
|
import { templateResourcesTable } from "../table.js";
|
|
7
8
|
import { activationChanges, parseTemplateDraft, toWorkingRow } from "../template-draft.js";
|
|
8
9
|
import { refuseMissingTemplate, refuseTemplateEdit, templateEditAccess, } from "../template-edit-policy.js";
|
|
10
|
+
import { readVariantOf } from "../template-variants.js";
|
|
9
11
|
import { executor } from "./shared.js";
|
|
10
|
-
import { publishGateIssues } from "./template-gates.js";
|
|
12
|
+
import { publishGateIssues, storedRecord } from "./template-gates.js";
|
|
13
|
+
import { baseVariableSchemaOf } from "./template-write.js";
|
|
11
14
|
function isRowOfTarget(row, target, callerTenantId) {
|
|
12
15
|
return target === "system"
|
|
13
16
|
? row.tenantId === SYSTEM_TENANT_ID && row.scope === "system"
|
|
@@ -28,18 +31,36 @@ export async function loadTargetRow(ctx, target, user, id) {
|
|
|
28
31
|
}
|
|
29
32
|
// Publishing promotes an open draft (gated as the version that goes live); a row
|
|
30
33
|
// without one is activated as stored. Archiving leaves the draft standing.
|
|
31
|
-
function planStatusChange(ctx, row, status) {
|
|
34
|
+
function planStatusChange(ctx, row, status, baseSchema) {
|
|
32
35
|
if (status === "archived")
|
|
33
36
|
return { ok: true, changes: { status } };
|
|
34
|
-
const
|
|
37
|
+
const syncedSchema = baseSchema === undefined ? {} : { variableSchema: JSON.stringify(baseSchema) };
|
|
38
|
+
const issues = publishGateIssues(ctx.registry, {
|
|
39
|
+
...toWorkingRow(row, row.tenantId),
|
|
40
|
+
...syncedSchema,
|
|
41
|
+
});
|
|
35
42
|
if (issues.length > 0) {
|
|
36
43
|
return { ok: false, failure: writeFailure(new ValidationError({ fields: [...issues] })) };
|
|
37
44
|
}
|
|
38
|
-
return {
|
|
45
|
+
return {
|
|
46
|
+
ok: true,
|
|
47
|
+
changes: { ...parseTemplateDraft(row.draft), ...syncedSchema, ...activationChanges(row) },
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
// A variant goes live with the base's current variables, so a base that gained a variable is gated and served consistently.
|
|
51
|
+
async function baseSchemaForPublish(db, row) {
|
|
52
|
+
if (row.scope !== "tenant")
|
|
53
|
+
return undefined;
|
|
54
|
+
const baseSlug = readVariantOf(storedRecord(toWorkingRow(row, row.tenantId).meta));
|
|
55
|
+
if (baseSlug === null)
|
|
56
|
+
return undefined;
|
|
57
|
+
const schema = await baseVariableSchemaOf(db, row.tenantId, baseSlug, row.locale);
|
|
58
|
+
return isPlainObject(schema) ? schema : undefined;
|
|
39
59
|
}
|
|
40
60
|
// The gated status change every publish and archive path shares.
|
|
41
61
|
export async function changeRowStatus(ctx, user, target, row, status) {
|
|
42
|
-
const
|
|
62
|
+
const baseSchema = status === "active" ? await baseSchemaForPublish(target.db, row) : undefined;
|
|
63
|
+
const plan = planStatusChange(ctx, row, status, baseSchema);
|
|
43
64
|
if (!plan.ok)
|
|
44
65
|
return plan.failure;
|
|
45
66
|
const result = await executor.update({ id: row.id, version: row.version, changes: plan.changes }, user, target.db, target.scoped ? { streamTenantId: target.scoped.streamTenantId } : undefined);
|
|
@@ -33,6 +33,7 @@ export function makeUpsertTenantWrite(policy) {
|
|
|
33
33
|
status: event.payload.status,
|
|
34
34
|
variables: resolveTemplateVariables(ctx.registry, event.payload.slug, event.payload.variableSchema),
|
|
35
35
|
payload: event.payload,
|
|
36
|
+
explicitVariableSchema: true,
|
|
36
37
|
});
|
|
37
38
|
},
|
|
38
39
|
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
export type VariantSummary = {
|
|
3
|
+
readonly slug: string;
|
|
4
|
+
readonly title: string | null;
|
|
5
|
+
readonly scope: "system" | "tenant";
|
|
6
|
+
readonly locale: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const variantsQuery: import("@cosmicdrift/kumiko-types/define-handler").QueryHandlerDefinition<"variants", z.ZodObject<{
|
|
9
|
+
baseSlug: z.ZodString;
|
|
10
|
+
locale: z.ZodString;
|
|
11
|
+
}, z.core.$strip>, VariantSummary[], import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { selectMany } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import { defineQueryHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
import { templateResourcesTable } from "../table.js";
|
|
5
|
+
import { deliveryPolicyOf, isRowServed } from "../template-delivery-policy.js";
|
|
6
|
+
import { isVariantSlugOf, readVariantOf, VARIANT_KIND, VARIANT_SEPARATOR, } from "../template-variants.js";
|
|
7
|
+
import { ACTIVE_TEMPLATE_STATUS, FALLBACK_LOCALE, SYSTEM_TENANT_ID } from "../web/constants.js";
|
|
8
|
+
import { localeSchema, templateSlugSchema } from "./shared.js";
|
|
9
|
+
import { parseStoredJson } from "./template-gates.js";
|
|
10
|
+
const VARIANT_ROW_LIMIT = 500;
|
|
11
|
+
// Tenant rows win over system rows of the same slug, the requested locale over the fallback locale.
|
|
12
|
+
function precedence(row, locale) {
|
|
13
|
+
return (row.tenantId === SYSTEM_TENANT_ID ? 1 : 0) + (row.locale === locale ? 0 : 2);
|
|
14
|
+
}
|
|
15
|
+
export const variantsQuery = defineQueryHandler({
|
|
16
|
+
name: "variants",
|
|
17
|
+
schema: z.object({ baseSlug: templateSlugSchema, locale: localeSchema }),
|
|
18
|
+
access: {
|
|
19
|
+
openToAll: {
|
|
20
|
+
reason: "lists only slug, title, scope and locale of the variants the caller's own tenant can print; " +
|
|
21
|
+
"whoever may create a document from the base template must be able to pick a variant, " +
|
|
22
|
+
"and the app's role names are unknown here",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
description: "Lists the active variants of a PDF document template (kind document-pdf, slug <baseSlug>:<name>) that the caller's tenant can use: the app's system variants and the tenant's own, each with slug, title (text or a translation key), scope (system or tenant) and locale. A tenant variant replaces a system variant of the same slug. Only published variants appear, and tenant variants only while the app still allows variants for the base template. Pass a returned slug as the variant when rendering the document.",
|
|
26
|
+
handler: async (query, ctx) => {
|
|
27
|
+
const { baseSlug, locale } = query.payload;
|
|
28
|
+
const locales = locale === FALLBACK_LOCALE ? [locale] : [locale, FALLBACK_LOCALE];
|
|
29
|
+
const rows = [];
|
|
30
|
+
for (const rowLocale of locales) {
|
|
31
|
+
const found = await selectMany(ctx.db, templateResourcesTable, {
|
|
32
|
+
kind: VARIANT_KIND,
|
|
33
|
+
status: ACTIVE_TEMPLATE_STATUS,
|
|
34
|
+
locale: rowLocale,
|
|
35
|
+
tenantId: { in: [query.user.tenantId, SYSTEM_TENANT_ID] },
|
|
36
|
+
// Slugs are kebab segments joined by ":", so the base slug holds no LIKE wildcard.
|
|
37
|
+
slug: { like: `${baseSlug}${VARIANT_SEPARATOR}%` },
|
|
38
|
+
}, { limit: VARIANT_ROW_LIMIT });
|
|
39
|
+
rows.push(...found);
|
|
40
|
+
}
|
|
41
|
+
// Delivery does not depend on the caller: a SystemAdmin sees the same list as the tenant's users.
|
|
42
|
+
const deliveryPolicy = deliveryPolicyOf(ctx.registry);
|
|
43
|
+
const delivered = rows.filter((row) => isVariantSlugOf(row.slug, baseSlug) &&
|
|
44
|
+
readVariantOf(parseStoredJson(row.meta)) === baseSlug &&
|
|
45
|
+
isRowServed(deliveryPolicy, row));
|
|
46
|
+
const best = new Map();
|
|
47
|
+
for (const row of delivered) {
|
|
48
|
+
const current = best.get(row.slug);
|
|
49
|
+
if (current === undefined || precedence(row, locale) < precedence(current, locale)) {
|
|
50
|
+
best.set(row.slug, row);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return [...best.values()]
|
|
54
|
+
.map((row) => ({
|
|
55
|
+
slug: row.slug,
|
|
56
|
+
title: row.title,
|
|
57
|
+
scope: row.tenantId === SYSTEM_TENANT_ID ? "system" : "tenant",
|
|
58
|
+
locale: row.locale,
|
|
59
|
+
}))
|
|
60
|
+
.sort((a, b) => a.slug.localeCompare(b.slug));
|
|
61
|
+
},
|
|
62
|
+
});
|
|
@@ -24,4 +24,5 @@ export const TEMPLATE_RESOLVER_I18N = {
|
|
|
24
24
|
[handlerTitleKey(H.applyLayout)]: { en: "Apply PDF layout" },
|
|
25
25
|
[handlerTitleKey(H.createLayout)]: { en: "Create PDF layout" },
|
|
26
26
|
[handlerTitleKey(H.publishTemplates)]: { en: "Publish several templates" },
|
|
27
|
+
[handlerTitleKey(H.createVariant)]: { en: "Create PDF template variant" },
|
|
27
28
|
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export { isResourceImageContentType } from "../shared/index.ts";
|
|
2
2
|
export { createTemplateResolverApi, type ResolveRequest, requireTemplateResolver, TemplateNotFoundError, type TemplateResolverApi, type TemplateResource, } from "./api.ts";
|
|
3
|
-
export { type CompileDocumentBodyOptions, collectDocumentImageVariables, collectDocumentResourceKeys, compileDocumentBodyToHandlebars, toResourceDataUri, } from "./compile-document-body.ts";
|
|
3
|
+
export { type CompileDocumentBodyOptions, collectDocumentImageVariables, collectDocumentResourceKeys, compileDocumentBodyToHandlebars, type DocumentImageEntry, type DocumentImageValue, type DocumentRecordImage, documentImageEntriesOf, toResourceDataUri, } from "./compile-document-body.ts";
|
|
4
4
|
export { flattenAddressSenderLine, flattenFramesForPreview, readTopLevelFrames, } from "./document-frames.ts";
|
|
5
|
-
export {
|
|
5
|
+
export { allowedBlocksFor } from "./document-op-blocks.ts";
|
|
6
|
+
export { applyDocumentOps, type DocumentOp, documentBlocksOf, documentOpsSchema, } from "./document-ops.ts";
|
|
7
|
+
export { type DocumentViolation, type DocumentViolationCode, documentViolationsToFieldIssues, flattenParagraphsToLineBreaks, parseDocumentJson, type SanitizeBodyResult, type SanitizeHtmlResult, sanitizeDocumentHtml, sanitizeTemplateBody, serializeDocumentJson, } from "./document-sanitizer.ts";
|
|
6
8
|
export { createTemplateResolverFeature, type TemplateResolverOptions } from "./feature.ts";
|
|
7
9
|
export type { LayoutSourceResponse, LayoutSourceRow } from "./handlers/layout-source.query.ts";
|
|
8
10
|
export type { OverviewKindGroup, OverviewOrigin, OverviewResponse, OverviewRow, OverviewUsage, } from "./handlers/overview.query.ts";
|
|
@@ -17,6 +19,7 @@ export { type PreparedTemplateImage, prepareTemplateResourceBeforeTransaction, r
|
|
|
17
19
|
export { type TemplateResourceRow, templateResourceEntity, templateResourcesTable, } from "./table.ts";
|
|
18
20
|
export { type TemplateBundle, templateBundleSchema } from "./template-bundle.ts";
|
|
19
21
|
export type { TemplateDeliveryPolicy } from "./template-delivery-policy.ts";
|
|
22
|
+
export { diffTemplateBlocks, splitTemplateBlocks, type TemplateDiffBlock, } from "./template-diff.ts";
|
|
20
23
|
export { type TemplateEditPolicy, type TenantEditableTemplate, templateEditAccess, } from "./template-edit-policy.ts";
|
|
21
24
|
export { isMailTemplateMeta, type LayoutTemplateMeta, MAIL_CATEGORIES, MAIL_TONES, type MailCategory, type MailLayoutMeta, type MailTemplateMeta, type MailTone, type PdfLayoutMeta, type PdfTemplateMeta, parseTemplateMeta, pdfAttachmentFileNamePatternSchema, readMailLayoutMeta, readPdfLayoutMeta, readPdfTemplateMeta, type TemplateMeta, type TemplateMetaIssue, type TemplateMetaResult, templateSlugSchema, } from "./template-meta.ts";
|
|
22
25
|
export { type DocumentDraftOutcome, evaluateDocumentDraft } from "./template-preview.ts";
|
|
@@ -24,6 +27,7 @@ export { readTemplateResource, type TemplateResourceFile } from "./template-reso
|
|
|
24
27
|
export { isSystemOnlyTemplate } from "./template-system-only.ts";
|
|
25
28
|
export { collectTemplateUsage, type TemplateUsageEntry } from "./template-usage.ts";
|
|
26
29
|
export { hasPlaceholderProblems, resolveTemplateVariables, SYSTEM_PLACEHOLDERS, systemPlaceholdersFor, type TemplateValidation, validateTemplatePlaceholders, } from "./template-validation.ts";
|
|
30
|
+
export { isVariantSlugOf, readVariantOf, VARIANT_NAME_MAX_LENGTH, VARIANT_UNKNOWN_REASON, variantNameSchema, variantSlug, } from "./template-variants.ts";
|
|
27
31
|
export { type UserContentEntryRow, userContentEntriesTable, userContentEntryEntity, } from "./user-content-table.ts";
|
|
28
32
|
export { COMPANY_PLACEHOLDERS, CONTENT_FORMATS, type ContentFormat, DEFAULT_MAIL_LAYOUT_SLUG, DEFAULT_PDF_LAYOUT_SLUG, DEFAULT_PDF_LAYOUT_SLUG_CONFIG_KEY, DOCUMENT_HTML_KINDS, type DocumentHtmlKind, FALLBACK_LOCALE, NON_RENDER_KINDS, type NonRenderKind, PDF_CORNER_MARK_INSET_MM, PDF_CORNER_MARK_LEG_MM, PDF_DEFAULT_FONT_SIZE_PT, PDF_DEFAULT_MARGIN_MM, PDF_FOLD_MARKS, PDF_FOOTER_MARGIN_MM, PDF_HEADER_MARGIN_MM, PDF_ORIENTATIONS, PDF_PAGE_FORMATS, PDF_TABLE_STYLES, type PdfFoldMarks, type PdfOrientation, type PdfPageFormat, type PdfTableStyle, RENDER_KINDS, type RenderKind, SYSTEM_TENANT_ID, TEMPLATE_KINDS, TEMPLATE_SCOPES, TEMPLATE_STATUSES, TENANT_LAYOUT_SLUG_PREFIX, TEXT_BLOCK_KIND, type TemplateKind, type TemplateScope, type TemplateStatus, UPSERT_KINDS, type UpsertKind, } from "./web/constants.ts";
|
|
29
33
|
export { DOCUMENT_FONT_LABELS, DOCUMENT_FONT_PRINT_STACKS, DOCUMENT_FONTS, type DocumentFont, } from "./web/document-fonts.ts";
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export { isResourceImageContentType } from "../shared/index.js";
|
|
2
2
|
export { createTemplateResolverApi, requireTemplateResolver, TemplateNotFoundError, } from "./api.js";
|
|
3
|
-
export { collectDocumentImageVariables, collectDocumentResourceKeys, compileDocumentBodyToHandlebars, toResourceDataUri, } from "./compile-document-body.js";
|
|
3
|
+
export { collectDocumentImageVariables, collectDocumentResourceKeys, compileDocumentBodyToHandlebars, documentImageEntriesOf, toResourceDataUri, } from "./compile-document-body.js";
|
|
4
4
|
export { flattenAddressSenderLine, flattenFramesForPreview, readTopLevelFrames, } from "./document-frames.js";
|
|
5
|
-
export {
|
|
5
|
+
export { allowedBlocksFor } from "./document-op-blocks.js";
|
|
6
|
+
export { applyDocumentOps, documentBlocksOf, documentOpsSchema, } from "./document-ops.js";
|
|
7
|
+
export { documentViolationsToFieldIssues, flattenParagraphsToLineBreaks, parseDocumentJson, sanitizeDocumentHtml, sanitizeTemplateBody, serializeDocumentJson, } from "./document-sanitizer.js";
|
|
6
8
|
export { createTemplateResolverFeature } from "./feature.js";
|
|
7
9
|
export { localeSchema } from "./handlers/shared.js";
|
|
8
10
|
export { companyConfigKey, defaultPdfLayoutSlug, effectivePdfLayout, readTenantConfigText, resolveDefaultPdfLayout, } from "./handlers/tenant-settings.js";
|
|
@@ -14,6 +16,7 @@ export { mapRecordToVariables } from "./record-mapping.js";
|
|
|
14
16
|
export { prepareTemplateResourceBeforeTransaction, readTemplateResourceMaxBytes, storePreparedTemplateResource, } from "./store-template-resource.js";
|
|
15
17
|
export { templateResourceEntity, templateResourcesTable, } from "./table.js";
|
|
16
18
|
export { templateBundleSchema } from "./template-bundle.js";
|
|
19
|
+
export { diffTemplateBlocks, splitTemplateBlocks, } from "./template-diff.js";
|
|
17
20
|
export { templateEditAccess, } from "./template-edit-policy.js";
|
|
18
21
|
export { isMailTemplateMeta, MAIL_CATEGORIES, MAIL_TONES, parseTemplateMeta, pdfAttachmentFileNamePatternSchema, readMailLayoutMeta, readPdfLayoutMeta, readPdfTemplateMeta, templateSlugSchema, } from "./template-meta.js";
|
|
19
22
|
export { evaluateDocumentDraft } from "./template-preview.js";
|
|
@@ -21,6 +24,7 @@ export { readTemplateResource } from "./template-resources.js";
|
|
|
21
24
|
export { isSystemOnlyTemplate } from "./template-system-only.js";
|
|
22
25
|
export { collectTemplateUsage } from "./template-usage.js";
|
|
23
26
|
export { hasPlaceholderProblems, resolveTemplateVariables, SYSTEM_PLACEHOLDERS, systemPlaceholdersFor, validateTemplatePlaceholders, } from "./template-validation.js";
|
|
27
|
+
export { isVariantSlugOf, readVariantOf, VARIANT_NAME_MAX_LENGTH, VARIANT_UNKNOWN_REASON, variantNameSchema, variantSlug, } from "./template-variants.js";
|
|
24
28
|
export { userContentEntriesTable, userContentEntryEntity, } from "./user-content-table.js";
|
|
25
29
|
export { COMPANY_PLACEHOLDERS, CONTENT_FORMATS, DEFAULT_MAIL_LAYOUT_SLUG, DEFAULT_PDF_LAYOUT_SLUG, DEFAULT_PDF_LAYOUT_SLUG_CONFIG_KEY, DOCUMENT_HTML_KINDS, FALLBACK_LOCALE, NON_RENDER_KINDS, PDF_CORNER_MARK_INSET_MM, PDF_CORNER_MARK_LEG_MM, PDF_DEFAULT_FONT_SIZE_PT, PDF_DEFAULT_MARGIN_MM, PDF_FOLD_MARKS, PDF_FOOTER_MARGIN_MM, PDF_HEADER_MARGIN_MM, PDF_ORIENTATIONS, PDF_PAGE_FORMATS, PDF_TABLE_STYLES, RENDER_KINDS, SYSTEM_TENANT_ID, TEMPLATE_KINDS, TEMPLATE_SCOPES, TEMPLATE_STATUSES, TENANT_LAYOUT_SLUG_PREFIX, TEXT_BLOCK_KIND, UPSERT_KINDS, } from "./web/constants.js";
|
|
26
30
|
export { DOCUMENT_FONT_LABELS, DOCUMENT_FONT_PRINT_STACKS, DOCUMENT_FONTS, } from "./web/document-fonts.js";
|
|
@@ -15,6 +15,8 @@ export type SeedSystemTemplateOptions = {
|
|
|
15
15
|
* system admin uploaded through resource-upload. */
|
|
16
16
|
readonly linkedResources?: Record<string, string>;
|
|
17
17
|
readonly meta?: unknown;
|
|
18
|
+
/** Seeds the template as a variant of this base slug (kind `document-pdf`, slug `<variantOf>:<name>`); the app must also list the base with `variants: true` in tenantEditable for tenants to get their own. */
|
|
19
|
+
readonly variantOf?: string;
|
|
18
20
|
readonly title?: string;
|
|
19
21
|
/** Translatable title for the overview and designer, stored in the title column; the client
|
|
20
22
|
* shows the translation and falls back to the key. Mutually exclusive with `title`. */
|
|
@@ -23,6 +23,7 @@ import { activeStampChanges, servedStampChanges } from "./template-draft.js";
|
|
|
23
23
|
import { refuseReservedLayoutSlug } from "./template-edit-policy.js";
|
|
24
24
|
import { templateSlugSchema } from "./template-meta.js";
|
|
25
25
|
import { variablesFromSchema } from "./template-validation.js";
|
|
26
|
+
import { variantOfMatchesSlug } from "./template-variants.js";
|
|
26
27
|
import { TEXT_BLOCK_KIND } from "./web/constants.js";
|
|
27
28
|
import { TEMPLATE_RESOLVER_FEATURE, TemplateResolverHandlers } from "./web/qualified-names.js";
|
|
28
29
|
const log = createFallbackLogger("template-resolver-seeding");
|
|
@@ -56,6 +57,15 @@ function seedTitleColumn(opts, label) {
|
|
|
56
57
|
}
|
|
57
58
|
return opts.titleI18nKey ?? opts.title ?? null;
|
|
58
59
|
}
|
|
60
|
+
function seedRawMeta(opts, label) {
|
|
61
|
+
const rawMeta = opts.variantOf === undefined
|
|
62
|
+
? opts.meta
|
|
63
|
+
: { ...(isPlainObject(opts.meta) ? opts.meta : {}), variantOf: opts.variantOf };
|
|
64
|
+
if (!variantOfMatchesSlug(opts.slug, rawMeta)) {
|
|
65
|
+
throw new Error(`${label} rejected: invalid_variant_of (slug must be <variantOf>:<name>)`);
|
|
66
|
+
}
|
|
67
|
+
return rawMeta;
|
|
68
|
+
}
|
|
59
69
|
export async function seedSystemTemplate(db, opts) {
|
|
60
70
|
const tenantId = SYSTEM_TENANT_ID;
|
|
61
71
|
const by = opts.by ?? createSystemUser(tenantId);
|
|
@@ -73,12 +83,13 @@ export async function seedSystemTemplate(db, opts) {
|
|
|
73
83
|
throw new Error(`${label} rejected: reserved_layout_slug`);
|
|
74
84
|
}
|
|
75
85
|
const title = seedTitleColumn(opts, label);
|
|
86
|
+
const rawMeta = seedRawMeta(opts, label);
|
|
76
87
|
const evaluated = evaluateTemplateWrite({
|
|
77
88
|
kind: opts.kind,
|
|
78
89
|
scope: "system",
|
|
79
90
|
contentFormat: opts.contentFormat,
|
|
80
91
|
content: opts.content,
|
|
81
|
-
rawMeta
|
|
92
|
+
rawMeta,
|
|
82
93
|
variables: variablesFromSchema(opts.variableSchema ?? {}),
|
|
83
94
|
resourceKeys: Object.keys(opts.linkedResources ?? {}),
|
|
84
95
|
});
|
|
@@ -3,15 +3,24 @@ type DeliveryTarget = {
|
|
|
3
3
|
readonly kind: string;
|
|
4
4
|
readonly slug: string;
|
|
5
5
|
};
|
|
6
|
+
type PolicyEntry = DeliveryTarget & {
|
|
7
|
+
readonly variants?: true;
|
|
8
|
+
};
|
|
9
|
+
type PolicyTarget = DeliveryTarget & {
|
|
10
|
+
/** Stored JSON text or a payload object; undefined when the caller has no row or payload yet. */
|
|
11
|
+
readonly meta?: unknown;
|
|
12
|
+
};
|
|
13
|
+
export declare function findVariantsEntry<Entry extends PolicyEntry>(entries: readonly Entry[], target: DeliveryTarget): Entry | undefined;
|
|
14
|
+
export declare function findPolicyEntry<Entry extends PolicyEntry>(entries: readonly Entry[], target: PolicyTarget): Entry | undefined;
|
|
6
15
|
export type TemplateDeliveryPolicy = {
|
|
7
|
-
readonly tenantEditable: readonly
|
|
16
|
+
readonly tenantEditable: readonly PolicyEntry[];
|
|
8
17
|
/** Kinds of collections mounted with an explicit `access`; their tenant rows are edited by design. */
|
|
9
18
|
readonly collectionKinds: readonly string[];
|
|
10
19
|
};
|
|
11
20
|
export declare function isTenantLayout(target: DeliveryTarget): boolean;
|
|
12
|
-
export declare function isTenantOverrideServed(policy: TemplateDeliveryPolicy, target:
|
|
13
|
-
export declare function isServedByCollectionOnly(policy: TemplateDeliveryPolicy, target:
|
|
14
|
-
export declare function isRowServed(policy: TemplateDeliveryPolicy, row:
|
|
21
|
+
export declare function isTenantOverrideServed(policy: TemplateDeliveryPolicy, target: PolicyTarget): boolean;
|
|
22
|
+
export declare function isServedByCollectionOnly(policy: TemplateDeliveryPolicy, target: PolicyTarget): boolean;
|
|
23
|
+
export declare function isRowServed(policy: TemplateDeliveryPolicy, row: PolicyTarget & {
|
|
15
24
|
readonly tenantId: string;
|
|
16
25
|
}): boolean;
|
|
17
26
|
export declare function deliveryPolicyOf(registry: Registry): TemplateDeliveryPolicy;
|
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
import { isPlainObject, propertyOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
2
|
+
import { isVariantSlugOf, readVariantOf, VARIANT_KIND } from "./template-variants.js";
|
|
2
3
|
import { SYSTEM_TENANT_ID, TENANT_LAYOUT_SLUG_PREFIX } from "./web/constants.js";
|
|
3
4
|
import { TEMPLATE_RESOLVER_FEATURE } from "./web/qualified-names.js";
|
|
5
|
+
function variantOfOfMeta(meta) {
|
|
6
|
+
if (typeof meta !== "string")
|
|
7
|
+
return readVariantOf(meta);
|
|
8
|
+
try {
|
|
9
|
+
return readVariantOf(JSON.parse(meta));
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
// An exact-slug entry always wins, so a separate template that merely has a ":" in its slug
|
|
16
|
+
// is never taken for a variant of another entry.
|
|
17
|
+
export function findVariantsEntry(entries, target) {
|
|
18
|
+
if (entries.some((entry) => entry.kind === target.kind && entry.slug === target.slug)) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
return entries.find((entry) => entry.variants === true &&
|
|
22
|
+
entry.kind === VARIANT_KIND &&
|
|
23
|
+
entry.kind === target.kind &&
|
|
24
|
+
isVariantSlugOf(target.slug, entry.slug));
|
|
25
|
+
}
|
|
26
|
+
// An entry covers its own slug and, with `variants`, the variants of a PDF template: targets whose
|
|
27
|
+
// meta.variantOf is the entry's slug. A target without meta (slug-only pre-check) is judged by
|
|
28
|
+
// slug alone; writeTemplateRow then requires the variantOf.
|
|
29
|
+
export function findPolicyEntry(entries, target) {
|
|
30
|
+
const exact = entries.find((entry) => entry.kind === target.kind && entry.slug === target.slug);
|
|
31
|
+
if (exact !== undefined)
|
|
32
|
+
return exact;
|
|
33
|
+
const variantsEntry = findVariantsEntry(entries, target);
|
|
34
|
+
if (variantsEntry === undefined)
|
|
35
|
+
return undefined;
|
|
36
|
+
return target.meta === undefined || variantOfOfMeta(target.meta) === variantsEntry.slug
|
|
37
|
+
? variantsEntry
|
|
38
|
+
: undefined;
|
|
39
|
+
}
|
|
4
40
|
const NOTHING_UNLOCKED = { tenantEditable: [], collectionKinds: [] };
|
|
5
41
|
// Tenant-created layouts have no entry of their own: they are served and edited once the app unlocks any PDF template.
|
|
6
42
|
export function isTenantLayout(target) {
|
|
@@ -12,13 +48,13 @@ function unlocksTenantLayouts(policy) {
|
|
|
12
48
|
export function isTenantOverrideServed(policy, target) {
|
|
13
49
|
return (policy.collectionKinds.includes(target.kind) ||
|
|
14
50
|
(isTenantLayout(target) && unlocksTenantLayouts(policy)) ||
|
|
15
|
-
policy.tenantEditable
|
|
51
|
+
findPolicyEntry(policy.tenantEditable, target) !== undefined);
|
|
16
52
|
}
|
|
17
53
|
// Served only because its kind belongs to a collection with `access`, not because the template
|
|
18
54
|
// is listed in tenantEditable. For these a system row of the same slug must win.
|
|
19
55
|
export function isServedByCollectionOnly(policy, target) {
|
|
20
56
|
return (policy.collectionKinds.includes(target.kind) &&
|
|
21
|
-
|
|
57
|
+
findPolicyEntry(policy.tenantEditable, target) === undefined);
|
|
22
58
|
}
|
|
23
59
|
export function isRowServed(policy, row) {
|
|
24
60
|
return row.tenantId === SYSTEM_TENANT_ID || isTenantOverrideServed(policy, row);
|
|
@@ -6,6 +6,8 @@ export type TenantEditableTemplate = {
|
|
|
6
6
|
readonly kind: TemplateKind;
|
|
7
7
|
readonly slug: string;
|
|
8
8
|
readonly access: RoleAccessRule;
|
|
9
|
+
/** Lets a tenant create variants (`<slug>:<name>`) of this PDF template; they follow this entry's access and delivery. Only for kind `document-pdf`. */
|
|
10
|
+
readonly variants?: true;
|
|
9
11
|
};
|
|
10
12
|
export type TemplateEditPolicy = {
|
|
11
13
|
readonly tenantEditable: readonly TenantEditableTemplate[];
|
|
@@ -13,6 +15,7 @@ export type TemplateEditPolicy = {
|
|
|
13
15
|
type EditTarget = {
|
|
14
16
|
readonly slug: string;
|
|
15
17
|
readonly kind: string;
|
|
18
|
+
readonly meta?: unknown;
|
|
16
19
|
};
|
|
17
20
|
type EditUser = Pick<SessionUser, "roles">;
|
|
18
21
|
export declare function templateEditAccess(entries: readonly TenantEditableTemplate[]): RoleAccessRule;
|
|
@@ -2,7 +2,7 @@ import { fetchOne } from "@cosmicdrift/kumiko-framework/db";
|
|
|
2
2
|
import { hasAccess, } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
3
|
import { AccessDeniedError, ValidationError, writeFailure, } from "@cosmicdrift/kumiko-framework/errors";
|
|
4
4
|
import { templateResourcesTable } from "./table.js";
|
|
5
|
-
import { isTenantLayout } from "./template-delivery-policy.js";
|
|
5
|
+
import { findPolicyEntry, isTenantLayout } from "./template-delivery-policy.js";
|
|
6
6
|
import { isSystemAdmin } from "./template-draft.js";
|
|
7
7
|
import { TENANT_LAYOUT_SLUG_PREFIX } from "./web/constants.js";
|
|
8
8
|
import { TemplateResolverErrors } from "./web/qualified-names.js";
|
|
@@ -20,7 +20,7 @@ export function mayEditTemplate(policy, user, target) {
|
|
|
20
20
|
return true;
|
|
21
21
|
if (isTenantLayout(target))
|
|
22
22
|
return mayEditTenantLayouts(policy, user);
|
|
23
|
-
const entry = policy.tenantEditable
|
|
23
|
+
const entry = findPolicyEntry(policy.tenantEditable, target);
|
|
24
24
|
return entry !== undefined && hasAccess(user, entry.access);
|
|
25
25
|
}
|
|
26
26
|
export function refuseTemplateEdit(policy, user, target) {
|
|
@@ -22,6 +22,7 @@ declare const mailMetaSchema: z.ZodPreprocess<z.ZodObject<{
|
|
|
22
22
|
export declare const pdfAttachmentFileNamePatternSchema: z.ZodString;
|
|
23
23
|
declare const pdfMetaSchema: z.ZodObject<{
|
|
24
24
|
layoutSlug: z.ZodOptional<z.ZodString>;
|
|
25
|
+
variantOf: z.ZodOptional<z.ZodString>;
|
|
25
26
|
pageFormat: z.ZodOptional<z.ZodEnum<{
|
|
26
27
|
A4: "A4";
|
|
27
28
|
Letter: "Letter";
|
|
@@ -54,6 +54,7 @@ export const pdfAttachmentFileNamePatternSchema = singleLine(200)
|
|
|
54
54
|
const pdfMetaSchema = z
|
|
55
55
|
.object({
|
|
56
56
|
layoutSlug: templateSlugSchema.optional(),
|
|
57
|
+
variantOf: templateSlugSchema.optional(),
|
|
57
58
|
pageFormat: z.enum(PDF_PAGE_FORMATS).optional(),
|
|
58
59
|
orientation: z.enum(PDF_ORIENTATIONS).optional(),
|
|
59
60
|
printBackground: z.boolean().optional(),
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
export declare const VARIANT_SEPARATOR = ":";
|
|
3
|
+
export declare const VARIANT_KIND: "document-pdf";
|
|
4
|
+
export declare const VARIANT_NAME_MAX_LENGTH = 40;
|
|
5
|
+
export declare const VARIANT_UNKNOWN_REASON = "template_variant_unknown";
|
|
6
|
+
export declare const variantNameSchema: z.ZodString;
|
|
7
|
+
export declare function variantSlug(baseSlug: string, name: string): string;
|
|
8
|
+
export declare function isVariantSlugOf(slug: string, baseSlug: string): boolean;
|
|
9
|
+
export declare function readVariantOf(meta: unknown): string | null;
|
|
10
|
+
export declare function variantOfMatchesSlug(slug: string, meta: unknown): boolean;
|
|
11
|
+
export declare function variantNameFromTitle(title: string): string;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { propertyOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
export const VARIANT_SEPARATOR = ":";
|
|
4
|
+
export const VARIANT_KIND = "document-pdf";
|
|
5
|
+
export const VARIANT_NAME_MAX_LENGTH = 40;
|
|
6
|
+
export const VARIANT_UNKNOWN_REASON = "template_variant_unknown";
|
|
7
|
+
// One kebab segment: the variant slug is `<base>:<name>` and a second ":" would read as another base.
|
|
8
|
+
export const variantNameSchema = z
|
|
9
|
+
.string()
|
|
10
|
+
.min(1)
|
|
11
|
+
.max(VARIANT_NAME_MAX_LENGTH)
|
|
12
|
+
.regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, "name must be kebab-case (lowercase, digits, dashes)");
|
|
13
|
+
export function variantSlug(baseSlug, name) {
|
|
14
|
+
return `${baseSlug}${VARIANT_SEPARATOR}${name}`;
|
|
15
|
+
}
|
|
16
|
+
export function isVariantSlugOf(slug, baseSlug) {
|
|
17
|
+
const prefix = `${baseSlug}${VARIANT_SEPARATOR}`;
|
|
18
|
+
return slug.startsWith(prefix) && variantNameSchema.safeParse(slug.slice(prefix.length)).success;
|
|
19
|
+
}
|
|
20
|
+
// Stored meta is untrusted JSON; only a string counts as a base slug.
|
|
21
|
+
export function readVariantOf(meta) {
|
|
22
|
+
const variantOf = propertyOf(meta, "variantOf");
|
|
23
|
+
return typeof variantOf === "string" ? variantOf : null;
|
|
24
|
+
}
|
|
25
|
+
export function variantOfMatchesSlug(slug, meta) {
|
|
26
|
+
const variantOf = readVariantOf(meta);
|
|
27
|
+
return variantOf === null || isVariantSlugOf(slug, variantOf);
|
|
28
|
+
}
|
|
29
|
+
// German special letters are transliterated and the rest kebab-cased, so any title yields a valid name.
|
|
30
|
+
export function variantNameFromTitle(title) {
|
|
31
|
+
return title
|
|
32
|
+
.toLowerCase()
|
|
33
|
+
.replace(/ä/g, "ae")
|
|
34
|
+
.replace(/ö/g, "oe")
|
|
35
|
+
.replace(/ü/g, "ue")
|
|
36
|
+
.replace(/ß/g, "ss")
|
|
37
|
+
.normalize("NFD")
|
|
38
|
+
.replace(/[̀-ͯ]/g, "")
|
|
39
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
40
|
+
.replace(/^-+|-+$/g, "")
|
|
41
|
+
.slice(0, VARIANT_NAME_MAX_LENGTH)
|
|
42
|
+
.replace(/-+$/g, "");
|
|
43
|
+
}
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import { Node } from "@tiptap/core";
|
|
2
|
+
export declare const DOCUMENT_IMAGE_COLUMNS: readonly ["1", "2", "3"];
|
|
3
|
+
export type DocumentImageColumns = (typeof DOCUMENT_IMAGE_COLUMNS)[number];
|
|
4
|
+
export declare function readDocumentImageColumns(value: unknown): DocumentImageColumns | null;
|
|
2
5
|
export declare function createDocumentImagesNode(isReferenceName: (value: string) => boolean): Node<any, any>;
|