@nocobase/plugin-workflow 0.12.0-alpha.5 → 0.13.0-alpha.1

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.
@@ -1,31 +1,31 @@
1
1
  'use strict';
2
2
 
3
3
  var path = require('path');
4
- var winston = require('winston');
5
4
  var LRUCache = require('lru-cache');
5
+ var winston = require('winston');
6
6
  var database = require('@nocobase/database');
7
7
  var server = require('@nocobase/server');
8
8
  var utils = require('@nocobase/utils');
9
- var initFields = require('./fields');
9
+ var logger = require('@nocobase/logger');
10
+ var Processor = require('./Processor');
10
11
  var initActions = require('./actions');
11
12
  var constants = require('./constants');
13
+ var initFields = require('./fields');
14
+ var initFunctions = require('./functions');
12
15
  var initInstructions = require('./instructions');
13
- var Processor = require('./Processor');
14
16
  var initTriggers = require('./triggers');
15
- var initFunctions = require('./functions');
16
- var logger = require('@nocobase/logger');
17
17
 
18
18
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
19
19
 
20
20
  var path__default = /*#__PURE__*/_interopDefault(path);
21
- var winston__default = /*#__PURE__*/_interopDefault(winston);
22
21
  var LRUCache__default = /*#__PURE__*/_interopDefault(LRUCache);
23
- var initFields__default = /*#__PURE__*/_interopDefault(initFields);
22
+ var winston__default = /*#__PURE__*/_interopDefault(winston);
23
+ var Processor__default = /*#__PURE__*/_interopDefault(Processor);
24
24
  var initActions__default = /*#__PURE__*/_interopDefault(initActions);
25
+ var initFields__default = /*#__PURE__*/_interopDefault(initFields);
26
+ var initFunctions__default = /*#__PURE__*/_interopDefault(initFunctions);
25
27
  var initInstructions__default = /*#__PURE__*/_interopDefault(initInstructions);
26
- var Processor__default = /*#__PURE__*/_interopDefault(Processor);
27
28
  var initTriggers__default = /*#__PURE__*/_interopDefault(initTriggers);
28
- var initFunctions__default = /*#__PURE__*/_interopDefault(initFunctions);
29
29
 
30
30
  class WorkflowPlugin extends server.Plugin {
31
31
  instructions = new utils.Registry();
@@ -147,6 +147,7 @@ class WorkflowPlugin extends server.Plugin {
147
147
  });
148
148
  });
149
149
  this.app.on("afterStart", () => {
150
+ this.app.setMaintainingMessage("check for not started executions");
150
151
  this.dispatch();
151
152
  });
152
153
  this.app.on("beforeStop", async () => {
@@ -186,6 +187,16 @@ class WorkflowPlugin extends server.Plugin {
186
187
  }
187
188
  setTimeout(this.prepare);
188
189
  }
190
+ async resume(job) {
191
+ if (!job.execution) {
192
+ job.execution = await job.getExecution();
193
+ }
194
+ this.pending.push([job.execution, job]);
195
+ this.dispatch();
196
+ }
197
+ createProcessor(execution, options = {}) {
198
+ return new Processor__default.default(execution, { ...options, plugin: this });
199
+ }
189
200
  prepare = async () => {
190
201
  var _a;
191
202
  const [event] = this.events;
@@ -241,13 +252,6 @@ class WorkflowPlugin extends server.Plugin {
241
252
  this.dispatch();
242
253
  }
243
254
  };
244
- async resume(job) {
245
- if (!job.execution) {
246
- job.execution = await job.getExecution();
247
- }
248
- this.pending.push([job.execution, job]);
249
- this.dispatch();
250
- }
251
255
  async dispatch() {
252
256
  if (this.executing) {
253
257
  return;
@@ -297,9 +301,6 @@ class WorkflowPlugin extends server.Plugin {
297
301
  this.getLogger(execution.workflowId).error(`execution (${execution.id}) error: ${err.message}`, err);
298
302
  }
299
303
  }
300
- createProcessor(execution, options = {}) {
301
- return new Processor__default.default(execution, { ...options, plugin: this });
302
- }
303
304
  }
304
305
 
305
306
  module.exports = WorkflowPlugin;
@@ -39,7 +39,7 @@ export default class Processor {
39
39
  findBranchEndNode(node: FlowNodeModel): FlowNodeModel | null;
40
40
  findBranchParentJob(job: JobModel, node: FlowNodeModel): JobModel | null;
41
41
  findBranchLastJob(node: FlowNodeModel): JobModel | null;
42
- getScope(node?: any): {
42
+ getScope(sourceNodeId: number): {
43
43
  $context: any;
44
44
  $jobsMapByNodeId: {
45
45
  [key: number]: any;
@@ -47,5 +47,5 @@ export default class Processor {
47
47
  $system: {};
48
48
  $scopes: {};
49
49
  };
50
- getParsedValue(value: any, node?: any, additionalScope?: object): any;
50
+ getParsedValue(value: any, sourceNodeId: number, additionalScope?: object): any;
51
51
  }
@@ -250,7 +250,8 @@ class Processor {
250
250
  }
251
251
  return null;
252
252
  }
253
- getScope(node) {
253
+ getScope(sourceNodeId) {
254
+ const node = this.nodesMap.get(sourceNodeId);
254
255
  const systemFns = {};
255
256
  const scope = {
256
257
  execution: this.execution,
@@ -260,12 +261,10 @@ class Processor {
260
261
  systemFns[name] = fn.bind(scope);
261
262
  }
262
263
  const $scopes = {};
263
- if (node) {
264
- for (let n = this.findBranchParentNode(node); n; n = this.findBranchParentNode(n)) {
265
- const instruction = this.options.plugin.instructions.get(n.type);
266
- if (typeof instruction.getScope === "function") {
267
- $scopes[n.id] = instruction.getScope(n, this.jobsMapByNodeId[n.id], this);
268
- }
264
+ for (let n = this.findBranchParentNode(node); n; n = this.findBranchParentNode(n)) {
265
+ const instruction = this.options.plugin.instructions.get(n.type);
266
+ if (typeof instruction.getScope === "function") {
267
+ $scopes[n.id] = instruction.getScope(n, this.jobsMapByNodeId[n.id], this);
269
268
  }
270
269
  }
271
270
  return {
@@ -275,9 +274,9 @@ class Processor {
275
274
  $scopes
276
275
  };
277
276
  }
278
- getParsedValue(value, node, additionalScope) {
277
+ getParsedValue(value, sourceNodeId, additionalScope) {
279
278
  const template = utils.parse(value);
280
- const scope = Object.assign(this.getScope(node), additionalScope);
279
+ const scope = Object.assign(this.getScope(sourceNodeId), additionalScope);
281
280
  template.parameters.forEach(({ key }) => {
282
281
  evaluators.appendArrayColumn(scope, key);
283
282
  });
@@ -13,11 +13,11 @@ const aggregators = {
13
13
  var aggregate_default = {
14
14
  async run(node, input, processor) {
15
15
  const { aggregator, associated, collection, association = {}, params = {} } = node.config;
16
- const options = processor.getParsedValue(params);
16
+ const options = processor.getParsedValue(params, node.id);
17
17
  const { database: database$1 } = node.constructor;
18
18
  const repo = associated ? database$1.getRepository(
19
19
  `${association == null ? void 0 : association.associatedCollection}.${association.name}`,
20
- processor.getParsedValue(association == null ? void 0 : association.associatedKey)
20
+ processor.getParsedValue(association == null ? void 0 : association.associatedKey, node.id)
21
21
  ) : database$1.getRepository(collection);
22
22
  if (!options.dataType && aggregator === "avg") {
23
23
  options.dataType = database.DataTypes.DOUBLE;
@@ -8,7 +8,7 @@ var calculation_default = {
8
8
  async run(node, prevJob, processor) {
9
9
  const { dynamic = false } = node.config || {};
10
10
  let { engine = "math.js", expression = "" } = node.config;
11
- let scope = processor.getScope(node);
11
+ let scope = processor.getScope(node.id);
12
12
  if (dynamic) {
13
13
  const parsed = utils.parse(dynamic)(scope) ?? {};
14
14
  engine = parsed.engine;
@@ -84,7 +84,7 @@ var condition_default = {
84
84
  const evaluator = evaluators.evaluators.get(engine);
85
85
  let result = true;
86
86
  try {
87
- result = evaluator ? evaluator(expression, processor.getScope()) : logicCalculate(processor.getParsedValue(calculation, node));
87
+ result = evaluator ? evaluator(expression, processor.getScope(node.id)) : logicCalculate(processor.getParsedValue(calculation, node.id));
88
88
  } catch (e) {
89
89
  return {
90
90
  result: e.toString(),
@@ -109,13 +109,15 @@ var condition_default = {
109
109
  return job;
110
110
  }
111
111
  const savedJob = await processor.saveJob(job);
112
- return processor.run(branchNode, savedJob);
112
+ await processor.run(branchNode, savedJob);
113
+ return null;
113
114
  },
114
115
  async resume(node, branchJob, processor) {
116
+ const job = processor.findBranchParentJob(branchJob, node);
115
117
  if (branchJob.status === constants.JOB_STATUS.RESOLVED) {
116
- return branchJob;
118
+ return job;
117
119
  }
118
- return processor.end(node, branchJob);
120
+ return processor.exit(branchJob.status);
119
121
  }
120
122
  };
121
123
 
@@ -7,7 +7,7 @@ var create_default = {
7
7
  async run(node, input, processor) {
8
8
  const { collection, params: { appends = [], ...params } = {} } = node.config;
9
9
  const { repository, model } = node.constructor.database.getCollection(collection);
10
- const options = processor.getParsedValue(params, node);
10
+ const options = processor.getParsedValue(params, node.id);
11
11
  const created = await repository.create({
12
12
  ...options,
13
13
  context: {
@@ -6,7 +6,7 @@ var destroy_default = {
6
6
  async run(node, input, processor) {
7
7
  const { collection, params = {} } = node.config;
8
8
  const repo = node.constructor.database.getRepository(collection);
9
- const options = processor.getParsedValue(params, node);
9
+ const options = processor.getParsedValue(params, node.id);
10
10
  const result = await repo.destroy({
11
11
  ...options,
12
12
  context: {
@@ -18,7 +18,7 @@ function getTargetLength(target) {
18
18
  var loop_default = {
19
19
  async run(node, prevJob, processor) {
20
20
  const [branch] = processor.getBranches(node);
21
- const target = processor.getParsedValue(node.config.target, node);
21
+ const target = processor.getParsedValue(node.config.target, node.id);
22
22
  const length = getTargetLength(target);
23
23
  if (!branch || !length) {
24
24
  return {
@@ -45,7 +45,7 @@ var loop_default = {
45
45
  return processor.exit();
46
46
  }
47
47
  const nextIndex = result + 1;
48
- const target = processor.getParsedValue(loop.config.target, node);
48
+ const target = processor.getParsedValue(loop.config.target, node.id);
49
49
  if (branchJob.status > constants.JOB_STATUS.PENDING) {
50
50
  job.set({ result: nextIndex });
51
51
  const length = getTargetLength(target);
@@ -61,7 +61,7 @@ var loop_default = {
61
61
  return job;
62
62
  },
63
63
  getScope(node, index, processor) {
64
- const target = processor.getParsedValue(node.config.target, node);
64
+ const target = processor.getParsedValue(node.config.target, node.id);
65
65
  const targets = (Array.isArray(target) ? target : [target]).filter((t) => t != null);
66
66
  const length = getTargetLength(target);
67
67
  const item = typeof target === "number" ? index : targets[index];
@@ -34,11 +34,11 @@ async function submit(context, next) {
34
34
  userJob.execution.workflow = userJob.workflow;
35
35
  const processor = plugin.createProcessor(userJob.execution);
36
36
  await processor.prepare();
37
- const assignees = processor.getParsedValue(userJob.node.config.assignees ?? []);
37
+ const assignees = processor.getParsedValue(userJob.node.config.assignees ?? [], userJob.nodeId);
38
38
  if (!assignees.includes(currentUser.id) || userJob.userId !== currentUser.id) {
39
39
  return context.throw(403);
40
40
  }
41
- const presetValues = processor.getParsedValue(actionItem.values ?? {}, null, {
41
+ const presetValues = processor.getParsedValue(actionItem.values ?? {}, userJob.nodeId, {
42
42
  currentUser: currentUser.toJSON(),
43
43
  currentRecord: values.result[formKey],
44
44
  currentTime: /* @__PURE__ */ new Date()
@@ -8,7 +8,7 @@ async function update_default(instance, { collection, filter = {} }, processor)
8
8
  const { _, ...form } = instance.result;
9
9
  const [values] = Object.values(form);
10
10
  await repo.update({
11
- filter: processor.getParsedValue(filter),
11
+ filter: processor.getParsedValue(filter, instance.nodeId),
12
12
  values: {
13
13
  ...values ?? {},
14
14
  updatedBy: instance.userId
@@ -1,5 +1,5 @@
1
1
  import { Registry } from '@nocobase/utils';
2
- import Plugin from '../..';
2
+ import Plugin, { Processor } from '../..';
3
3
  import { Instruction } from '..';
4
4
  import { FormHandler } from './forms';
5
5
  type FormType = {
@@ -23,7 +23,7 @@ export default class implements Instruction {
23
23
  protected plugin: Plugin;
24
24
  formTypes: Registry<FormHandler>;
25
25
  constructor(plugin: Plugin);
26
- run(node: any, prevJob: any, processor: any): Promise<any>;
27
- resume(node: any, job: any, processor: any): Promise<any>;
26
+ run(node: any, prevJob: any, processor: Processor): Promise<any>;
27
+ resume(node: any, job: any, processor: Processor): Promise<any>;
28
28
  }
29
29
  export {};
@@ -108,7 +108,7 @@ class manual_default {
108
108
  formTypes = new utils.Registry();
109
109
  async run(node, prevJob, processor) {
110
110
  const { mode, ...config } = node.config;
111
- const assignees = [...new Set(processor.getParsedValue(config.assignees) || [])];
111
+ const assignees = [...new Set(processor.getParsedValue(config.assignees, node.id) || [])];
112
112
  const job = await processor.saveJob({
113
113
  status: constants.JOB_STATUS.PENDING,
114
114
  result: mode ? [] : null,
@@ -13,7 +13,7 @@ var query_default = {
13
13
  pageSize = actions.DEFAULT_PER_PAGE,
14
14
  sort = [],
15
15
  ...options
16
- } = processor.getParsedValue(params, node);
16
+ } = processor.getParsedValue(params, node.id);
17
17
  const appends = options.appends ? Array.from(
18
18
  options.appends.reduce((set, field) => {
19
19
  set.add(field.split(".")[0]);
@@ -39,7 +39,7 @@ class request_default {
39
39
  nodeId: node.id,
40
40
  upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null
41
41
  });
42
- const config = processor.getParsedValue(node.config, node);
42
+ const config = processor.getParsedValue(node.config, node.id);
43
43
  request(config).then((response) => {
44
44
  job.set({
45
45
  status: constants.JOB_STATUS.RESOLVED,
@@ -5,7 +5,7 @@ var __ = require('..');
5
5
  var sql_default = {
6
6
  async run(node, input, processor) {
7
7
  const { sequelize } = node.constructor.database;
8
- const sql = processor.getParsedValue(node.config.sql ?? "", node).trim();
8
+ const sql = processor.getParsedValue(node.config.sql ?? "", node.id).trim();
9
9
  if (!sql) {
10
10
  return {
11
11
  status: __.JOB_STATUS.RESOLVED
@@ -6,7 +6,7 @@ var update_default = {
6
6
  async run(node, input, processor) {
7
7
  const { collection, params = {} } = node.config;
8
8
  const repo = node.constructor.database.getRepository(collection);
9
- const options = processor.getParsedValue(params, node);
9
+ const options = processor.getParsedValue(params, node.id);
10
10
  const result = await repo.update({
11
11
  ...options,
12
12
  context: {