@zerodep-dev/flow-ts-engine 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/client/client.d.ts +112 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.js +67 -0
- package/dist/client/client.js.map +1 -0
- package/dist/client/endpoint.d.ts +20 -0
- package/dist/client/endpoint.d.ts.map +1 -0
- package/dist/client/endpoint.js +153 -0
- package/dist/client/endpoint.js.map +1 -0
- package/dist/client/http.d.ts +30 -0
- package/dist/client/http.d.ts.map +1 -0
- package/dist/client/http.js +67 -0
- package/dist/client/http.js.map +1 -0
- package/dist/client/index.d.ts +12 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +11 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/port.d.ts +24 -0
- package/dist/client/port.d.ts.map +1 -0
- package/dist/client/port.js +53 -0
- package/dist/client/port.js.map +1 -0
- package/dist/client/protocol.d.ts +26 -0
- package/dist/client/protocol.d.ts.map +1 -0
- package/dist/client/protocol.js +13 -0
- package/dist/client/protocol.js.map +1 -0
- package/dist/commands/admin.d.ts +41 -0
- package/dist/commands/admin.d.ts.map +1 -0
- package/dist/commands/admin.js +228 -0
- package/dist/commands/admin.js.map +1 -0
- package/dist/commands/complete.d.ts +36 -0
- package/dist/commands/complete.d.ts.map +1 -0
- package/dist/commands/complete.js +289 -0
- package/dist/commands/complete.js.map +1 -0
- package/dist/commands/dispatch.d.ts +11 -0
- package/dist/commands/dispatch.d.ts.map +1 -0
- package/dist/commands/dispatch.js +77 -0
- package/dist/commands/dispatch.js.map +1 -0
- package/dist/commands/operate.d.ts +14 -0
- package/dist/commands/operate.d.ts.map +1 -0
- package/dist/commands/operate.js +72 -0
- package/dist/commands/operate.js.map +1 -0
- package/dist/commands/operations.d.ts +75 -0
- package/dist/commands/operations.d.ts.map +1 -0
- package/dist/commands/operations.js +417 -0
- package/dist/commands/operations.js.map +1 -0
- package/dist/commands/signal.d.ts +9 -0
- package/dist/commands/signal.d.ts.map +1 -0
- package/dist/commands/signal.js +78 -0
- package/dist/commands/signal.js.map +1 -0
- package/dist/commands/start.d.ts +41 -0
- package/dist/commands/start.d.ts.map +1 -0
- package/dist/commands/start.js +186 -0
- package/dist/commands/start.js.map +1 -0
- package/dist/commands/submission.d.ts +20 -0
- package/dist/commands/submission.d.ts.map +1 -0
- package/dist/commands/submission.js +29 -0
- package/dist/commands/submission.js.map +1 -0
- package/dist/core/channels.d.ts +13 -0
- package/dist/core/channels.d.ts.map +1 -0
- package/dist/core/channels.js +13 -0
- package/dist/core/channels.js.map +1 -0
- package/dist/core/context.d.ts +76 -0
- package/dist/core/context.d.ts.map +1 -0
- package/dist/core/context.js +148 -0
- package/dist/core/context.js.map +1 -0
- package/dist/core/entities.d.ts +392 -0
- package/dist/core/entities.d.ts.map +1 -0
- package/dist/core/entities.js +282 -0
- package/dist/core/entities.js.map +1 -0
- package/dist/core/errors.d.ts +35 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +18 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/engine.d.ts +146 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +85 -0
- package/dist/engine.js.map +1 -0
- package/dist/extensions.d.ts +198 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +7 -0
- package/dist/extensions.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/jobs/jobs.d.ts +27 -0
- package/dist/jobs/jobs.d.ts.map +1 -0
- package/dist/jobs/jobs.js +299 -0
- package/dist/jobs/jobs.js.map +1 -0
- package/dist/notify/collect.d.ts +38 -0
- package/dist/notify/collect.d.ts.map +1 -0
- package/dist/notify/collect.js +100 -0
- package/dist/notify/collect.js.map +1 -0
- package/dist/notify/inbox.d.ts +26 -0
- package/dist/notify/inbox.d.ts.map +1 -0
- package/dist/notify/inbox.js +87 -0
- package/dist/notify/inbox.js.map +1 -0
- package/dist/org/directory.d.ts +74 -0
- package/dist/org/directory.d.ts.map +1 -0
- package/dist/org/directory.js +254 -0
- package/dist/org/directory.js.map +1 -0
- package/dist/org/entities.d.ts +69 -0
- package/dist/org/entities.d.ts.map +1 -0
- package/dist/org/entities.js +39 -0
- package/dist/org/entities.js.map +1 -0
- package/dist/org/index.d.ts +5 -0
- package/dist/org/index.d.ts.map +1 -0
- package/dist/org/index.js +5 -0
- package/dist/org/index.js.map +1 -0
- package/dist/proxy/proxies.d.ts +18 -0
- package/dist/proxy/proxies.d.ts.map +1 -0
- package/dist/proxy/proxies.js +102 -0
- package/dist/proxy/proxies.js.map +1 -0
- package/dist/publish/project.d.ts +45 -0
- package/dist/publish/project.d.ts.map +1 -0
- package/dist/publish/project.js +176 -0
- package/dist/publish/project.js.map +1 -0
- package/dist/publish/publish.d.ts +42 -0
- package/dist/publish/publish.d.ts.map +1 -0
- package/dist/publish/publish.js +274 -0
- package/dist/publish/publish.js.map +1 -0
- package/dist/queries/queries.d.ts +24 -0
- package/dist/queries/queries.d.ts.map +1 -0
- package/dist/queries/queries.js +326 -0
- package/dist/queries/queries.js.map +1 -0
- package/dist/queries/stats.d.ts +9 -0
- package/dist/queries/stats.d.ts.map +1 -0
- package/dist/queries/stats.js +189 -0
- package/dist/queries/stats.js.map +1 -0
- package/dist/runtime/assignees.d.ts +32 -0
- package/dist/runtime/assignees.d.ts.map +1 -0
- package/dist/runtime/assignees.js +93 -0
- package/dist/runtime/assignees.js.map +1 -0
- package/dist/runtime/calls.d.ts +18 -0
- package/dist/runtime/calls.d.ts.map +1 -0
- package/dist/runtime/calls.js +182 -0
- package/dist/runtime/calls.js.map +1 -0
- package/dist/runtime/countersign.d.ts +26 -0
- package/dist/runtime/countersign.d.ts.map +1 -0
- package/dist/runtime/countersign.js +63 -0
- package/dist/runtime/countersign.js.map +1 -0
- package/dist/runtime/execution.d.ts +150 -0
- package/dist/runtime/execution.d.ts.map +1 -0
- package/dist/runtime/execution.js +792 -0
- package/dist/runtime/execution.js.map +1 -0
- package/dist/runtime/next-steps.d.ts +56 -0
- package/dist/runtime/next-steps.d.ts.map +1 -0
- package/dist/runtime/next-steps.js +145 -0
- package/dist/runtime/next-steps.js.map +1 -0
- package/dist/runtime/state.d.ts +26 -0
- package/dist/runtime/state.d.ts.map +1 -0
- package/dist/runtime/state.js +32 -0
- package/dist/runtime/state.js.map +1 -0
- package/dist/testing/scenarios.d.ts +35 -0
- package/dist/testing/scenarios.d.ts.map +1 -0
- package/dist/testing/scenarios.js +63 -0
- package/dist/testing/scenarios.js.map +1 -0
- package/dist/testing/simulate.d.ts +55 -0
- package/dist/testing/simulate.d.ts.map +1 -0
- package/dist/testing/simulate.js +122 -0
- package/dist/testing/simulate.js.map +1 -0
- package/dist/testing/testing.d.ts +31 -0
- package/dist/testing/testing.d.ts.map +1 -0
- package/dist/testing/testing.js +54 -0
- package/dist/testing/testing.js.map +1 -0
- package/dist/types.d.ts +283 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/views.d.ts +317 -0
- package/dist/views.d.ts.map +1 -0
- package/dist/views.js +9 -0
- package/dist/views.js.map +1 -0
- package/package.json +76 -3
- package/src/client/client.ts +194 -0
- package/src/client/endpoint.ts +174 -0
- package/src/client/http.ts +99 -0
- package/src/client/index.ts +12 -0
- package/src/client/port.ts +78 -0
- package/src/client/protocol.ts +36 -0
- package/src/commands/admin.ts +308 -0
- package/src/commands/complete.ts +368 -0
- package/src/commands/dispatch.ts +106 -0
- package/src/commands/operate.ts +110 -0
- package/src/commands/operations.ts +524 -0
- package/src/commands/signal.ts +96 -0
- package/src/commands/start.ts +255 -0
- package/src/commands/submission.ts +51 -0
- package/src/core/channels.ts +14 -0
- package/src/core/context.ts +252 -0
- package/src/core/entities.ts +335 -0
- package/src/core/errors.ts +42 -0
- package/src/engine.ts +247 -0
- package/src/extensions.ts +231 -0
- package/src/index.ts +77 -0
- package/src/jobs/jobs.ts +320 -0
- package/src/notify/collect.ts +155 -0
- package/src/notify/inbox.ts +107 -0
- package/src/org/directory.ts +332 -0
- package/src/org/entities.ts +62 -0
- package/src/org/index.ts +21 -0
- package/src/proxy/proxies.ts +132 -0
- package/src/publish/project.ts +230 -0
- package/src/publish/publish.ts +314 -0
- package/src/queries/queries.ts +427 -0
- package/src/queries/stats.ts +207 -0
- package/src/runtime/assignees.ts +121 -0
- package/src/runtime/calls.ts +196 -0
- package/src/runtime/countersign.ts +83 -0
- package/src/runtime/execution.ts +915 -0
- package/src/runtime/next-steps.ts +213 -0
- package/src/runtime/state.ts +55 -0
- package/src/testing/scenarios.ts +95 -0
- package/src/testing/simulate.ts +190 -0
- package/src/testing/testing.ts +84 -0
- package/src/types.ts +286 -0
- package/src/views.ts +334 -0
- package/README.md +0 -3
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 发起:建实例并从开始节点走起(发起与调用流程共用 launch),发起前的草稿(drafts),发起时单据的值按清单收与查。
|
|
3
|
+
*/
|
|
4
|
+
import { type Row } from '@zerodep-dev/easy-query-ts';
|
|
5
|
+
import {
|
|
6
|
+
fieldStates,
|
|
7
|
+
isRef,
|
|
8
|
+
formManifest,
|
|
9
|
+
type AssigneeSource,
|
|
10
|
+
type ProcessDefinition,
|
|
11
|
+
plainValue,
|
|
12
|
+
} from '@zerodep-dev/flow-ts-model';
|
|
13
|
+
import { checkPicks, nextSteps, picksOf, type Picks } from '../runtime/next-steps.ts';
|
|
14
|
+
import { FlowDefinition, FlowInstance, type FlowDb } from '../core/entities.ts';
|
|
15
|
+
import { FlowError } from '../core/errors.ts';
|
|
16
|
+
import { Execution, renderTitle, workItemSummary, type InstanceRow } from '../runtime/execution.ts';
|
|
17
|
+
import { emptyRunState } from '../runtime/state.ts';
|
|
18
|
+
import type { Actor, FlowCommand, RunResult } from '../types.ts';
|
|
19
|
+
import type { EngineCore } from '../core/context.ts';
|
|
20
|
+
import { submit, throwFormErrors } from './submission.ts';
|
|
21
|
+
import { predictScope } from '../runtime/next-steps.ts';
|
|
22
|
+
import type { FlowEngine } from '../engine.ts';
|
|
23
|
+
|
|
24
|
+
/** 定义里写的变量名:`form.字段` 就是流程变量里的“字段” */
|
|
25
|
+
export const variableName = (name: string): string =>
|
|
26
|
+
name.startsWith('form.') ? name.slice(5) : name;
|
|
27
|
+
|
|
28
|
+
/** 定义里直接写的编号(引用在发起前取不到值) */
|
|
29
|
+
const literal = (value: unknown): string | undefined =>
|
|
30
|
+
isRef(value) ? undefined : String(value as string | number);
|
|
31
|
+
|
|
32
|
+
/** 能不能发起:发起人名单里的人、角色、部门 */
|
|
33
|
+
export function canStart(starters: readonly AssigneeSource[], actor: Actor): boolean {
|
|
34
|
+
return starters.some((source) => {
|
|
35
|
+
if ('user' in source) return literal(source.user) === actor.id;
|
|
36
|
+
if ('role' in source) return actor.roles?.includes(literal(source.role) ?? '') ?? false;
|
|
37
|
+
if ('department' in source) {
|
|
38
|
+
return actor.departments?.includes(literal(source.department) ?? '') ?? false;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 变量声明里的默认值 */
|
|
45
|
+
export function defaultsOf(definition: ProcessDefinition): Record<string, unknown> {
|
|
46
|
+
return Object.fromEntries(
|
|
47
|
+
(definition.variables ?? [])
|
|
48
|
+
.filter((v) => v.default !== undefined)
|
|
49
|
+
.map((v) => [v.name, v.default]),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 发起:装进引擎核心(core.starts),别的模块经它调用 */
|
|
54
|
+
export function createStarts(core: EngineCore) {
|
|
55
|
+
const { options, db, now, execution, latestDefinition } = core;
|
|
56
|
+
|
|
57
|
+
/** 建一个实例并从开始节点走起(发起与调用流程共用),返回推进现场(还没写回) */
|
|
58
|
+
async function launch(
|
|
59
|
+
tx: FlowDb,
|
|
60
|
+
definition: Row<typeof FlowDefinition>,
|
|
61
|
+
variables: Readonly<Record<string, unknown>>,
|
|
62
|
+
actor: Actor,
|
|
63
|
+
extra: Pick<InstanceRow, 'businessKey' | 'title' | 'parentId' | 'parentNode' | 'parentRound'>,
|
|
64
|
+
/** 从草稿发起:用草稿那一行(同一个实例) */
|
|
65
|
+
draftId?: number,
|
|
66
|
+
/** 发起人给下一步选的处理人;null 时不查(调用流程发起的子实例没有人选) */
|
|
67
|
+
picks: Picks | null = null,
|
|
68
|
+
): Promise<Execution> {
|
|
69
|
+
const model = definition.model;
|
|
70
|
+
const values = {
|
|
71
|
+
definitionId: definition.id,
|
|
72
|
+
processKey: model.key,
|
|
73
|
+
version: definition.version,
|
|
74
|
+
status: 'active' as const,
|
|
75
|
+
result: null,
|
|
76
|
+
current: [],
|
|
77
|
+
variables: { ...defaultsOf(model), ...variables },
|
|
78
|
+
state: emptyRunState(),
|
|
79
|
+
startedBy: actor.id,
|
|
80
|
+
startedAt: now(),
|
|
81
|
+
endedAt: null,
|
|
82
|
+
rev: 0,
|
|
83
|
+
...extra,
|
|
84
|
+
};
|
|
85
|
+
const id = draftId ?? Number(await tx.insert(FlowInstance).values(values).returnId());
|
|
86
|
+
if (draftId !== undefined)
|
|
87
|
+
await tx
|
|
88
|
+
.update(FlowInstance)
|
|
89
|
+
.set(values)
|
|
90
|
+
.where((i) => i.id.eq(draftId))
|
|
91
|
+
.execute();
|
|
92
|
+
const exec = await execution(tx, model, { id, ...values }, actor);
|
|
93
|
+
exec.addEvent(
|
|
94
|
+
'instance.started',
|
|
95
|
+
extra.parentId === null
|
|
96
|
+
? {}
|
|
97
|
+
: { data: { parentId: extra.parentId, parentNode: extra.parentNode } },
|
|
98
|
+
);
|
|
99
|
+
const startNode = model.nodes.find((node) => node.type === 'start');
|
|
100
|
+
if (!startNode) throw new FlowError('invalid-definition', `流程 ${model.key} 没有开始节点`);
|
|
101
|
+
exec.lastHandler = actor.id;
|
|
102
|
+
if (picks !== null)
|
|
103
|
+
exec.state.picks = checkPicks(
|
|
104
|
+
picksOf(
|
|
105
|
+
await nextSteps(predictScope(options, exec, actor.id), startNode.id, { read: exec.read }),
|
|
106
|
+
),
|
|
107
|
+
picks,
|
|
108
|
+
);
|
|
109
|
+
await exec.enter(startNode.id, 'start');
|
|
110
|
+
return exec;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function start(
|
|
114
|
+
command: Extract<FlowCommand, { type: 'start' }>,
|
|
115
|
+
actor: Actor,
|
|
116
|
+
): Promise<RunResult> {
|
|
117
|
+
return db.transaction(async (tx) => {
|
|
118
|
+
const draft =
|
|
119
|
+
command.draft === undefined ? undefined : await ownDraft(tx, command.draft, actor);
|
|
120
|
+
const definition = await latestDefinition(tx, draft?.processKey ?? command.process);
|
|
121
|
+
if (definition.model.starters && !canStart(definition.model.starters, actor)) {
|
|
122
|
+
throw new FlowError('forbidden', `${actor.id} 不能发起 ${command.process}`);
|
|
123
|
+
}
|
|
124
|
+
const submitted = { ...(draft?.variables ?? {}), ...(command.variables ?? {}) };
|
|
125
|
+
const variables = startValues(definition.model, submitted, { required: true });
|
|
126
|
+
const exec = await launch(
|
|
127
|
+
tx,
|
|
128
|
+
definition,
|
|
129
|
+
variables,
|
|
130
|
+
actor,
|
|
131
|
+
{
|
|
132
|
+
businessKey: command.businessKey ?? draft?.businessKey ?? null,
|
|
133
|
+
title: command.title ?? null,
|
|
134
|
+
parentId: null,
|
|
135
|
+
parentNode: null,
|
|
136
|
+
parentRound: null,
|
|
137
|
+
},
|
|
138
|
+
draft?.id,
|
|
139
|
+
command.picks ?? {},
|
|
140
|
+
);
|
|
141
|
+
const { instance, events } = await exec.flush();
|
|
142
|
+
const result = { instance, workItems: exec.created.map(workItemSummary), events };
|
|
143
|
+
await options.intercept?.after?.(command, result, actor);
|
|
144
|
+
return result;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** 发起时的值:按发起节点的权限收(只取能改的、显示条件不成立的丢掉),查值;required 时查必填。清单以外的变量原样留着 */
|
|
149
|
+
function startValues(
|
|
150
|
+
model: ProcessDefinition,
|
|
151
|
+
submitted: Readonly<Record<string, unknown>>,
|
|
152
|
+
{ required }: { readonly required: boolean },
|
|
153
|
+
): Record<string, unknown> {
|
|
154
|
+
const manifest = formManifest(model);
|
|
155
|
+
// 强类型的值先写成文字:存的是普通 JSON,校验时再按清单读成强类型
|
|
156
|
+
submitted = plainValue(submitted) as Readonly<Record<string, unknown>>;
|
|
157
|
+
if (Object.keys(manifest).length === 0) return { ...submitted };
|
|
158
|
+
const start = model.nodes.find((node) => node.type === 'start');
|
|
159
|
+
const stored = defaultsOf(model);
|
|
160
|
+
const read = (name: string) => {
|
|
161
|
+
const key = variableName(name);
|
|
162
|
+
return key in submitted ? submitted[key] : stored[key];
|
|
163
|
+
};
|
|
164
|
+
const states = fieldStates(manifest, { access: start?.form, isStart: true, read });
|
|
165
|
+
const { values, errors } = submit(
|
|
166
|
+
manifest,
|
|
167
|
+
states,
|
|
168
|
+
stored,
|
|
169
|
+
submitted,
|
|
170
|
+
options.extensions?.fieldTypes,
|
|
171
|
+
);
|
|
172
|
+
throwFormErrors(manifest, required ? errors : errors.filter((e) => e.code !== 'required'));
|
|
173
|
+
const others = Object.fromEntries(
|
|
174
|
+
Object.entries(submitted).filter(([name]) => !(name in manifest)),
|
|
175
|
+
);
|
|
176
|
+
return { ...others, ...values };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 自己的草稿(别人的、不是草稿的都当没有) */
|
|
180
|
+
async function ownDraft(tx: FlowDb, id: number, actor: Actor): Promise<InstanceRow> {
|
|
181
|
+
const row = await tx.queryable(FlowInstance).find(id);
|
|
182
|
+
if (!row || row.status !== 'draft' || row.startedBy !== actor.id)
|
|
183
|
+
throw new FlowError('not-found', `没有草稿 ${id}`);
|
|
184
|
+
return row;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const drafts: FlowEngine['drafts'] = {
|
|
188
|
+
save: (input, actor) =>
|
|
189
|
+
db.transaction(async (tx) => {
|
|
190
|
+
const existing =
|
|
191
|
+
input.draft === undefined ? undefined : await ownDraft(tx, input.draft, actor);
|
|
192
|
+
const definition = await latestDefinition(tx, existing?.processKey ?? input.process ?? '');
|
|
193
|
+
const model = definition.model;
|
|
194
|
+
const variables = startValues(
|
|
195
|
+
model,
|
|
196
|
+
{ ...(existing?.variables ?? {}), ...(input.variables ?? {}) },
|
|
197
|
+
{ required: false },
|
|
198
|
+
);
|
|
199
|
+
const title = renderTitle(model.title, (field) => variables[variableName(field)] ?? null);
|
|
200
|
+
const values = {
|
|
201
|
+
definitionId: definition.id,
|
|
202
|
+
processKey: model.key,
|
|
203
|
+
version: definition.version,
|
|
204
|
+
businessKey: input.businessKey ?? existing?.businessKey ?? null,
|
|
205
|
+
title,
|
|
206
|
+
variables,
|
|
207
|
+
startedAt: now(),
|
|
208
|
+
};
|
|
209
|
+
if (existing) {
|
|
210
|
+
await tx
|
|
211
|
+
.update(FlowInstance)
|
|
212
|
+
.set(values)
|
|
213
|
+
.where((i) => i.id.eq(existing.id))
|
|
214
|
+
.execute();
|
|
215
|
+
return { ...existing, ...values };
|
|
216
|
+
}
|
|
217
|
+
const row = {
|
|
218
|
+
...values,
|
|
219
|
+
status: 'draft' as const,
|
|
220
|
+
result: null,
|
|
221
|
+
current: [],
|
|
222
|
+
state: emptyRunState(),
|
|
223
|
+
startedBy: actor.id,
|
|
224
|
+
endedAt: null,
|
|
225
|
+
parentId: null,
|
|
226
|
+
parentNode: null,
|
|
227
|
+
parentRound: null,
|
|
228
|
+
rev: 0,
|
|
229
|
+
};
|
|
230
|
+
const id = Number(await tx.insert(FlowInstance).values(row).returnId());
|
|
231
|
+
return { id, ...row };
|
|
232
|
+
}),
|
|
233
|
+
list: (actor) =>
|
|
234
|
+
db
|
|
235
|
+
.queryable(FlowInstance)
|
|
236
|
+
.where((i) => [i.startedBy.eq(actor.id), i.status.eq('draft')])
|
|
237
|
+
.orderBy((i) => i.id.desc())
|
|
238
|
+
.toList(),
|
|
239
|
+
get: (id, actor) => db.transaction((tx) => ownDraft(tx, id, actor)),
|
|
240
|
+
discard: (id, actor) =>
|
|
241
|
+
db.transaction(async (tx) => {
|
|
242
|
+
await ownDraft(tx, id, actor);
|
|
243
|
+
await tx
|
|
244
|
+
.delete(FlowInstance)
|
|
245
|
+
.where((i) => [i.id.eq(id), i.status.eq('draft')])
|
|
246
|
+
.execute();
|
|
247
|
+
return true;
|
|
248
|
+
}),
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
return { launch, start, startValues, ownDraft, drafts };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** createStarts 给出的函数 */
|
|
255
|
+
export type Starts = ReturnType<typeof createStarts>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 收单据的值:按清单与字段权限合并、查值(引擎存普通 JSON,查的时候读成强类型),不合格时报 invalid-input。
|
|
3
|
+
* 发起、处理、改单据数据、动作里的暂存共用。
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
mergeSubmission,
|
|
7
|
+
typedValue,
|
|
8
|
+
type FieldState,
|
|
9
|
+
type FieldTypeChecks,
|
|
10
|
+
type FormManifest,
|
|
11
|
+
} from '@zerodep-dev/flow-ts-model';
|
|
12
|
+
import { FlowError } from '../core/errors.ts';
|
|
13
|
+
|
|
14
|
+
/** 候选匹配这个人 */
|
|
15
|
+
/**
|
|
16
|
+
* 收提交(mergeSubmission):引擎存的是普通 JSON(小数、日期是文字),清单的值检查要强类型(json-wire 还原的 Decimal、日期),
|
|
17
|
+
* 所以按 typedValue 读成强类型再查;取的值仍是原样的 JSON
|
|
18
|
+
*/
|
|
19
|
+
export function submit(
|
|
20
|
+
manifest: FormManifest,
|
|
21
|
+
states: ReadonlyMap<string, FieldState>,
|
|
22
|
+
stored: Readonly<Record<string, unknown>>,
|
|
23
|
+
submitted: Readonly<Record<string, unknown>>,
|
|
24
|
+
fieldTypes: FieldTypeChecks | undefined,
|
|
25
|
+
) {
|
|
26
|
+
const typed = (row: Readonly<Record<string, unknown>>) =>
|
|
27
|
+
Object.fromEntries(
|
|
28
|
+
Object.entries(row).map(([name, value]) => [
|
|
29
|
+
name,
|
|
30
|
+
manifest[name] ? typedValue(manifest[name], value) : value,
|
|
31
|
+
]),
|
|
32
|
+
);
|
|
33
|
+
const { errors } = mergeSubmission(manifest, states, typed(stored), typed(submitted), fieldTypes);
|
|
34
|
+
const { values } = mergeSubmission(manifest, states, stored, submitted);
|
|
35
|
+
return { values, errors };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 单据的值不合格:报 invalid-input,details.errors 是全部问题,消息说第一处 */
|
|
39
|
+
export function throwFormErrors(
|
|
40
|
+
manifest: FormManifest,
|
|
41
|
+
errors: readonly { readonly field: string; readonly code: string }[],
|
|
42
|
+
): void {
|
|
43
|
+
const [first] = errors;
|
|
44
|
+
if (!first) return;
|
|
45
|
+
const label = manifest[first.field.split(/[.[]/)[0]!]?.label ?? first.field;
|
|
46
|
+
throw new FlowError(
|
|
47
|
+
'invalid-input',
|
|
48
|
+
first.code === 'required' ? `“${label}”要填` : `“${label}”的值不对`,
|
|
49
|
+
{ field: first.field, code: first.code, errors },
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 广播的频道(PG 的 NOTIFY,docs/decisions/0010 第 2 节):引擎在推进、发布的事务里发,随提交发出、回滚就不发。
|
|
3
|
+
* 多实例部署时各进程收听它们,别的进程的改动也能推给自己的页面、刷新自己的组织快照:
|
|
4
|
+
*
|
|
5
|
+
* import { listen } from '@zerodep-dev/easy-query-ts-pg'; // PGlite 用 easy-query-ts-pg/pglite 的 listen
|
|
6
|
+
* await listen(pool, FLOW_CHANNEL, () => endpoint.changed());
|
|
7
|
+
* await listen(pool, ORG_CHANNEL, () => directory.refresh());
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** 流程的数据变了(推进、作业、发布、启停):负载是 JSON,推进时带着实例编号 `{ "instance": 12 }` */
|
|
11
|
+
export const FLOW_CHANNEL = 'flow_changed';
|
|
12
|
+
|
|
13
|
+
/** 组织变了(组织目录的 sync、save*、removeDepartment) */
|
|
14
|
+
export const ORG_CHANNEL = 'flow_org_changed';
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 引擎核心:各模块共用的数据库、选项、时钟、定义与逻辑的缓存,以及锁实例、建推进现场、读待办这些基本动作。
|
|
3
|
+
* 各模块(发起、处理、动作、调用流程、作业……)建好后挂在核心上(core.tasks、core.calls……),
|
|
4
|
+
* 彼此调用都经核心,模块之间不直接导入,也就没有循环依赖。
|
|
5
|
+
*/
|
|
6
|
+
import { createDb, type Row } from '@zerodep-dev/easy-query-ts';
|
|
7
|
+
import { type ProcessDefinition } from '@zerodep-dev/flow-ts-model';
|
|
8
|
+
import { actorTokens } from '../runtime/assignees.ts';
|
|
9
|
+
import { FlowDefinition, FlowInstance, FlowWorkItem, flowSchema, type FlowDb } from './entities.ts';
|
|
10
|
+
import { FlowError } from './errors.ts';
|
|
11
|
+
import {
|
|
12
|
+
Execution,
|
|
13
|
+
workItemSummary,
|
|
14
|
+
type InstanceRow,
|
|
15
|
+
type WorkItemRow,
|
|
16
|
+
} from '../runtime/execution.ts';
|
|
17
|
+
import { excludeToken, operationsOf } from '../commands/operations.ts';
|
|
18
|
+
import { engineModules, linkProject } from '../publish/project.ts';
|
|
19
|
+
import type { FlowLogic } from '../extensions.ts';
|
|
20
|
+
import type { Actor, FlowCommand, FlowEngineOptions, RunResult } from '../types.ts';
|
|
21
|
+
import type { Calls } from '../runtime/calls.ts';
|
|
22
|
+
import type { Starts } from '../commands/start.ts';
|
|
23
|
+
import type { Tasks } from '../commands/complete.ts';
|
|
24
|
+
import type { Ops } from '../commands/operate.ts';
|
|
25
|
+
import type { Admin } from '../commands/admin.ts';
|
|
26
|
+
import type { Signals } from '../commands/signal.ts';
|
|
27
|
+
import type { Commands } from '../commands/dispatch.ts';
|
|
28
|
+
import type { Publishing } from '../publish/publish.ts';
|
|
29
|
+
import type { Queries } from '../queries/queries.ts';
|
|
30
|
+
import type { Jobs } from '../jobs/jobs.ts';
|
|
31
|
+
import type { Notifications } from '../notify/inbox.ts';
|
|
32
|
+
import type { Proxies } from '../proxy/proxies.ts';
|
|
33
|
+
import type { Stats } from '../queries/stats.ts';
|
|
34
|
+
|
|
35
|
+
/** 引擎核心:共用的部分,加上各模块(createFlowEngine 依次装上) */
|
|
36
|
+
export interface EngineCore {
|
|
37
|
+
readonly db: FlowDb;
|
|
38
|
+
readonly options: FlowEngineOptions;
|
|
39
|
+
/** 现在(测试里是假时钟) */
|
|
40
|
+
readonly now: () => Date;
|
|
41
|
+
/** 定义的缓存:版本号 → 定义 */
|
|
42
|
+
readonly definitions: Map<number, ProcessDefinition>;
|
|
43
|
+
/** 版本 → 流程工程的逻辑(每个版本加载一次) */
|
|
44
|
+
readonly logics: Map<number, Promise<FlowLogic | undefined>>;
|
|
45
|
+
/** 待办上的动作:内置的加上选项里的 */
|
|
46
|
+
readonly operations: ReturnType<typeof operationsOf>;
|
|
47
|
+
readonly logicOf: (tx: FlowDb, id: number) => Promise<FlowLogic | undefined>;
|
|
48
|
+
readonly definitionOf: (tx: FlowDb, id: number) => Promise<ProcessDefinition>;
|
|
49
|
+
readonly withInstance: (
|
|
50
|
+
id: number,
|
|
51
|
+
actor: Actor | null,
|
|
52
|
+
work: (exec: Execution, tx: FlowDb) => Promise<void>,
|
|
53
|
+
command?: FlowCommand,
|
|
54
|
+
) => Promise<RunResult>;
|
|
55
|
+
readonly instanceOfItem: (id: number) => Promise<number>;
|
|
56
|
+
readonly openItem: (exec: Execution, tx: FlowDb, id: number) => Promise<WorkItemRow>;
|
|
57
|
+
readonly isCandidate: (item: WorkItemRow, actor: Actor) => boolean;
|
|
58
|
+
readonly execution: (
|
|
59
|
+
tx: FlowDb,
|
|
60
|
+
definition: ProcessDefinition,
|
|
61
|
+
instance: InstanceRow,
|
|
62
|
+
actor: Actor | null,
|
|
63
|
+
) => Promise<Execution>;
|
|
64
|
+
readonly latestDefinition: (tx: FlowDb, key: string) => Promise<Row<typeof FlowDefinition>>;
|
|
65
|
+
readonly itemsOf: (tx: FlowDb, instanceId: number) => Promise<WorkItemRow[]>;
|
|
66
|
+
// ── 各模块(createFlowEngine 依次装上;彼此调用都经这里) ──
|
|
67
|
+
/** 调用流程:发起子实例、子实例结束后回到父实例(runtime/calls.ts) */
|
|
68
|
+
calls: Calls;
|
|
69
|
+
/** 发起与发起前的草稿(commands/start.ts) */
|
|
70
|
+
starts: Starts;
|
|
71
|
+
/** 处理待办与会签推进(commands/complete.ts) */
|
|
72
|
+
tasks: Tasks;
|
|
73
|
+
/** 待办上的动作(commands/operate.ts) */
|
|
74
|
+
ops: Ops;
|
|
75
|
+
/** 实例上的操作:撤回、终止、跳转、暂停、催办、改数据、迁移(commands/admin.ts) */
|
|
76
|
+
admin: Admin;
|
|
77
|
+
/** 外部消息(commands/signal.ts) */
|
|
78
|
+
signals: Signals;
|
|
79
|
+
/** 命令入口与批量执行(commands/dispatch.ts) */
|
|
80
|
+
commands: Commands;
|
|
81
|
+
/** 发布与版本(publish/publish.ts) */
|
|
82
|
+
publishing: Publishing;
|
|
83
|
+
/** 查询(queries/queries.ts) */
|
|
84
|
+
queries: Queries;
|
|
85
|
+
/** 作业(jobs/jobs.ts) */
|
|
86
|
+
jobs: Jobs;
|
|
87
|
+
/** 通知的信箱与发件箱(notify/inbox.ts) */
|
|
88
|
+
notifications: Notifications;
|
|
89
|
+
/** 代理规则(proxy/proxies.ts) */
|
|
90
|
+
proxies: Proxies;
|
|
91
|
+
/** 监控与统计(queries/stats.ts) */
|
|
92
|
+
stats: Stats;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 建引擎核心;各模块的槽由 createFlowEngine 依次装上 */
|
|
96
|
+
export function createCore(options: FlowEngineOptions): EngineCore {
|
|
97
|
+
const db = createDb({ schema: flowSchema, adapter: options.adapter });
|
|
98
|
+
|
|
99
|
+
const now = () => options.clock?.() ?? new Date();
|
|
100
|
+
|
|
101
|
+
const definitions = new Map<number, ProcessDefinition>();
|
|
102
|
+
|
|
103
|
+
const operations = operationsOf(options.operations);
|
|
104
|
+
|
|
105
|
+
/** 版本 → 流程工程的逻辑(每个版本加载一次) */
|
|
106
|
+
const logics = new Map<number, Promise<FlowLogic | undefined>>();
|
|
107
|
+
|
|
108
|
+
function logicOf(tx: FlowDb, id: number): Promise<FlowLogic | undefined> {
|
|
109
|
+
let logic = logics.get(id);
|
|
110
|
+
if (!logic) {
|
|
111
|
+
logic = tx
|
|
112
|
+
.queryable(FlowDefinition)
|
|
113
|
+
.where((d) => d.id.eq(id))
|
|
114
|
+
.select((d) => ({ modules: d.modules }))
|
|
115
|
+
.first()
|
|
116
|
+
.then(async (row) =>
|
|
117
|
+
row?.modules
|
|
118
|
+
? (await linkProject(engineModules(row.modules), options.modules)).logic
|
|
119
|
+
: undefined,
|
|
120
|
+
);
|
|
121
|
+
logics.set(id, logic);
|
|
122
|
+
logic.catch(() => logics.delete(id));
|
|
123
|
+
}
|
|
124
|
+
return logic;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function definitionOf(tx: FlowDb, id: number): Promise<ProcessDefinition> {
|
|
128
|
+
const cached = definitions.get(id);
|
|
129
|
+
if (cached) return cached;
|
|
130
|
+
const row = await tx.queryable(FlowDefinition).findOrThrow(id, `没有流程定义 ${id}`);
|
|
131
|
+
definitions.set(id, row.model);
|
|
132
|
+
return row.model;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 锁住实例,在推进现场里做事,最后写回;是命令时提交前交给 intercept.after */
|
|
136
|
+
async function withInstance(
|
|
137
|
+
id: number,
|
|
138
|
+
actor: Actor | null,
|
|
139
|
+
work: (exec: Execution, tx: FlowDb) => Promise<void>,
|
|
140
|
+
command?: FlowCommand,
|
|
141
|
+
): Promise<RunResult> {
|
|
142
|
+
return db.transaction(async (tx) => {
|
|
143
|
+
if (!(await tx.lock(FlowInstance, id))) throw new FlowError('not-found', `没有实例 ${id}`);
|
|
144
|
+
const instance = await tx.queryable(FlowInstance).findOrThrow(id);
|
|
145
|
+
const exec = await execution(
|
|
146
|
+
tx,
|
|
147
|
+
await definitionOf(tx, instance.definitionId),
|
|
148
|
+
instance,
|
|
149
|
+
actor,
|
|
150
|
+
);
|
|
151
|
+
await work(exec, tx);
|
|
152
|
+
const { instance: summary, events } = await exec.flush();
|
|
153
|
+
await core.calls.afterFlush(tx, exec);
|
|
154
|
+
const result = { instance: summary, workItems: exec.created.map(workItemSummary), events };
|
|
155
|
+
if (command && actor) await options.intercept?.after?.(command, result, actor);
|
|
156
|
+
return result;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 待办所在的实例(先读一次,锁住实例后再重读待办) */
|
|
161
|
+
async function instanceOfItem(id: number): Promise<number> {
|
|
162
|
+
const item = await db.queryable(FlowWorkItem).find(id);
|
|
163
|
+
if (!item) throw new FlowError('not-found', `没有待办 ${id}`);
|
|
164
|
+
return item.instanceId;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** 锁住实例后重读待办:要开着、实例要在进行中 */
|
|
168
|
+
async function openItem(exec: Execution, tx: FlowDb, id: number): Promise<WorkItemRow> {
|
|
169
|
+
const item = await tx.queryable(FlowWorkItem).findOrThrow(id);
|
|
170
|
+
if (exec.status !== 'active')
|
|
171
|
+
throw new FlowError('instance-not-active', `实例 ${exec.instance.id} 不在进行中`);
|
|
172
|
+
if (item.state !== 'open')
|
|
173
|
+
throw new FlowError('already-handled', `待办 ${id} 已经处理过或取消了`);
|
|
174
|
+
return item;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 这个人能不能处理:落到他的,或收件箱里他是候选 */
|
|
178
|
+
function isCandidate(item: WorkItemRow, actor: Actor): boolean {
|
|
179
|
+
if (item.assignee) return item.assignee === actor.id;
|
|
180
|
+
if (item.candidates.includes(excludeToken(actor.id))) return false;
|
|
181
|
+
const tokens = actorTokens(actor);
|
|
182
|
+
return item.candidates.some((token) => tokens.includes(token));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 建推进现场:调用流程时由引擎发起子实例 */
|
|
186
|
+
async function execution(
|
|
187
|
+
tx: FlowDb,
|
|
188
|
+
definition: ProcessDefinition,
|
|
189
|
+
instance: InstanceRow,
|
|
190
|
+
actor: Actor | null,
|
|
191
|
+
): Promise<Execution> {
|
|
192
|
+
const exec = new Execution(tx, options, definition, instance, actor, now());
|
|
193
|
+
exec.startChild = (parent, node) => core.calls.startChild(parent, node);
|
|
194
|
+
exec.onNotified = () => core.notifications.deliverPending();
|
|
195
|
+
exec.logic = await logicOf(tx, instance.definitionId);
|
|
196
|
+
return exec;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** 流程的最新启用版本 */
|
|
200
|
+
async function latestDefinition(tx: FlowDb, key: string) {
|
|
201
|
+
const definition = await tx
|
|
202
|
+
.queryable(FlowDefinition)
|
|
203
|
+
.where((d) => [d.processKey.eq(key), d.enabled.eq(true)])
|
|
204
|
+
.orderBy((d) => d.version.desc())
|
|
205
|
+
.first();
|
|
206
|
+
if (!definition) throw new FlowError('not-found', `没有可发起的流程 ${key}`);
|
|
207
|
+
definitions.set(definition.id, definition.model);
|
|
208
|
+
return definition;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** 实例上的全部待办 */
|
|
212
|
+
function itemsOf(tx: FlowDb, instanceId: number): Promise<WorkItemRow[]> {
|
|
213
|
+
return tx
|
|
214
|
+
.queryable(FlowWorkItem)
|
|
215
|
+
.where((w) => w.instanceId.eq(instanceId))
|
|
216
|
+
.orderBy((w) => w.id)
|
|
217
|
+
.toList();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const core: EngineCore = {
|
|
221
|
+
db,
|
|
222
|
+
options,
|
|
223
|
+
now,
|
|
224
|
+
definitions,
|
|
225
|
+
logics,
|
|
226
|
+
operations,
|
|
227
|
+
logicOf,
|
|
228
|
+
definitionOf,
|
|
229
|
+
withInstance,
|
|
230
|
+
instanceOfItem,
|
|
231
|
+
openItem,
|
|
232
|
+
isCandidate,
|
|
233
|
+
execution,
|
|
234
|
+
latestDefinition,
|
|
235
|
+
itemsOf,
|
|
236
|
+
// 各模块先占位:createFlowEngine 建完核心马上依次装好,之后才会有调用
|
|
237
|
+
calls: undefined as never,
|
|
238
|
+
starts: undefined as never,
|
|
239
|
+
tasks: undefined as never,
|
|
240
|
+
ops: undefined as never,
|
|
241
|
+
admin: undefined as never,
|
|
242
|
+
signals: undefined as never,
|
|
243
|
+
commands: undefined as never,
|
|
244
|
+
publishing: undefined as never,
|
|
245
|
+
queries: undefined as never,
|
|
246
|
+
jobs: undefined as never,
|
|
247
|
+
notifications: undefined as never,
|
|
248
|
+
proxies: undefined as never,
|
|
249
|
+
stats: undefined as never,
|
|
250
|
+
};
|
|
251
|
+
return core;
|
|
252
|
+
}
|