@zerodep-dev/flow-ts-engine 0.0.0-stage → 1.0.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 +202 -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,524 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 待办上的动作(docs/decisions/0003-operations.md):每个动作是一个插件——叫什么、谁现在能做(available)、要填什么(fields)、
|
|
3
|
+
* 做了什么(run)。待办详情给出这个人此刻能做的动作与字段,前端照着渲染按钮与对话框;加新动作不用改引擎与页面。
|
|
4
|
+
* 引擎选项 `operations` 加自定义的,同名的覆盖内置的。命令是 `{ type: 'operate', workItem, operation, input }`。
|
|
5
|
+
*
|
|
6
|
+
* 处理(选一个结果)不是动作:结果来自节点的 outcomes,走 complete。
|
|
7
|
+
*/
|
|
8
|
+
import type { TaskNode } from '@zerodep-dev/flow-ts-model';
|
|
9
|
+
import { FlowEvent, FlowWorkItem, type FlowDb } from '../core/entities.ts';
|
|
10
|
+
import { FlowError, type FlowErrorCode } from '../core/errors.ts';
|
|
11
|
+
import type { Execution, InstanceRow, WorkItemRow } from '../runtime/execution.ts';
|
|
12
|
+
import type { Actor } from '../types.ts';
|
|
13
|
+
|
|
14
|
+
/** 动作要填的一项(前端按 type 渲染:选人、选多人、文字、多行文字、单选) */
|
|
15
|
+
export interface OperationField {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly label: string;
|
|
18
|
+
readonly type: 'user' | 'users' | 'text' | 'textarea' | 'select';
|
|
19
|
+
readonly required?: boolean;
|
|
20
|
+
readonly options?: readonly { readonly value: string; readonly label: string }[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 判断能不能做时看的 */
|
|
24
|
+
export interface OperationScope {
|
|
25
|
+
readonly item: WorkItemRow;
|
|
26
|
+
readonly node: TaskNode;
|
|
27
|
+
readonly instance: InstanceRow;
|
|
28
|
+
readonly actor: Actor;
|
|
29
|
+
/** 他是这份待办的处理人,或收件箱里的候选人 */
|
|
30
|
+
readonly candidate: boolean;
|
|
31
|
+
/** 实例上的全部待办(按编号),撤回这一步之类要看前后的动作用 */
|
|
32
|
+
readonly items: readonly WorkItemRow[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 做的时候拿到的:实例已锁住、待办重读过 */
|
|
36
|
+
export interface OperationContext extends OperationScope {
|
|
37
|
+
readonly exec: Execution;
|
|
38
|
+
readonly tx: FlowDb;
|
|
39
|
+
/** 取消这份待办(带状态条件,别人先处理了报 already-handled) */
|
|
40
|
+
cancel(comment: string | null): Promise<void>;
|
|
41
|
+
/** 这份待办按这个结果处理完(同 complete:会签判断、往下走) */
|
|
42
|
+
finish(outcome: string, comment: string | null): Promise<void>;
|
|
43
|
+
/** 这份待办下的加签都办完(或减掉)了时让它恢复:前加签回到处理人,后加签的挂着的结果生效 */
|
|
44
|
+
settle(): Promise<void>;
|
|
45
|
+
/** 暂存的值:只留这一步能改的字段、只查值不查必填,不合格报 invalid-input */
|
|
46
|
+
draftValues(submitted: Readonly<Record<string, unknown>>): Record<string, unknown>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 一个动作 */
|
|
50
|
+
export interface WorkItemOperation<I extends Record<string, unknown> = Record<string, unknown>> {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly label: string;
|
|
53
|
+
readonly tone?: 'accent' | 'neutral' | 'danger';
|
|
54
|
+
/** 要填的;按待办算(比如加签的方式只在会签节点上有“并加签”) */
|
|
55
|
+
readonly fields?: (scope: OperationScope) => readonly OperationField[];
|
|
56
|
+
readonly available: (scope: OperationScope) => boolean;
|
|
57
|
+
/** 不能做时报什么;不给(或给 undefined)时:实例不在进行中、待办办完了、其余没有权限 */
|
|
58
|
+
readonly refusal?: (scope: OperationScope) => FlowErrorCode | undefined;
|
|
59
|
+
readonly run: (context: OperationContext, input: I) => Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 待办详情里的一个动作 */
|
|
63
|
+
export interface OperationView {
|
|
64
|
+
readonly name: string;
|
|
65
|
+
readonly label: string;
|
|
66
|
+
readonly tone: 'accent' | 'neutral' | 'danger';
|
|
67
|
+
readonly fields: readonly OperationField[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 节点的 allow 没关掉这个动作 */
|
|
71
|
+
const allowed = (node: TaskNode, name: keyof NonNullable<TaskNode['allow']>) =>
|
|
72
|
+
node.allow?.[name] !== false;
|
|
73
|
+
|
|
74
|
+
const isOpen = ({ item, instance }: OperationScope) =>
|
|
75
|
+
item.state === 'open' && instance.status === 'active';
|
|
76
|
+
|
|
77
|
+
/** 落到他(认领了或直接指定) */
|
|
78
|
+
const holds = ({ item, actor }: OperationScope) => item.assignee === actor.id;
|
|
79
|
+
|
|
80
|
+
const COMMENT: OperationField = { name: 'comment', label: '说明', type: 'textarea' };
|
|
81
|
+
|
|
82
|
+
/** 取输入里的文字:必填的空着报 invalid-input */
|
|
83
|
+
function text(input: Record<string, unknown>, name: string, required = true): string | null {
|
|
84
|
+
const value = input[name];
|
|
85
|
+
if (typeof value === 'string' && value.trim()) return value.trim();
|
|
86
|
+
if (required)
|
|
87
|
+
throw new FlowError('invalid-input', `要填 ${name}`, { field: name, code: 'required' });
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 取输入里的人:一个或多个编号 */
|
|
92
|
+
function users(input: Record<string, unknown>, name: string): string[] {
|
|
93
|
+
const value = input[name];
|
|
94
|
+
const list = (Array.isArray(value) ? value : value === undefined ? [] : [value]).map(String);
|
|
95
|
+
const unique = [...new Set(list.filter(Boolean))];
|
|
96
|
+
if (unique.length === 0)
|
|
97
|
+
throw new FlowError('invalid-input', `要选人`, { field: name, code: 'required' });
|
|
98
|
+
return unique;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 把这份待办交给别人:取消这份,给他建一份(同组、同序号),记一条事件 */
|
|
102
|
+
async function handOver(
|
|
103
|
+
context: OperationContext,
|
|
104
|
+
input: Record<string, unknown>,
|
|
105
|
+
to: string,
|
|
106
|
+
arrival: string,
|
|
107
|
+
event: string,
|
|
108
|
+
) {
|
|
109
|
+
const { exec, item, node, actor } = context;
|
|
110
|
+
const comment = text(input, 'comment', false);
|
|
111
|
+
await context.cancel(comment);
|
|
112
|
+
// 先记交接、再记新待办(同 Java 版的顺序)
|
|
113
|
+
exec.addEvent(event, {
|
|
114
|
+
workItemId: item.id,
|
|
115
|
+
node: node.id,
|
|
116
|
+
actor: actor.id,
|
|
117
|
+
comment,
|
|
118
|
+
data: { to },
|
|
119
|
+
});
|
|
120
|
+
await exec.insertItem(node, item.round, arrival, {
|
|
121
|
+
assignee: to,
|
|
122
|
+
assignment: 'direct',
|
|
123
|
+
state: 'open',
|
|
124
|
+
signGroup: item.signGroup,
|
|
125
|
+
signSeq: item.signSeq,
|
|
126
|
+
parentItemId: item.parentItemId,
|
|
127
|
+
fromUser: actor.id,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 内置的动作 */
|
|
132
|
+
export const BUILTIN_OPERATIONS: readonly WorkItemOperation[] = [
|
|
133
|
+
{
|
|
134
|
+
name: 'claim',
|
|
135
|
+
label: '认领',
|
|
136
|
+
available: (scope) => isOpen(scope) && scope.candidate && scope.item.assignee === null,
|
|
137
|
+
// 收件箱里的被认领了(别人或自己先认领的):已有人处理
|
|
138
|
+
refusal: (scope) =>
|
|
139
|
+
isOpen(scope) && scope.item.assignee !== null ? 'already-handled' : undefined,
|
|
140
|
+
run: async ({ exec, tx, item, actor }) => {
|
|
141
|
+
await tx
|
|
142
|
+
.update(FlowWorkItem)
|
|
143
|
+
.set({ assignee: actor.id, assignment: 'claimed' })
|
|
144
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open'), w.assignee.isNull()])
|
|
145
|
+
.expectRows(1, () => new FlowError('already-handled', `待办 ${item.id} 被别人认领了`))
|
|
146
|
+
.execute();
|
|
147
|
+
exec.addEvent('workItem.claimed', { workItemId: item.id, node: item.node });
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: 'release',
|
|
152
|
+
label: '放回收件箱',
|
|
153
|
+
tone: 'neutral',
|
|
154
|
+
fields: () => [{ ...COMMENT, label: '原因' }],
|
|
155
|
+
available: (scope) =>
|
|
156
|
+
isOpen(scope) &&
|
|
157
|
+
holds(scope) &&
|
|
158
|
+
scope.item.assignment === 'claimed' &&
|
|
159
|
+
scope.item.candidates.length > 0,
|
|
160
|
+
// 放回表示“不是我的”:他从这份待办的候选里排除(排除标记 -user:编号,查待办与判断候选时看它)
|
|
161
|
+
run: async ({ exec, tx, item, actor }, input) => {
|
|
162
|
+
await tx
|
|
163
|
+
.update(FlowWorkItem)
|
|
164
|
+
.set({
|
|
165
|
+
assignee: null,
|
|
166
|
+
assignment: 'pool',
|
|
167
|
+
candidates: [...item.candidates, excludeToken(actor.id)],
|
|
168
|
+
})
|
|
169
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open')])
|
|
170
|
+
.execute();
|
|
171
|
+
exec.addEvent('workItem.released', {
|
|
172
|
+
workItemId: item.id,
|
|
173
|
+
node: item.node,
|
|
174
|
+
comment: text(input, 'comment', false),
|
|
175
|
+
});
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
name: 'transfer',
|
|
180
|
+
label: '转办',
|
|
181
|
+
tone: 'neutral',
|
|
182
|
+
fields: () => [{ name: 'to', label: '转给', type: 'user', required: true }, COMMENT],
|
|
183
|
+
// 处理人、收件箱里的候选人(不用先认领)、管理员
|
|
184
|
+
available: (scope) =>
|
|
185
|
+
isOpen(scope) && (scope.candidate || !!scope.actor.admin) && allowed(scope.node, 'transfer'),
|
|
186
|
+
run: (context, input) =>
|
|
187
|
+
handOver(context, input, users(input, 'to')[0]!, 'transfer', 'workItem.transferred'),
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: 'reassign',
|
|
191
|
+
label: '改派',
|
|
192
|
+
tone: 'neutral',
|
|
193
|
+
fields: () => [{ name: 'to', label: '改派给', type: 'user', required: true }, COMMENT],
|
|
194
|
+
available: (scope) => isOpen(scope) && !!scope.actor.admin,
|
|
195
|
+
// 原地换人(同 Java 版):不新建待办,收件箱里的也直接落到他
|
|
196
|
+
run: async ({ exec, tx, item, actor }, input) => {
|
|
197
|
+
const to = users(input, 'to')[0]!;
|
|
198
|
+
await tx
|
|
199
|
+
.update(FlowWorkItem)
|
|
200
|
+
.set({ assignee: to, assignment: 'direct', fromUser: item.assignee ?? actor.id })
|
|
201
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open')])
|
|
202
|
+
.expectRows(1, () => new FlowError('already-handled', `待办 ${item.id} 已经处理过`))
|
|
203
|
+
.execute();
|
|
204
|
+
exec.addEvent('workItem.reassigned', {
|
|
205
|
+
workItemId: item.id,
|
|
206
|
+
node: item.node,
|
|
207
|
+
actor: actor.id,
|
|
208
|
+
comment: text(input, 'comment', false),
|
|
209
|
+
data: { to },
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
name: 'delegate',
|
|
215
|
+
label: '委托',
|
|
216
|
+
tone: 'neutral',
|
|
217
|
+
fields: () => [{ name: 'to', label: '委托给', type: 'user', required: true }, COMMENT],
|
|
218
|
+
available: (scope) =>
|
|
219
|
+
isOpen(scope) &&
|
|
220
|
+
holds(scope) &&
|
|
221
|
+
scope.item.parentItemId === null &&
|
|
222
|
+
allowed(scope.node, 'delegate'),
|
|
223
|
+
// 只加一个人的前加签:自己这份先等,受托人办完(他选的结果记作意见)回到自己
|
|
224
|
+
run: async (context, input) => {
|
|
225
|
+
const to = users(input, 'to')[0]!;
|
|
226
|
+
await holdFor(context, [to], 'delegate');
|
|
227
|
+
context.exec.addEvent('workItem.delegated', {
|
|
228
|
+
workItemId: context.item.id,
|
|
229
|
+
node: context.node.id,
|
|
230
|
+
actor: context.actor.id,
|
|
231
|
+
comment: text(input, 'comment', false),
|
|
232
|
+
data: { to },
|
|
233
|
+
});
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
name: 'addSign',
|
|
238
|
+
label: '加签',
|
|
239
|
+
tone: 'neutral',
|
|
240
|
+
fields: ({ node }) => [
|
|
241
|
+
{ name: 'users', label: '加签给', type: 'users', required: true },
|
|
242
|
+
{
|
|
243
|
+
name: 'mode',
|
|
244
|
+
label: '方式',
|
|
245
|
+
type: 'select',
|
|
246
|
+
required: true,
|
|
247
|
+
options: [
|
|
248
|
+
{ value: 'before', label: '前加签:他们先办,办完回到我' },
|
|
249
|
+
{ value: 'after', label: '后加签:我办完后他们再办' },
|
|
250
|
+
...(node.multi ? [{ value: 'parallel', label: '并加签:和会签的人一起办' }] : []),
|
|
251
|
+
],
|
|
252
|
+
},
|
|
253
|
+
COMMENT,
|
|
254
|
+
],
|
|
255
|
+
available: (scope) =>
|
|
256
|
+
isOpen(scope) &&
|
|
257
|
+
holds(scope) &&
|
|
258
|
+
scope.item.parentItemId === null &&
|
|
259
|
+
allowed(scope.node, 'addSign'),
|
|
260
|
+
run: async (context, input) => {
|
|
261
|
+
const { exec, item, node, actor } = context;
|
|
262
|
+
const mode = text(input, 'mode');
|
|
263
|
+
const signers = users(input, 'users').filter((user) => user !== actor.id);
|
|
264
|
+
const comment = text(input, 'comment', false);
|
|
265
|
+
if (mode === 'parallel') {
|
|
266
|
+
if (!node.multi || !item.signGroup)
|
|
267
|
+
throw new FlowError('invalid-input', '并加签只在会签节点上', { field: 'mode' });
|
|
268
|
+
for (const user of signers) {
|
|
269
|
+
await exec.insertItem(node, item.round, 'addSign.parallel', {
|
|
270
|
+
assignee: user,
|
|
271
|
+
assignment: 'direct',
|
|
272
|
+
state: 'open',
|
|
273
|
+
signGroup: item.signGroup,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
} else if (mode === 'before') {
|
|
277
|
+
await holdFor(context, signers, 'addSign.before');
|
|
278
|
+
} else if (mode === 'after') {
|
|
279
|
+
// 后加签:先给他们建好排队的待办,我选了结果时才打开(引擎的 finishItem)
|
|
280
|
+
for (const user of signers)
|
|
281
|
+
await signerItem(exec, node, item, user, 'addSign.after', 'waiting');
|
|
282
|
+
} else {
|
|
283
|
+
throw new FlowError('invalid-input', `没有这种加签方式 ${mode}`, { field: 'mode' });
|
|
284
|
+
}
|
|
285
|
+
exec.addEvent('workItem.signAdded', {
|
|
286
|
+
workItemId: item.id,
|
|
287
|
+
node: node.id,
|
|
288
|
+
actor: actor.id,
|
|
289
|
+
comment,
|
|
290
|
+
data: { users: signers, mode },
|
|
291
|
+
});
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
name: 'removeSign',
|
|
296
|
+
label: '减签',
|
|
297
|
+
tone: 'neutral',
|
|
298
|
+
// 选的是还没办的加签(委托)的人
|
|
299
|
+
fields: (scope) => [
|
|
300
|
+
{
|
|
301
|
+
name: 'users',
|
|
302
|
+
label: '减掉',
|
|
303
|
+
type: 'select',
|
|
304
|
+
required: true,
|
|
305
|
+
options: pendingSigners(scope).map((w) => ({ value: w.assignee!, label: w.assignee! })),
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
available: (scope) =>
|
|
309
|
+
scope.instance.status === 'active' &&
|
|
310
|
+
scope.item.assignee === scope.actor.id &&
|
|
311
|
+
(scope.item.state === 'open' || scope.item.state === 'waiting') &&
|
|
312
|
+
pendingSigners(scope).length > 0,
|
|
313
|
+
// 取消选中的人的加签待办;都减掉了,原来那份恢复(后加签的挂着的结果生效)
|
|
314
|
+
run: async (context, input) => {
|
|
315
|
+
const removed = new Set(users(input, 'users'));
|
|
316
|
+
for (const child of pendingSigners(context)) {
|
|
317
|
+
if (!removed.has(child.assignee!)) continue;
|
|
318
|
+
await context.tx
|
|
319
|
+
.update(FlowWorkItem)
|
|
320
|
+
.set({ state: 'canceled', completedAt: context.exec.now })
|
|
321
|
+
.where((w) => [w.id.eq(child.id), w.state.in(['open', 'waiting'])])
|
|
322
|
+
.execute();
|
|
323
|
+
context.exec.addEvent('workItem.signersRemoved', {
|
|
324
|
+
workItemId: child.id,
|
|
325
|
+
node: child.node,
|
|
326
|
+
actor: context.actor.id,
|
|
327
|
+
data: { user: child.assignee },
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
await context.settle();
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
name: 'assign',
|
|
335
|
+
label: '指定承办人',
|
|
336
|
+
fields: () => [{ name: 'to', label: '指定给', type: 'user', required: true }, COMMENT],
|
|
337
|
+
// 等他指定的(收件箱的负责人);管理员也能替他指定
|
|
338
|
+
available: (scope) =>
|
|
339
|
+
isOpen(scope) &&
|
|
340
|
+
scope.item.awaitingDesignation &&
|
|
341
|
+
(scope.item.designator === scope.actor.id || !!scope.actor.admin),
|
|
342
|
+
run: async ({ exec, tx, item, actor }, input) => {
|
|
343
|
+
const to = users(input, 'to')[0]!;
|
|
344
|
+
await tx
|
|
345
|
+
.update(FlowWorkItem)
|
|
346
|
+
.set({ assignee: to, assignment: 'designated', awaitingDesignation: false })
|
|
347
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open'), w.awaitingDesignation.eq(true)])
|
|
348
|
+
.expectRows(1, () => new FlowError('already-handled', `待办 ${item.id} 已经指定过了`))
|
|
349
|
+
.execute();
|
|
350
|
+
exec.addEvent('workItem.assigned', {
|
|
351
|
+
workItemId: item.id,
|
|
352
|
+
node: item.node,
|
|
353
|
+
actor: actor.id,
|
|
354
|
+
comment: text(input, 'comment', false),
|
|
355
|
+
data: { to },
|
|
356
|
+
});
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
name: 'requestReassign',
|
|
361
|
+
label: '申请改派',
|
|
362
|
+
tone: 'neutral',
|
|
363
|
+
fields: () => [{ ...COMMENT, label: '原因', required: true }],
|
|
364
|
+
// 负责人指定给我的:说明原因,回到负责人的“待我指定”里;他重新指定之前仍由我处理(同 Java 版)
|
|
365
|
+
available: (scope) =>
|
|
366
|
+
isOpen(scope) &&
|
|
367
|
+
scope.item.assignment === 'designated' &&
|
|
368
|
+
scope.item.assignee === scope.actor.id &&
|
|
369
|
+
scope.item.designator !== null &&
|
|
370
|
+
!scope.item.awaitingDesignation,
|
|
371
|
+
run: async ({ exec, tx, item, actor }, input) => {
|
|
372
|
+
const comment = text(input, 'comment');
|
|
373
|
+
await tx
|
|
374
|
+
.update(FlowWorkItem)
|
|
375
|
+
.set({ awaitingDesignation: true })
|
|
376
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open'), w.assignee.eq(actor.id)])
|
|
377
|
+
.expectRows(1, () => new FlowError('already-handled', `待办 ${item.id} 已经处理过`))
|
|
378
|
+
.execute();
|
|
379
|
+
exec.addEvent('workItem.reassignRequested', {
|
|
380
|
+
workItemId: item.id,
|
|
381
|
+
node: item.node,
|
|
382
|
+
actor: actor.id,
|
|
383
|
+
comment,
|
|
384
|
+
});
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
name: 'saveDraft',
|
|
389
|
+
label: '暂存',
|
|
390
|
+
tone: 'neutral',
|
|
391
|
+
// 处理人、收件箱里的候选人(暂存时先认领给自己)
|
|
392
|
+
available: (scope) => isOpen(scope) && scope.candidate && scope.item.parentItemId === null,
|
|
393
|
+
run: async (context, input) => {
|
|
394
|
+
const { exec, tx, item, actor } = context;
|
|
395
|
+
const values = context.draftValues(
|
|
396
|
+
(input['variables'] ?? {}) as Readonly<Record<string, unknown>>,
|
|
397
|
+
);
|
|
398
|
+
await tx
|
|
399
|
+
.update(FlowWorkItem)
|
|
400
|
+
.set({
|
|
401
|
+
draft: { values, comment: text(input, 'comment', false) },
|
|
402
|
+
...(item.assignee === null ? { assignee: actor.id, assignment: 'claimed' as const } : {}),
|
|
403
|
+
})
|
|
404
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open')])
|
|
405
|
+
.execute();
|
|
406
|
+
if (item.assignee === null)
|
|
407
|
+
exec.addEvent('workItem.claimed', { workItemId: item.id, node: item.node });
|
|
408
|
+
},
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
name: 'withdrawStep',
|
|
412
|
+
label: '撤回',
|
|
413
|
+
tone: 'danger',
|
|
414
|
+
fields: () => [{ ...COMMENT, label: '撤回原因' }],
|
|
415
|
+
available: (scope) => withdrawable(scope),
|
|
416
|
+
refusal: () => 'forbidden',
|
|
417
|
+
// 之后开出的待办取消,这一份重新打开(同一个编号),这一步改过的单据值恢复
|
|
418
|
+
run: async ({ exec, tx, item, node, actor }, input) => {
|
|
419
|
+
const later = await tx
|
|
420
|
+
.queryable(FlowWorkItem)
|
|
421
|
+
.where((w) => [w.instanceId.eq(item.instanceId), w.id.gt(item.id), w.state.eq('open')])
|
|
422
|
+
.toList();
|
|
423
|
+
await exec.cancelWhere((w) => [w.id.gt(item.id)]);
|
|
424
|
+
for (const next of later) exec.current.delete(next.node);
|
|
425
|
+
await tx
|
|
426
|
+
.update(FlowWorkItem)
|
|
427
|
+
.set({ state: 'open', outcome: null, comment: null, completedAt: null, completedBy: null })
|
|
428
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('done')])
|
|
429
|
+
.expectRows(1, () => new FlowError('already-handled', `待办 ${item.id} 不能再撤回`))
|
|
430
|
+
.execute();
|
|
431
|
+
const completed = await tx
|
|
432
|
+
.queryable(FlowEvent)
|
|
433
|
+
.where((e) => [e.workItemId.eq(item.id), e.type.eq('workItem.completed')])
|
|
434
|
+
.orderBy((e) => e.id.desc())
|
|
435
|
+
.first();
|
|
436
|
+
const before = (completed?.data?.['before'] ?? {}) as Record<string, unknown>;
|
|
437
|
+
if (Object.keys(before).length > 0) exec.setVariables(before);
|
|
438
|
+
exec.current.add(node.id);
|
|
439
|
+
exec.addEvent('workItem.withdrawn', {
|
|
440
|
+
workItemId: item.id,
|
|
441
|
+
node: node.id,
|
|
442
|
+
actor: actor.id,
|
|
443
|
+
outcome: item.outcome,
|
|
444
|
+
comment: text(input, 'comment', false),
|
|
445
|
+
});
|
|
446
|
+
},
|
|
447
|
+
},
|
|
448
|
+
];
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* 撤回这一步:他办完的这一份(不是会签、加签里的),之后开出的待办都还开着、没人认领、没人动过,节点没关掉撤回
|
|
452
|
+
*/
|
|
453
|
+
function withdrawable({ item, node, instance, actor, items }: OperationScope): boolean {
|
|
454
|
+
if (instance.status !== 'active' || item.state !== 'done' || item.completedBy !== actor.id)
|
|
455
|
+
return false;
|
|
456
|
+
if (item.parentItemId !== null || item.signGroup !== null || !allowed(node, 'withdraw'))
|
|
457
|
+
return false;
|
|
458
|
+
const later = items.filter((w) => w.id > item.id);
|
|
459
|
+
return (
|
|
460
|
+
later.length > 0 &&
|
|
461
|
+
later.every((w) => w.state === 'open' && w.assignment !== 'claimed' && w.arrival === 'forward')
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** 这份待办下还没办完的加签(委托)待办 */
|
|
466
|
+
const pendingSigners = ({ item, items }: OperationScope) =>
|
|
467
|
+
items.filter((w) => w.parentItemId === item.id && (w.state === 'open' || w.state === 'waiting'));
|
|
468
|
+
|
|
469
|
+
/** 给加签(委托)的人建一份待办,挂在原来那份下面 */
|
|
470
|
+
function signerItem(
|
|
471
|
+
exec: Execution,
|
|
472
|
+
node: TaskNode,
|
|
473
|
+
parent: WorkItemRow,
|
|
474
|
+
user: string,
|
|
475
|
+
arrival: 'delegate' | 'addSign.before' | 'addSign.after',
|
|
476
|
+
state: 'open' | 'waiting',
|
|
477
|
+
) {
|
|
478
|
+
return exec.insertItem(node, parent.round, arrival, {
|
|
479
|
+
assignee: user,
|
|
480
|
+
assignment: 'direct',
|
|
481
|
+
state,
|
|
482
|
+
parentItemId: parent.id,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** 前加签、委托:自己这份先等,给他们各开一份;都办完时引擎让这份恢复(workItem.resumed) */
|
|
487
|
+
async function holdFor(
|
|
488
|
+
{ exec, tx, item, node }: OperationContext,
|
|
489
|
+
people: readonly string[],
|
|
490
|
+
arrival: 'delegate' | 'addSign.before',
|
|
491
|
+
) {
|
|
492
|
+
await tx
|
|
493
|
+
.update(FlowWorkItem)
|
|
494
|
+
.set({ state: 'waiting' })
|
|
495
|
+
.where((w) => [w.id.eq(item.id), w.state.eq('open')])
|
|
496
|
+
.expectRows(1, () => new FlowError('already-handled', `待办 ${item.id} 已经处理过`))
|
|
497
|
+
.execute();
|
|
498
|
+
for (const user of people) await signerItem(exec, node, item, user, arrival, 'open');
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** 放回收件箱的人的排除标记 */
|
|
502
|
+
export const excludeToken = (user: string) => `-user:${user}`;
|
|
503
|
+
|
|
504
|
+
/** 内置的加上选项里的(同名的以选项里的为准) */
|
|
505
|
+
export function operationsOf(custom: readonly WorkItemOperation[] = []): WorkItemOperation[] {
|
|
506
|
+
const byName = new Map(BUILTIN_OPERATIONS.map((operation) => [operation.name, operation]));
|
|
507
|
+
for (const operation of custom) byName.set(operation.name, operation);
|
|
508
|
+
return [...byName.values()];
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** 这个人此刻能做的动作 */
|
|
512
|
+
export function availableOperations(
|
|
513
|
+
operations: readonly WorkItemOperation[],
|
|
514
|
+
scope: OperationScope,
|
|
515
|
+
): OperationView[] {
|
|
516
|
+
return operations
|
|
517
|
+
.filter((operation) => operation.available(scope))
|
|
518
|
+
.map((operation) => ({
|
|
519
|
+
name: operation.name,
|
|
520
|
+
label: operation.label,
|
|
521
|
+
tone: operation.tone ?? 'accent',
|
|
522
|
+
fields: operation.fields?.(scope) ?? [],
|
|
523
|
+
}));
|
|
524
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 外部消息:等消息的节点(receive)按名字与关联键收到后往下走。
|
|
3
|
+
*/
|
|
4
|
+
import { FlowInstance } from '../core/entities.ts';
|
|
5
|
+
import { FlowError } from '../core/errors.ts';
|
|
6
|
+
import { type CustomNodeDef } from '../runtime/execution.ts';
|
|
7
|
+
import type { Actor, RunResult, SignalInput } from '../types.ts';
|
|
8
|
+
import type { EngineCore } from '../core/context.ts';
|
|
9
|
+
|
|
10
|
+
/** 外部消息:装进引擎核心(core.signals),别的模块经它调用 */
|
|
11
|
+
export function createSignals(core: EngineCore) {
|
|
12
|
+
const { db, withInstance } = core;
|
|
13
|
+
|
|
14
|
+
/** 外部事件:在等它的实例往下走,返回这些实例的结果;不在等的只记一条 signal.ignored */
|
|
15
|
+
async function signal(command: SignalInput, actor: Actor): Promise<RunResult[]> {
|
|
16
|
+
const ids =
|
|
17
|
+
command.instance !== undefined
|
|
18
|
+
? [command.instance]
|
|
19
|
+
: (
|
|
20
|
+
await db
|
|
21
|
+
.queryable(FlowInstance)
|
|
22
|
+
.where((i) => [i.businessKey.eq(command.businessKey ?? ''), i.status.eq('active')])
|
|
23
|
+
.select((i) => ({ id: i.id }))
|
|
24
|
+
.toList()
|
|
25
|
+
).map((row) => row.id);
|
|
26
|
+
const results: RunResult[] = [];
|
|
27
|
+
for (const id of ids) {
|
|
28
|
+
let received = false;
|
|
29
|
+
const result = await withInstance(id, actor, async (exec) => {
|
|
30
|
+
const waiting = [...exec.current].find((nodeId) => {
|
|
31
|
+
const node = exec.graph.node(nodeId);
|
|
32
|
+
return (
|
|
33
|
+
(node?.type === 'receive' && node.event === command.event) ||
|
|
34
|
+
exec.state.events[nodeId] === command.event
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
const node = waiting ? exec.node(waiting) : undefined;
|
|
38
|
+
// 自定义节点在等这个事件:交给它的 received(不写时按事件带的结果往下走)
|
|
39
|
+
if (node && exec.status === 'active' && exec.state.events[node.id] === command.event) {
|
|
40
|
+
const custom = node as unknown as CustomNodeDef;
|
|
41
|
+
delete exec.state.events[node.id];
|
|
42
|
+
exec.current.delete(node.id);
|
|
43
|
+
exec.addEvent('signal.received', {
|
|
44
|
+
node: node.id,
|
|
45
|
+
outcome: command.outcome ?? null,
|
|
46
|
+
data: { event: command.event, data: command.data ?? null },
|
|
47
|
+
});
|
|
48
|
+
received = true;
|
|
49
|
+
const handler = exec.customNode(custom.type);
|
|
50
|
+
const event = { outcome: command.outcome ?? null, data: command.data ?? {} };
|
|
51
|
+
await exec.applyCustom(
|
|
52
|
+
custom,
|
|
53
|
+
handler?.received
|
|
54
|
+
? await handler.received(exec.customContext(custom), event)
|
|
55
|
+
: { outcome: event.outcome },
|
|
56
|
+
);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!node || node.type !== 'receive' || exec.status !== 'active') {
|
|
60
|
+
exec.addEvent('signal.ignored', {
|
|
61
|
+
outcome: command.outcome ?? null,
|
|
62
|
+
data: { event: command.event },
|
|
63
|
+
});
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (command.outcome !== undefined && !(node.outcomes ?? []).includes(command.outcome)) {
|
|
67
|
+
throw new FlowError(
|
|
68
|
+
'invalid-input',
|
|
69
|
+
`事件 ${command.event} 没有结果 ${command.outcome}`,
|
|
70
|
+
{ field: 'outcome' },
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
for (const [variable, name] of Object.entries(node.output ?? {})) {
|
|
74
|
+
const target = variable.startsWith('form.') ? variable.slice(5) : variable;
|
|
75
|
+
exec.setVariables({ [target]: command.data?.[name] });
|
|
76
|
+
}
|
|
77
|
+
exec.current.delete(node.id);
|
|
78
|
+
exec.addEvent('signal.received', {
|
|
79
|
+
node: node.id,
|
|
80
|
+
outcome: command.outcome ?? null,
|
|
81
|
+
data: { event: command.event, data: command.data ?? null },
|
|
82
|
+
});
|
|
83
|
+
received = true;
|
|
84
|
+
await exec.leave(node.id, command.outcome ?? null);
|
|
85
|
+
});
|
|
86
|
+
// 按实例送的事件:没在等也给出这个实例的结果(只记了一笔);按业务编号送的只给往下走了的
|
|
87
|
+
if (received || command.instance !== undefined) results.push(result);
|
|
88
|
+
}
|
|
89
|
+
return results;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return { signal };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** createSignals 给出的函数 */
|
|
96
|
+
export type Signals = ReturnType<typeof createSignals>;
|