@geonosis/document 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/core/actor.cjs +48 -0
  3. package/dist/core/actor.d.cts +3 -0
  4. package/dist/core/actor.d.ts +3 -0
  5. package/dist/core/actor.js +8 -0
  6. package/dist/core/body-version.cjs +75 -0
  7. package/dist/core/body-version.d.cts +25 -0
  8. package/dist/core/body-version.d.ts +25 -0
  9. package/dist/core/body-version.js +35 -0
  10. package/dist/core/canonical.cjs +55 -0
  11. package/dist/core/canonical.d.cts +5 -0
  12. package/dist/core/canonical.d.ts +5 -0
  13. package/dist/core/canonical.js +15 -0
  14. package/dist/core/document.cjs +136 -0
  15. package/dist/core/document.d.cts +137 -0
  16. package/dist/core/document.d.ts +137 -0
  17. package/dist/core/document.js +96 -0
  18. package/dist/core/entities.cjs +68 -0
  19. package/dist/core/entities.d.cts +42 -0
  20. package/dist/core/entities.d.ts +42 -0
  21. package/dist/core/entities.js +28 -0
  22. package/dist/core/naming.cjs +56 -0
  23. package/dist/core/naming.d.cts +17 -0
  24. package/dist/core/naming.d.ts +17 -0
  25. package/dist/core/naming.js +16 -0
  26. package/dist/core/numbering.cjs +41 -0
  27. package/dist/core/numbering.d.cts +12 -0
  28. package/dist/core/numbering.d.ts +12 -0
  29. package/dist/core/numbering.js +0 -0
  30. package/dist/core/ports.cjs +41 -0
  31. package/dist/core/ports.d.cts +29 -0
  32. package/dist/core/ports.d.ts +29 -0
  33. package/dist/core/ports.js +0 -0
  34. package/dist/core/schema.cjs +173 -0
  35. package/dist/core/schema.d.cts +33 -0
  36. package/dist/core/schema.d.ts +33 -0
  37. package/dist/{chunk-NA3DLROL.js → core/schema.js} +34 -72
  38. package/dist/core/status.cjs +47 -0
  39. package/dist/core/status.d.cts +8 -0
  40. package/dist/core/status.d.ts +8 -0
  41. package/dist/core/status.js +7 -0
  42. package/dist/index.cjs +70 -398
  43. package/dist/index.d.cts +11 -32
  44. package/dist/index.d.ts +11 -32
  45. package/dist/index.js +13 -25
  46. package/dist/store/ddl.cjs +209 -0
  47. package/dist/store/ddl.d.cts +56 -0
  48. package/dist/store/ddl.d.ts +56 -0
  49. package/dist/store/ddl.js +175 -0
  50. package/dist/store/hash.cjs +50 -0
  51. package/dist/store/hash.d.cts +2 -0
  52. package/dist/store/hash.d.ts +2 -0
  53. package/dist/store/hash.js +10 -0
  54. package/dist/store/store.cjs +606 -0
  55. package/dist/store/store.d.cts +169 -0
  56. package/dist/store/store.d.ts +169 -0
  57. package/dist/store/store.js +578 -0
  58. package/dist/store.cjs +51 -1358
  59. package/dist/store.d.cts +4 -277
  60. package/dist/store.d.ts +4 -277
  61. package/dist/store.js +17 -1114
  62. package/dist/surface/table.cjs +326 -0
  63. package/dist/surface/table.d.cts +70 -0
  64. package/dist/surface/table.d.ts +70 -0
  65. package/dist/surface/table.js +296 -0
  66. package/dist/workflows/document-workflows.cjs +314 -0
  67. package/dist/workflows/document-workflows.d.cts +112 -0
  68. package/dist/workflows/document-workflows.d.ts +112 -0
  69. package/dist/workflows/document-workflows.js +281 -0
  70. package/dist/workflows/effects.cjs +41 -0
  71. package/dist/workflows/effects.d.cts +68 -0
  72. package/dist/workflows/effects.d.ts +68 -0
  73. package/dist/workflows/effects.js +0 -0
  74. package/dist/workflows/ports.cjs +50 -0
  75. package/dist/workflows/ports.d.cts +72 -0
  76. package/dist/workflows/ports.d.ts +72 -0
  77. package/dist/workflows/ports.js +10 -0
  78. package/dist/workflows/refusals.cjs +80 -0
  79. package/dist/workflows/refusals.d.cts +59 -0
  80. package/dist/workflows/refusals.d.ts +59 -0
  81. package/dist/workflows/refusals.js +40 -0
  82. package/dist/workflows.cjs +52 -504
  83. package/dist/workflows.d.cts +4 -310
  84. package/dist/workflows.d.ts +4 -310
  85. package/dist/workflows.js +21 -368
  86. package/package.json +8 -8
  87. package/dist/chunk-A6ZU4QU3.js +0 -171
  88. package/dist/entities-DTNJMeVf.d.cts +0 -205
  89. package/dist/entities-DTNJMeVf.d.ts +0 -205
  90. package/dist/ports-C_OfflMQ.d.cts +0 -66
  91. package/dist/ports-C_OfflMQ.d.ts +0 -66
@@ -0,0 +1,112 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import type { BodyOf, DraftOf, IncomingOf, NameOf } from '../core/document.cjs';
3
+ import type { AnyDocumentEntity } from '../core/entities.cjs';
4
+ import type { DocumentNumbering } from '../core/numbering.cjs';
5
+ import { type DocumentStatusMachine } from '../core/status.cjs';
6
+ import type { DocumentEffects, Written } from './effects.cjs';
7
+ import type { DocumentWorkflowPorts } from './ports.cjs';
8
+ /**
9
+ * What the body needs off an entity: the registry's minimal bound plus the facts and pure
10
+ * decisions every workflow is written over — the shape `Document.extend` returns, never a second
11
+ * one. `never` parameters are the same device the surface uses: a definition typed to its own
12
+ * body still fits, and the factory writes what that definition decided.
13
+ */
14
+ export type WorkflowDocumentEntity = AnyDocumentEntity & {
15
+ noun: string;
16
+ draft: StandardSchemaV1;
17
+ incoming: StandardSchemaV1;
18
+ statuses: DocumentStatusMachine<string>;
19
+ numbering: DocumentNumbering<never>;
20
+ fromDraft: (draft: never, number: string) => unknown;
21
+ prepare: (body: never) => unknown;
22
+ merge: (stored: never, incoming: never, at: string) => unknown;
23
+ statusOf: (body: never) => string;
24
+ withStatus: (body: never, status: never, at: string) => unknown;
25
+ onSent: (body: never) => unknown;
26
+ copy: (body: never) => unknown;
27
+ removable: boolean;
28
+ };
29
+ export type DocumentWorkflowOptions<E, Ctx> = {
30
+ effects: DocumentEffects<E, Ctx>;
31
+ ports: DocumentWorkflowPorts<Ctx>;
32
+ };
33
+ type Minted = {
34
+ sequence: number;
35
+ } | null;
36
+ /**
37
+ * `documentWorkflows(entity, { effects, ports })`: the whole of what a Document can do, minted
38
+ * once per entity that extends it. The workflows are named for the entity (`invoice.create`,
39
+ * `purchase-order.send`), their inputs are the entity's own schemas, and their bodies are written
40
+ * here exactly once — a workflow named for one entity anywhere else is the fork this replaces.
41
+ */
42
+ export declare const documentWorkflows: <E extends WorkflowDocumentEntity, Ctx extends object>(entity: E, options: DocumentWorkflowOptions<E, Ctx>) => {
43
+ entity: E;
44
+ create: import("@geonosis/workflows").Workflow<{
45
+ ref?: string;
46
+ body: DraftOf<E>;
47
+ templateId?: string;
48
+ }, {
49
+ entity: NameOf<E>;
50
+ number: string;
51
+ body: BodyOf<E>;
52
+ minted: Minted;
53
+ }, Ctx>;
54
+ save: import("@geonosis/workflows").Workflow<{
55
+ ref: string;
56
+ body: IncomingOf<E>;
57
+ }, Written<E>, Ctx>;
58
+ setStatus: import("@geonosis/workflows").Workflow<{
59
+ ref: string;
60
+ to: string;
61
+ }, {
62
+ entity: NameOf<E>;
63
+ number: string;
64
+ body: BodyOf<E>;
65
+ to: string;
66
+ }, Ctx>;
67
+ remove: import("@geonosis/workflows").Workflow<{
68
+ ref: string;
69
+ }, {
70
+ number: string;
71
+ }, Ctx>;
72
+ send: import("@geonosis/workflows").Workflow<{
73
+ ref: string;
74
+ to: string;
75
+ ttlDays?: number;
76
+ now: number;
77
+ }, {
78
+ entity: NameOf<E>;
79
+ number: string;
80
+ body: BodyOf<E>;
81
+ to: string;
82
+ url: string;
83
+ expiresAt: number;
84
+ }, Ctx>;
85
+ convert: import("@geonosis/workflows").Workflow<{
86
+ ref: string;
87
+ }, {
88
+ entity: string;
89
+ number: string;
90
+ }, Ctx>;
91
+ duplicate: import("@geonosis/workflows").Workflow<{
92
+ ref: string;
93
+ targetRef?: string;
94
+ }, {
95
+ entity: NameOf<E>;
96
+ number: string;
97
+ body: BodyOf<E>;
98
+ minted: Minted;
99
+ }, Ctx>;
100
+ share: import("@geonosis/workflows").Workflow<{
101
+ ref: string;
102
+ now: number;
103
+ ttlDays?: number;
104
+ }, {
105
+ number: string;
106
+ token: string;
107
+ url: string;
108
+ expiresAt: number;
109
+ }, Ctx>;
110
+ };
111
+ export type DocumentWorkflows<E extends WorkflowDocumentEntity, Ctx extends object> = ReturnType<typeof documentWorkflows<E, Ctx>>;
112
+ export {};
@@ -0,0 +1,112 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import type { BodyOf, DraftOf, IncomingOf, NameOf } from '../core/document.js';
3
+ import type { AnyDocumentEntity } from '../core/entities.js';
4
+ import type { DocumentNumbering } from '../core/numbering.js';
5
+ import { type DocumentStatusMachine } from '../core/status.js';
6
+ import type { DocumentEffects, Written } from './effects.js';
7
+ import type { DocumentWorkflowPorts } from './ports.js';
8
+ /**
9
+ * What the body needs off an entity: the registry's minimal bound plus the facts and pure
10
+ * decisions every workflow is written over — the shape `Document.extend` returns, never a second
11
+ * one. `never` parameters are the same device the surface uses: a definition typed to its own
12
+ * body still fits, and the factory writes what that definition decided.
13
+ */
14
+ export type WorkflowDocumentEntity = AnyDocumentEntity & {
15
+ noun: string;
16
+ draft: StandardSchemaV1;
17
+ incoming: StandardSchemaV1;
18
+ statuses: DocumentStatusMachine<string>;
19
+ numbering: DocumentNumbering<never>;
20
+ fromDraft: (draft: never, number: string) => unknown;
21
+ prepare: (body: never) => unknown;
22
+ merge: (stored: never, incoming: never, at: string) => unknown;
23
+ statusOf: (body: never) => string;
24
+ withStatus: (body: never, status: never, at: string) => unknown;
25
+ onSent: (body: never) => unknown;
26
+ copy: (body: never) => unknown;
27
+ removable: boolean;
28
+ };
29
+ export type DocumentWorkflowOptions<E, Ctx> = {
30
+ effects: DocumentEffects<E, Ctx>;
31
+ ports: DocumentWorkflowPorts<Ctx>;
32
+ };
33
+ type Minted = {
34
+ sequence: number;
35
+ } | null;
36
+ /**
37
+ * `documentWorkflows(entity, { effects, ports })`: the whole of what a Document can do, minted
38
+ * once per entity that extends it. The workflows are named for the entity (`invoice.create`,
39
+ * `purchase-order.send`), their inputs are the entity's own schemas, and their bodies are written
40
+ * here exactly once — a workflow named for one entity anywhere else is the fork this replaces.
41
+ */
42
+ export declare const documentWorkflows: <E extends WorkflowDocumentEntity, Ctx extends object>(entity: E, options: DocumentWorkflowOptions<E, Ctx>) => {
43
+ entity: E;
44
+ create: import("@geonosis/workflows").Workflow<{
45
+ ref?: string;
46
+ body: DraftOf<E>;
47
+ templateId?: string;
48
+ }, {
49
+ entity: NameOf<E>;
50
+ number: string;
51
+ body: BodyOf<E>;
52
+ minted: Minted;
53
+ }, Ctx>;
54
+ save: import("@geonosis/workflows").Workflow<{
55
+ ref: string;
56
+ body: IncomingOf<E>;
57
+ }, Written<E>, Ctx>;
58
+ setStatus: import("@geonosis/workflows").Workflow<{
59
+ ref: string;
60
+ to: string;
61
+ }, {
62
+ entity: NameOf<E>;
63
+ number: string;
64
+ body: BodyOf<E>;
65
+ to: string;
66
+ }, Ctx>;
67
+ remove: import("@geonosis/workflows").Workflow<{
68
+ ref: string;
69
+ }, {
70
+ number: string;
71
+ }, Ctx>;
72
+ send: import("@geonosis/workflows").Workflow<{
73
+ ref: string;
74
+ to: string;
75
+ ttlDays?: number;
76
+ now: number;
77
+ }, {
78
+ entity: NameOf<E>;
79
+ number: string;
80
+ body: BodyOf<E>;
81
+ to: string;
82
+ url: string;
83
+ expiresAt: number;
84
+ }, Ctx>;
85
+ convert: import("@geonosis/workflows").Workflow<{
86
+ ref: string;
87
+ }, {
88
+ entity: string;
89
+ number: string;
90
+ }, Ctx>;
91
+ duplicate: import("@geonosis/workflows").Workflow<{
92
+ ref: string;
93
+ targetRef?: string;
94
+ }, {
95
+ entity: NameOf<E>;
96
+ number: string;
97
+ body: BodyOf<E>;
98
+ minted: Minted;
99
+ }, Ctx>;
100
+ share: import("@geonosis/workflows").Workflow<{
101
+ ref: string;
102
+ now: number;
103
+ ttlDays?: number;
104
+ }, {
105
+ number: string;
106
+ token: string;
107
+ url: string;
108
+ expiresAt: number;
109
+ }, Ctx>;
110
+ };
111
+ export type DocumentWorkflows<E extends WorkflowDocumentEntity, Ctx extends object> = ReturnType<typeof documentWorkflows<E, Ctx>>;
112
+ export {};
@@ -0,0 +1,281 @@
1
+ // src/workflows/document-workflows.ts
2
+ import { createStep, createWorkflow } from "@geonosis/workflows";
3
+ import { systemActor } from "../core/actor.js";
4
+ import { enumOf, nonEmptyString, objectOf, optional, positiveInt, validate } from "../core/schema.js";
5
+ import { canTransition } from "../core/status.js";
6
+ import {
7
+ ConversionRefused,
8
+ DocumentNotFound,
9
+ DocumentNotRemovable,
10
+ DocumentTaken,
11
+ NumberingRefused,
12
+ TransitionRefused
13
+ } from "./refusals.js";
14
+ var withArticle = (noun) => `${/^[aeiou]/i.test(noun) ? "an" : "a"} ${noun}`;
15
+ var capitalised = (noun) => `${noun.charAt(0).toUpperCase()}${noun.slice(1)}`;
16
+ var mailText = (opening, url, ttlDays) => `${opening}
17
+
18
+ ${url}
19
+
20
+ The link works for ${ttlDays} days.
21
+ `;
22
+ var typed = (schema) => schema;
23
+ var defaultActor = (ctx, workflowName) => {
24
+ const held = ctx?.actor;
25
+ return typeof held === "string" ? held : systemActor(workflowName);
26
+ };
27
+ var documentWorkflows = (entity, options) => {
28
+ const { effects, ports } = options;
29
+ const name = entity.name;
30
+ const article = withArticle(entity.noun);
31
+ const noun = capitalised(entity.noun);
32
+ const actorOf = ports.actor ?? defaultActor;
33
+ const guard = ports.guard ?? (() => Promise.resolve());
34
+ const now = ports.now ?? (() => new Date().toISOString());
35
+ const seed = ports.seed ?? ((_ctx, seeding) => {
36
+ if (seeding.templateId !== undefined) {
37
+ throw new Error(`${article} takes no template: this host declares no seed port`);
38
+ }
39
+ return Promise.resolve(seeding.input);
40
+ });
41
+ const beforeCreate = effects.beforeCreate ?? ((_ctx, body) => Promise.resolve(body));
42
+ const mailWords = effects.mail ?? ((number) => ({
43
+ subject: `${noun} ${number}`,
44
+ opening: `${noun} ${number} is ready for you to read.`
45
+ }));
46
+ const announce = (fact) => effects.announce(fact);
47
+ const write = async (ctx, written, workflowName) => {
48
+ await ports.door(ctx, written.entity).commit({
49
+ actor: actorOf(ctx, workflowName),
50
+ body: written.body,
51
+ ref: written.number
52
+ });
53
+ return written;
54
+ };
55
+ const stored = async (ctx, at) => {
56
+ const found = await ports.door(ctx, name).get({ ref: at.ref });
57
+ if (!found) {
58
+ throw new DocumentNotFound({
59
+ entity: name,
60
+ message: `no ${entity.noun} is stored as ${at.ref}`,
61
+ ref: at.ref
62
+ });
63
+ }
64
+ return found;
65
+ };
66
+ const refuseTaken = async (ctx, at) => {
67
+ const owner = at.entity ?? name;
68
+ if (await ports.door(ctx, owner).get({ ref: at.ref })) {
69
+ throw new DocumentTaken({
70
+ entity: owner,
71
+ message: `a ${owner} is already stored as ${at.ref}`,
72
+ ref: at.ref
73
+ });
74
+ }
75
+ };
76
+ const refFor = async (ctx, given, draft) => {
77
+ const numbering = entity.numbering;
78
+ if (numbering.mode === "given") {
79
+ if (given === undefined) {
80
+ throw new NumberingRefused({
81
+ entity: name,
82
+ message: `${article} is numbered by whoever creates it`,
83
+ mode: "given"
84
+ });
85
+ }
86
+ await refuseTaken(ctx, { ref: given });
87
+ return { ref: given, minted: null };
88
+ }
89
+ if (given !== undefined) {
90
+ throw new NumberingRefused({
91
+ entity: name,
92
+ message: `${article} is numbered by the organisation`,
93
+ mode: "counter"
94
+ });
95
+ }
96
+ if (ports.counter === undefined) {
97
+ throw new NumberingRefused({
98
+ entity: name,
99
+ message: `${article} is numbered from a counter and this host declares no counter port`,
100
+ mode: "counter"
101
+ });
102
+ }
103
+ const series = numbering.series(draft);
104
+ const { number: sequence } = await ports.counter(ctx, numbering.counter);
105
+ return { ref: numbering.format(series, sequence), minted: { sequence } };
106
+ };
107
+ const shareLink = createStep({ name: "create-share-link", noCompensation: true }, (input, ctx) => ports.link(ctx, { entity: name, ...input }));
108
+ const sendMail = createStep({ name: "send-email", noCompensation: true }, async (input, ctx) => {
109
+ const sent = await ports.mail(ctx, input);
110
+ if (sent.isErr())
111
+ throw sent.error;
112
+ return { to: input.to };
113
+ });
114
+ const createName = `${name}.create`;
115
+ const create = createWorkflow(createName, async (input, ctx) => {
116
+ await guard(ctx);
117
+ const draft = await validate(entity.draft, await seed(ctx, {
118
+ entity: name,
119
+ templateId: input.templateId,
120
+ input: input.body
121
+ }));
122
+ const minted = await refFor(ctx, input.ref, draft);
123
+ const body = entity.prepare(await beforeCreate(ctx, entity.fromDraft(draft, minted.ref)));
124
+ const written = await write(ctx, { entity: name, number: minted.ref, body }, createName);
125
+ return { ...written, minted: minted.minted };
126
+ }, {
127
+ announce: ({ minted, ...written }) => announce({ kind: "created", written, minted }),
128
+ input: typed(objectOf({
129
+ body: entity.draft,
130
+ ref: optional(nonEmptyString),
131
+ templateId: optional(nonEmptyString)
132
+ }))
133
+ });
134
+ const saveName = `${name}.save`;
135
+ const save = createWorkflow(saveName, async (input, ctx) => {
136
+ const current = await stored(ctx, input);
137
+ const body = entity.prepare(entity.merge(current.body, input.body, now()));
138
+ return write(ctx, { entity: name, number: input.ref, body }, saveName);
139
+ }, {
140
+ announce: (written) => announce({ kind: "saved", written }),
141
+ input: typed(objectOf({ body: entity.incoming, ref: nonEmptyString }))
142
+ });
143
+ const setStatusName = `${name}.set-status`;
144
+ const setStatus = createWorkflow(setStatusName, async (input, ctx) => {
145
+ const current = await stored(ctx, input);
146
+ const from = entity.statusOf(current.body);
147
+ if (!canTransition(entity.statuses, from, input.to)) {
148
+ throw new TransitionRefused({
149
+ entity: name,
150
+ from,
151
+ message: `${article} cannot go from ${from} to ${input.to}`,
152
+ ref: input.ref,
153
+ to: input.to
154
+ });
155
+ }
156
+ const written = await write(ctx, {
157
+ entity: name,
158
+ number: input.ref,
159
+ body: entity.withStatus(current.body, input.to, now())
160
+ }, setStatusName);
161
+ return { ...written, to: input.to };
162
+ }, {
163
+ announce: ({ to, ...written }) => announce({ kind: "moved", written, to }),
164
+ input: typed(objectOf({ ref: nonEmptyString, to: enumOf(entity.statuses.statuses) }))
165
+ });
166
+ const refInput = typed(objectOf({ ref: nonEmptyString }));
167
+ const deleteName = `${name}.delete`;
168
+ const remove = createWorkflow(deleteName, async (input, ctx) => {
169
+ if (!entity.removable) {
170
+ throw new DocumentNotRemovable({
171
+ entity: name,
172
+ message: `deleting is not something ${article} does`
173
+ });
174
+ }
175
+ const door = ports.door(ctx, name);
176
+ const found = await door.get({ ref: input.ref });
177
+ if (!found) {
178
+ throw new DocumentNotFound({
179
+ entity: name,
180
+ message: `no ${entity.noun} is stored as ${input.ref}`,
181
+ ref: input.ref
182
+ });
183
+ }
184
+ await door.retire({ actor: actorOf(ctx, deleteName), ref: input.ref });
185
+ return { number: input.ref };
186
+ }, {
187
+ announce: (removed) => announce({ kind: "deleted", number: removed.number }),
188
+ input: refInput
189
+ });
190
+ const sendName = `${name}.send`;
191
+ const send = createWorkflow(sendName, async (input, ctx) => {
192
+ const current = await stored(ctx, input);
193
+ const link = await shareLink({
194
+ ref: input.ref,
195
+ now: input.now,
196
+ ...input.ttlDays === undefined ? {} : { ttlDays: input.ttlDays }
197
+ });
198
+ const marked = entity.onSent(current.body);
199
+ const written = marked === current.body ? { entity: name, number: input.ref, body: current.body } : await write(ctx, { entity: name, number: input.ref, body: marked }, sendName);
200
+ const words = mailWords(input.ref);
201
+ await sendMail({
202
+ to: input.to,
203
+ subject: words.subject,
204
+ text: mailText(words.opening, link.url, link.ttlDays)
205
+ });
206
+ return { ...written, to: input.to, url: link.url, expiresAt: link.expiresAt };
207
+ }, {
208
+ announce: ({ to, url, expiresAt, ...written }) => announce({ kind: "sent", written, to, url, expiresAt }),
209
+ execution: "durable",
210
+ idempotent: (input) => `${sendName}:${input.ref}:${input.to}`,
211
+ input: typed(objectOf({
212
+ now: positiveInt,
213
+ ref: nonEmptyString,
214
+ to: nonEmptyString,
215
+ ttlDays: optional(positiveInt)
216
+ }))
217
+ });
218
+ const convertName = `${name}.convert`;
219
+ const convert = createWorkflow(convertName, async (input, ctx) => {
220
+ const conversion = effects.conversion;
221
+ if (conversion === undefined) {
222
+ throw new ConversionRefused({
223
+ entity: name,
224
+ message: `converting is not something ${article} does`,
225
+ ref: input.ref
226
+ });
227
+ }
228
+ const source = await stored(ctx, input);
229
+ const refusal = conversion.refuse(source.body);
230
+ if (refusal !== null) {
231
+ throw new ConversionRefused({
232
+ entity: name,
233
+ message: `${input.ref} ${refusal}`,
234
+ ref: input.ref
235
+ });
236
+ }
237
+ await refuseTaken(ctx, { ref: input.ref, entity: conversion.to.name });
238
+ await ports.door(ctx, conversion.to.name).commit({
239
+ actor: actorOf(ctx, convertName),
240
+ body: conversion.bodyFor(source.body, input.ref),
241
+ ref: input.ref
242
+ });
243
+ await write(ctx, {
244
+ entity: name,
245
+ number: input.ref,
246
+ body: conversion.markSource(source.body, input.ref)
247
+ }, convertName);
248
+ return { entity: conversion.to.name, number: input.ref };
249
+ }, {
250
+ announce: (converted) => announce({ kind: "converted", number: converted.number, to: converted.entity }),
251
+ input: refInput
252
+ });
253
+ const duplicateName = `${name}.duplicate`;
254
+ const duplicate = createWorkflow(duplicateName, async (input, ctx) => {
255
+ await guard(ctx);
256
+ const current = await stored(ctx, input);
257
+ const body = entity.copy(current.body);
258
+ const minted = await refFor(ctx, input.targetRef, body);
259
+ const written = await write(ctx, { entity: name, number: minted.ref, body }, duplicateName);
260
+ return { ...written, minted: minted.minted };
261
+ }, {
262
+ announce: ({ minted, ...written }) => announce({ kind: "created", written, minted }),
263
+ input: typed(objectOf({ ref: nonEmptyString, targetRef: optional(nonEmptyString) }))
264
+ });
265
+ const share = createWorkflow(`${name}.share`, async (input, ctx) => {
266
+ await stored(ctx, input);
267
+ const link = await shareLink({
268
+ ref: input.ref,
269
+ now: input.now,
270
+ ...input.ttlDays === undefined ? {} : { ttlDays: input.ttlDays }
271
+ });
272
+ return { number: input.ref, token: link.token, url: link.url, expiresAt: link.expiresAt };
273
+ }, {
274
+ announce: (link) => announce({ kind: "shared", number: link.number, url: link.url, expiresAt: link.expiresAt }),
275
+ input: typed(objectOf({ now: positiveInt, ref: nonEmptyString, ttlDays: optional(positiveInt) }))
276
+ });
277
+ return { entity, create, save, setStatus, remove, send, convert, duplicate, share };
278
+ };
279
+ export {
280
+ documentWorkflows
281
+ };
@@ -0,0 +1,41 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
29
+ var __export = (target, all) => {
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name)
36
+ });
37
+ };
38
+
39
+ // src/workflows/effects.ts
40
+ var exports_effects = {};
41
+ module.exports = __toCommonJS(exports_effects);
@@ -0,0 +1,68 @@
1
+ import type { Announcement } from '@geonosis/workflows';
2
+ import type { BodyOf, NameOf } from '../core/document.cjs';
3
+ /** What a write left behind: the entity, the number the document carries, and its body. */
4
+ export type Written<E> = {
5
+ entity: NameOf<E>;
6
+ number: string;
7
+ body: BodyOf<E>;
8
+ };
9
+ /**
10
+ * What a workflow did, as the entity is asked to announce it: one function per entity turns
11
+ * these into the product's own events — or into nothing, which is a decision too. `sent` and
12
+ * `shared` carry the link so a product that publishes a shared-link event can say so from either.
13
+ */
14
+ export type DocumentFact<E> = {
15
+ kind: 'created';
16
+ written: Written<E>;
17
+ minted: {
18
+ sequence: number;
19
+ } | null;
20
+ } | {
21
+ kind: 'saved';
22
+ written: Written<E>;
23
+ } | {
24
+ kind: 'moved';
25
+ written: Written<E>;
26
+ to: string;
27
+ } | {
28
+ kind: 'deleted';
29
+ number: string;
30
+ } | {
31
+ kind: 'sent';
32
+ written: Written<E>;
33
+ to: string;
34
+ url: string;
35
+ expiresAt: number;
36
+ } | {
37
+ kind: 'shared';
38
+ number: string;
39
+ url: string;
40
+ expiresAt: number;
41
+ } | {
42
+ kind: 'converted';
43
+ number: string;
44
+ to: string;
45
+ };
46
+ /** One entity's conversion into another: what refuses it, the target's body, how the source is marked. */
47
+ export type DocumentConversion<E, To> = {
48
+ to: To;
49
+ refuse: (body: BodyOf<E>) => string | null;
50
+ bodyFor: (body: BodyOf<E>, number: string) => BodyOf<To>;
51
+ markSource: (body: BodyOf<E>, number: string) => BodyOf<E>;
52
+ };
53
+ /**
54
+ * What an entity decides with a side effect. `announce` is the one decision every entity makes —
55
+ * the events are the product's, and the kit cannot guess their names. The rest default: no
56
+ * `beforeCreate`, no `conversion`, and mail words made of the entity's noun and the number.
57
+ */
58
+ export type DocumentEffects<E, Ctx> = {
59
+ announce: (fact: DocumentFact<E>) => Announcement | readonly Announcement[] | null;
60
+ beforeCreate?: (ctx: Ctx, body: BodyOf<E>) => Promise<BodyOf<E>>;
61
+ conversion?: DocumentConversion<E, {
62
+ name: string;
63
+ }>;
64
+ mail?: (number: string) => {
65
+ subject: string;
66
+ opening: string;
67
+ };
68
+ };
@@ -0,0 +1,68 @@
1
+ import type { Announcement } from '@geonosis/workflows';
2
+ import type { BodyOf, NameOf } from '../core/document.js';
3
+ /** What a write left behind: the entity, the number the document carries, and its body. */
4
+ export type Written<E> = {
5
+ entity: NameOf<E>;
6
+ number: string;
7
+ body: BodyOf<E>;
8
+ };
9
+ /**
10
+ * What a workflow did, as the entity is asked to announce it: one function per entity turns
11
+ * these into the product's own events — or into nothing, which is a decision too. `sent` and
12
+ * `shared` carry the link so a product that publishes a shared-link event can say so from either.
13
+ */
14
+ export type DocumentFact<E> = {
15
+ kind: 'created';
16
+ written: Written<E>;
17
+ minted: {
18
+ sequence: number;
19
+ } | null;
20
+ } | {
21
+ kind: 'saved';
22
+ written: Written<E>;
23
+ } | {
24
+ kind: 'moved';
25
+ written: Written<E>;
26
+ to: string;
27
+ } | {
28
+ kind: 'deleted';
29
+ number: string;
30
+ } | {
31
+ kind: 'sent';
32
+ written: Written<E>;
33
+ to: string;
34
+ url: string;
35
+ expiresAt: number;
36
+ } | {
37
+ kind: 'shared';
38
+ number: string;
39
+ url: string;
40
+ expiresAt: number;
41
+ } | {
42
+ kind: 'converted';
43
+ number: string;
44
+ to: string;
45
+ };
46
+ /** One entity's conversion into another: what refuses it, the target's body, how the source is marked. */
47
+ export type DocumentConversion<E, To> = {
48
+ to: To;
49
+ refuse: (body: BodyOf<E>) => string | null;
50
+ bodyFor: (body: BodyOf<E>, number: string) => BodyOf<To>;
51
+ markSource: (body: BodyOf<E>, number: string) => BodyOf<E>;
52
+ };
53
+ /**
54
+ * What an entity decides with a side effect. `announce` is the one decision every entity makes —
55
+ * the events are the product's, and the kit cannot guess their names. The rest default: no
56
+ * `beforeCreate`, no `conversion`, and mail words made of the entity's noun and the number.
57
+ */
58
+ export type DocumentEffects<E, Ctx> = {
59
+ announce: (fact: DocumentFact<E>) => Announcement | readonly Announcement[] | null;
60
+ beforeCreate?: (ctx: Ctx, body: BodyOf<E>) => Promise<BodyOf<E>>;
61
+ conversion?: DocumentConversion<E, {
62
+ name: string;
63
+ }>;
64
+ mail?: (number: string) => {
65
+ subject: string;
66
+ opening: string;
67
+ };
68
+ };
File without changes