@nocobase/plugin-workflow 2.0.0-alpha.9 → 2.0.0-beta.10

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.
Files changed (69) hide show
  1. package/dist/client/{f68fbc145c3ddec3.js → 2754af22a0084603.js} +1 -1
  2. package/dist/client/Branch.d.ts +2 -0
  3. package/dist/client/WorkflowTasks.d.ts +3 -1
  4. package/dist/client/aa06821a0d1e7318.js +10 -0
  5. package/dist/client/e078314a62391f36.js +10 -0
  6. package/dist/client/flows/triggerWorkflows.d.ts +15 -42
  7. package/dist/client/index.d.ts +3 -0
  8. package/dist/client/index.js +1 -1
  9. package/dist/client/models/NodeDetailsModel.d.ts +34 -0
  10. package/dist/client/models/NodeValueModel.d.ts +15 -0
  11. package/dist/client/models/index.d.ts +10 -0
  12. package/dist/client/nodes/calculation.d.ts +7 -0
  13. package/dist/client/nodes/create.d.ts +7 -0
  14. package/dist/client/nodes/index.d.ts +14 -0
  15. package/dist/client/nodes/multi-conditions.d.ts +57 -0
  16. package/dist/client/nodes/output.d.ts +53 -0
  17. package/dist/client/nodes/query.d.ts +7 -0
  18. package/dist/client/schemas/executions.d.ts +1 -1
  19. package/dist/client/variable.d.ts +1 -1
  20. package/dist/common/collections/executions.d.ts +1 -1
  21. package/dist/common/collections/executions.js +13 -1
  22. package/dist/common/collections/jobs.js +4 -0
  23. package/dist/externalVersion.js +12 -11
  24. package/dist/locale/de-DE.json +229 -188
  25. package/dist/locale/en-US.json +236 -188
  26. package/dist/locale/es-ES.json +237 -78
  27. package/dist/locale/fr-FR.json +237 -78
  28. package/dist/locale/hu-HU.json +258 -0
  29. package/dist/locale/id-ID.json +258 -0
  30. package/dist/locale/it-IT.json +228 -176
  31. package/dist/locale/ja-JP.json +233 -164
  32. package/dist/locale/ko-KR.json +259 -150
  33. package/dist/locale/nl-NL.json +256 -99
  34. package/dist/locale/pt-BR.json +237 -78
  35. package/dist/locale/ru-RU.json +242 -67
  36. package/dist/locale/tr-TR.json +238 -63
  37. package/dist/locale/uk-UA.json +257 -0
  38. package/dist/locale/vi-VN.json +258 -0
  39. package/dist/locale/zh-CN.json +239 -237
  40. package/dist/locale/zh-TW.json +257 -0
  41. package/dist/node_modules/cron-parser/package.json +1 -1
  42. package/dist/node_modules/lru-cache/package.json +1 -1
  43. package/dist/node_modules/nodejs-snowflake/package.json +1 -1
  44. package/dist/server/Dispatcher.d.ts +1 -3
  45. package/dist/server/Dispatcher.js +30 -30
  46. package/dist/server/Plugin.d.ts +3 -0
  47. package/dist/server/Plugin.js +59 -21
  48. package/dist/server/Processor.d.ts +9 -0
  49. package/dist/server/Processor.js +43 -13
  50. package/dist/server/actions/index.js +3 -0
  51. package/dist/server/actions/jobs.d.ts +9 -0
  52. package/dist/server/actions/jobs.js +64 -0
  53. package/dist/server/actions/nodes.d.ts +1 -0
  54. package/dist/server/actions/nodes.js +78 -1
  55. package/dist/server/instructions/ConditionInstruction.js +4 -1
  56. package/dist/server/instructions/MultiConditionsInstruction.d.ts +18 -0
  57. package/dist/server/instructions/MultiConditionsInstruction.js +118 -0
  58. package/dist/server/instructions/OutputInstruction.d.ts +21 -0
  59. package/dist/server/instructions/OutputInstruction.js +54 -0
  60. package/dist/server/instructions/index.d.ts +3 -1
  61. package/dist/server/repositories/WorkflowRepository.js +10 -2
  62. package/dist/server/triggers/CollectionTrigger.d.ts +3 -0
  63. package/dist/server/triggers/CollectionTrigger.js +31 -3
  64. package/dist/server/triggers/ScheduleTrigger/DateFieldScheduleTrigger.js +8 -0
  65. package/dist/server/triggers/index.d.ts +3 -1
  66. package/dist/server/types/Job.d.ts +1 -0
  67. package/package.json +5 -2
  68. package/dist/client/248e211bb2d99aee.js +0 -10
  69. package/dist/client/e7c028a099537ab1.js +0 -10
@@ -36,6 +36,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
36
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
37
  var Plugin_exports = {};
38
38
  __export(Plugin_exports, {
39
+ WORKER_JOB_WORKFLOW_PROCESS: () => WORKER_JOB_WORKFLOW_PROCESS,
39
40
  default: () => PluginWorkflowServer
40
41
  });
41
42
  module.exports = __toCommonJS(Plugin_exports);
@@ -54,11 +55,14 @@ var import_ScheduleTrigger = __toESM(require("./triggers/ScheduleTrigger"));
54
55
  var import_CalculationInstruction = __toESM(require("./instructions/CalculationInstruction"));
55
56
  var import_ConditionInstruction = __toESM(require("./instructions/ConditionInstruction"));
56
57
  var import_EndInstruction = __toESM(require("./instructions/EndInstruction"));
58
+ var import_OutputInstruction = __toESM(require("./instructions/OutputInstruction"));
57
59
  var import_CreateInstruction = __toESM(require("./instructions/CreateInstruction"));
58
60
  var import_DestroyInstruction = __toESM(require("./instructions/DestroyInstruction"));
59
61
  var import_QueryInstruction = __toESM(require("./instructions/QueryInstruction"));
60
62
  var import_UpdateInstruction = __toESM(require("./instructions/UpdateInstruction"));
63
+ var import_MultiConditionsInstruction = __toESM(require("./instructions/MultiConditionsInstruction"));
61
64
  var import_WorkflowRepository = __toESM(require("./repositories/WorkflowRepository"));
65
+ const WORKER_JOB_WORKFLOW_PROCESS = "workflow:process";
62
66
  class PluginWorkflowServer extends import_server.Plugin {
63
67
  instructions = new import_utils.Registry();
64
68
  triggers = new import_utils.Registry();
@@ -66,6 +70,9 @@ class PluginWorkflowServer extends import_server.Plugin {
66
70
  enabledCache = /* @__PURE__ */ new Map();
67
71
  snowflake;
68
72
  dispatcher = new import_Dispatcher.default(this);
73
+ get channelPendingExecution() {
74
+ return `${this.name}.pendingExecution`;
75
+ }
69
76
  loggerCache;
70
77
  meter = null;
71
78
  checker = null;
@@ -138,7 +145,6 @@ class PluginWorkflowServer extends import_server.Plugin {
138
145
  // * add all hooks for enabled workflows
139
146
  // * add hooks for create/update[enabled]/delete workflow to add/remove specific hooks
140
147
  onAfterStart = async () => {
141
- this.dispatcher.setReady(true);
142
148
  const collection = this.db.getCollection("workflows");
143
149
  const workflows = await collection.repository.find({
144
150
  appends: ["versionStats"]
@@ -165,19 +171,20 @@ class PluginWorkflowServer extends import_server.Plugin {
165
171
  this.app.logger.info("workflow:dispatch");
166
172
  this.dispatcher.dispatch();
167
173
  });
174
+ this.dispatcher.setReady(true);
168
175
  this.getLogger("dispatcher").info("(starting) check for queueing executions");
169
176
  this.dispatcher.dispatch();
170
- this.dispatcher.setReady(true);
171
177
  };
172
178
  onBeforeStop = async () => {
179
+ if (this.checker) {
180
+ clearInterval(this.checker);
181
+ }
182
+ await this.dispatcher.beforeStop();
173
183
  this.app.logger.info(`stopping workflow plugin before app (${this.app.name}) shutdown...`);
174
184
  for (const workflow of this.enabledCache.values()) {
175
185
  this.toggle(workflow, false, { silent: true });
176
186
  }
177
- await this.dispatcher.beforeStop();
178
- if (this.checker) {
179
- clearInterval(this.checker);
180
- }
187
+ this.app.eventQueue.unsubscribe(this.channelPendingExecution);
181
188
  this.loggerCache.clear();
182
189
  };
183
190
  async handleSyncMessage(message) {
@@ -202,13 +209,16 @@ class PluginWorkflowServer extends import_server.Plugin {
202
209
  }
203
210
  }
204
211
  }
212
+ serving() {
213
+ return this.app.serving(WORKER_JOB_WORKFLOW_PROCESS);
214
+ }
205
215
  /**
206
216
  * @experimental
207
217
  */
208
218
  getLogger(workflowId = "dispatcher") {
209
219
  const now = /* @__PURE__ */ new Date();
210
220
  const date = `${now.getFullYear()}-${`0${now.getMonth() + 1}`.slice(-2)}-${`0${now.getDate()}`.slice(-2)}`;
211
- const key = `${date}-${workflowId}}`;
221
+ const key = `${date}-${workflowId}`;
212
222
  if (this.loggerCache.has(key)) {
213
223
  return this.loggerCache.get(key);
214
224
  }
@@ -259,7 +269,9 @@ class PluginWorkflowServer extends import_server.Plugin {
259
269
  initInstructions(more = {}) {
260
270
  this.registerInstruction("calculation", import_CalculationInstruction.default);
261
271
  this.registerInstruction("condition", import_ConditionInstruction.default);
272
+ this.registerInstruction("multi-conditions", import_MultiConditionsInstruction.default);
262
273
  this.registerInstruction("end", import_EndInstruction.default);
274
+ this.registerInstruction("output", import_OutputInstruction.default);
263
275
  this.registerInstruction("create", import_CreateInstruction.default);
264
276
  this.registerInstruction("destroy", import_DestroyInstruction.default);
265
277
  this.registerInstruction("query", import_QueryInstruction.default);
@@ -277,11 +289,8 @@ class PluginWorkflowServer extends import_server.Plugin {
277
289
  filter: { name: this.name }
278
290
  });
279
291
  this.snowflake = new import_nodejs_snowflake.Snowflake({
280
- custom_epoch: pluginRecord == null ? void 0 : pluginRecord.createdAt.getTime()
281
- });
282
- this.app.backgroundJobManager.subscribe(`${this.name}.pendingExecution`, {
283
- idle: () => this.app.serving(import_Dispatcher.WORKER_JOB_WORKFLOW_PROCESS) && this.dispatcher.idle,
284
- process: this.dispatcher.onQueueExecution
292
+ custom_epoch: pluginRecord == null ? void 0 : pluginRecord.createdAt.getTime(),
293
+ instance_id: this.app.instanceId
285
294
  });
286
295
  }
287
296
  /**
@@ -293,18 +302,30 @@ class PluginWorkflowServer extends import_server.Plugin {
293
302
  this.initTriggers(options.triggers);
294
303
  this.initInstructions(options.instructions);
295
304
  (0, import_functions.default)(this, options.functions);
305
+ this.functions.register("instanceId", () => this.app.instanceId);
306
+ this.functions.register("epoch", () => 1605024e3);
307
+ this.functions.register("genSnowflakeId", () => this.app.snowflakeIdGenerator.generate());
296
308
  this.loggerCache = new import_lru_cache.default({
297
309
  max: 20,
298
310
  updateAgeOnGet: true,
299
311
  dispose(logger) {
300
- logger.end();
312
+ const cachedLogger = logger;
313
+ if (!cachedLogger) {
314
+ return;
315
+ }
316
+ cachedLogger.silent = true;
317
+ if (typeof cachedLogger.close === "function") {
318
+ cachedLogger.close();
319
+ }
301
320
  }
302
321
  });
303
322
  this.meter = this.app.telemetry.metric.getMeter();
304
- const counter = this.meter.createObservableGauge("workflow.events.counter");
305
- counter.addCallback((result) => {
306
- result.observe(this.dispatcher.getEventsCount());
307
- });
323
+ if (this.meter) {
324
+ const counter = this.meter.createObservableGauge("workflow.events.counter");
325
+ counter.addCallback((result) => {
326
+ result.observe(this.dispatcher.getEventsCount());
327
+ });
328
+ }
308
329
  this.app.acl.registerSnippet({
309
330
  name: `pm.${this.name}.workflows`,
310
331
  actions: [
@@ -316,6 +337,7 @@ class PluginWorkflowServer extends import_server.Plugin {
316
337
  "executions:destroy",
317
338
  "flow_nodes:update",
318
339
  "flow_nodes:destroy",
340
+ "flow_nodes:destroyBranch",
319
341
  "flow_nodes:test",
320
342
  "jobs:get",
321
343
  "workflowCategories:*"
@@ -333,12 +355,18 @@ class PluginWorkflowServer extends import_server.Plugin {
333
355
  db.on("workflows.afterDestroy", this.onAfterDestroy);
334
356
  this.app.on("afterStart", this.onAfterStart);
335
357
  this.app.on("beforeStop", this.onBeforeStop);
358
+ this.app.eventQueue.subscribe(this.channelPendingExecution, {
359
+ idle: () => this.serving() && this.dispatcher.idle,
360
+ process: this.dispatcher.onQueueExecution
361
+ });
336
362
  }
337
363
  toggle(workflow, enable, { silent, transaction } = {}) {
338
364
  const type = workflow.get("type");
339
365
  const trigger = this.triggers.get(type);
340
366
  if (!trigger) {
341
- this.getLogger(workflow.id).error(`trigger type ${workflow.type} of workflow ${workflow.id} is not implemented`);
367
+ this.getLogger(workflow.id).error(`trigger type ${workflow.type} of workflow ${workflow.id} is not implemented`, {
368
+ workflowId: workflow.id
369
+ });
342
370
  return;
343
371
  }
344
372
  const next = enable ?? workflow.get("enabled");
@@ -346,14 +374,20 @@ class PluginWorkflowServer extends import_server.Plugin {
346
374
  const prev = workflow.previous();
347
375
  if (prev.config) {
348
376
  trigger.off({ ...workflow.get(), ...prev });
349
- this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id} based on configuration before updated`);
377
+ this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id} based on configuration before updated`, {
378
+ workflowId: workflow.id
379
+ });
350
380
  }
351
381
  trigger.on(workflow);
352
- this.getLogger(workflow.id).info(`toggle ON workflow ${workflow.id}`);
382
+ this.getLogger(workflow.id).info(`toggle ON workflow ${workflow.id}`, {
383
+ workflowId: workflow.id
384
+ });
353
385
  this.enabledCache.set(workflow.id, workflow);
354
386
  } else {
355
387
  trigger.off(workflow);
356
- this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id}`);
388
+ this.getLogger(workflow.id).info(`toggle OFF workflow ${workflow.id}`, {
389
+ workflowId: workflow.id
390
+ });
357
391
  this.enabledCache.delete(workflow.id);
358
392
  }
359
393
  if (!silent) {
@@ -453,3 +487,7 @@ class PluginWorkflowServer extends import_server.Plugin {
453
487
  }
454
488
  }
455
489
  }
490
+ // Annotate the CommonJS export names for ESM import in node:
491
+ 0 && (module.exports = {
492
+ WORKER_JOB_WORKFLOW_PROCESS
493
+ });
@@ -96,6 +96,15 @@ export default class Processor {
96
96
  * @experimental
97
97
  */
98
98
  getScope(sourceNodeId?: number | string, includeSelfScope?: boolean): {
99
+ ctx: {
100
+ $context: any;
101
+ $jobsMapByNodeKey: {
102
+ [key: string]: any;
103
+ };
104
+ $system: {};
105
+ $scopes: {};
106
+ $env: {};
107
+ };
99
108
  $context: any;
100
109
  $jobsMapByNodeKey: {
101
110
  [key: string]: any;
@@ -102,11 +102,15 @@ class Processor {
102
102
  });
103
103
  }
104
104
  makeJobs(jobs) {
105
- jobs.forEach((job) => {
105
+ for (const job of jobs) {
106
106
  const node = this.nodesMap.get(job.nodeId);
107
+ if (!node) {
108
+ this.logger.warn(`node (#${job.nodeId}) not found for job (#${job.id}), this will lead to unexpected error`);
109
+ continue;
110
+ }
107
111
  this.jobsMapByNodeKey[node.key] = job;
108
112
  this.jobResultsMapByNodeKey[node.key] = job.result;
109
- });
113
+ }
110
114
  }
111
115
  async prepare() {
112
116
  const {
@@ -144,7 +148,9 @@ class Processor {
144
148
  async start() {
145
149
  const { execution } = this;
146
150
  if (execution.status) {
147
- this.logger.warn(`execution was ended with status ${execution.status} before, could not be started again`);
151
+ this.logger.warn(`execution was ended with status ${execution.status} before, could not be started again`, {
152
+ workflowId: execution.workflowId
153
+ });
148
154
  return;
149
155
  }
150
156
  await this.prepare();
@@ -158,7 +164,9 @@ class Processor {
158
164
  async resume(job) {
159
165
  const { execution } = this;
160
166
  if (execution.status) {
161
- this.logger.warn(`execution was ended with status ${execution.status} before, could not be resumed`);
167
+ this.logger.warn(`execution was ended with status ${execution.status} before, could not be resumed`, {
168
+ workflowId: execution.workflowId
169
+ });
162
170
  return;
163
171
  }
164
172
  await this.prepare();
@@ -168,7 +176,7 @@ class Processor {
168
176
  async exec(instruction, node, prevJob) {
169
177
  let job;
170
178
  try {
171
- this.logger.debug(`config of node`, { data: node.config });
179
+ this.logger.debug(`config of node`, { data: node.config, workflowId: node.workflowId });
172
180
  job = await instruction(node, prevJob, this);
173
181
  if (job === null) {
174
182
  return this.exit();
@@ -179,7 +187,7 @@ class Processor {
179
187
  } catch (err) {
180
188
  this.logger.error(
181
189
  `execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) failed: `,
182
- err
190
+ { error: err, workflowId: node.workflowId }
183
191
  );
184
192
  job = {
185
193
  result: err instanceof Error ? {
@@ -199,7 +207,10 @@ class Processor {
199
207
  }
200
208
  const savedJob = this.saveJob(job);
201
209
  this.logger.info(
202
- `execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) finished as status: ${savedJob.status}`
210
+ `execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id}) finished as status: ${savedJob.status}`,
211
+ {
212
+ workflowId: node.workflowId
213
+ }
203
214
  );
204
215
  this.logger.debug(`result of node`, { data: savedJob.result });
205
216
  if (savedJob.status === import_constants.JOB_STATUS.RESOLVED && node.downstream) {
@@ -217,7 +228,9 @@ class Processor {
217
228
  if (typeof instruction.run !== "function") {
218
229
  return Promise.reject(new Error("`run` should be implemented for customized execution of the node"));
219
230
  }
220
- this.logger.info(`execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id})`);
231
+ this.logger.info(`execution (${this.execution.id}) run instruction [${node.type}] for node (${node.id})`, {
232
+ workflowId: node.workflowId
233
+ });
221
234
  return this.exec(instruction.run.bind(instruction), node, input);
222
235
  }
223
236
  // parent node should take over the control
@@ -225,7 +238,9 @@ class Processor {
225
238
  this.logger.debug(`branch ended at node (${node.id})`);
226
239
  const parentNode = this.findBranchParentNode(node);
227
240
  if (parentNode) {
228
- this.logger.debug(`not on main, recall to parent entry node (${node.id})})`);
241
+ this.logger.debug(`not on main, recall to parent entry node (${node.id})})`, {
242
+ workflowId: node.workflowId
243
+ });
229
244
  await this.recall(parentNode, job);
230
245
  return null;
231
246
  }
@@ -242,7 +257,9 @@ class Processor {
242
257
  new Error(`"resume" method should be implemented for [${node.type}] instruction of node (#${node.id})`)
243
258
  );
244
259
  }
245
- this.logger.info(`execution (${this.execution.id}) resume instruction [${node.type}] for node (${node.id})`);
260
+ this.logger.info(`execution (${this.execution.id}) resume instruction [${node.type}] for node (${node.id})`, {
261
+ workflowId: node.workflowId
262
+ });
246
263
  return this.exec(instruction.resume.bind(instruction), node, job);
247
264
  }
248
265
  async exit(s) {
@@ -261,6 +278,10 @@ class Processor {
261
278
  changes.push([`status`, job.status]);
262
279
  job.changed("status", false);
263
280
  }
281
+ if (job.changed("meta")) {
282
+ changes.push([`meta`, JSON.stringify(job.meta ?? null)]);
283
+ job.changed("meta", false);
284
+ }
264
285
  if (job.changed("result")) {
265
286
  changes.push([`result`, JSON.stringify(job.result ?? null)]);
266
287
  job.changed("result", false);
@@ -295,7 +316,9 @@ class Processor {
295
316
  if (this.mainTransaction && this.mainTransaction !== this.transaction) {
296
317
  await this.mainTransaction.commit();
297
318
  }
298
- this.logger.info(`execution (${this.execution.id}) exiting with status ${this.execution.status}`);
319
+ this.logger.info(`execution (${this.execution.id}) exiting with status ${this.execution.status}`, {
320
+ workflowId: this.execution.workflowId
321
+ });
299
322
  return null;
300
323
  }
301
324
  /**
@@ -326,7 +349,9 @@ class Processor {
326
349
  this.lastSavedJob = job;
327
350
  this.jobsMapByNodeKey[job.nodeKey] = job;
328
351
  this.jobResultsMapByNodeKey[job.nodeKey] = job.result;
329
- this.logger.debug(`job added to save list: ${JSON.stringify(job)}`);
352
+ this.logger.debug(`job added to save list: ${JSON.stringify(job)}`, {
353
+ workflowId: this.execution.workflowId
354
+ });
330
355
  return job;
331
356
  }
332
357
  /**
@@ -414,13 +439,18 @@ class Processor {
414
439
  $scopes[n.id] = $scopes[n.key] = instruction.getScope(n, this.jobResultsMapByNodeKey[n.key], this);
415
440
  }
416
441
  }
417
- return {
442
+ const scopes = {
418
443
  $context: this.execution.context,
419
444
  $jobsMapByNodeKey: this.jobResultsMapByNodeKey,
420
445
  $system: systemFns,
421
446
  $scopes,
422
447
  $env: this.options.plugin.app.environment.getVariables()
423
448
  };
449
+ return {
450
+ ...scopes,
451
+ ctx: scopes
452
+ // 2.0
453
+ };
424
454
  }
425
455
  /**
426
456
  * @experimental
@@ -41,6 +41,7 @@ __export(actions_exports, {
41
41
  module.exports = __toCommonJS(actions_exports);
42
42
  var workflows = __toESM(require("./workflows"));
43
43
  var nodes = __toESM(require("./nodes"));
44
+ var jobs = __toESM(require("./jobs"));
44
45
  var executions = __toESM(require("./executions"));
45
46
  var userWorkflowTasks = __toESM(require("./userWorkflowTasks"));
46
47
  function make(name, mod) {
@@ -61,8 +62,10 @@ function actions_default({ app }) {
61
62
  ...make("flow_nodes", {
62
63
  update: nodes.update,
63
64
  destroy: nodes.destroy,
65
+ destroyBranch: nodes.destroyBranch,
64
66
  test: nodes.test
65
67
  }),
68
+ ...make("jobs", jobs),
66
69
  ...make("executions", executions),
67
70
  ...make("userWorkflowTasks", userWorkflowTasks)
68
71
  });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ export declare function resume(context: any, next: any): Promise<any>;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var jobs_exports = {};
38
+ __export(jobs_exports, {
39
+ resume: () => resume
40
+ });
41
+ module.exports = __toCommonJS(jobs_exports);
42
+ var import_actions = require("@nocobase/actions");
43
+ var import_Plugin = __toESM(require("../Plugin"));
44
+ async function resume(context, next) {
45
+ const repository = import_actions.utils.getRepositoryFromParams(context);
46
+ const workflowPlugin = context.app.pm.get(import_Plugin.default);
47
+ const { filterByTk, values = {} } = context.action.params;
48
+ const job = await repository.findOne({
49
+ filterByTk
50
+ });
51
+ if (!job) {
52
+ return context.throw(404, "Job not found");
53
+ }
54
+ workflowPlugin.getLogger(job.workflowId).warn(`Resuming job #${job.id}...`);
55
+ await job.update(values);
56
+ context.body = job;
57
+ context.status = 202;
58
+ await next();
59
+ workflowPlugin.resume(job);
60
+ }
61
+ // Annotate the CommonJS export names for ESM import in node:
62
+ 0 && (module.exports = {
63
+ resume
64
+ });
@@ -9,5 +9,6 @@
9
9
  import { Context } from '@nocobase/actions';
10
10
  export declare function create(context: Context, next: any): Promise<void>;
11
11
  export declare function destroy(context: Context, next: any): Promise<void>;
12
+ export declare function destroyBranch(context: Context, next: any): Promise<void>;
12
13
  export declare function update(context: Context, next: any): Promise<void>;
13
14
  export declare function test(context: Context, next: any): Promise<void>;
@@ -38,6 +38,7 @@ var nodes_exports = {};
38
38
  __export(nodes_exports, {
39
39
  create: () => create,
40
40
  destroy: () => destroy,
41
+ destroyBranch: () => destroyBranch,
41
42
  test: () => test,
42
43
  update: () => update
43
44
  });
@@ -158,7 +159,7 @@ async function destroy(context, next) {
158
159
  const repository = import_actions.utils.getRepositoryFromParams(context);
159
160
  const { filterByTk, keepBranch } = context.action.params;
160
161
  const keepBranchIndex = keepBranch == null || keepBranch === "" ? null : Number.parseInt(keepBranch, 10);
161
- const fields = ["id", "upstreamId", "downstreamId", "branchIndex"];
162
+ const fields = ["id", "upstreamId", "downstreamId", "branchIndex", "key"];
162
163
  const instance = await repository.findOne({
163
164
  filterByTk,
164
165
  fields: [...fields, "workflowId"],
@@ -260,6 +261,81 @@ async function destroy(context, next) {
260
261
  context.body = instance;
261
262
  await next();
262
263
  }
264
+ async function destroyBranch(context, next) {
265
+ const { db } = context;
266
+ const repository = import_actions.utils.getRepositoryFromParams(context);
267
+ const { filterByTk, branchIndex: branchIndexParam, shift: shiftParam } = context.action.params;
268
+ if (branchIndexParam == null || branchIndexParam === "") {
269
+ context.throw(400, "branchIndex is required");
270
+ }
271
+ const branchIndex = Number.parseInt(branchIndexParam, 10);
272
+ if (Number.isNaN(branchIndex)) {
273
+ context.throw(400, "branchIndex must be a number");
274
+ }
275
+ const shift = !(shiftParam == null || shiftParam === "") && Number.parseInt(String(shiftParam), 10) === 1;
276
+ const fields = ["id", "upstreamId", "downstreamId", "branchIndex", "key"];
277
+ const instance = await repository.findOne({
278
+ filterByTk,
279
+ fields: [...fields, "workflowId"],
280
+ appends: ["workflow.versionStats.executed"]
281
+ });
282
+ if (!instance) {
283
+ context.throw(404, "Node not found");
284
+ }
285
+ if (instance.workflow.versionStats.executed > 0) {
286
+ context.throw(400, "Branches in executed workflow could not be deleted");
287
+ }
288
+ let deletedBranchHead = null;
289
+ await db.sequelize.transaction(async (transaction) => {
290
+ const nodes = await repository.find({
291
+ filter: {
292
+ workflowId: instance.workflowId
293
+ },
294
+ fields,
295
+ transaction
296
+ });
297
+ const nodesMap = /* @__PURE__ */ new Map();
298
+ nodes.forEach((item) => {
299
+ nodesMap.set(item.id, item);
300
+ });
301
+ nodes.forEach((item) => {
302
+ if (item.upstreamId) {
303
+ item.upstream = nodesMap.get(item.upstreamId);
304
+ }
305
+ if (item.downstreamId) {
306
+ item.downstream = nodesMap.get(item.downstreamId);
307
+ }
308
+ });
309
+ const branchHeads = nodes.filter((item) => item.upstreamId === instance.id && item.branchIndex != null).sort((a, b) => a.branchIndex - b.branchIndex);
310
+ const branchHead = branchHeads.find((item) => item.branchIndex === branchIndex);
311
+ deletedBranchHead = branchHead || null;
312
+ if (branchHead) {
313
+ const nodesToDelete = searchBranchDownstreams(nodes, branchHead);
314
+ const idsToDelete = nodesToDelete.map((item) => item.id);
315
+ if (idsToDelete.length) {
316
+ await repository.destroy({
317
+ filterByTk: idsToDelete,
318
+ transaction
319
+ });
320
+ }
321
+ }
322
+ if (shift) {
323
+ const headsToShift = branchHeads.filter((item) => item.branchIndex > branchIndex);
324
+ await Promise.all(
325
+ headsToShift.map(
326
+ (item) => item.update(
327
+ {
328
+ branchIndex: item.branchIndex - 1
329
+ },
330
+ { transaction }
331
+ )
332
+ )
333
+ );
334
+ }
335
+ });
336
+ context.body = deletedBranchHead;
337
+ await next();
338
+ }
263
339
  async function update(context, next) {
264
340
  const { db } = context;
265
341
  const repository = import_actions.utils.getRepositoryFromParams(context);
@@ -308,6 +384,7 @@ async function test(context, next) {
308
384
  0 && (module.exports = {
309
385
  create,
310
386
  destroy,
387
+ destroyBranch,
311
388
  test,
312
389
  update
313
390
  });
@@ -81,7 +81,10 @@ class ConditionInstruction extends import__.Instruction {
81
81
  if (branchJob.status === import_constants.JOB_STATUS.RESOLVED) {
82
82
  return job;
83
83
  }
84
- return processor.exit(branchJob.status);
84
+ if (branchJob.status === import_constants.JOB_STATUS.PENDING) {
85
+ return processor.exit(branchJob.status);
86
+ }
87
+ return branchJob;
85
88
  }
86
89
  async test({ engine, calculation, expression = "" }) {
87
90
  const evaluator = import_evaluators.evaluators.get(engine);
@@ -0,0 +1,18 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Instruction } from '.';
10
+ import type Processor from '../Processor';
11
+ import type { FlowNodeModel, JobModel } from '../types';
12
+ export declare class MultiConditionsInstruction extends Instruction {
13
+ run(node: FlowNodeModel, prevJob: any, processor: Processor): Promise<JobModel>;
14
+ resume(node: FlowNodeModel, branchJob: JobModel, processor: Processor): Promise<any>;
15
+ private evaluateCondition;
16
+ private getBranchNode;
17
+ }
18
+ export default MultiConditionsInstruction;