@geonosis/document 0.9.1 → 0.9.2
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/CHANGELOG.md +10 -0
- package/dist/core/actor.cjs +48 -0
- package/dist/core/actor.d.cts +3 -0
- package/dist/core/actor.d.ts +3 -0
- package/dist/core/actor.js +8 -0
- package/dist/core/body-version.cjs +75 -0
- package/dist/core/body-version.d.cts +25 -0
- package/dist/core/body-version.d.ts +25 -0
- package/dist/core/body-version.js +35 -0
- package/dist/core/canonical.cjs +55 -0
- package/dist/core/canonical.d.cts +5 -0
- package/dist/core/canonical.d.ts +5 -0
- package/dist/core/canonical.js +15 -0
- package/dist/core/document.cjs +136 -0
- package/dist/core/document.d.cts +137 -0
- package/dist/core/document.d.ts +137 -0
- package/dist/core/document.js +96 -0
- package/dist/core/entities.cjs +68 -0
- package/dist/core/entities.d.cts +42 -0
- package/dist/core/entities.d.ts +42 -0
- package/dist/core/entities.js +28 -0
- package/dist/core/naming.cjs +56 -0
- package/dist/core/naming.d.cts +17 -0
- package/dist/core/naming.d.ts +17 -0
- package/dist/core/naming.js +16 -0
- package/dist/core/numbering.cjs +41 -0
- package/dist/core/numbering.d.cts +12 -0
- package/dist/core/numbering.d.ts +12 -0
- package/dist/core/numbering.js +0 -0
- package/dist/core/ports.cjs +41 -0
- package/dist/core/ports.d.cts +29 -0
- package/dist/core/ports.d.ts +29 -0
- package/dist/core/ports.js +0 -0
- package/dist/core/schema.cjs +173 -0
- package/dist/core/schema.d.cts +33 -0
- package/dist/core/schema.d.ts +33 -0
- package/dist/{chunk-NA3DLROL.js → core/schema.js} +34 -72
- package/dist/core/status.cjs +47 -0
- package/dist/core/status.d.cts +8 -0
- package/dist/core/status.d.ts +8 -0
- package/dist/core/status.js +7 -0
- package/dist/index.cjs +70 -398
- package/dist/index.d.cts +11 -32
- package/dist/index.d.ts +11 -32
- package/dist/index.js +13 -25
- package/dist/store/ddl.cjs +209 -0
- package/dist/store/ddl.d.cts +56 -0
- package/dist/store/ddl.d.ts +56 -0
- package/dist/store/ddl.js +175 -0
- package/dist/store/hash.cjs +50 -0
- package/dist/store/hash.d.cts +2 -0
- package/dist/store/hash.d.ts +2 -0
- package/dist/store/hash.js +10 -0
- package/dist/store/store.cjs +606 -0
- package/dist/store/store.d.cts +169 -0
- package/dist/store/store.d.ts +169 -0
- package/dist/store/store.js +578 -0
- package/dist/store.cjs +51 -1358
- package/dist/store.d.cts +4 -277
- package/dist/store.d.ts +4 -277
- package/dist/store.js +17 -1114
- package/dist/surface/table.cjs +326 -0
- package/dist/surface/table.d.cts +70 -0
- package/dist/surface/table.d.ts +70 -0
- package/dist/surface/table.js +296 -0
- package/dist/workflows/document-workflows.cjs +314 -0
- package/dist/workflows/document-workflows.d.cts +112 -0
- package/dist/workflows/document-workflows.d.ts +112 -0
- package/dist/workflows/document-workflows.js +281 -0
- package/dist/workflows/effects.cjs +41 -0
- package/dist/workflows/effects.d.cts +68 -0
- package/dist/workflows/effects.d.ts +68 -0
- package/dist/workflows/effects.js +0 -0
- package/dist/workflows/ports.cjs +50 -0
- package/dist/workflows/ports.d.cts +72 -0
- package/dist/workflows/ports.d.ts +72 -0
- package/dist/workflows/ports.js +10 -0
- package/dist/workflows/refusals.cjs +80 -0
- package/dist/workflows/refusals.d.cts +59 -0
- package/dist/workflows/refusals.d.ts +59 -0
- package/dist/workflows/refusals.js +40 -0
- package/dist/workflows.cjs +52 -504
- package/dist/workflows.d.cts +4 -310
- package/dist/workflows.d.ts +4 -310
- package/dist/workflows.js +21 -368
- package/package.json +8 -8
- package/dist/chunk-A6ZU4QU3.js +0 -171
- package/dist/entities-DTNJMeVf.d.cts +0 -205
- package/dist/entities-DTNJMeVf.d.ts +0 -205
- package/dist/ports-C_OfflMQ.d.cts +0 -66
- package/dist/ports-C_OfflMQ.d.ts +0 -66
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// src/surface/table.ts
|
|
2
|
+
import { Document } from "../core/document.js";
|
|
3
|
+
import {
|
|
4
|
+
arrayOf,
|
|
5
|
+
booleanSchema,
|
|
6
|
+
literalOf,
|
|
7
|
+
merge,
|
|
8
|
+
nullable,
|
|
9
|
+
numberSchema,
|
|
10
|
+
optional,
|
|
11
|
+
stringSchema,
|
|
12
|
+
unknownSchema
|
|
13
|
+
} from "../core/schema.js";
|
|
14
|
+
import { canTransition } from "../core/status.js";
|
|
15
|
+
var doorFor = (store, name) => {
|
|
16
|
+
const door = store[name];
|
|
17
|
+
if (door === undefined) {
|
|
18
|
+
throw new Error(`"${name}" names no store this table's registry can find — the table and the store must share one registry`);
|
|
19
|
+
}
|
|
20
|
+
return door;
|
|
21
|
+
};
|
|
22
|
+
var HEAD_COLUMNS = Object.keys(Document.headFields);
|
|
23
|
+
var SORT_COLUMNS = [...HEAD_COLUMNS, "number", "createdAt", "updatedAt"];
|
|
24
|
+
var rowsFor = (definition, store, schema) => {
|
|
25
|
+
const { name } = definition;
|
|
26
|
+
const door = doorFor(store, name);
|
|
27
|
+
const basePath = `/${definition.basePath}`;
|
|
28
|
+
const instanceSchema = merge([
|
|
29
|
+
Document.head,
|
|
30
|
+
Document.record,
|
|
31
|
+
schema.object({ body: definition.view, entity: schema.enum([name]), number: stringSchema })
|
|
32
|
+
]);
|
|
33
|
+
const commitOutput = schema.union([
|
|
34
|
+
schema.object({
|
|
35
|
+
committed: literalOf(false),
|
|
36
|
+
lineage: numberSchema,
|
|
37
|
+
ref: stringSchema,
|
|
38
|
+
version: numberSchema
|
|
39
|
+
}),
|
|
40
|
+
schema.object({
|
|
41
|
+
archived: optional(booleanSchema),
|
|
42
|
+
committed: literalOf(true),
|
|
43
|
+
hash: stringSchema,
|
|
44
|
+
lineage: numberSchema,
|
|
45
|
+
previous: nullable(schema.object({ hash: stringSchema, version: numberSchema })),
|
|
46
|
+
ref: stringSchema,
|
|
47
|
+
version: numberSchema
|
|
48
|
+
})
|
|
49
|
+
]);
|
|
50
|
+
const retireLikeOutput = schema.object({
|
|
51
|
+
hash: stringSchema,
|
|
52
|
+
lineage: numberSchema,
|
|
53
|
+
ref: stringSchema,
|
|
54
|
+
version: numberSchema
|
|
55
|
+
});
|
|
56
|
+
const verifyOutput = schema.union([
|
|
57
|
+
schema.object({ ok: literalOf(true), versions: numberSchema }),
|
|
58
|
+
schema.object({
|
|
59
|
+
brokenAt: numberSchema,
|
|
60
|
+
expected: stringSchema,
|
|
61
|
+
found: stringSchema,
|
|
62
|
+
ok: literalOf(false)
|
|
63
|
+
})
|
|
64
|
+
]);
|
|
65
|
+
const historyOutput = arrayOf(schema.object({
|
|
66
|
+
actor: stringSchema,
|
|
67
|
+
at: numberSchema,
|
|
68
|
+
body: unknownSchema,
|
|
69
|
+
hash: stringSchema,
|
|
70
|
+
kind: schema.enum(["committed", "retired", "reinstated"]),
|
|
71
|
+
reason: nullable(stringSchema),
|
|
72
|
+
version: numberSchema
|
|
73
|
+
}));
|
|
74
|
+
const listInput = schema.object({
|
|
75
|
+
cursor: optional(nullable(stringSchema)),
|
|
76
|
+
filter: optional(schema.object(Object.fromEntries(HEAD_COLUMNS.map((column) => [column, optional(stringSchema)])))),
|
|
77
|
+
limit: optional(numberSchema),
|
|
78
|
+
order: optional(schema.object({
|
|
79
|
+
column: schema.enum(SORT_COLUMNS),
|
|
80
|
+
direction: optional(schema.enum(["asc", "desc"]))
|
|
81
|
+
}))
|
|
82
|
+
});
|
|
83
|
+
const listOutput = schema.object({
|
|
84
|
+
items: arrayOf(merge([instanceSchema, schema.object({ card: definition.card.schema })])),
|
|
85
|
+
nextCursor: nullable(stringSchema)
|
|
86
|
+
});
|
|
87
|
+
const ackOutput = schema.object({ ref: stringSchema });
|
|
88
|
+
return [
|
|
89
|
+
{
|
|
90
|
+
call: (handle, params) => door.get(handle, params),
|
|
91
|
+
input: schema.object({ ref: stringSchema }),
|
|
92
|
+
name: `${name}.get`,
|
|
93
|
+
output: nullable(instanceSchema),
|
|
94
|
+
path: `${basePath}/:ref`,
|
|
95
|
+
scope: "org",
|
|
96
|
+
verb: "query"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
call: async (handle, params) => {
|
|
100
|
+
const page = await door.list(handle, params);
|
|
101
|
+
return {
|
|
102
|
+
items: page.items.map((item) => ({
|
|
103
|
+
...item,
|
|
104
|
+
card: definition.card.of(item, { counterpartyName: "" })
|
|
105
|
+
})),
|
|
106
|
+
nextCursor: page.nextCursor
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
input: listInput,
|
|
110
|
+
name: `${name}.list`,
|
|
111
|
+
output: listOutput,
|
|
112
|
+
path: basePath,
|
|
113
|
+
scope: "org",
|
|
114
|
+
verb: "query"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
call: (handle, params) => door.history(handle, params),
|
|
118
|
+
input: schema.object({ lineage: optional(numberSchema), ref: stringSchema }),
|
|
119
|
+
name: `${name}.history`,
|
|
120
|
+
output: historyOutput,
|
|
121
|
+
path: `${basePath}/:ref/history`,
|
|
122
|
+
scope: "org",
|
|
123
|
+
verb: "query"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
call: (handle, params) => door.verify(handle, params),
|
|
127
|
+
input: schema.object({ lineage: optional(numberSchema), ref: stringSchema }),
|
|
128
|
+
name: `${name}.verify`,
|
|
129
|
+
output: verifyOutput,
|
|
130
|
+
path: `${basePath}/:ref/verify`,
|
|
131
|
+
scope: "org",
|
|
132
|
+
verb: "query"
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
call: (handle, params) => door.commit(handle, params),
|
|
136
|
+
input: schema.object({
|
|
137
|
+
actor: stringSchema,
|
|
138
|
+
body: definition.body,
|
|
139
|
+
reason: optional(stringSchema),
|
|
140
|
+
ref: stringSchema
|
|
141
|
+
}),
|
|
142
|
+
name: `${name}.commit`,
|
|
143
|
+
output: commitOutput,
|
|
144
|
+
path: basePath,
|
|
145
|
+
scope: "org",
|
|
146
|
+
verb: "mutation"
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
call: (handle, params) => door.retire(handle, params),
|
|
150
|
+
input: schema.object({
|
|
151
|
+
actor: stringSchema,
|
|
152
|
+
reason: optional(stringSchema),
|
|
153
|
+
ref: stringSchema
|
|
154
|
+
}),
|
|
155
|
+
name: `${name}.retire`,
|
|
156
|
+
output: retireLikeOutput,
|
|
157
|
+
path: `${basePath}/:ref/retire`,
|
|
158
|
+
scope: "org",
|
|
159
|
+
verb: "mutation"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
call: (handle, params) => door.reinstate(handle, params),
|
|
163
|
+
input: schema.object({
|
|
164
|
+
actor: stringSchema,
|
|
165
|
+
reason: optional(stringSchema),
|
|
166
|
+
ref: stringSchema
|
|
167
|
+
}),
|
|
168
|
+
name: `${name}.reinstate`,
|
|
169
|
+
output: retireLikeOutput,
|
|
170
|
+
path: `${basePath}/:ref/reinstate`,
|
|
171
|
+
scope: "org",
|
|
172
|
+
verb: "mutation"
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
call: async (handle, params) => {
|
|
176
|
+
const asked = params;
|
|
177
|
+
const current = await door.get(handle, params);
|
|
178
|
+
if (current === null) {
|
|
179
|
+
throw new Error(`"${asked.ref}" (${name}) has no document to set a status on`);
|
|
180
|
+
}
|
|
181
|
+
const from = definition.statusOf(current.body);
|
|
182
|
+
if (!canTransition(definition.statuses, from, asked.status)) {
|
|
183
|
+
throw new Error(`"${name}" cannot move "${asked.ref}" from "${from}" to "${asked.status}": the status machine declares no such transition`);
|
|
184
|
+
}
|
|
185
|
+
const body = definition.withStatus(current.body, asked.status, new Date().toISOString());
|
|
186
|
+
return door.commit(handle, { ...asked, body });
|
|
187
|
+
},
|
|
188
|
+
input: schema.object({
|
|
189
|
+
actor: stringSchema,
|
|
190
|
+
reason: optional(stringSchema),
|
|
191
|
+
ref: stringSchema,
|
|
192
|
+
status: schema.enum(definition.statuses.statuses)
|
|
193
|
+
}),
|
|
194
|
+
name: `${name}.setStatus`,
|
|
195
|
+
output: commitOutput,
|
|
196
|
+
path: `${basePath}/:ref/status`,
|
|
197
|
+
scope: "org",
|
|
198
|
+
verb: "mutation"
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
call: { pending: "T3", workflow: `${name}.create` },
|
|
202
|
+
input: schema.object({ draft: definition.draft }),
|
|
203
|
+
name: `${name}.create`,
|
|
204
|
+
output: commitOutput,
|
|
205
|
+
path: basePath,
|
|
206
|
+
scope: "org",
|
|
207
|
+
verb: "mutation"
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
call: { pending: "T3", workflow: `${name}.send` },
|
|
211
|
+
input: schema.object({ ref: stringSchema }),
|
|
212
|
+
name: `${name}.send`,
|
|
213
|
+
output: ackOutput,
|
|
214
|
+
path: `${basePath}/:ref/send`,
|
|
215
|
+
scope: "org",
|
|
216
|
+
verb: "mutation"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
call: { pending: "T3", workflow: `${name}.duplicate` },
|
|
220
|
+
input: schema.object({ ref: stringSchema }),
|
|
221
|
+
name: `${name}.duplicate`,
|
|
222
|
+
output: commitOutput,
|
|
223
|
+
path: `${basePath}/:ref/duplicate`,
|
|
224
|
+
scope: "org",
|
|
225
|
+
verb: "mutation"
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
call: { pending: "T3", workflow: `${name}.runStarted` },
|
|
229
|
+
input: schema.object({ ref: stringSchema }),
|
|
230
|
+
name: `${name}.runStarted`,
|
|
231
|
+
output: ackOutput,
|
|
232
|
+
path: `${basePath}/:ref/run-started`,
|
|
233
|
+
scope: "org",
|
|
234
|
+
verb: "mutation"
|
|
235
|
+
}
|
|
236
|
+
];
|
|
237
|
+
};
|
|
238
|
+
var documentSurface = (options) => {
|
|
239
|
+
const { registry, schema, store } = options;
|
|
240
|
+
const headsColumns = [...HEAD_COLUMNS, "entity", "number"];
|
|
241
|
+
const headsInput = schema.object({
|
|
242
|
+
columns: arrayOf(schema.enum(headsColumns)),
|
|
243
|
+
entity: optional(schema.union([stringSchema, arrayOf(stringSchema)])),
|
|
244
|
+
order: arrayOf(schema.object({
|
|
245
|
+
column: schema.enum([...SORT_COLUMNS, "entity"]),
|
|
246
|
+
direction: optional(schema.enum(["asc", "desc"]))
|
|
247
|
+
})),
|
|
248
|
+
where: optional(unknownSchema)
|
|
249
|
+
});
|
|
250
|
+
const aggregateInput = schema.object({
|
|
251
|
+
by: optional(unknownSchema),
|
|
252
|
+
entities: optional(arrayOf(stringSchema)),
|
|
253
|
+
entity: optional(stringSchema),
|
|
254
|
+
measures: arrayOf(schema.enum(["count", "sum:total"])),
|
|
255
|
+
where: optional(unknownSchema)
|
|
256
|
+
});
|
|
257
|
+
const aggregateOutput = arrayOf(schema.object({
|
|
258
|
+
by: optional(stringSchema),
|
|
259
|
+
count: optional(numberSchema),
|
|
260
|
+
sum: optional(stringSchema)
|
|
261
|
+
}));
|
|
262
|
+
const sharedOutput = schema.union(registry.fold((entity) => schema.object({ entity: schema.enum([entity.name]), view: entity.view })));
|
|
263
|
+
return [
|
|
264
|
+
...registry.fold((entity) => rowsFor(entity, store, schema)).flat(),
|
|
265
|
+
{
|
|
266
|
+
call: (handle, params) => store.heads(handle, params),
|
|
267
|
+
input: headsInput,
|
|
268
|
+
name: "heads",
|
|
269
|
+
output: arrayOf(unknownSchema),
|
|
270
|
+
path: "/documents/heads",
|
|
271
|
+
scope: "org",
|
|
272
|
+
verb: "query"
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
call: (handle, params) => store.aggregate(handle, params),
|
|
276
|
+
input: aggregateInput,
|
|
277
|
+
name: "aggregate",
|
|
278
|
+
output: aggregateOutput,
|
|
279
|
+
path: "/documents/aggregate",
|
|
280
|
+
scope: "org",
|
|
281
|
+
verb: "query"
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
call: { pending: "T3", workflow: "document.shared" },
|
|
285
|
+
input: schema.object({ token: stringSchema }),
|
|
286
|
+
name: "shared",
|
|
287
|
+
output: sharedOutput,
|
|
288
|
+
path: "/documents/shared",
|
|
289
|
+
scope: "token",
|
|
290
|
+
verb: "query"
|
|
291
|
+
}
|
|
292
|
+
];
|
|
293
|
+
};
|
|
294
|
+
export {
|
|
295
|
+
documentSurface
|
|
296
|
+
};
|
|
@@ -0,0 +1,314 @@
|
|
|
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/document-workflows.ts
|
|
40
|
+
var exports_document_workflows = {};
|
|
41
|
+
__export(exports_document_workflows, {
|
|
42
|
+
documentWorkflows: () => documentWorkflows
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(exports_document_workflows);
|
|
45
|
+
var import_workflows = require("@geonosis/workflows");
|
|
46
|
+
var import_actor = require("../core/actor.cjs");
|
|
47
|
+
var import_schema = require("../core/schema.cjs");
|
|
48
|
+
var import_status = require("../core/status.cjs");
|
|
49
|
+
var import_refusals = require("./refusals.cjs");
|
|
50
|
+
var withArticle = (noun) => `${/^[aeiou]/i.test(noun) ? "an" : "a"} ${noun}`;
|
|
51
|
+
var capitalised = (noun) => `${noun.charAt(0).toUpperCase()}${noun.slice(1)}`;
|
|
52
|
+
var mailText = (opening, url, ttlDays) => `${opening}
|
|
53
|
+
|
|
54
|
+
${url}
|
|
55
|
+
|
|
56
|
+
The link works for ${ttlDays} days.
|
|
57
|
+
`;
|
|
58
|
+
var typed = (schema) => schema;
|
|
59
|
+
var defaultActor = (ctx, workflowName) => {
|
|
60
|
+
const held = ctx?.actor;
|
|
61
|
+
return typeof held === "string" ? held : import_actor.systemActor(workflowName);
|
|
62
|
+
};
|
|
63
|
+
var documentWorkflows = (entity, options) => {
|
|
64
|
+
const { effects, ports } = options;
|
|
65
|
+
const name = entity.name;
|
|
66
|
+
const article = withArticle(entity.noun);
|
|
67
|
+
const noun = capitalised(entity.noun);
|
|
68
|
+
const actorOf = ports.actor ?? defaultActor;
|
|
69
|
+
const guard = ports.guard ?? (() => Promise.resolve());
|
|
70
|
+
const now = ports.now ?? (() => new Date().toISOString());
|
|
71
|
+
const seed = ports.seed ?? ((_ctx, seeding) => {
|
|
72
|
+
if (seeding.templateId !== undefined) {
|
|
73
|
+
throw new Error(`${article} takes no template: this host declares no seed port`);
|
|
74
|
+
}
|
|
75
|
+
return Promise.resolve(seeding.input);
|
|
76
|
+
});
|
|
77
|
+
const beforeCreate = effects.beforeCreate ?? ((_ctx, body) => Promise.resolve(body));
|
|
78
|
+
const mailWords = effects.mail ?? ((number) => ({
|
|
79
|
+
subject: `${noun} ${number}`,
|
|
80
|
+
opening: `${noun} ${number} is ready for you to read.`
|
|
81
|
+
}));
|
|
82
|
+
const announce = (fact) => effects.announce(fact);
|
|
83
|
+
const write = async (ctx, written, workflowName) => {
|
|
84
|
+
await ports.door(ctx, written.entity).commit({
|
|
85
|
+
actor: actorOf(ctx, workflowName),
|
|
86
|
+
body: written.body,
|
|
87
|
+
ref: written.number
|
|
88
|
+
});
|
|
89
|
+
return written;
|
|
90
|
+
};
|
|
91
|
+
const stored = async (ctx, at) => {
|
|
92
|
+
const found = await ports.door(ctx, name).get({ ref: at.ref });
|
|
93
|
+
if (!found) {
|
|
94
|
+
throw new import_refusals.DocumentNotFound({
|
|
95
|
+
entity: name,
|
|
96
|
+
message: `no ${entity.noun} is stored as ${at.ref}`,
|
|
97
|
+
ref: at.ref
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return found;
|
|
101
|
+
};
|
|
102
|
+
const refuseTaken = async (ctx, at) => {
|
|
103
|
+
const owner = at.entity ?? name;
|
|
104
|
+
if (await ports.door(ctx, owner).get({ ref: at.ref })) {
|
|
105
|
+
throw new import_refusals.DocumentTaken({
|
|
106
|
+
entity: owner,
|
|
107
|
+
message: `a ${owner} is already stored as ${at.ref}`,
|
|
108
|
+
ref: at.ref
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const refFor = async (ctx, given, draft) => {
|
|
113
|
+
const numbering = entity.numbering;
|
|
114
|
+
if (numbering.mode === "given") {
|
|
115
|
+
if (given === undefined) {
|
|
116
|
+
throw new import_refusals.NumberingRefused({
|
|
117
|
+
entity: name,
|
|
118
|
+
message: `${article} is numbered by whoever creates it`,
|
|
119
|
+
mode: "given"
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
await refuseTaken(ctx, { ref: given });
|
|
123
|
+
return { ref: given, minted: null };
|
|
124
|
+
}
|
|
125
|
+
if (given !== undefined) {
|
|
126
|
+
throw new import_refusals.NumberingRefused({
|
|
127
|
+
entity: name,
|
|
128
|
+
message: `${article} is numbered by the organisation`,
|
|
129
|
+
mode: "counter"
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (ports.counter === undefined) {
|
|
133
|
+
throw new import_refusals.NumberingRefused({
|
|
134
|
+
entity: name,
|
|
135
|
+
message: `${article} is numbered from a counter and this host declares no counter port`,
|
|
136
|
+
mode: "counter"
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const series = numbering.series(draft);
|
|
140
|
+
const { number: sequence } = await ports.counter(ctx, numbering.counter);
|
|
141
|
+
return { ref: numbering.format(series, sequence), minted: { sequence } };
|
|
142
|
+
};
|
|
143
|
+
const shareLink = import_workflows.createStep({ name: "create-share-link", noCompensation: true }, (input, ctx) => ports.link(ctx, { entity: name, ...input }));
|
|
144
|
+
const sendMail = import_workflows.createStep({ name: "send-email", noCompensation: true }, async (input, ctx) => {
|
|
145
|
+
const sent = await ports.mail(ctx, input);
|
|
146
|
+
if (sent.isErr())
|
|
147
|
+
throw sent.error;
|
|
148
|
+
return { to: input.to };
|
|
149
|
+
});
|
|
150
|
+
const createName = `${name}.create`;
|
|
151
|
+
const create = import_workflows.createWorkflow(createName, async (input, ctx) => {
|
|
152
|
+
await guard(ctx);
|
|
153
|
+
const draft = await import_schema.validate(entity.draft, await seed(ctx, {
|
|
154
|
+
entity: name,
|
|
155
|
+
templateId: input.templateId,
|
|
156
|
+
input: input.body
|
|
157
|
+
}));
|
|
158
|
+
const minted = await refFor(ctx, input.ref, draft);
|
|
159
|
+
const body = entity.prepare(await beforeCreate(ctx, entity.fromDraft(draft, minted.ref)));
|
|
160
|
+
const written = await write(ctx, { entity: name, number: minted.ref, body }, createName);
|
|
161
|
+
return { ...written, minted: minted.minted };
|
|
162
|
+
}, {
|
|
163
|
+
announce: ({ minted, ...written }) => announce({ kind: "created", written, minted }),
|
|
164
|
+
input: typed(import_schema.objectOf({
|
|
165
|
+
body: entity.draft,
|
|
166
|
+
ref: import_schema.optional(import_schema.nonEmptyString),
|
|
167
|
+
templateId: import_schema.optional(import_schema.nonEmptyString)
|
|
168
|
+
}))
|
|
169
|
+
});
|
|
170
|
+
const saveName = `${name}.save`;
|
|
171
|
+
const save = import_workflows.createWorkflow(saveName, async (input, ctx) => {
|
|
172
|
+
const current = await stored(ctx, input);
|
|
173
|
+
const body = entity.prepare(entity.merge(current.body, input.body, now()));
|
|
174
|
+
return write(ctx, { entity: name, number: input.ref, body }, saveName);
|
|
175
|
+
}, {
|
|
176
|
+
announce: (written) => announce({ kind: "saved", written }),
|
|
177
|
+
input: typed(import_schema.objectOf({ body: entity.incoming, ref: import_schema.nonEmptyString }))
|
|
178
|
+
});
|
|
179
|
+
const setStatusName = `${name}.set-status`;
|
|
180
|
+
const setStatus = import_workflows.createWorkflow(setStatusName, async (input, ctx) => {
|
|
181
|
+
const current = await stored(ctx, input);
|
|
182
|
+
const from = entity.statusOf(current.body);
|
|
183
|
+
if (!import_status.canTransition(entity.statuses, from, input.to)) {
|
|
184
|
+
throw new import_refusals.TransitionRefused({
|
|
185
|
+
entity: name,
|
|
186
|
+
from,
|
|
187
|
+
message: `${article} cannot go from ${from} to ${input.to}`,
|
|
188
|
+
ref: input.ref,
|
|
189
|
+
to: input.to
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const written = await write(ctx, {
|
|
193
|
+
entity: name,
|
|
194
|
+
number: input.ref,
|
|
195
|
+
body: entity.withStatus(current.body, input.to, now())
|
|
196
|
+
}, setStatusName);
|
|
197
|
+
return { ...written, to: input.to };
|
|
198
|
+
}, {
|
|
199
|
+
announce: ({ to, ...written }) => announce({ kind: "moved", written, to }),
|
|
200
|
+
input: typed(import_schema.objectOf({ ref: import_schema.nonEmptyString, to: import_schema.enumOf(entity.statuses.statuses) }))
|
|
201
|
+
});
|
|
202
|
+
const refInput = typed(import_schema.objectOf({ ref: import_schema.nonEmptyString }));
|
|
203
|
+
const deleteName = `${name}.delete`;
|
|
204
|
+
const remove = import_workflows.createWorkflow(deleteName, async (input, ctx) => {
|
|
205
|
+
if (!entity.removable) {
|
|
206
|
+
throw new import_refusals.DocumentNotRemovable({
|
|
207
|
+
entity: name,
|
|
208
|
+
message: `deleting is not something ${article} does`
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const door = ports.door(ctx, name);
|
|
212
|
+
const found = await door.get({ ref: input.ref });
|
|
213
|
+
if (!found) {
|
|
214
|
+
throw new import_refusals.DocumentNotFound({
|
|
215
|
+
entity: name,
|
|
216
|
+
message: `no ${entity.noun} is stored as ${input.ref}`,
|
|
217
|
+
ref: input.ref
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
await door.retire({ actor: actorOf(ctx, deleteName), ref: input.ref });
|
|
221
|
+
return { number: input.ref };
|
|
222
|
+
}, {
|
|
223
|
+
announce: (removed) => announce({ kind: "deleted", number: removed.number }),
|
|
224
|
+
input: refInput
|
|
225
|
+
});
|
|
226
|
+
const sendName = `${name}.send`;
|
|
227
|
+
const send = import_workflows.createWorkflow(sendName, async (input, ctx) => {
|
|
228
|
+
const current = await stored(ctx, input);
|
|
229
|
+
const link = await shareLink({
|
|
230
|
+
ref: input.ref,
|
|
231
|
+
now: input.now,
|
|
232
|
+
...input.ttlDays === undefined ? {} : { ttlDays: input.ttlDays }
|
|
233
|
+
});
|
|
234
|
+
const marked = entity.onSent(current.body);
|
|
235
|
+
const written = marked === current.body ? { entity: name, number: input.ref, body: current.body } : await write(ctx, { entity: name, number: input.ref, body: marked }, sendName);
|
|
236
|
+
const words = mailWords(input.ref);
|
|
237
|
+
await sendMail({
|
|
238
|
+
to: input.to,
|
|
239
|
+
subject: words.subject,
|
|
240
|
+
text: mailText(words.opening, link.url, link.ttlDays)
|
|
241
|
+
});
|
|
242
|
+
return { ...written, to: input.to, url: link.url, expiresAt: link.expiresAt };
|
|
243
|
+
}, {
|
|
244
|
+
announce: ({ to, url, expiresAt, ...written }) => announce({ kind: "sent", written, to, url, expiresAt }),
|
|
245
|
+
execution: "durable",
|
|
246
|
+
idempotent: (input) => `${sendName}:${input.ref}:${input.to}`,
|
|
247
|
+
input: typed(import_schema.objectOf({
|
|
248
|
+
now: import_schema.positiveInt,
|
|
249
|
+
ref: import_schema.nonEmptyString,
|
|
250
|
+
to: import_schema.nonEmptyString,
|
|
251
|
+
ttlDays: import_schema.optional(import_schema.positiveInt)
|
|
252
|
+
}))
|
|
253
|
+
});
|
|
254
|
+
const convertName = `${name}.convert`;
|
|
255
|
+
const convert = import_workflows.createWorkflow(convertName, async (input, ctx) => {
|
|
256
|
+
const conversion = effects.conversion;
|
|
257
|
+
if (conversion === undefined) {
|
|
258
|
+
throw new import_refusals.ConversionRefused({
|
|
259
|
+
entity: name,
|
|
260
|
+
message: `converting is not something ${article} does`,
|
|
261
|
+
ref: input.ref
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
const source = await stored(ctx, input);
|
|
265
|
+
const refusal = conversion.refuse(source.body);
|
|
266
|
+
if (refusal !== null) {
|
|
267
|
+
throw new import_refusals.ConversionRefused({
|
|
268
|
+
entity: name,
|
|
269
|
+
message: `${input.ref} ${refusal}`,
|
|
270
|
+
ref: input.ref
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
await refuseTaken(ctx, { ref: input.ref, entity: conversion.to.name });
|
|
274
|
+
await ports.door(ctx, conversion.to.name).commit({
|
|
275
|
+
actor: actorOf(ctx, convertName),
|
|
276
|
+
body: conversion.bodyFor(source.body, input.ref),
|
|
277
|
+
ref: input.ref
|
|
278
|
+
});
|
|
279
|
+
await write(ctx, {
|
|
280
|
+
entity: name,
|
|
281
|
+
number: input.ref,
|
|
282
|
+
body: conversion.markSource(source.body, input.ref)
|
|
283
|
+
}, convertName);
|
|
284
|
+
return { entity: conversion.to.name, number: input.ref };
|
|
285
|
+
}, {
|
|
286
|
+
announce: (converted) => announce({ kind: "converted", number: converted.number, to: converted.entity }),
|
|
287
|
+
input: refInput
|
|
288
|
+
});
|
|
289
|
+
const duplicateName = `${name}.duplicate`;
|
|
290
|
+
const duplicate = import_workflows.createWorkflow(duplicateName, async (input, ctx) => {
|
|
291
|
+
await guard(ctx);
|
|
292
|
+
const current = await stored(ctx, input);
|
|
293
|
+
const body = entity.copy(current.body);
|
|
294
|
+
const minted = await refFor(ctx, input.targetRef, body);
|
|
295
|
+
const written = await write(ctx, { entity: name, number: minted.ref, body }, duplicateName);
|
|
296
|
+
return { ...written, minted: minted.minted };
|
|
297
|
+
}, {
|
|
298
|
+
announce: ({ minted, ...written }) => announce({ kind: "created", written, minted }),
|
|
299
|
+
input: typed(import_schema.objectOf({ ref: import_schema.nonEmptyString, targetRef: import_schema.optional(import_schema.nonEmptyString) }))
|
|
300
|
+
});
|
|
301
|
+
const share = import_workflows.createWorkflow(`${name}.share`, async (input, ctx) => {
|
|
302
|
+
await stored(ctx, input);
|
|
303
|
+
const link = await shareLink({
|
|
304
|
+
ref: input.ref,
|
|
305
|
+
now: input.now,
|
|
306
|
+
...input.ttlDays === undefined ? {} : { ttlDays: input.ttlDays }
|
|
307
|
+
});
|
|
308
|
+
return { number: input.ref, token: link.token, url: link.url, expiresAt: link.expiresAt };
|
|
309
|
+
}, {
|
|
310
|
+
announce: (link) => announce({ kind: "shared", number: link.number, url: link.url, expiresAt: link.expiresAt }),
|
|
311
|
+
input: typed(import_schema.objectOf({ now: import_schema.positiveInt, ref: import_schema.nonEmptyString, ttlDays: import_schema.optional(import_schema.positiveInt) }))
|
|
312
|
+
});
|
|
313
|
+
return { entity, create, save, setStatus, remove, send, convert, duplicate, share };
|
|
314
|
+
};
|