@nocobase/plugin-workflow 0.7.0-alpha.34 → 0.7.0-alpha.57

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 (132) hide show
  1. package/lib/actions/index.js +31 -32
  2. package/lib/actions/nodes.js +310 -182
  3. package/lib/actions/workflows.js +260 -157
  4. package/lib/calculators/index.js +142 -90
  5. package/lib/collections/executions.js +40 -43
  6. package/lib/collections/flow_nodes.js +60 -65
  7. package/lib/collections/jobs.js +45 -47
  8. package/lib/collections/workflows.js +78 -87
  9. package/lib/constants.js +22 -17
  10. package/lib/index.js +71 -22
  11. package/lib/instructions/calculation.js +34 -29
  12. package/lib/instructions/condition.js +94 -87
  13. package/lib/instructions/create.js +43 -26
  14. package/lib/instructions/destroy.js +42 -25
  15. package/lib/instructions/index.js +46 -25
  16. package/lib/instructions/parallel.js +99 -84
  17. package/lib/instructions/prompt.js +21 -13
  18. package/lib/instructions/query.js +47 -29
  19. package/lib/instructions/update.js +44 -25
  20. package/lib/models/Execution.js +401 -248
  21. package/lib/models/FlowNode.js +18 -5
  22. package/lib/models/Job.js +18 -5
  23. package/lib/models/Workflow.js +132 -88
  24. package/lib/server.js +93 -66
  25. package/lib/triggers/collection.js +113 -51
  26. package/lib/triggers/index.js +25 -11
  27. package/package.json +7 -12
  28. package/esm/actions/index.d.ts +0 -1
  29. package/esm/actions/index.js +0 -14
  30. package/esm/actions/index.js.map +0 -1
  31. package/esm/actions/nodes.d.ts +0 -4
  32. package/esm/actions/nodes.js +0 -187
  33. package/esm/actions/nodes.js.map +0 -1
  34. package/esm/actions/workflows.d.ts +0 -3
  35. package/esm/actions/workflows.js +0 -163
  36. package/esm/actions/workflows.js.map +0 -1
  37. package/esm/calculators/index.d.ts +0 -38
  38. package/esm/calculators/index.js +0 -128
  39. package/esm/calculators/index.js.map +0 -1
  40. package/esm/collections/executions.d.ts +0 -3
  41. package/esm/collections/executions.js +0 -43
  42. package/esm/collections/executions.js.map +0 -1
  43. package/esm/collections/flow_nodes.d.ts +0 -3
  44. package/esm/collections/flow_nodes.js +0 -65
  45. package/esm/collections/flow_nodes.js.map +0 -1
  46. package/esm/collections/jobs.d.ts +0 -3
  47. package/esm/collections/jobs.js +0 -47
  48. package/esm/collections/jobs.js.map +0 -1
  49. package/esm/collections/workflows.d.ts +0 -3
  50. package/esm/collections/workflows.js +0 -87
  51. package/esm/collections/workflows.js.map +0 -1
  52. package/esm/constants.d.ts +0 -17
  53. package/esm/constants.js +0 -18
  54. package/esm/constants.js.map +0 -1
  55. package/esm/index.d.ts +0 -5
  56. package/esm/index.js +0 -6
  57. package/esm/index.js.map +0 -1
  58. package/esm/instructions/calculation.d.ts +0 -8
  59. package/esm/instructions/calculation.js +0 -55
  60. package/esm/instructions/calculation.js.map +0 -1
  61. package/esm/instructions/condition.d.ts +0 -5
  62. package/esm/instructions/condition.js +0 -99
  63. package/esm/instructions/condition.js.map +0 -1
  64. package/esm/instructions/create.d.ts +0 -8
  65. package/esm/instructions/create.js +0 -26
  66. package/esm/instructions/create.js.map +0 -1
  67. package/esm/instructions/destroy.d.ts +0 -8
  68. package/esm/instructions/destroy.js +0 -25
  69. package/esm/instructions/destroy.js.map +0 -1
  70. package/esm/instructions/index.d.ts +0 -15
  71. package/esm/instructions/index.js +0 -20
  72. package/esm/instructions/index.js.map +0 -1
  73. package/esm/instructions/parallel.d.ts +0 -13
  74. package/esm/instructions/parallel.js +0 -88
  75. package/esm/instructions/parallel.js.map +0 -1
  76. package/esm/instructions/prompt.d.ts +0 -7
  77. package/esm/instructions/prompt.js +0 -13
  78. package/esm/instructions/prompt.js.map +0 -1
  79. package/esm/instructions/query.d.ts +0 -8
  80. package/esm/instructions/query.js +0 -29
  81. package/esm/instructions/query.js.map +0 -1
  82. package/esm/instructions/update.d.ts +0 -9
  83. package/esm/instructions/update.js +0 -25
  84. package/esm/instructions/update.js.map +0 -1
  85. package/esm/models/Execution.d.ts +0 -51
  86. package/esm/models/Execution.js +0 -256
  87. package/esm/models/Execution.js.map +0 -1
  88. package/esm/models/FlowNode.d.ts +0 -17
  89. package/esm/models/FlowNode.js +0 -4
  90. package/esm/models/FlowNode.js.map +0 -1
  91. package/esm/models/Job.d.ts +0 -15
  92. package/esm/models/Job.js +0 -4
  93. package/esm/models/Job.js.map +0 -1
  94. package/esm/models/Workflow.d.ts +0 -29
  95. package/esm/models/Workflow.js +0 -88
  96. package/esm/models/Workflow.js.map +0 -1
  97. package/esm/server.d.ts +0 -5
  98. package/esm/server.js +0 -62
  99. package/esm/server.js.map +0 -1
  100. package/esm/triggers/collection.d.ts +0 -12
  101. package/esm/triggers/collection.js +0 -61
  102. package/esm/triggers/collection.js.map +0 -1
  103. package/esm/triggers/index.d.ts +0 -9
  104. package/esm/triggers/index.js +0 -6
  105. package/esm/triggers/index.js.map +0 -1
  106. package/lib/actions/index.js.map +0 -1
  107. package/lib/actions/nodes.js.map +0 -1
  108. package/lib/actions/workflows.js.map +0 -1
  109. package/lib/calculators/index.js.map +0 -1
  110. package/lib/collections/executions.js.map +0 -1
  111. package/lib/collections/flow_nodes.js.map +0 -1
  112. package/lib/collections/jobs.js.map +0 -1
  113. package/lib/collections/workflows.js.map +0 -1
  114. package/lib/constants.js.map +0 -1
  115. package/lib/index.js.map +0 -1
  116. package/lib/instructions/calculation.js.map +0 -1
  117. package/lib/instructions/condition.js.map +0 -1
  118. package/lib/instructions/create.js.map +0 -1
  119. package/lib/instructions/destroy.js.map +0 -1
  120. package/lib/instructions/index.js.map +0 -1
  121. package/lib/instructions/parallel.js.map +0 -1
  122. package/lib/instructions/prompt.js.map +0 -1
  123. package/lib/instructions/query.js.map +0 -1
  124. package/lib/instructions/update.js.map +0 -1
  125. package/lib/models/Execution.js.map +0 -1
  126. package/lib/models/FlowNode.js.map +0 -1
  127. package/lib/models/Job.js.map +0 -1
  128. package/lib/models/Workflow.js.map +0 -1
  129. package/lib/server.js.map +0 -1
  130. package/lib/triggers/collection.js.map +0 -1
  131. package/lib/triggers/index.js.map +0 -1
  132. package/tsconfig.build.json +0 -9
@@ -1,36 +1,35 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
13
5
  });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- const workflows = __importStar(require("./workflows"));
23
- const nodes = __importStar(require("./nodes"));
6
+ exports.default = _default;
7
+
8
+ var workflows = _interopRequireWildcard(require("./workflows"));
9
+
10
+ var nodes = _interopRequireWildcard(require("./nodes"));
11
+
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+
16
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
17
+
18
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
19
+
20
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
+
24
22
  function make(name, mod) {
25
- return Object.keys(mod).reduce((result, key) => (Object.assign(Object.assign({}, result), { [`${name}:${key}`]: mod[key] })), {});
23
+ return Object.keys(mod).reduce((result, key) => _objectSpread(_objectSpread({}, result), {}, {
24
+ [`${name}:${key}`]: mod[key]
25
+ }), {});
26
26
  }
27
- function default_1(app) {
28
- app.actions(Object.assign(Object.assign(Object.assign({}, make('workflows', workflows)), make('workflows.nodes', {
29
- create: nodes.create,
30
- destroy: nodes.destroy
31
- })), make('flow_nodes', {
32
- update: nodes.update
33
- })));
34
- }
35
- exports.default = default_1;
36
- //# sourceMappingURL=index.js.map
27
+
28
+ function _default(app) {
29
+ app.actions(_objectSpread(_objectSpread(_objectSpread({}, make('workflows', workflows)), make('workflows.nodes', {
30
+ create: nodes.create,
31
+ destroy: nodes.destroy
32
+ })), make('flow_nodes', {
33
+ update: nodes.update
34
+ })));
35
+ }
@@ -1,193 +1,321 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.update = exports.destroy = exports.create = void 0;
13
- const sequelize_1 = require("sequelize");
14
- const actions_1 = require("@nocobase/actions");
15
- function create(context, next) {
16
- return __awaiter(this, void 0, void 0, function* () {
17
- const { db } = context;
18
- const repository = actions_1.utils.getRepositoryFromParams(context);
19
- const { whitelist, blacklist, updateAssociationValues, values, associatedIndex: workflowId } = context.action.params;
20
- context.body = yield db.sequelize.transaction((transaction) => __awaiter(this, void 0, void 0, function* () {
21
- const workflow = yield repository.getSourceModel(transaction);
22
- if (workflow.executed) {
23
- context.throw(400, 'Node could not be created in executed workflow');
24
- }
25
- const instance = yield repository.create({
26
- values,
27
- whitelist,
28
- blacklist,
29
- updateAssociationValues,
30
- context,
31
- transaction
32
- });
33
- if (!instance.upstreamId) {
34
- const previousHead = yield repository.findOne({
35
- filter: {
36
- id: {
37
- $ne: instance.id
38
- },
39
- upstreamId: null
40
- },
41
- transaction
42
- });
43
- if (previousHead) {
44
- yield previousHead.setUpstream(instance, { transaction });
45
- yield instance.setDownstream(previousHead, { transaction });
46
- instance.set('downstream', previousHead);
47
- }
48
- return instance;
49
- }
50
- const upstream = yield instance.getUpstream({ transaction });
51
- if (instance.branchIndex == null) {
52
- const downstream = yield upstream.getDownstream({ transaction });
53
- if (downstream) {
54
- yield downstream.setUpstream(instance, { transaction });
55
- yield instance.setDownstream(downstream, { transaction });
56
- instance.set('downstream', downstream);
57
- }
58
- yield upstream.update({
59
- downstreamId: instance.id
60
- }, { transaction });
61
- upstream.set('downstream', instance);
62
- }
63
- else {
64
- const [downstream] = yield upstream.getBranches({
65
- where: {
66
- id: {
67
- [sequelize_1.Op.ne]: instance.id
68
- },
69
- branchIndex: instance.branchIndex
70
- },
71
- transaction
72
- });
73
- if (downstream) {
74
- yield downstream.update({
75
- upstreamId: instance.id,
76
- branchIndex: null
77
- }, { transaction });
78
- yield instance.setDownstream(downstream, { transaction });
79
- instance.set('downstream', downstream);
80
- }
81
- }
82
- instance.set('upstream', upstream);
83
- }));
84
- yield next();
85
- });
86
- }
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
87
6
  exports.create = create;
88
- function searchBranchNodes(nodes, from) {
89
- const branchHeads = nodes
90
- .filter((item) => item.upstreamId === from.id && item.branchIndex != null);
91
- return branchHeads.reduce((flatten, head) => flatten.concat(searchBranchDownstreams(nodes, head)), []);
7
+ exports.destroy = destroy;
8
+ exports.update = update;
9
+
10
+ function _sequelize() {
11
+ const data = require("sequelize");
12
+
13
+ _sequelize = function _sequelize() {
14
+ return data;
15
+ };
16
+
17
+ return data;
92
18
  }
93
- function searchBranchDownstreams(nodes, from) {
94
- let result = [];
95
- for (let search = from; search; search = search.downstream) {
96
- result = [...result, search, ...searchBranchNodes(nodes, search)];
97
- }
98
- return result;
19
+
20
+ function _actions() {
21
+ const data = require("@nocobase/actions");
22
+
23
+ _actions = function _actions() {
24
+ return data;
25
+ };
26
+
27
+ return data;
99
28
  }
100
- function destroy(context, next) {
101
- return __awaiter(this, void 0, void 0, function* () {
102
- const { db } = context;
103
- const repository = actions_1.utils.getRepositoryFromParams(context);
104
- const { filterByTk } = context.action.params;
105
- context.body = yield db.sequelize.transaction((transaction) => __awaiter(this, void 0, void 0, function* () {
106
- const workflow = yield repository.getSourceModel(transaction);
107
- if (workflow.executed) {
108
- context.throw(400, 'Nodes in executed workflow could not be deleted');
109
- }
110
- const fields = ['id', 'upstreamId', 'downstreamId', 'branchIndex'];
111
- const instance = yield repository.findOne({
112
- filterByTk,
113
- fields: [...fields, 'workflowId'],
114
- appends: ['upstream', 'downstream'],
115
- transaction
116
- });
117
- const { upstream, downstream } = instance.get();
118
- if (upstream && upstream.downstreamId === instance.id) {
119
- yield upstream.update({
120
- downstreamId: instance.downstreamId
121
- }, { transaction });
122
- }
123
- if (downstream) {
124
- yield downstream.update({
125
- upstreamId: instance.upstreamId,
126
- branchIndex: instance.branchIndex
127
- }, { transaction });
128
- }
129
- const nodes = yield repository.find({
130
- filter: {
131
- workflowId: instance.workflowId
132
- },
133
- fields,
134
- transaction
29
+
30
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
31
+
32
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
33
+
34
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
35
+
36
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
37
+
38
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
39
+
40
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
41
+
42
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
43
+
44
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
45
+
46
+ function create(_x, _x2) {
47
+ return _create.apply(this, arguments);
48
+ }
49
+
50
+ function _create() {
51
+ _create = _asyncToGenerator(function* (context, next) {
52
+ const db = context.db;
53
+
54
+ const repository = _actions().utils.getRepositoryFromParams(context);
55
+
56
+ const _context$action$param = context.action.params,
57
+ whitelist = _context$action$param.whitelist,
58
+ blacklist = _context$action$param.blacklist,
59
+ updateAssociationValues = _context$action$param.updateAssociationValues,
60
+ values = _context$action$param.values,
61
+ workflowId = _context$action$param.associatedIndex;
62
+ context.body = yield db.sequelize.transaction( /*#__PURE__*/function () {
63
+ var _ref = _asyncToGenerator(function* (transaction) {
64
+ const workflow = yield repository.getSourceModel(transaction);
65
+
66
+ if (workflow.executed) {
67
+ context.throw(400, 'Node could not be created in executed workflow');
68
+ }
69
+
70
+ const instance = yield repository.create({
71
+ values,
72
+ whitelist,
73
+ blacklist,
74
+ updateAssociationValues,
75
+ context,
76
+ transaction
77
+ });
78
+
79
+ if (!instance.upstreamId) {
80
+ const previousHead = yield repository.findOne({
81
+ filter: {
82
+ id: {
83
+ $ne: instance.id
84
+ },
85
+ upstreamId: null
86
+ },
87
+ transaction
88
+ });
89
+
90
+ if (previousHead) {
91
+ yield previousHead.setUpstream(instance, {
92
+ transaction
135
93
  });
136
- const nodesMap = new Map();
137
- // make map
138
- nodes.forEach(item => {
139
- nodesMap.set(item.id, item);
94
+ yield instance.setDownstream(previousHead, {
95
+ transaction
140
96
  });
141
- // overwrite
142
- nodesMap.set(instance.id, instance);
143
- // make linked list
144
- nodes.forEach(item => {
145
- if (item.upstreamId) {
146
- item.upstream = nodesMap.get(item.upstreamId);
147
- }
148
- if (item.downstreamId) {
149
- item.downstream = nodesMap.get(item.downstreamId);
150
- }
97
+ instance.set('downstream', previousHead);
98
+ }
99
+
100
+ return instance;
101
+ }
102
+
103
+ const upstream = yield instance.getUpstream({
104
+ transaction
105
+ });
106
+
107
+ if (instance.branchIndex == null) {
108
+ const downstream = yield upstream.getDownstream({
109
+ transaction
110
+ });
111
+
112
+ if (downstream) {
113
+ yield downstream.setUpstream(instance, {
114
+ transaction
151
115
  });
152
- const branchNodes = searchBranchNodes(nodes, instance);
153
- yield repository.destroy({
154
- filterByTk: [instance.id, ...branchNodes.map(item => item.id)],
155
- transaction
116
+ yield instance.setDownstream(downstream, {
117
+ transaction
156
118
  });
157
- return instance;
158
- }));
159
- yield next();
160
- });
161
- }
162
- exports.destroy = destroy;
163
- function update(context, next) {
164
- return __awaiter(this, void 0, void 0, function* () {
165
- const { db } = context;
166
- const repository = actions_1.utils.getRepositoryFromParams(context);
167
- const { filterByTk, values, whitelist, blacklist, filter, updateAssociationValues } = context.action.params;
168
- context.body = yield db.sequelize.transaction((transaction) => __awaiter(this, void 0, void 0, function* () {
169
- // TODO(optimize): duplicated instance query
170
- const { workflow } = yield repository.findOne({
171
- filterByTk,
172
- appends: ['workflow.executed'],
173
- transaction
119
+ instance.set('downstream', downstream);
120
+ }
121
+
122
+ yield upstream.update({
123
+ downstreamId: instance.id
124
+ }, {
125
+ transaction
126
+ });
127
+ upstream.set('downstream', instance);
128
+ } else {
129
+ const _yield$upstream$getBr = yield upstream.getBranches({
130
+ where: {
131
+ id: {
132
+ [_sequelize().Op.ne]: instance.id
133
+ },
134
+ branchIndex: instance.branchIndex
135
+ },
136
+ transaction
137
+ }),
138
+ _yield$upstream$getBr2 = _slicedToArray(_yield$upstream$getBr, 1),
139
+ downstream = _yield$upstream$getBr2[0];
140
+
141
+ if (downstream) {
142
+ yield downstream.update({
143
+ upstreamId: instance.id,
144
+ branchIndex: null
145
+ }, {
146
+ transaction
174
147
  });
175
- if (workflow.executed) {
176
- context.throw(400, 'Nodes in executed workflow could not be reconfigured');
177
- }
178
- return repository.update({
179
- filterByTk,
180
- values,
181
- whitelist,
182
- blacklist,
183
- filter,
184
- updateAssociationValues,
185
- context,
186
- transaction
148
+ yield instance.setDownstream(downstream, {
149
+ transaction
187
150
  });
188
- }));
189
- yield next();
190
- });
151
+ instance.set('downstream', downstream);
152
+ }
153
+ }
154
+
155
+ instance.set('upstream', upstream);
156
+ });
157
+
158
+ return function (_x7) {
159
+ return _ref.apply(this, arguments);
160
+ };
161
+ }());
162
+ yield next();
163
+ });
164
+ return _create.apply(this, arguments);
191
165
  }
192
- exports.update = update;
193
- //# sourceMappingURL=nodes.js.map
166
+
167
+ function searchBranchNodes(nodes, from) {
168
+ const branchHeads = nodes.filter(item => item.upstreamId === from.id && item.branchIndex != null);
169
+ return branchHeads.reduce((flatten, head) => flatten.concat(searchBranchDownstreams(nodes, head)), []);
170
+ }
171
+
172
+ function searchBranchDownstreams(nodes, from) {
173
+ let result = [];
174
+
175
+ for (let search = from; search; search = search.downstream) {
176
+ result = [...result, search, ...searchBranchNodes(nodes, search)];
177
+ }
178
+
179
+ return result;
180
+ }
181
+
182
+ function destroy(_x3, _x4) {
183
+ return _destroy.apply(this, arguments);
184
+ }
185
+
186
+ function _destroy() {
187
+ _destroy = _asyncToGenerator(function* (context, next) {
188
+ const db = context.db;
189
+
190
+ const repository = _actions().utils.getRepositoryFromParams(context);
191
+
192
+ const filterByTk = context.action.params.filterByTk;
193
+ context.body = yield db.sequelize.transaction( /*#__PURE__*/function () {
194
+ var _ref2 = _asyncToGenerator(function* (transaction) {
195
+ const workflow = yield repository.getSourceModel(transaction);
196
+
197
+ if (workflow.executed) {
198
+ context.throw(400, 'Nodes in executed workflow could not be deleted');
199
+ }
200
+
201
+ const fields = ['id', 'upstreamId', 'downstreamId', 'branchIndex'];
202
+ const instance = yield repository.findOne({
203
+ filterByTk,
204
+ fields: [...fields, 'workflowId'],
205
+ appends: ['upstream', 'downstream'],
206
+ transaction
207
+ });
208
+
209
+ const _instance$get = instance.get(),
210
+ upstream = _instance$get.upstream,
211
+ downstream = _instance$get.downstream;
212
+
213
+ if (upstream && upstream.downstreamId === instance.id) {
214
+ yield upstream.update({
215
+ downstreamId: instance.downstreamId
216
+ }, {
217
+ transaction
218
+ });
219
+ }
220
+
221
+ if (downstream) {
222
+ yield downstream.update({
223
+ upstreamId: instance.upstreamId,
224
+ branchIndex: instance.branchIndex
225
+ }, {
226
+ transaction
227
+ });
228
+ }
229
+
230
+ const nodes = yield repository.find({
231
+ filter: {
232
+ workflowId: instance.workflowId
233
+ },
234
+ fields,
235
+ transaction
236
+ });
237
+ const nodesMap = new Map(); // make map
238
+
239
+ nodes.forEach(item => {
240
+ nodesMap.set(item.id, item);
241
+ }); // overwrite
242
+
243
+ nodesMap.set(instance.id, instance); // make linked list
244
+
245
+ nodes.forEach(item => {
246
+ if (item.upstreamId) {
247
+ item.upstream = nodesMap.get(item.upstreamId);
248
+ }
249
+
250
+ if (item.downstreamId) {
251
+ item.downstream = nodesMap.get(item.downstreamId);
252
+ }
253
+ });
254
+ const branchNodes = searchBranchNodes(nodes, instance);
255
+ yield repository.destroy({
256
+ filterByTk: [instance.id, ...branchNodes.map(item => item.id)],
257
+ transaction
258
+ });
259
+ return instance;
260
+ });
261
+
262
+ return function (_x8) {
263
+ return _ref2.apply(this, arguments);
264
+ };
265
+ }());
266
+ yield next();
267
+ });
268
+ return _destroy.apply(this, arguments);
269
+ }
270
+
271
+ function update(_x5, _x6) {
272
+ return _update.apply(this, arguments);
273
+ }
274
+
275
+ function _update() {
276
+ _update = _asyncToGenerator(function* (context, next) {
277
+ const db = context.db;
278
+
279
+ const repository = _actions().utils.getRepositoryFromParams(context);
280
+
281
+ const _context$action$param2 = context.action.params,
282
+ filterByTk = _context$action$param2.filterByTk,
283
+ values = _context$action$param2.values,
284
+ whitelist = _context$action$param2.whitelist,
285
+ blacklist = _context$action$param2.blacklist,
286
+ filter = _context$action$param2.filter,
287
+ updateAssociationValues = _context$action$param2.updateAssociationValues;
288
+ context.body = yield db.sequelize.transaction( /*#__PURE__*/function () {
289
+ var _ref3 = _asyncToGenerator(function* (transaction) {
290
+ // TODO(optimize): duplicated instance query
291
+ const _yield$repository$fin = yield repository.findOne({
292
+ filterByTk,
293
+ appends: ['workflow.executed'],
294
+ transaction
295
+ }),
296
+ workflow = _yield$repository$fin.workflow;
297
+
298
+ if (workflow.executed) {
299
+ context.throw(400, 'Nodes in executed workflow could not be reconfigured');
300
+ }
301
+
302
+ return repository.update({
303
+ filterByTk,
304
+ values,
305
+ whitelist,
306
+ blacklist,
307
+ filter,
308
+ updateAssociationValues,
309
+ context,
310
+ transaction
311
+ });
312
+ });
313
+
314
+ return function (_x9) {
315
+ return _ref3.apply(this, arguments);
316
+ };
317
+ }());
318
+ yield next();
319
+ });
320
+ return _update.apply(this, arguments);
321
+ }