@nocobase/plugin-workflow-action-trigger 0.20.0-alpha.7 → 0.20.0-alpha.8
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/dist/externalVersion.js +9 -7
- package/dist/server/ActionTrigger.d.ts +8 -2
- package/dist/server/ActionTrigger.js +12 -4
- package/dist/server/migrations/{20240227172623-change-name.js → 20240307215012-change-name.js} +1 -1
- package/package.json +2 -2
- package/src/server/ActionTrigger.ts +28 -13
- package/src/server/__tests__/trigger.test.ts +70 -1
- package/src/server/migrations/{20240227172623-change-name.ts → 20240307215012-change-name.ts} +1 -1
- /package/dist/server/migrations/{20240227172623-change-name.d.ts → 20240307215012-change-name.d.ts} +0 -0
package/dist/externalVersion.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
module.exports = {
|
|
2
2
|
"@formily/react": "2.3.0",
|
|
3
|
-
"@nocobase/client": "0.20.0-alpha.
|
|
4
|
-
"@nocobase/plugin-workflow": "0.20.0-alpha.
|
|
3
|
+
"@nocobase/client": "0.20.0-alpha.8",
|
|
4
|
+
"@nocobase/plugin-workflow": "0.20.0-alpha.8",
|
|
5
5
|
"react-i18next": "11.18.6",
|
|
6
6
|
"lodash": "4.17.21",
|
|
7
7
|
"sequelize": "6.35.2",
|
|
8
|
-
"@nocobase/database": "0.20.0-alpha.
|
|
9
|
-
"@nocobase/server": "0.20.0-alpha.
|
|
10
|
-
"@nocobase/
|
|
11
|
-
"@nocobase/
|
|
12
|
-
"@nocobase/
|
|
8
|
+
"@nocobase/database": "0.20.0-alpha.8",
|
|
9
|
+
"@nocobase/server": "0.20.0-alpha.8",
|
|
10
|
+
"@nocobase/resourcer": "0.20.0-alpha.8",
|
|
11
|
+
"@nocobase/actions": "0.20.0-alpha.8",
|
|
12
|
+
"@nocobase/plugin-workflow-test": "0.20.0-alpha.8",
|
|
13
|
+
"@nocobase/test": "0.20.0-alpha.8",
|
|
14
|
+
"@nocobase/utils": "0.20.0-alpha.8"
|
|
13
15
|
};
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
import { DefaultContext } from '@nocobase/server';
|
|
2
|
+
import { ActionContext } from '@nocobase/resourcer';
|
|
3
|
+
import { Next } from '@nocobase/actions';
|
|
1
4
|
import WorkflowPlugin, { Trigger, WorkflowModel } from '@nocobase/plugin-workflow';
|
|
5
|
+
interface Context extends ActionContext, DefaultContext {
|
|
6
|
+
}
|
|
2
7
|
export default class extends Trigger {
|
|
3
8
|
constructor(workflow: WorkflowPlugin);
|
|
4
|
-
triggerAction(context:
|
|
5
|
-
middleware: (context:
|
|
9
|
+
triggerAction(context: Context, next: Next): Promise<any>;
|
|
10
|
+
middleware: (context: Context, next: Next) => Promise<any>;
|
|
6
11
|
private trigger;
|
|
7
12
|
on(workflow: WorkflowModel): void;
|
|
8
13
|
off(workflow: WorkflowModel): void;
|
|
9
14
|
}
|
|
15
|
+
export {};
|
|
@@ -64,17 +64,18 @@ class ActionTrigger_default extends import_plugin_workflow.Trigger {
|
|
|
64
64
|
};
|
|
65
65
|
const triggers = triggerWorkflows.split(",").map((trigger) => trigger.split("!"));
|
|
66
66
|
const workflowRepo = this.workflow.db.getRepository("workflows");
|
|
67
|
-
const workflows = await workflowRepo.find({
|
|
67
|
+
const workflows = (await workflowRepo.find({
|
|
68
68
|
filter: {
|
|
69
69
|
key: triggers.map((trigger) => trigger[0]),
|
|
70
70
|
current: true,
|
|
71
71
|
type: "action",
|
|
72
72
|
enabled: true
|
|
73
73
|
}
|
|
74
|
-
});
|
|
74
|
+
})).filter((workflow) => Boolean(workflow.config.collection));
|
|
75
75
|
const syncGroup = [];
|
|
76
76
|
const asyncGroup = [];
|
|
77
77
|
for (const workflow of workflows) {
|
|
78
|
+
const { collection, appends = [] } = workflow.config;
|
|
78
79
|
const trigger = triggers.find((trigger2) => trigger2[0] == workflow.key);
|
|
79
80
|
const event = [workflow];
|
|
80
81
|
if (context.action.resourceName !== "workflows") {
|
|
@@ -95,7 +96,6 @@ class ActionTrigger_default extends import_plugin_workflow.Trigger {
|
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
98
|
}
|
|
98
|
-
const { collection, appends = [] } = workflow.config;
|
|
99
99
|
const model = payload.constructor;
|
|
100
100
|
if (payload instanceof import_database.Model) {
|
|
101
101
|
if (collection !== model.collection.name) {
|
|
@@ -111,7 +111,15 @@ class ActionTrigger_default extends import_plugin_workflow.Trigger {
|
|
|
111
111
|
event.push({ data: (0, import_plugin_workflow.toJSON)(payload), ...userInfo });
|
|
112
112
|
}
|
|
113
113
|
} else {
|
|
114
|
-
const
|
|
114
|
+
const { model, repository } = context.db.getCollection(collection);
|
|
115
|
+
let data = trigger[1] ? (0, import_lodash.get)(values, trigger[1]) : values;
|
|
116
|
+
const pk = (0, import_lodash.get)(data, model.primaryKeyAttribute);
|
|
117
|
+
if (appends.length && pk != null) {
|
|
118
|
+
data = await repository.findOne({
|
|
119
|
+
filterByTk: pk,
|
|
120
|
+
appends
|
|
121
|
+
});
|
|
122
|
+
}
|
|
115
123
|
event.push({ data, ...userInfo });
|
|
116
124
|
}
|
|
117
125
|
(workflow.sync ? syncGroup : asyncGroup).push(event);
|
package/dist/server/migrations/{20240227172623-change-name.js → 20240307215012-change-name.js}
RENAMED
|
@@ -22,7 +22,7 @@ __export(change_name_exports, {
|
|
|
22
22
|
module.exports = __toCommonJS(change_name_exports);
|
|
23
23
|
var import_server = require("@nocobase/server");
|
|
24
24
|
class change_name_default extends import_server.Migration {
|
|
25
|
-
appVersion = "<0.20.0-alpha.
|
|
25
|
+
appVersion = "<0.20.0-alpha.7";
|
|
26
26
|
on = "afterSync";
|
|
27
27
|
async up() {
|
|
28
28
|
const { db } = this.context;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"displayName.zh-CN": "工作流:数据操作触发器",
|
|
5
5
|
"description": "Bind action buttons to trigger workflow events when clicked.",
|
|
6
6
|
"description.zh-CN": "可对数据操作按钮绑定,在点击后触发对应的工作流事件。",
|
|
7
|
-
"version": "0.20.0-alpha.
|
|
7
|
+
"version": "0.20.0-alpha.8",
|
|
8
8
|
"license": "AGPL-3.0",
|
|
9
9
|
"main": "./dist/server/index.js",
|
|
10
10
|
"homepage": "https://docs.nocobase.com/plugins/workflow-action-trigger",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@nocobase/server": "0.x",
|
|
22
22
|
"@nocobase/test": "0.x"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "39ebc1cefe5a826aecf0b14d73530dfa5f9c2b1c",
|
|
25
25
|
"keywords": [
|
|
26
26
|
"Workflow"
|
|
27
27
|
]
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { get } from 'lodash';
|
|
2
2
|
import { BelongsTo, HasOne } from 'sequelize';
|
|
3
3
|
import { Model, modelAssociationByKey } from '@nocobase/database';
|
|
4
|
+
import { DefaultContext } from '@nocobase/server';
|
|
5
|
+
import { ActionContext } from '@nocobase/resourcer';
|
|
6
|
+
import { Next } from '@nocobase/actions';
|
|
4
7
|
|
|
5
8
|
import WorkflowPlugin, { Trigger, WorkflowModel, toJSON } from '@nocobase/plugin-workflow';
|
|
6
9
|
|
|
10
|
+
interface Context extends ActionContext, DefaultContext {}
|
|
11
|
+
|
|
7
12
|
export default class extends Trigger {
|
|
8
13
|
constructor(workflow: WorkflowPlugin) {
|
|
9
14
|
super(workflow);
|
|
@@ -11,7 +16,7 @@ export default class extends Trigger {
|
|
|
11
16
|
workflow.app.resourcer.use(this.middleware);
|
|
12
17
|
}
|
|
13
18
|
|
|
14
|
-
async triggerAction(context, next) {
|
|
19
|
+
async triggerAction(context: Context, next: Next) {
|
|
15
20
|
const { triggerWorkflows } = context.action.params;
|
|
16
21
|
|
|
17
22
|
if (!triggerWorkflows) {
|
|
@@ -24,7 +29,7 @@ export default class extends Trigger {
|
|
|
24
29
|
this.trigger(context);
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
middleware = async (context, next) => {
|
|
32
|
+
middleware = async (context: Context, next: Next) => {
|
|
28
33
|
const {
|
|
29
34
|
resourceName,
|
|
30
35
|
actionName,
|
|
@@ -48,7 +53,7 @@ export default class extends Trigger {
|
|
|
48
53
|
return this.trigger(context);
|
|
49
54
|
};
|
|
50
55
|
|
|
51
|
-
private async trigger(context) {
|
|
56
|
+
private async trigger(context: Context) {
|
|
52
57
|
const { triggerWorkflows = '', values } = context.action.params;
|
|
53
58
|
|
|
54
59
|
const { currentUser, currentRole } = context.state;
|
|
@@ -59,17 +64,20 @@ export default class extends Trigger {
|
|
|
59
64
|
|
|
60
65
|
const triggers = triggerWorkflows.split(',').map((trigger) => trigger.split('!'));
|
|
61
66
|
const workflowRepo = this.workflow.db.getRepository('workflows');
|
|
62
|
-
const workflows =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
const workflows = (
|
|
68
|
+
await workflowRepo.find({
|
|
69
|
+
filter: {
|
|
70
|
+
key: triggers.map((trigger) => trigger[0]),
|
|
71
|
+
current: true,
|
|
72
|
+
type: 'action',
|
|
73
|
+
enabled: true,
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
).filter((workflow) => Boolean(workflow.config.collection));
|
|
70
77
|
const syncGroup = [];
|
|
71
78
|
const asyncGroup = [];
|
|
72
79
|
for (const workflow of workflows) {
|
|
80
|
+
const { collection, appends = [] } = workflow.config;
|
|
73
81
|
const trigger = triggers.find((trigger) => trigger[0] == workflow.key);
|
|
74
82
|
const event = [workflow];
|
|
75
83
|
if (context.action.resourceName !== 'workflows') {
|
|
@@ -90,7 +98,6 @@ export default class extends Trigger {
|
|
|
90
98
|
}
|
|
91
99
|
}
|
|
92
100
|
}
|
|
93
|
-
const { collection, appends = [] } = workflow.config;
|
|
94
101
|
const model = payload.constructor;
|
|
95
102
|
if (payload instanceof Model) {
|
|
96
103
|
if (collection !== model.collection.name) {
|
|
@@ -107,7 +114,15 @@ export default class extends Trigger {
|
|
|
107
114
|
event.push({ data: toJSON(payload), ...userInfo });
|
|
108
115
|
}
|
|
109
116
|
} else {
|
|
110
|
-
const
|
|
117
|
+
const { model, repository } = context.db.getCollection(collection);
|
|
118
|
+
let data = trigger[1] ? get(values, trigger[1]) : values;
|
|
119
|
+
const pk = get(data, model.primaryKeyAttribute);
|
|
120
|
+
if (appends.length && pk != null) {
|
|
121
|
+
data = await repository.findOne({
|
|
122
|
+
filterByTk: pk,
|
|
123
|
+
appends,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
111
126
|
// this.workflow.trigger(workflow, {
|
|
112
127
|
// data,
|
|
113
128
|
// ...userInfo,
|
|
@@ -214,6 +214,7 @@ describe('workflow > action-trigger', () => {
|
|
|
214
214
|
type: 'action',
|
|
215
215
|
config: {
|
|
216
216
|
collection: 'posts',
|
|
217
|
+
appends: ['createdBy'],
|
|
217
218
|
},
|
|
218
219
|
});
|
|
219
220
|
|
|
@@ -238,6 +239,7 @@ describe('workflow > action-trigger', () => {
|
|
|
238
239
|
const [e2] = await workflow.getExecutions();
|
|
239
240
|
expect(e2.status).toBe(EXECUTION_STATUS.RESOLVED);
|
|
240
241
|
expect(e2.context.data).toHaveProperty('title', 't2');
|
|
242
|
+
expect(e2.context.data).toHaveProperty('createdBy');
|
|
241
243
|
});
|
|
242
244
|
});
|
|
243
245
|
|
|
@@ -281,10 +283,32 @@ describe('workflow > action-trigger', () => {
|
|
|
281
283
|
});
|
|
282
284
|
|
|
283
285
|
describe('directly trigger', () => {
|
|
284
|
-
it('
|
|
286
|
+
it('no collection configured should not be triggered', async () => {
|
|
287
|
+
const workflow = await WorkflowModel.create({
|
|
288
|
+
enabled: true,
|
|
289
|
+
type: 'action',
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
const res1 = await userAgents[0].resource('workflows').trigger({
|
|
293
|
+
values: { title: 't1' },
|
|
294
|
+
triggerWorkflows: `${workflow.key}`,
|
|
295
|
+
});
|
|
296
|
+
expect(res1.status).toBe(202);
|
|
297
|
+
|
|
298
|
+
await sleep(500);
|
|
299
|
+
|
|
300
|
+
const e1s = await workflow.getExecutions();
|
|
301
|
+
expect(e1s.length).toBe(0);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('trigger on form data', async () => {
|
|
285
305
|
const workflow = await WorkflowModel.create({
|
|
286
306
|
enabled: true,
|
|
287
307
|
type: 'action',
|
|
308
|
+
config: {
|
|
309
|
+
collection: 'posts',
|
|
310
|
+
appends: ['createdBy'],
|
|
311
|
+
},
|
|
288
312
|
});
|
|
289
313
|
|
|
290
314
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
|
@@ -298,17 +322,53 @@ describe('workflow > action-trigger', () => {
|
|
|
298
322
|
const [e1] = await workflow.getExecutions();
|
|
299
323
|
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
|
|
300
324
|
expect(e1.context.data).toMatchObject({ title: 't1' });
|
|
325
|
+
expect(e1.context.data.createdBy).toBeUndefined();
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('trigger on record data', async () => {
|
|
329
|
+
const workflow = await WorkflowModel.create({
|
|
330
|
+
enabled: true,
|
|
331
|
+
type: 'action',
|
|
332
|
+
config: {
|
|
333
|
+
collection: 'posts',
|
|
334
|
+
appends: ['createdBy'],
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const post = await PostRepo.create({
|
|
339
|
+
values: { title: 't1', createdBy: users[0].id },
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
const res1 = await userAgents[0].resource('workflows').trigger({
|
|
343
|
+
values: post.toJSON(),
|
|
344
|
+
triggerWorkflows: `${workflow.key}`,
|
|
345
|
+
});
|
|
346
|
+
expect(res1.status).toBe(202);
|
|
347
|
+
|
|
348
|
+
await sleep(500);
|
|
349
|
+
|
|
350
|
+
const [e1] = await workflow.getExecutions();
|
|
351
|
+
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
|
|
352
|
+
expect(e1.context.data).toMatchObject({ title: 't1' });
|
|
353
|
+
expect(e1.context.data).toHaveProperty('createdBy');
|
|
354
|
+
expect(e1.context.data.createdBy.id).toBe(users[0].id);
|
|
301
355
|
});
|
|
302
356
|
|
|
303
357
|
it('multi trigger', async () => {
|
|
304
358
|
const w1 = await WorkflowModel.create({
|
|
305
359
|
enabled: true,
|
|
306
360
|
type: 'action',
|
|
361
|
+
config: {
|
|
362
|
+
collection: 'posts',
|
|
363
|
+
},
|
|
307
364
|
});
|
|
308
365
|
|
|
309
366
|
const w2 = await WorkflowModel.create({
|
|
310
367
|
enabled: true,
|
|
311
368
|
type: 'action',
|
|
369
|
+
config: {
|
|
370
|
+
collection: 'posts',
|
|
371
|
+
},
|
|
312
372
|
});
|
|
313
373
|
|
|
314
374
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
|
@@ -358,6 +418,9 @@ describe('workflow > action-trigger', () => {
|
|
|
358
418
|
const workflow = await WorkflowModel.create({
|
|
359
419
|
enabled: true,
|
|
360
420
|
type: 'action',
|
|
421
|
+
config: {
|
|
422
|
+
collection: 'posts',
|
|
423
|
+
},
|
|
361
424
|
});
|
|
362
425
|
|
|
363
426
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
|
@@ -377,6 +440,9 @@ describe('workflow > action-trigger', () => {
|
|
|
377
440
|
const workflow = await WorkflowModel.create({
|
|
378
441
|
enabled: true,
|
|
379
442
|
type: 'action',
|
|
443
|
+
config: {
|
|
444
|
+
collection: 'posts',
|
|
445
|
+
},
|
|
380
446
|
});
|
|
381
447
|
|
|
382
448
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
|
@@ -398,6 +464,9 @@ describe('workflow > action-trigger', () => {
|
|
|
398
464
|
const w1 = await WorkflowModel.create({
|
|
399
465
|
enabled: true,
|
|
400
466
|
type: 'action',
|
|
467
|
+
config: {
|
|
468
|
+
collection: 'posts',
|
|
469
|
+
},
|
|
401
470
|
});
|
|
402
471
|
|
|
403
472
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
/package/dist/server/migrations/{20240227172623-change-name.d.ts → 20240307215012-change-name.d.ts}
RENAMED
|
File without changes
|