@cosmicdrift/kumiko-samples 0.422.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/package.json +1 -1
- package/packages/bundled-features/package.json +11 -1
- package/packages/bundled-features/src/document-draft/README.md +84 -0
- package/packages/bundled-features/src/document-draft/changes.json +8 -0
- package/packages/bundled-features/src/document-draft/entity.ts +125 -0
- package/packages/bundled-features/src/document-draft/feature.ts +56 -0
- package/packages/bundled-features/src/document-draft/handlers/blocks.query.ts +63 -0
- package/packages/bundled-features/src/document-draft/handlers/by-subject.query.ts +50 -0
- package/packages/bundled-features/src/document-draft/handlers/create.write.ts +110 -0
- package/packages/bundled-features/src/document-draft/handlers/discard.write.ts +32 -0
- package/packages/bundled-features/src/document-draft/handlers/finalize.write.ts +103 -0
- package/packages/bundled-features/src/document-draft/handlers/patch.write.ts +86 -0
- package/packages/bundled-features/src/document-draft/handlers/revise.write.ts +46 -0
- package/packages/bundled-features/src/document-draft/handlers/save.write.ts +53 -0
- package/packages/bundled-features/src/document-draft/i18n.ts +22 -0
- package/packages/bundled-features/src/document-draft/index.ts +21 -0
- package/packages/bundled-features/src/document-draft/lib.ts +77 -0
- package/packages/bundled-features/src/document-draft/options.ts +51 -0
- package/packages/bundled-features/src/document-draft/payloads.ts +45 -0
- package/packages/bundled-features/src/document-draft/screens/editor-screen.ts +17 -0
- package/packages/bundled-features/src/document-draft/user-data.ts +63 -0
- package/packages/bundled-features/src/document-draft/web/client-plugin.tsx +20 -0
- package/packages/bundled-features/src/document-draft/web/constants.ts +56 -0
- package/packages/bundled-features/src/document-draft/web/editor-screen.tsx +418 -0
- package/packages/bundled-features/src/document-draft/web/i18n.ts +54 -0
- package/packages/bundled-features/src/document-draft/web/index.ts +14 -0
- package/packages/bundled-features/src/helpdesk/changes.json +6 -0
- package/packages/bundled-features/src/shared/index.ts +6 -0
- package/packages/bundled-features/src/{signature → shared}/subject-scope.ts +17 -6
- package/packages/bundled-features/src/signature/changes.json +6 -0
- package/packages/bundled-features/src/signature/feature.ts +1 -1
- package/packages/bundled-features/src/signature/handlers/cancel.write.ts +1 -1
- package/packages/bundled-features/src/signature/handlers/issue-link.write.ts +1 -1
- package/packages/bundled-features/src/signature/handlers/request.write.ts +1 -1
- package/packages/bundled-features/src/signature/handlers/revoke-link.write.ts +1 -1
- package/packages/bundled-features/src/signature/handlers/sign-in-person.write.ts +1 -1
- package/packages/bundled-features/src/signature/options.ts +2 -1
- package/packages/bundled-features/src/signature/subject-queries.ts +1 -1
- package/packages/bundled-features/src/template-resolver/api.ts +3 -1
- package/packages/bundled-features/src/template-resolver/boot-rules.ts +5 -0
- package/packages/bundled-features/src/template-resolver/changes.json +30 -0
- package/packages/bundled-features/src/template-resolver/feature.ts +15 -1
- package/packages/bundled-features/src/template-resolver/handlers/create-variant.write.ts +134 -0
- package/packages/bundled-features/src/template-resolver/handlers/document-base.ts +3 -0
- package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +44 -1
- package/packages/bundled-features/src/template-resolver/handlers/patch-draft.write.ts +12 -1
- package/packages/bundled-features/src/template-resolver/handlers/template-write.ts +111 -11
- package/packages/bundled-features/src/template-resolver/handlers/toggle-status.write.ts +29 -4
- package/packages/bundled-features/src/template-resolver/handlers/upsert-tenant.write.ts +1 -0
- package/packages/bundled-features/src/template-resolver/handlers/variants.query.ts +89 -0
- package/packages/bundled-features/src/template-resolver/i18n.ts +1 -0
- package/packages/bundled-features/src/template-resolver/index.ts +22 -0
- package/packages/bundled-features/src/template-resolver/seeding.ts +16 -1
- package/packages/bundled-features/src/template-resolver/template-delivery-policy.ts +56 -6
- package/packages/bundled-features/src/template-resolver/template-edit-policy.ts +5 -5
- package/packages/bundled-features/src/template-resolver/template-meta.ts +1 -0
- package/packages/bundled-features/src/template-resolver/template-variants.ts +50 -0
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-content.tsx +10 -1
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar.tsx +10 -4
- package/packages/bundled-features/src/template-resolver/web/i18n.ts +26 -0
- package/packages/bundled-features/src/template-resolver/web/index.ts +5 -0
- package/packages/bundled-features/src/template-resolver/web/qualified-names.ts +5 -0
- package/packages/bundled-features/src/template-resolver/web/system-placeholders.ts +17 -0
- package/packages/bundled-features/src/tenant/changes.json +7 -0
- package/packages/bundled-features/src/tenant/handlers/update-member-roles.write.ts +1 -1
- package/packages/bundled-features/src/tenant/i18n.ts +0 -1
- package/packages/bundled-features/src/tenant-inactivity/changes.json +7 -0
- package/packages/bundled-features/src/tenant-inactivity/constants.ts +1 -1
- package/packages/bundled-features/src/tenant-lifecycle/changes.json +7 -0
- package/packages/bundled-features/src/tenant-lifecycle/constants.ts +1 -1
- package/samples/apps/ui-walkthrough/src/run-config.ts +1 -0
- package/samples/apps/use-all-bundled/feature-manifest.json +37 -1
- package/samples/apps/use-all-bundled/kumiko/migrations/0069_document-draft.rebuild.json +4 -0
- package/samples/apps/use-all-bundled/src/app/client.tsx +3 -0
- package/samples/apps/use-all-bundled/src/run-config.ts +8 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AccessRule,
|
|
3
|
+
defineWriteHandler,
|
|
4
|
+
type KumikoEventTypeMap,
|
|
5
|
+
} from "@cosmicdrift/kumiko-framework/engine";
|
|
6
|
+
import type { SubjectScope } from "../../shared/index.ts";
|
|
7
|
+
import { documentDraftExecutor } from "../entity.ts";
|
|
8
|
+
import {
|
|
9
|
+
checkOpenAtBase,
|
|
10
|
+
draftNotFound,
|
|
11
|
+
loadScopedDraft,
|
|
12
|
+
pdfMetaOf,
|
|
13
|
+
recordOrEmpty,
|
|
14
|
+
} from "../lib.ts";
|
|
15
|
+
import type { DocumentDraftPdf } from "../options.ts";
|
|
16
|
+
import { DraftBaseSchema } from "../payloads.ts";
|
|
17
|
+
import { DOCUMENT_DRAFT_STATUS } from "../web/constants.ts";
|
|
18
|
+
|
|
19
|
+
type FinalizeData = {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly fileRefId: string;
|
|
22
|
+
readonly fileName: string;
|
|
23
|
+
readonly revision: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const FINALIZE_RATE_LIMIT = { per: "user+handler", limit: 10, windowSeconds: 60 } as const;
|
|
27
|
+
|
|
28
|
+
// Printing takes seconds, so it runs before the transaction opens. The subject scope is checked
|
|
29
|
+
// first, so a caller who may not see the draft gets NotFound without status hints and without a print.
|
|
30
|
+
export function createFinalizeHandler<TUpload>(
|
|
31
|
+
access: AccessRule,
|
|
32
|
+
scope: SubjectScope,
|
|
33
|
+
pdf: DocumentDraftPdf<TUpload>,
|
|
34
|
+
) {
|
|
35
|
+
return defineWriteHandler<
|
|
36
|
+
"finalize",
|
|
37
|
+
typeof DraftBaseSchema,
|
|
38
|
+
FinalizeData,
|
|
39
|
+
KumikoEventTypeMap,
|
|
40
|
+
{ readonly upload: TUpload }
|
|
41
|
+
>({
|
|
42
|
+
name: "finalize",
|
|
43
|
+
schema: DraftBaseSchema,
|
|
44
|
+
access,
|
|
45
|
+
rateLimit: FINALIZE_RATE_LIMIT,
|
|
46
|
+
agent: { risk: "mid" },
|
|
47
|
+
description:
|
|
48
|
+
"Prints an open document draft to a PDF with the values frozen at creation and stores it: the draft becomes finalized and carries the file (fileRefId, fileName). base is the draft version from the blocks query. Further edits need revise first. Returns the draft id, the file and the revision.",
|
|
49
|
+
beforeTransaction: async (event, ctx) => {
|
|
50
|
+
const { id, base } = event.payload;
|
|
51
|
+
const draft = await loadScopedDraft(ctx, event.user, scope, id);
|
|
52
|
+
if (draft === null) return draftNotFound(id);
|
|
53
|
+
const notOpenAtBase = checkOpenAtBase(draft, id, base);
|
|
54
|
+
if (notOpenAtBase !== null) return notOpenAtBase;
|
|
55
|
+
const rendered = await pdf.render(ctx, {
|
|
56
|
+
slug: String(draft.templateSlug),
|
|
57
|
+
...(typeof draft.templateVariant === "string" && { variant: draft.templateVariant }),
|
|
58
|
+
locale: String(draft.locale),
|
|
59
|
+
variables: recordOrEmpty(draft.variables),
|
|
60
|
+
draft: {
|
|
61
|
+
content: String(draft.body),
|
|
62
|
+
meta: pdfMetaOf(draft.meta),
|
|
63
|
+
variableSchema: recordOrEmpty(draft.variableSchema),
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
return rendered.isSuccess ? { ...rendered, data: { upload: rendered.data } } : rendered;
|
|
67
|
+
},
|
|
68
|
+
handler: async (event, ctx) => {
|
|
69
|
+
const { id, base } = event.payload;
|
|
70
|
+
const user = event.user;
|
|
71
|
+
const draft = await loadScopedDraft(ctx, user, scope, id);
|
|
72
|
+
if (draft === null) return draftNotFound(id);
|
|
73
|
+
const notOpenAtBase = checkOpenAtBase(draft, id, base);
|
|
74
|
+
if (notOpenAtBase !== null) return notOpenAtBase;
|
|
75
|
+
const stored = await pdf.store(ctx, event.prepared.upload);
|
|
76
|
+
if (!stored.isSuccess) return stored;
|
|
77
|
+
const updated = await documentDraftExecutor.update(
|
|
78
|
+
{
|
|
79
|
+
id,
|
|
80
|
+
version: base,
|
|
81
|
+
changes: {
|
|
82
|
+
status: DOCUMENT_DRAFT_STATUS.Finalized,
|
|
83
|
+
fileRefId: stored.data.fileRefId,
|
|
84
|
+
fileName: stored.data.fileName,
|
|
85
|
+
finalizedAt: ctx.tz.now(),
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
user,
|
|
89
|
+
ctx.db,
|
|
90
|
+
);
|
|
91
|
+
if (!updated.isSuccess) return updated;
|
|
92
|
+
return {
|
|
93
|
+
isSuccess: true,
|
|
94
|
+
data: {
|
|
95
|
+
id,
|
|
96
|
+
fileRefId: stored.data.fileRefId,
|
|
97
|
+
fileName: stored.data.fileName,
|
|
98
|
+
revision: Number(draft.revision),
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type AccessRule, defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { ValidationError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import type { SubjectScope } from "../../shared/index.ts";
|
|
4
|
+
import {
|
|
5
|
+
applyDocumentOps,
|
|
6
|
+
type DocumentOp,
|
|
7
|
+
parseDocumentJson,
|
|
8
|
+
serializeDocumentJson,
|
|
9
|
+
} from "../../template-resolver/index.ts";
|
|
10
|
+
import { documentDraftExecutor } from "../entity.ts";
|
|
11
|
+
import { draftNotFound, loadScopedDraft, notInStatus, recordOrEmpty } from "../lib.ts";
|
|
12
|
+
import { PatchDraftSchema } from "../payloads.ts";
|
|
13
|
+
import {
|
|
14
|
+
DOCUMENT_DRAFT_ERROR,
|
|
15
|
+
DOCUMENT_DRAFT_LAST_CHANGE_SOURCE,
|
|
16
|
+
DOCUMENT_DRAFT_STATUS,
|
|
17
|
+
} from "../web/constants.ts";
|
|
18
|
+
|
|
19
|
+
type PatchData = { readonly id: string; readonly version: number };
|
|
20
|
+
|
|
21
|
+
// The draft keeps the variables and settings of its template: only the text changes.
|
|
22
|
+
const REFUSED_DRAFT_OPS: ReadonlySet<DocumentOp["op"]> = new Set([
|
|
23
|
+
"setVariable",
|
|
24
|
+
"setMeta",
|
|
25
|
+
"setResource",
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
export function createPatchHandler(access: AccessRule, scope: SubjectScope) {
|
|
29
|
+
return defineWriteHandler<"patch", typeof PatchDraftSchema, PatchData>({
|
|
30
|
+
name: "patch",
|
|
31
|
+
schema: PatchDraftSchema,
|
|
32
|
+
access,
|
|
33
|
+
agent: { risk: "low", effect: "proposal" },
|
|
34
|
+
description:
|
|
35
|
+
"Changes the text of an open document draft with the same structured operations as patch-draft of a template (insertBlock, replaceBody, formatBlock, replaceText, removeBlock; never raw HTML), without touching the template. setVariable, setMeta and setResource are refused with op_not_allowed. Block ids and base (the draft version) come from the blocks query and refer to the state that was read; if the draft changed since, the call fails with version_conflict and nothing is written. Placeholders may be removed. Returns the draft id and its new version.",
|
|
36
|
+
handler: async (event, ctx) => {
|
|
37
|
+
const { id, base, ops } = event.payload;
|
|
38
|
+
const user = event.user;
|
|
39
|
+
const draft = await loadScopedDraft(ctx, user, scope, id);
|
|
40
|
+
if (draft === null) return draftNotFound(id);
|
|
41
|
+
if (draft.status !== DOCUMENT_DRAFT_STATUS.Open)
|
|
42
|
+
return notInStatus(DOCUMENT_DRAFT_STATUS.Open);
|
|
43
|
+
|
|
44
|
+
const refusedIndex = ops.findIndex((op) => REFUSED_DRAFT_OPS.has(op.op));
|
|
45
|
+
if (refusedIndex !== -1) {
|
|
46
|
+
return writeFailure(
|
|
47
|
+
new ValidationError({
|
|
48
|
+
fields: [
|
|
49
|
+
{
|
|
50
|
+
path: `ops.${refusedIndex}`,
|
|
51
|
+
code: DOCUMENT_DRAFT_ERROR.OpNotAllowed,
|
|
52
|
+
i18nKey: "documentDraft.errors.opNotAllowed",
|
|
53
|
+
params: { op: ops[refusedIndex]?.op },
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const applied = applyDocumentOps({
|
|
61
|
+
doc: parseDocumentJson(String(draft.body)),
|
|
62
|
+
meta: recordOrEmpty(draft.meta),
|
|
63
|
+
variableSchema: recordOrEmpty(draft.variableSchema),
|
|
64
|
+
kind: "document-pdf",
|
|
65
|
+
region: undefined,
|
|
66
|
+
ops,
|
|
67
|
+
});
|
|
68
|
+
if (!applied.ok) return writeFailure(new ValidationError({ fields: applied.issues }));
|
|
69
|
+
|
|
70
|
+
const updated = await documentDraftExecutor.update(
|
|
71
|
+
{
|
|
72
|
+
id,
|
|
73
|
+
version: base,
|
|
74
|
+
changes: {
|
|
75
|
+
body: serializeDocumentJson(applied.doc),
|
|
76
|
+
lastChange: { source: DOCUMENT_DRAFT_LAST_CHANGE_SOURCE.Ops, ops },
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
user,
|
|
80
|
+
ctx.db,
|
|
81
|
+
);
|
|
82
|
+
if (!updated.isSuccess) return updated;
|
|
83
|
+
return { isSuccess: true, data: { id, version: updated.data.event.version } };
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type AccessRule, defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { SubjectScope } from "../../shared/index.ts";
|
|
3
|
+
import { documentDraftExecutor } from "../entity.ts";
|
|
4
|
+
import { draftNotFound, loadScopedDraft, notInStatus } from "../lib.ts";
|
|
5
|
+
import { DraftBaseSchema } from "../payloads.ts";
|
|
6
|
+
import { DOCUMENT_DRAFT_STATUS } from "../web/constants.ts";
|
|
7
|
+
|
|
8
|
+
type ReviseData = { readonly id: string; readonly version: number; readonly revision: number };
|
|
9
|
+
|
|
10
|
+
export function createReviseHandler(access: AccessRule, scope: SubjectScope) {
|
|
11
|
+
return defineWriteHandler<"revise", typeof DraftBaseSchema, ReviseData>({
|
|
12
|
+
name: "revise",
|
|
13
|
+
schema: DraftBaseSchema,
|
|
14
|
+
access,
|
|
15
|
+
agent: { risk: "mid" },
|
|
16
|
+
description:
|
|
17
|
+
"Reopens a finalized document draft for editing: the revision counts up and the file reference is cleared (the stored PDF stays in the files). The next finalize prints a new PDF.",
|
|
18
|
+
handler: async (event, ctx) => {
|
|
19
|
+
const { id, base } = event.payload;
|
|
20
|
+
const user = event.user;
|
|
21
|
+
const draft = await loadScopedDraft(ctx, user, scope, id);
|
|
22
|
+
if (draft === null) return draftNotFound(id);
|
|
23
|
+
if (draft.status !== DOCUMENT_DRAFT_STATUS.Finalized) {
|
|
24
|
+
return notInStatus(DOCUMENT_DRAFT_STATUS.Finalized);
|
|
25
|
+
}
|
|
26
|
+
const revision = Number(draft.revision) + 1;
|
|
27
|
+
const updated = await documentDraftExecutor.update(
|
|
28
|
+
{
|
|
29
|
+
id,
|
|
30
|
+
version: base,
|
|
31
|
+
changes: {
|
|
32
|
+
status: DOCUMENT_DRAFT_STATUS.Open,
|
|
33
|
+
revision,
|
|
34
|
+
fileRefId: null,
|
|
35
|
+
fileName: null,
|
|
36
|
+
finalizedAt: null,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
user,
|
|
40
|
+
ctx.db,
|
|
41
|
+
);
|
|
42
|
+
if (!updated.isSuccess) return updated;
|
|
43
|
+
return { isSuccess: true, data: { id, version: updated.data.event.version, revision } };
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type AccessRule, defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { ValidationError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import type { SubjectScope } from "../../shared/index.ts";
|
|
4
|
+
import {
|
|
5
|
+
documentViolationsToFieldIssues,
|
|
6
|
+
sanitizeDocumentHtml,
|
|
7
|
+
} from "../../template-resolver/index.ts";
|
|
8
|
+
import { documentDraftExecutor } from "../entity.ts";
|
|
9
|
+
import { draftNotFound, loadScopedDraft, notInStatus } from "../lib.ts";
|
|
10
|
+
import { SaveDraftSchema } from "../payloads.ts";
|
|
11
|
+
import { DOCUMENT_DRAFT_LAST_CHANGE_SOURCE, DOCUMENT_DRAFT_STATUS } from "../web/constants.ts";
|
|
12
|
+
|
|
13
|
+
type SaveData = { readonly id: string; readonly version: number };
|
|
14
|
+
|
|
15
|
+
export function createSaveHandler(access: AccessRule, scope: SubjectScope) {
|
|
16
|
+
return defineWriteHandler<"save", typeof SaveDraftSchema, SaveData>({
|
|
17
|
+
name: "save",
|
|
18
|
+
schema: SaveDraftSchema,
|
|
19
|
+
access,
|
|
20
|
+
agent: { expose: false },
|
|
21
|
+
description:
|
|
22
|
+
"Replaces the text of an open document draft with the HTML of the editor; the HTML is checked and cleaned like a template body, and anything it does not allow is refused with the violation codes.",
|
|
23
|
+
handler: async (event, ctx) => {
|
|
24
|
+
const { id, base, content } = event.payload;
|
|
25
|
+
const user = event.user;
|
|
26
|
+
const draft = await loadScopedDraft(ctx, user, scope, id);
|
|
27
|
+
if (draft === null) return draftNotFound(id);
|
|
28
|
+
if (draft.status !== DOCUMENT_DRAFT_STATUS.Open)
|
|
29
|
+
return notInStatus(DOCUMENT_DRAFT_STATUS.Open);
|
|
30
|
+
|
|
31
|
+
const sanitized = sanitizeDocumentHtml(content);
|
|
32
|
+
if (!sanitized.ok) {
|
|
33
|
+
return writeFailure(
|
|
34
|
+
new ValidationError({ fields: documentViolationsToFieldIssues(sanitized.violations) }),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const updated = await documentDraftExecutor.update(
|
|
38
|
+
{
|
|
39
|
+
id,
|
|
40
|
+
version: base,
|
|
41
|
+
changes: {
|
|
42
|
+
body: sanitized.html,
|
|
43
|
+
lastChange: { source: DOCUMENT_DRAFT_LAST_CHANGE_SOURCE.Editor },
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
user,
|
|
47
|
+
ctx.db,
|
|
48
|
+
);
|
|
49
|
+
if (!updated.isSuccess) return updated;
|
|
50
|
+
return { isSuccess: true, data: { id, version: updated.data.event.version } };
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type LocalizedString = { readonly en: string; readonly de: string };
|
|
2
|
+
|
|
3
|
+
export const DOCUMENT_DRAFT_I18N: Readonly<Record<string, LocalizedString>> = {
|
|
4
|
+
"document-draft:feature.label": { en: "Document drafts", de: "Dokumententwürfe" },
|
|
5
|
+
"screen:document-draft-editor.title": { en: "Document draft", de: "Dokumententwurf" },
|
|
6
|
+
"documentDraft.errors.notOpen": {
|
|
7
|
+
en: "This draft is not open for editing.",
|
|
8
|
+
de: "Dieser Entwurf ist nicht zur Bearbeitung offen.",
|
|
9
|
+
},
|
|
10
|
+
"documentDraft.errors.notFinalized": {
|
|
11
|
+
en: "This draft is not finalized.",
|
|
12
|
+
de: "Dieser Entwurf ist nicht abgeschlossen.",
|
|
13
|
+
},
|
|
14
|
+
"documentDraft.errors.templateNotFound": {
|
|
15
|
+
en: "The template {slug} does not exist.",
|
|
16
|
+
de: "Die Vorlage {slug} existiert nicht.",
|
|
17
|
+
},
|
|
18
|
+
"documentDraft.errors.opNotAllowed": {
|
|
19
|
+
en: "The operation {op} is not allowed on a draft: only its text can change.",
|
|
20
|
+
de: "Die Operation {op} ist bei einem Entwurf nicht erlaubt: nur der Text ist änderbar.",
|
|
21
|
+
},
|
|
22
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type { DocumentDraftRetention } from "./entity.ts";
|
|
2
|
+
export { createDocumentDraftFeature } from "./feature.ts";
|
|
3
|
+
export type {
|
|
4
|
+
AuthorizeDocumentDraftSubject,
|
|
5
|
+
DocumentDraftOptions,
|
|
6
|
+
DocumentDraftPdf,
|
|
7
|
+
DocumentDraftRenderArgs,
|
|
8
|
+
DocumentDraftSubject,
|
|
9
|
+
} from "./options.ts";
|
|
10
|
+
export { forgetDocumentDraftsOfSubject } from "./user-data.ts";
|
|
11
|
+
export {
|
|
12
|
+
DOCUMENT_DRAFT_EDITOR_SCREEN_ID,
|
|
13
|
+
DOCUMENT_DRAFT_EDITOR_SCREEN_QN,
|
|
14
|
+
DOCUMENT_DRAFT_ENTITY,
|
|
15
|
+
DOCUMENT_DRAFT_ERROR,
|
|
16
|
+
DOCUMENT_DRAFT_FEATURE,
|
|
17
|
+
DOCUMENT_DRAFT_STATUS,
|
|
18
|
+
DocumentDraftHandlers,
|
|
19
|
+
DocumentDraftQueries,
|
|
20
|
+
type DocumentDraftStatus,
|
|
21
|
+
} from "./web/constants.ts";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { AccessRule, SessionUser } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import {
|
|
3
|
+
NotFoundError,
|
|
4
|
+
UnprocessableError,
|
|
5
|
+
VersionConflictError,
|
|
6
|
+
writeFailure,
|
|
7
|
+
} from "@cosmicdrift/kumiko-framework/errors";
|
|
8
|
+
import { isPlainObject } from "@cosmicdrift/kumiko-types/type-guards";
|
|
9
|
+
import type { SubjectScope, SubjectScopeContext } from "../shared/index.ts";
|
|
10
|
+
import {
|
|
11
|
+
type PdfTemplateMeta,
|
|
12
|
+
parseTemplateMeta,
|
|
13
|
+
readPdfTemplateMeta,
|
|
14
|
+
} from "../template-resolver/index.ts";
|
|
15
|
+
import { documentDraftExecutor } from "./entity.ts";
|
|
16
|
+
import {
|
|
17
|
+
DOCUMENT_DRAFT_ENTITY,
|
|
18
|
+
DOCUMENT_DRAFT_ERROR,
|
|
19
|
+
DOCUMENT_DRAFT_STATUS,
|
|
20
|
+
type DocumentDraftStatus,
|
|
21
|
+
} from "./web/constants.ts";
|
|
22
|
+
|
|
23
|
+
export const DEFAULT_DOCUMENT_DRAFT_ACCESS: AccessRule = { roles: ["TenantAdmin", "TenantMember"] };
|
|
24
|
+
|
|
25
|
+
export type DraftRow = NonNullable<Awaited<ReturnType<typeof documentDraftExecutor.detail>>>;
|
|
26
|
+
|
|
27
|
+
export function draftNotFound(id?: string) {
|
|
28
|
+
return writeFailure(new NotFoundError(DOCUMENT_DRAFT_ENTITY, id));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// A draft of another tenant, an unknown id and a subject the user may not see all answer the same.
|
|
32
|
+
export async function loadScopedDraft(
|
|
33
|
+
ctx: SubjectScopeContext,
|
|
34
|
+
user: SessionUser,
|
|
35
|
+
scope: SubjectScope,
|
|
36
|
+
id: string,
|
|
37
|
+
): Promise<DraftRow | null> {
|
|
38
|
+
const row = await documentDraftExecutor.detail({ id }, user, ctx.db);
|
|
39
|
+
if (row === null || !(await scope.allowsRequest(ctx, user, row))) return null;
|
|
40
|
+
return row;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function notInStatus(expected: DocumentDraftStatus) {
|
|
44
|
+
const open = expected === DOCUMENT_DRAFT_STATUS.Open;
|
|
45
|
+
return writeFailure(
|
|
46
|
+
new UnprocessableError(
|
|
47
|
+
open ? DOCUMENT_DRAFT_ERROR.NotOpen : DOCUMENT_DRAFT_ERROR.NotFinalized,
|
|
48
|
+
{
|
|
49
|
+
i18nKey: open ? "documentDraft.errors.notOpen" : "documentDraft.errors.notFinalized",
|
|
50
|
+
},
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Null when the draft is open at the version the caller read; otherwise the failure to return.
|
|
56
|
+
export function checkOpenAtBase(draft: DraftRow, id: string, base: number) {
|
|
57
|
+
if (draft.status !== DOCUMENT_DRAFT_STATUS.Open) return notInStatus(DOCUMENT_DRAFT_STATUS.Open);
|
|
58
|
+
if (draft.version !== base) {
|
|
59
|
+
return writeFailure(
|
|
60
|
+
new VersionConflictError({
|
|
61
|
+
entityId: id,
|
|
62
|
+
expectedVersion: base,
|
|
63
|
+
currentVersion: Number(draft.version),
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function recordOrEmpty(value: unknown): Readonly<Record<string, unknown>> {
|
|
71
|
+
return isPlainObject(value) ? value : {};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function pdfMetaOf(raw: unknown): PdfTemplateMeta | null {
|
|
75
|
+
const parsed = parseTemplateMeta("document-pdf", raw);
|
|
76
|
+
return parsed.ok ? readPdfTemplateMeta(parsed.meta) : null;
|
|
77
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AccessRule,
|
|
3
|
+
BeforeTransactionContext,
|
|
4
|
+
BeforeTransactionResult,
|
|
5
|
+
HandlerContext,
|
|
6
|
+
WriteResult,
|
|
7
|
+
} from "@cosmicdrift/kumiko-framework/engine";
|
|
8
|
+
import type { DocumentPdfDraft } from "../renderer-foundation/index.ts";
|
|
9
|
+
import type { SubjectScopeOptions } from "../shared/index.ts";
|
|
10
|
+
import type { DocumentDraftRetention } from "./entity.ts";
|
|
11
|
+
|
|
12
|
+
export type AuthorizeDocumentDraftSubject = NonNullable<SubjectScopeOptions["authorizeSubject"]>;
|
|
13
|
+
export type DocumentDraftSubject = Parameters<AuthorizeDocumentDraftSubject>[2];
|
|
14
|
+
|
|
15
|
+
export type DocumentDraftRenderArgs = {
|
|
16
|
+
readonly slug: string;
|
|
17
|
+
/** Slug of a variant of `slug`, printed instead of the base. */
|
|
18
|
+
readonly variant?: string;
|
|
19
|
+
readonly locale: string;
|
|
20
|
+
readonly variables: Readonly<Record<string, unknown>>;
|
|
21
|
+
/** The draft's own body, meta and variable schema, printed instead of the stored template. */
|
|
22
|
+
readonly draft: DocumentPdfDraft;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Injected by the app: bundled-features cannot import renderer-puppeteer-client, which itself
|
|
26
|
+
// depends on bundled-features. `{ render: renderPdfBeforeTransaction, store: storeRenderedPdf }`
|
|
27
|
+
// from that package fits without an adapter.
|
|
28
|
+
export type DocumentDraftPdf<TUpload> = {
|
|
29
|
+
/** Prints the draft to a PDF and uploads it while no transaction is open. */
|
|
30
|
+
readonly render: (
|
|
31
|
+
ctx: BeforeTransactionContext,
|
|
32
|
+
args: DocumentDraftRenderArgs,
|
|
33
|
+
) => Promise<BeforeTransactionResult<TUpload>>;
|
|
34
|
+
/** Stores the uploaded PDF as a file inside the finalize transaction. */
|
|
35
|
+
readonly store: (
|
|
36
|
+
ctx: HandlerContext,
|
|
37
|
+
upload: TUpload,
|
|
38
|
+
) => Promise<WriteResult<{ readonly fileRefId: string; readonly fileName: string }>>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type DocumentDraftOptions<TUpload> = SubjectScopeOptions & {
|
|
42
|
+
/** The kinds of object a draft can belong to, e.g. `lease`; create refuses any other. */
|
|
43
|
+
readonly subjectTypes: readonly [string, ...string[]];
|
|
44
|
+
readonly pdf: DocumentDraftPdf<TUpload>;
|
|
45
|
+
/** How long drafts are kept, data-retention keepFor syntax, counted from creation. Default 10y. */
|
|
46
|
+
readonly retention?: DocumentDraftRetention;
|
|
47
|
+
/** Access rule of the handlers and queries. Default `{ roles: ["TenantAdmin", "TenantMember"] }`. */
|
|
48
|
+
readonly access?: AccessRule;
|
|
49
|
+
/** Screen per subject type the editor returns to after finalize or discard, opened with the subject id. */
|
|
50
|
+
readonly subjectScreens?: Readonly<Record<string, string>>;
|
|
51
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { documentOpsSchema, localeSchema, templateSlugSchema } from "../template-resolver/index.ts";
|
|
3
|
+
import {
|
|
4
|
+
DOCUMENT_DRAFT_DEFAULT_LOCALE,
|
|
5
|
+
DOCUMENT_DRAFT_MAX_CONTENT_LENGTH,
|
|
6
|
+
} from "./web/constants.ts";
|
|
7
|
+
|
|
8
|
+
const draftIdSchema = z.uuid();
|
|
9
|
+
const baseVersionSchema = z.number().int().nonnegative();
|
|
10
|
+
const subjectIdSchema = z.string().min(1).max(100);
|
|
11
|
+
|
|
12
|
+
export const DraftIdSchema = z.object({ id: draftIdSchema });
|
|
13
|
+
|
|
14
|
+
// `base` is the draft's version as the caller read it; a stale base fails with version_conflict.
|
|
15
|
+
export const DraftBaseSchema = z.object({ id: draftIdSchema, base: baseVersionSchema });
|
|
16
|
+
|
|
17
|
+
export const PatchDraftSchema = z.object({
|
|
18
|
+
id: draftIdSchema,
|
|
19
|
+
base: baseVersionSchema,
|
|
20
|
+
ops: documentOpsSchema,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const SaveDraftSchema = z.object({
|
|
24
|
+
id: draftIdSchema,
|
|
25
|
+
base: baseVersionSchema,
|
|
26
|
+
content: z.string().max(DOCUMENT_DRAFT_MAX_CONTENT_LENGTH),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const BySubjectSchema = z.object({
|
|
30
|
+
subjectType: z.string().min(1).max(100),
|
|
31
|
+
subjectId: subjectIdSchema,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export function createDraftSchema(subjectTypes: readonly [string, ...string[]]) {
|
|
35
|
+
return z.object({
|
|
36
|
+
subjectType: z.enum(subjectTypes),
|
|
37
|
+
subjectId: subjectIdSchema,
|
|
38
|
+
templateSlug: templateSlugSchema,
|
|
39
|
+
/** Slug of a variant of `templateSlug` (`<templateSlug>:<name>`), used instead of the base. */
|
|
40
|
+
variant: templateSlugSchema.optional(),
|
|
41
|
+
locale: localeSchema.default(DOCUMENT_DRAFT_DEFAULT_LOCALE),
|
|
42
|
+
/** Values of the placeholders, frozen with the draft; they stay personal data of the draft. */
|
|
43
|
+
variables: z.record(z.string(), z.unknown()),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { CustomScreenDefinition } from "@cosmicdrift/kumiko-framework/ui-types";
|
|
3
|
+
import { DOCUMENT_DRAFT_EDITOR_SCREEN_ID } from "../web/constants.ts";
|
|
4
|
+
|
|
5
|
+
// Opened with the draft id as entityId from the screen of the object the draft belongs to; the
|
|
6
|
+
// client plugin mounts the component under this id. Navigation is the app's business, so it is dormant.
|
|
7
|
+
export function createDocumentDraftEditorScreen(access: AccessRule): CustomScreenDefinition {
|
|
8
|
+
return {
|
|
9
|
+
id: DOCUMENT_DRAFT_EDITOR_SCREEN_ID,
|
|
10
|
+
type: "custom",
|
|
11
|
+
renderer: { react: { __component: DOCUMENT_DRAFT_EDITOR_SCREEN_ID } },
|
|
12
|
+
description:
|
|
13
|
+
"Edit one document draft: the text of the template copy with its placeholders and the deviations from the template beside it, then save, finalize to a PDF or discard.",
|
|
14
|
+
access,
|
|
15
|
+
dormant: true,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { configuredPiiSubjectKms } from "@cosmicdrift/kumiko-framework/crypto";
|
|
2
|
+
import type { TenantDb } from "@cosmicdrift/kumiko-framework/db";
|
|
3
|
+
import type { SessionUser } from "@cosmicdrift/kumiko-framework/engine";
|
|
4
|
+
import { InternalError } from "@cosmicdrift/kumiko-framework/errors";
|
|
5
|
+
import { documentDraftExecutor } from "./entity.ts";
|
|
6
|
+
import { DOCUMENT_DRAFT_ENTITY } from "./web/constants.ts";
|
|
7
|
+
|
|
8
|
+
// A handler ctx fits, and so does a system-mode tenant db in an erasure job.
|
|
9
|
+
type DraftErasureContext = { readonly db: TenantDb; readonly user: SessionUser };
|
|
10
|
+
|
|
11
|
+
const ERASE_REASON = "document-draft:subject-forget";
|
|
12
|
+
// Equals the executor's MAX_LIST_LIMIT.
|
|
13
|
+
const LIST_PAGE_SIZE = 200;
|
|
14
|
+
|
|
15
|
+
async function draftIdsOfSubject(
|
|
16
|
+
ctx: DraftErasureContext,
|
|
17
|
+
subject: { readonly subjectType: string; readonly subjectId: string },
|
|
18
|
+
): Promise<readonly string[]> {
|
|
19
|
+
const ids: string[] = [];
|
|
20
|
+
for (;;) {
|
|
21
|
+
const page = await documentDraftExecutor.list(
|
|
22
|
+
{
|
|
23
|
+
filters: [
|
|
24
|
+
{ field: "subjectType", op: "eq", value: subject.subjectType },
|
|
25
|
+
{ field: "subjectId", op: "eq", value: subject.subjectId },
|
|
26
|
+
],
|
|
27
|
+
limit: LIST_PAGE_SIZE,
|
|
28
|
+
offset: ids.length,
|
|
29
|
+
},
|
|
30
|
+
ctx.user,
|
|
31
|
+
ctx.db,
|
|
32
|
+
);
|
|
33
|
+
ids.push(...page.rows.map((row) => String(row.id)));
|
|
34
|
+
if (page.rows.length < LIST_PAGE_SIZE) return ids;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A draft's body, variables and last change belong to the draft's own record key, so the user
|
|
39
|
+
// forget run never shreds them: the app calls this when the object the drafts belong to is erased.
|
|
40
|
+
// The row is purged and its key erased, so the old events stay unreadable too. Without a mounted
|
|
41
|
+
// KMS the key erase is a no-op and the old events keep plaintext (record-owned fields are plaintext
|
|
42
|
+
// framework-wide, see signature user-data), so apps that need Art. 17 coverage mount one.
|
|
43
|
+
export async function forgetDocumentDraftsOfSubject(
|
|
44
|
+
ctx: DraftErasureContext,
|
|
45
|
+
subject: { readonly subjectType: string; readonly subjectId: string },
|
|
46
|
+
): Promise<{ readonly erased: number }> {
|
|
47
|
+
const kms = configuredPiiSubjectKms();
|
|
48
|
+
const ids = await draftIdsOfSubject(ctx, subject);
|
|
49
|
+
for (const id of ids) {
|
|
50
|
+
const forgotten = await documentDraftExecutor.forget({ id }, ctx.user, ctx.db);
|
|
51
|
+
if (!forgotten.isSuccess) {
|
|
52
|
+
throw new InternalError({
|
|
53
|
+
message: `[document-draft] draft ${id} could not be forgotten: ${forgotten.error.message}`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
// skip: no KMS adapter mounted, nothing is encrypted per record
|
|
57
|
+
await kms?.eraseKey(
|
|
58
|
+
{ kind: "record", entity: DOCUMENT_DRAFT_ENTITY, id },
|
|
59
|
+
{ requestId: ERASE_REASON, userId: ctx.user.id, eraseReason: ERASE_REASON },
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
return { erased: ids.length };
|
|
63
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { mergeTranslations, type TranslationsByLocale } from "@cosmicdrift/kumiko-renderer";
|
|
2
|
+
import type { ClientFeatureDefinition } from "@cosmicdrift/kumiko-renderer/web";
|
|
3
|
+
import { WebPreviewRenderer } from "../../template-resolver/web/index.ts";
|
|
4
|
+
import { DOCUMENT_DRAFT_EDITOR_SCREEN_ID, DOCUMENT_DRAFT_FEATURE } from "./constants.ts";
|
|
5
|
+
import { DocumentDraftEditorScreen } from "./editor-screen.tsx";
|
|
6
|
+
import { defaultTranslations } from "./i18n.ts";
|
|
7
|
+
|
|
8
|
+
export type DocumentDraftClientOptions = {
|
|
9
|
+
/** Key-wise overrides of the default bundles (de/en). */
|
|
10
|
+
readonly translations?: TranslationsByLocale;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function documentDraftClient(options?: DocumentDraftClientOptions): ClientFeatureDefinition {
|
|
14
|
+
return {
|
|
15
|
+
name: DOCUMENT_DRAFT_FEATURE,
|
|
16
|
+
translations: mergeTranslations(defaultTranslations, options?.translations ?? {}),
|
|
17
|
+
contentPreviews: { web: WebPreviewRenderer },
|
|
18
|
+
components: { [DOCUMENT_DRAFT_EDITOR_SCREEN_ID]: DocumentDraftEditorScreen },
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Pure string constants, safe for browser code.
|
|
2
|
+
export const DOCUMENT_DRAFT_FEATURE = "document-draft" as const;
|
|
3
|
+
|
|
4
|
+
export const DOCUMENT_DRAFT_ENTITY = "documentDraft" as const;
|
|
5
|
+
|
|
6
|
+
export const DOCUMENT_DRAFT_STATUS = {
|
|
7
|
+
Open: "open",
|
|
8
|
+
Finalized: "finalized",
|
|
9
|
+
Discarded: "discarded",
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
export type DocumentDraftStatus =
|
|
13
|
+
(typeof DOCUMENT_DRAFT_STATUS)[keyof typeof DOCUMENT_DRAFT_STATUS];
|
|
14
|
+
|
|
15
|
+
export const DOCUMENT_DRAFT_STATUS_TONE = {
|
|
16
|
+
[DOCUMENT_DRAFT_STATUS.Open]: "warn",
|
|
17
|
+
[DOCUMENT_DRAFT_STATUS.Finalized]: "ok",
|
|
18
|
+
[DOCUMENT_DRAFT_STATUS.Discarded]: "neutral",
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
export const DOCUMENT_DRAFT_LAST_CHANGE_SOURCE = {
|
|
22
|
+
Create: "create",
|
|
23
|
+
Ops: "ops",
|
|
24
|
+
Editor: "editor",
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
export const DocumentDraftHandlers = {
|
|
28
|
+
create: `${DOCUMENT_DRAFT_FEATURE}:write:create`,
|
|
29
|
+
patch: `${DOCUMENT_DRAFT_FEATURE}:write:patch`,
|
|
30
|
+
save: `${DOCUMENT_DRAFT_FEATURE}:write:save`,
|
|
31
|
+
finalize: `${DOCUMENT_DRAFT_FEATURE}:write:finalize`,
|
|
32
|
+
revise: `${DOCUMENT_DRAFT_FEATURE}:write:revise`,
|
|
33
|
+
discard: `${DOCUMENT_DRAFT_FEATURE}:write:discard`,
|
|
34
|
+
} as const;
|
|
35
|
+
|
|
36
|
+
export const DocumentDraftQueries = {
|
|
37
|
+
blocks: `${DOCUMENT_DRAFT_FEATURE}:query:blocks`,
|
|
38
|
+
bySubject: `${DOCUMENT_DRAFT_FEATURE}:query:by-subject`,
|
|
39
|
+
} as const;
|
|
40
|
+
|
|
41
|
+
export const DOCUMENT_DRAFT_ERROR = {
|
|
42
|
+
NotOpen: "document_draft_not_open",
|
|
43
|
+
NotFinalized: "document_draft_not_finalized",
|
|
44
|
+
TemplateNotFound: "template_not_found",
|
|
45
|
+
OpNotAllowed: "op_not_allowed",
|
|
46
|
+
} as const;
|
|
47
|
+
|
|
48
|
+
export const DOCUMENT_DRAFT_DEFAULT_LOCALE = "de";
|
|
49
|
+
export const DOCUMENT_DRAFT_MAX_CONTENT_LENGTH = 200_000;
|
|
50
|
+
|
|
51
|
+
// Short screen id as in the URL and in `r.screen`; the qualified name is `document-draft:screen:<id>`.
|
|
52
|
+
export const DOCUMENT_DRAFT_EDITOR_SCREEN_ID = "document-draft-editor" as const;
|
|
53
|
+
|
|
54
|
+
/** Qualified name for targets in other features, e.g. an app actionForm `redirect`. */
|
|
55
|
+
export const DOCUMENT_DRAFT_EDITOR_SCREEN_QN =
|
|
56
|
+
`${DOCUMENT_DRAFT_FEATURE}:screen:${DOCUMENT_DRAFT_EDITOR_SCREEN_ID}` as const;
|