@nocobase/plugin-workflow 0.10.1-alpha.1 → 0.11.1-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.
Files changed (289) hide show
  1. package/client.d.ts +2 -3
  2. package/client.js +1 -30
  3. package/lib/client/AddButton.js +13 -11
  4. package/lib/client/Branch.js +10 -8
  5. package/lib/client/CanvasContent.js +12 -10
  6. package/lib/client/ExecutionCanvas.js +37 -33
  7. package/lib/client/ExecutionPage.js +4 -9
  8. package/lib/client/WorkflowCanvas.js +18 -15
  9. package/lib/client/WorkflowPage.js +4 -9
  10. package/lib/client/WorkflowProvider.js +1 -40
  11. package/lib/client/components/CollectionBlockInitializer.js +3 -3
  12. package/lib/client/components/CollectionFieldset.d.ts +1 -1
  13. package/lib/client/components/CollectionFieldset.js +15 -16
  14. package/lib/client/components/Duration.js +5 -5
  15. package/lib/client/components/DynamicExpression.d.ts +3 -3
  16. package/lib/client/components/FieldsSelect.d.ts +1 -1
  17. package/lib/client/components/FieldsSelect.js +10 -7
  18. package/lib/client/components/NodeDescription.js +45 -31
  19. package/lib/client/components/RadioWithTooltip.js +13 -20
  20. package/lib/client/components/ValueBlock.js +14 -21
  21. package/lib/client/components/renderEngineReference.js +1 -8
  22. package/lib/client/index.d.ts +12 -4
  23. package/lib/client/index.js +78 -15
  24. package/lib/client/locale/zh-CN.d.ts +5 -1
  25. package/lib/client/locale/zh-CN.js +6 -2
  26. package/lib/client/nodes/aggregate.d.ts +8 -3
  27. package/lib/client/nodes/aggregate.js +5 -4
  28. package/lib/client/nodes/calculation.d.ts +6 -4
  29. package/lib/client/nodes/calculation.js +22 -28
  30. package/lib/client/nodes/condition.d.ts +2 -10
  31. package/lib/client/nodes/condition.js +19 -37
  32. package/lib/client/nodes/create.d.ts +5 -6
  33. package/lib/client/nodes/create.js +1 -3
  34. package/lib/client/nodes/destroy.d.ts +1 -1
  35. package/lib/client/nodes/index.d.ts +2 -3
  36. package/lib/client/nodes/index.js +95 -102
  37. package/lib/client/nodes/loop.d.ts +1 -1
  38. package/lib/client/nodes/loop.js +46 -54
  39. package/lib/client/nodes/manual/FormBlockInitializer.js +6 -5
  40. package/lib/client/nodes/manual/ModeConfig.js +23 -30
  41. package/lib/client/nodes/manual/SchemaConfig.d.ts +4 -5
  42. package/lib/client/nodes/manual/SchemaConfig.js +180 -25
  43. package/lib/client/nodes/manual/WorkflowTodo.js +95 -110
  44. package/lib/client/nodes/manual/WorkflowTodoBlockInitializer.d.ts +2 -5
  45. package/lib/client/nodes/manual/WorkflowTodoBlockInitializer.js +6 -5
  46. package/lib/client/nodes/manual/forms/create.js +8 -1
  47. package/lib/client/nodes/manual/forms/custom.js +22 -22
  48. package/lib/client/nodes/manual/forms/update.js +8 -1
  49. package/lib/client/nodes/manual/index.d.ts +6 -1
  50. package/lib/client/nodes/manual/index.js +5 -4
  51. package/lib/client/nodes/parallel.js +23 -20
  52. package/lib/client/nodes/query.d.ts +3 -5
  53. package/lib/client/nodes/query.js +1 -3
  54. package/lib/client/nodes/request.d.ts +2 -2
  55. package/lib/client/nodes/request.js +7 -7
  56. package/lib/client/nodes/sql.d.ts +26 -0
  57. package/lib/client/{triggers/schedule/DateFieldsSelect.js → nodes/sql.js} +37 -46
  58. package/lib/client/nodes/update.d.ts +2 -2
  59. package/lib/client/nodes/update.js +1 -1
  60. package/lib/client/schemas/collection.d.ts +3 -4
  61. package/lib/client/schemas/collection.js +11 -17
  62. package/lib/client/style.d.ts +18 -13
  63. package/lib/client/style.js +315 -292
  64. package/lib/client/triggers/collection.d.ts +13 -13
  65. package/lib/client/triggers/collection.js +5 -1
  66. package/lib/client/triggers/index.d.ts +3 -4
  67. package/lib/client/triggers/index.js +51 -53
  68. package/lib/client/triggers/schedule/EndsByField.js +11 -11
  69. package/lib/client/triggers/schedule/OnField.js +45 -33
  70. package/lib/client/triggers/schedule/RepeatField.js +4 -4
  71. package/lib/client/triggers/schedule/ScheduleConfig.js +24 -31
  72. package/lib/client/triggers/schedule/index.d.ts +1 -1
  73. package/lib/client/triggers/schedule/index.js +32 -20
  74. package/lib/client/variable.d.ts +31 -13
  75. package/lib/client/variable.js +44 -29
  76. package/lib/server/Plugin.d.ts +3 -6
  77. package/lib/server/Plugin.js +15 -12
  78. package/lib/server/Processor.d.ts +3 -5
  79. package/lib/server/Processor.js +2 -2
  80. package/lib/server/actions/nodes.js +7 -7
  81. package/lib/server/fields/expression-field.d.ts +1 -2
  82. package/lib/server/fields/expression-field.js +1 -8
  83. package/lib/server/functions/index.d.ts +2 -3
  84. package/lib/server/index.d.ts +1 -0
  85. package/lib/server/index.js +12 -0
  86. package/lib/server/instructions/aggregate.d.ts +1 -1
  87. package/lib/server/instructions/aggregate.js +5 -5
  88. package/lib/server/instructions/condition.d.ts +2 -1
  89. package/lib/server/instructions/create.d.ts +2 -2
  90. package/lib/server/instructions/create.js +13 -13
  91. package/lib/server/instructions/delay.d.ts +3 -3
  92. package/lib/server/instructions/delay.js +66 -64
  93. package/lib/server/instructions/destroy.d.ts +1 -1
  94. package/lib/server/instructions/index.d.ts +5 -5
  95. package/lib/server/instructions/index.js +1 -1
  96. package/lib/server/instructions/loop.d.ts +1 -2
  97. package/lib/server/instructions/manual/actions.js +19 -7
  98. package/lib/server/instructions/manual/forms/create.js +7 -1
  99. package/lib/server/instructions/manual/forms/index.d.ts +1 -1
  100. package/lib/server/instructions/manual/forms/update.js +7 -1
  101. package/lib/server/instructions/manual/index.d.ts +1 -1
  102. package/lib/server/instructions/parallel.d.ts +1 -2
  103. package/lib/server/instructions/query.d.ts +1 -1
  104. package/lib/server/instructions/query.js +8 -1
  105. package/lib/server/instructions/request.d.ts +3 -3
  106. package/lib/server/instructions/request.js +5 -2
  107. package/lib/server/instructions/sql.d.ts +12 -0
  108. package/lib/server/instructions/sql.js +34 -0
  109. package/lib/server/instructions/update.d.ts +1 -1
  110. package/lib/server/migrations/20230221071831-calculation-expression.js +1 -1
  111. package/lib/server/migrations/20230221121203-condition-calculation.js +1 -1
  112. package/lib/server/migrations/20230221162902-jsonb-to-json.js +7 -7
  113. package/lib/server/migrations/20230411034722-manual-multi-form.js +1 -8
  114. package/lib/server/migrations/20230710115902-manual-action-values.d.ts +4 -0
  115. package/lib/server/migrations/20230710115902-manual-action-values.js +97 -0
  116. package/lib/server/triggers/collection.d.ts +1 -1
  117. package/lib/server/triggers/collection.js +15 -13
  118. package/lib/server/triggers/index.d.ts +1 -1
  119. package/lib/server/triggers/schedule.d.ts +1 -1
  120. package/lib/server/triggers/schedule.js +18 -18
  121. package/lib/server/{models → types}/Execution.d.ts +2 -3
  122. package/lib/server/{models → types}/FlowNode.d.ts +1 -2
  123. package/lib/server/{models → types}/Job.d.ts +1 -2
  124. package/lib/server/{models → types}/Workflow.d.ts +1 -2
  125. package/lib/server/types/index.d.ts +4 -0
  126. package/lib/server/types/index.js +5 -0
  127. package/lib/server/utils.d.ts +2 -0
  128. package/lib/server/utils.js +21 -0
  129. package/package.json +39 -18
  130. package/server.d.ts +2 -3
  131. package/server.js +1 -30
  132. package/src/client/AddButton.tsx +111 -0
  133. package/src/client/Branch.tsx +37 -0
  134. package/src/client/CanvasContent.tsx +25 -0
  135. package/src/client/ExecutionCanvas.tsx +166 -0
  136. package/src/client/ExecutionLink.tsx +16 -0
  137. package/src/client/ExecutionPage.tsx +45 -0
  138. package/src/client/ExecutionResourceProvider.tsx +21 -0
  139. package/src/client/FlowContext.ts +7 -0
  140. package/src/client/WorkflowCanvas.tsx +221 -0
  141. package/src/client/WorkflowLink.tsx +16 -0
  142. package/src/client/WorkflowPage.tsx +52 -0
  143. package/src/client/WorkflowProvider.tsx +84 -0
  144. package/src/client/components/CollectionBlockInitializer.tsx +71 -0
  145. package/src/client/components/CollectionFieldset.tsx +160 -0
  146. package/src/client/components/Duration.tsx +45 -0
  147. package/src/client/components/DynamicExpression.tsx +53 -0
  148. package/src/client/components/FieldsSelect.tsx +32 -0
  149. package/src/client/components/FilterDynamicComponent.tsx +15 -0
  150. package/src/client/components/NodeDescription.tsx +51 -0
  151. package/src/client/components/NullRender.tsx +3 -0
  152. package/src/client/components/OpenDrawer.tsx +24 -0
  153. package/src/client/components/RadioWithTooltip.tsx +38 -0
  154. package/src/client/components/ValueBlock.tsx +67 -0
  155. package/src/client/components/renderEngineReference.tsx +30 -0
  156. package/src/client/constants.tsx +91 -0
  157. package/src/client/index.tsx +51 -0
  158. package/src/client/interfaces/expression.tsx +25 -0
  159. package/src/client/locale/en-US.ts +136 -0
  160. package/src/client/locale/es-ES.ts +129 -0
  161. package/src/client/locale/index.ts +18 -0
  162. package/src/client/locale/ja-JP.ts +90 -0
  163. package/src/client/locale/pt-BR.ts +136 -0
  164. package/src/client/locale/ru-RU.ts +90 -0
  165. package/src/client/locale/tr-TR.ts +90 -0
  166. package/src/client/locale/zh-CN.ts +248 -0
  167. package/src/client/nodes/aggregate.tsx +327 -0
  168. package/src/client/nodes/calculation.tsx +216 -0
  169. package/src/client/nodes/condition.tsx +463 -0
  170. package/src/client/nodes/create.tsx +85 -0
  171. package/src/client/nodes/delay.tsx +37 -0
  172. package/src/client/nodes/destroy.tsx +34 -0
  173. package/src/client/nodes/index.tsx +485 -0
  174. package/src/client/nodes/loop.tsx +144 -0
  175. package/src/client/nodes/manual/AssigneesSelect.tsx +33 -0
  176. package/src/client/nodes/manual/DetailsBlockProvider.tsx +80 -0
  177. package/src/client/nodes/manual/FormBlockInitializer.tsx +69 -0
  178. package/src/client/nodes/manual/FormBlockProvider.tsx +75 -0
  179. package/src/client/nodes/manual/ModeConfig.tsx +84 -0
  180. package/src/client/nodes/manual/SchemaConfig.tsx +509 -0
  181. package/src/client/nodes/manual/WorkflowTodo.tsx +607 -0
  182. package/src/client/nodes/manual/WorkflowTodoBlockInitializer.tsx +28 -0
  183. package/src/client/nodes/manual/forms/create.tsx +92 -0
  184. package/src/client/nodes/manual/forms/custom.tsx +392 -0
  185. package/src/client/nodes/manual/forms/update.tsx +134 -0
  186. package/src/client/nodes/manual/index.tsx +162 -0
  187. package/src/client/nodes/manual/utils.ts +28 -0
  188. package/src/client/nodes/parallel.tsx +138 -0
  189. package/src/client/nodes/query.tsx +88 -0
  190. package/src/client/nodes/request.tsx +185 -0
  191. package/src/client/nodes/sql.tsx +37 -0
  192. package/src/client/nodes/update.tsx +99 -0
  193. package/src/client/schemas/collection.ts +75 -0
  194. package/src/client/schemas/executions.tsx +169 -0
  195. package/src/client/schemas/workflows.ts +364 -0
  196. package/src/client/style.tsx +347 -0
  197. package/src/client/triggers/collection.tsx +190 -0
  198. package/src/client/triggers/index.tsx +311 -0
  199. package/src/client/triggers/schedule/EndsByField.tsx +40 -0
  200. package/src/client/triggers/schedule/OnField.tsx +64 -0
  201. package/src/client/triggers/schedule/RepeatField.tsx +116 -0
  202. package/src/client/triggers/schedule/ScheduleConfig.tsx +227 -0
  203. package/src/client/triggers/schedule/constants.ts +4 -0
  204. package/src/client/triggers/schedule/index.tsx +78 -0
  205. package/src/client/triggers/schedule/locale/Cron.zh-CN.ts +79 -0
  206. package/src/client/utils.ts +36 -0
  207. package/src/client/variable.tsx +318 -0
  208. package/src/index.ts +1 -0
  209. package/src/server/Plugin.ts +355 -0
  210. package/src/server/Processor.ts +354 -0
  211. package/src/server/__tests__/Plugin.test.ts +398 -0
  212. package/src/server/__tests__/Processor.test.ts +474 -0
  213. package/src/server/__tests__/actions/workflows.test.ts +419 -0
  214. package/src/server/__tests__/collections/categories.ts +27 -0
  215. package/src/server/__tests__/collections/comments.ts +24 -0
  216. package/src/server/__tests__/collections/posts.ts +42 -0
  217. package/src/server/__tests__/collections/replies.ts +9 -0
  218. package/src/server/__tests__/collections/tags.ts +15 -0
  219. package/src/server/__tests__/index.ts +89 -0
  220. package/src/server/__tests__/instructions/aggregate.test.ts +294 -0
  221. package/src/server/__tests__/instructions/calculation.test.ts +265 -0
  222. package/src/server/__tests__/instructions/condition.test.ts +335 -0
  223. package/src/server/__tests__/instructions/create.test.ts +129 -0
  224. package/src/server/__tests__/instructions/delay.test.ts +182 -0
  225. package/src/server/__tests__/instructions/destroy.test.ts +58 -0
  226. package/src/server/__tests__/instructions/loop.test.ts +331 -0
  227. package/src/server/__tests__/instructions/manual.test.ts +1173 -0
  228. package/src/server/__tests__/instructions/parallel.test.ts +445 -0
  229. package/src/server/__tests__/instructions/query.test.ts +359 -0
  230. package/src/server/__tests__/instructions/request.test.ts +247 -0
  231. package/src/server/__tests__/instructions/sql.test.ts +162 -0
  232. package/src/server/__tests__/instructions/update.test.ts +189 -0
  233. package/src/server/__tests__/triggers/collection.test.ts +333 -0
  234. package/src/server/__tests__/triggers/schedule.test.ts +369 -0
  235. package/src/server/actions/index.ts +25 -0
  236. package/src/server/actions/nodes.ts +214 -0
  237. package/src/server/actions/workflows.ts +178 -0
  238. package/src/server/collections/executions.ts +35 -0
  239. package/src/server/collections/flow_nodes.ts +54 -0
  240. package/src/server/collections/jobs.ts +31 -0
  241. package/src/server/collections/workflows.ts +88 -0
  242. package/src/server/constants.ts +26 -0
  243. package/src/server/fields/expression-field.ts +11 -0
  244. package/src/server/fields/index.ts +7 -0
  245. package/src/server/functions/index.ts +16 -0
  246. package/src/server/index.ts +6 -0
  247. package/src/server/instructions/aggregate.ts +42 -0
  248. package/src/server/instructions/calculation.ts +41 -0
  249. package/src/server/instructions/condition.ts +172 -0
  250. package/src/server/instructions/create.ts +39 -0
  251. package/src/server/instructions/delay.ts +105 -0
  252. package/src/server/instructions/destroy.ts +23 -0
  253. package/src/server/instructions/index.ts +64 -0
  254. package/src/server/instructions/loop.ts +99 -0
  255. package/src/server/instructions/manual/actions.ts +91 -0
  256. package/src/server/instructions/manual/collecions/jobs.ts +17 -0
  257. package/src/server/instructions/manual/collecions/users.ts +15 -0
  258. package/src/server/instructions/manual/collecions/users_jobs.ts +50 -0
  259. package/src/server/instructions/manual/forms/create.ts +23 -0
  260. package/src/server/instructions/manual/forms/index.ts +12 -0
  261. package/src/server/instructions/manual/forms/update.ts +23 -0
  262. package/src/server/instructions/manual/index.ts +184 -0
  263. package/src/server/instructions/parallel.ts +121 -0
  264. package/src/server/instructions/query.ts +42 -0
  265. package/src/server/instructions/request.ts +88 -0
  266. package/src/server/instructions/sql.ts +25 -0
  267. package/src/server/instructions/update.ts +24 -0
  268. package/src/server/migrations/20221129153547-calculation-variables.ts +64 -0
  269. package/src/server/migrations/20230221032941-change-request-body-type.ts +76 -0
  270. package/src/server/migrations/20230221071831-calculation-expression.ts +102 -0
  271. package/src/server/migrations/20230221121203-condition-calculation.ts +82 -0
  272. package/src/server/migrations/20230221162902-jsonb-to-json.ts +51 -0
  273. package/src/server/migrations/20230411034722-manual-multi-form.ts +282 -0
  274. package/src/server/migrations/20230612021134-manual-collection-block.ts +138 -0
  275. package/src/server/migrations/20230710115902-manual-action-values.ts +78 -0
  276. package/src/server/triggers/collection.ts +146 -0
  277. package/src/server/triggers/index.ts +22 -0
  278. package/src/server/triggers/schedule.ts +567 -0
  279. package/src/server/types/Execution.ts +26 -0
  280. package/src/server/types/FlowNode.ts +21 -0
  281. package/src/server/types/Job.ts +18 -0
  282. package/src/server/types/Workflow.ts +36 -0
  283. package/src/server/types/index.ts +4 -0
  284. package/src/server/utils.ts +17 -0
  285. package/lib/client/triggers/schedule/DateFieldsSelect.d.ts +0 -2
  286. /package/lib/server/{models → types}/Execution.js +0 -0
  287. /package/lib/server/{models → types}/FlowNode.js +0 -0
  288. /package/lib/server/{models → types}/Job.js +0 -0
  289. /package/lib/server/{models → types}/Workflow.js +0 -0
@@ -0,0 +1,178 @@
1
+ import actions, { Context, utils } from '@nocobase/actions';
2
+ import { Op, Repository } from '@nocobase/database';
3
+
4
+ export async function update(context: Context, next) {
5
+ const repository = utils.getRepositoryFromParams(context) as Repository;
6
+ const { filterByTk, values } = context.action.params;
7
+ context.action.mergeParams({
8
+ whitelist: ['title', 'description', 'enabled', 'config'],
9
+ });
10
+ // only enable/disable
11
+ if (Object.keys(values).includes('config')) {
12
+ const workflow = await repository.findById(filterByTk);
13
+ if (workflow.get('executed')) {
14
+ return context.throw(400, 'config of executed workflow can not be updated');
15
+ }
16
+ }
17
+ return actions.update(context, next);
18
+ }
19
+
20
+ export async function destroy(context: Context, next) {
21
+ const repository = utils.getRepositoryFromParams(context) as Repository;
22
+ const { filterByTk, filter } = context.action.params;
23
+
24
+ await context.db.sequelize.transaction(async (transaction) => {
25
+ const items = await repository.find({
26
+ filterByTk,
27
+ filter,
28
+ fields: ['id', 'key', 'current'],
29
+ transaction,
30
+ });
31
+ const ids = new Set<number>(items.map((item) => item.id));
32
+ const keysSet = new Set<string>(items.filter((item) => item.current).map((item) => item.key));
33
+ const revisions = await repository.find({
34
+ filter: {
35
+ key: Array.from(keysSet),
36
+ current: { [Op.not]: true },
37
+ },
38
+ fields: ['id'],
39
+ transaction,
40
+ });
41
+
42
+ revisions.forEach((item) => ids.add(item.id));
43
+
44
+ context.body = await repository.destroy({
45
+ filterByTk: Array.from(ids),
46
+ individualHooks: true,
47
+ transaction,
48
+ });
49
+ });
50
+
51
+ next();
52
+ }
53
+
54
+ function typeOf(value) {
55
+ if (Array.isArray(value)) {
56
+ return 'array';
57
+ } else if (value instanceof Date) {
58
+ return 'date';
59
+ } else if (value === null) {
60
+ return 'null';
61
+ }
62
+
63
+ return typeof value;
64
+ }
65
+
66
+ function migrateConfig(config, oldToNew) {
67
+ function migrate(value) {
68
+ switch (typeOf(value)) {
69
+ case 'object':
70
+ if (value.type === '$jobsMapByNodeId') {
71
+ return {
72
+ ...value,
73
+ options: {
74
+ ...value.options,
75
+ nodeId: oldToNew.get(value.options?.nodeId)?.id,
76
+ },
77
+ };
78
+ }
79
+ return Object.keys(value).reduce((result, key) => ({ ...result, [key]: migrate(value[key]) }), {});
80
+ case 'array':
81
+ return value.map((item) => migrate(item));
82
+ case 'string':
83
+ return value.replace(/({{\$jobsMapByNodeId|{{\$scopes)\.([\w-]+)/g, (_, jobVar, oldNodeId) => {
84
+ const newNode = oldToNew.get(Number.parseInt(oldNodeId, 10));
85
+ if (!newNode) {
86
+ throw new Error('node configurated for result is not existed');
87
+ }
88
+ return `${jobVar}.${newNode.id}`;
89
+ });
90
+ default:
91
+ return value;
92
+ }
93
+ }
94
+
95
+ return migrate(config);
96
+ }
97
+
98
+ export async function revision(context: Context, next) {
99
+ const { db } = context;
100
+ const repository = utils.getRepositoryFromParams(context);
101
+ const { filterByTk, filter = {} } = context.action.params;
102
+
103
+ context.body = await db.sequelize.transaction(async (transaction) => {
104
+ const origin = await repository.findOne({
105
+ filterByTk,
106
+ filter,
107
+ appends: ['nodes'],
108
+ context,
109
+ transaction,
110
+ });
111
+
112
+ const revisionData = filter.key
113
+ ? {
114
+ key: filter.key,
115
+ title: origin.title,
116
+ allExecuted: origin.allExecuted,
117
+ }
118
+ : {};
119
+
120
+ const instance = await repository.create({
121
+ values: {
122
+ title: `${origin.title} copy`,
123
+ description: origin.description,
124
+ type: origin.type,
125
+ config: origin.config,
126
+ ...revisionData,
127
+ },
128
+ transaction,
129
+ });
130
+
131
+ const originalNodesMap = new Map();
132
+ origin.nodes.forEach((node) => {
133
+ originalNodesMap.set(node.id, node);
134
+ });
135
+
136
+ const oldToNew = new Map();
137
+ const newToOld = new Map();
138
+ for await (const node of origin.nodes) {
139
+ const newNode = await instance.createNode(
140
+ {
141
+ type: node.type,
142
+ config: node.config,
143
+ title: node.title,
144
+ branchIndex: node.branchIndex,
145
+ },
146
+ { transaction },
147
+ );
148
+ // NOTE: keep original node references for later replacement
149
+ oldToNew.set(node.id, newNode);
150
+ newToOld.set(newNode.id, node);
151
+ }
152
+
153
+ for await (const [oldId, newNode] of oldToNew.entries()) {
154
+ const oldNode = originalNodesMap.get(oldId);
155
+ const newUpstream = oldNode.upstreamId ? oldToNew.get(oldNode.upstreamId) : null;
156
+ const newDownstream = oldNode.downstreamId ? oldToNew.get(oldNode.downstreamId) : null;
157
+ let migratedConfig;
158
+ try {
159
+ migratedConfig = migrateConfig(oldNode.config, oldToNew);
160
+ } catch (err) {
161
+ return context.throw(400, err.message);
162
+ }
163
+
164
+ await newNode.update(
165
+ {
166
+ upstreamId: newUpstream?.id ?? null,
167
+ downstreamId: newDownstream?.id ?? null,
168
+ config: migratedConfig,
169
+ },
170
+ { transaction },
171
+ );
172
+ }
173
+
174
+ return instance;
175
+ });
176
+
177
+ await next();
178
+ }
@@ -0,0 +1,35 @@
1
+ import { CollectionOptions } from '@nocobase/database';
2
+
3
+ export default {
4
+ namespace: 'workflow.executionLogs',
5
+ duplicator: 'optional',
6
+ name: 'executions',
7
+ fields: [
8
+ {
9
+ type: 'belongsTo',
10
+ name: 'workflow',
11
+ },
12
+ {
13
+ type: 'uid',
14
+ name: 'key',
15
+ },
16
+ {
17
+ type: 'boolean',
18
+ name: 'useTransaction',
19
+ defaultValue: false,
20
+ },
21
+ {
22
+ type: 'hasMany',
23
+ name: 'jobs',
24
+ onDelete: 'CASCADE',
25
+ },
26
+ {
27
+ type: 'json',
28
+ name: 'context',
29
+ },
30
+ {
31
+ type: 'integer',
32
+ name: 'status',
33
+ },
34
+ ],
35
+ } as CollectionOptions;
@@ -0,0 +1,54 @@
1
+ import { CollectionOptions } from '@nocobase/database';
2
+
3
+ export default {
4
+ namespace: 'workflow.workflowConfig',
5
+ duplicator: 'required',
6
+ name: 'flow_nodes',
7
+ fields: [
8
+ {
9
+ type: 'string',
10
+ name: 'title',
11
+ },
12
+ // which workflow belongs to
13
+ {
14
+ name: 'workflow',
15
+ type: 'belongsTo',
16
+ },
17
+ {
18
+ name: 'upstream',
19
+ type: 'belongsTo',
20
+ target: 'flow_nodes',
21
+ },
22
+ {
23
+ name: 'branches',
24
+ type: 'hasMany',
25
+ target: 'flow_nodes',
26
+ sourceKey: 'id',
27
+ foreignKey: 'upstreamId',
28
+ },
29
+ // only works when upstream node is branching type, such as condition and parallel.
30
+ // put here because the design of flow-links model is not really necessary for now.
31
+ // or it should be put into flow-links model.
32
+ {
33
+ name: 'branchIndex',
34
+ type: 'integer',
35
+ },
36
+ // Note: for reasons:
37
+ // 1. redirect type node to solve cycle flow.
38
+ // 2. recognize as real next node after branches.
39
+ {
40
+ name: 'downstream',
41
+ type: 'belongsTo',
42
+ target: 'flow_nodes',
43
+ },
44
+ {
45
+ type: 'string',
46
+ name: 'type',
47
+ },
48
+ {
49
+ type: 'json',
50
+ name: 'config',
51
+ defaultValue: {},
52
+ },
53
+ ],
54
+ } as CollectionOptions;
@@ -0,0 +1,31 @@
1
+ import { CollectionOptions } from '@nocobase/database';
2
+
3
+ export default {
4
+ namespace: 'workflow.executionLogs',
5
+ duplicator: 'optional',
6
+ name: 'jobs',
7
+ fields: [
8
+ {
9
+ type: 'belongsTo',
10
+ name: 'execution',
11
+ },
12
+ {
13
+ type: 'belongsTo',
14
+ name: 'node',
15
+ target: 'flow_nodes',
16
+ },
17
+ {
18
+ type: 'belongsTo',
19
+ name: 'upstream',
20
+ target: 'jobs',
21
+ },
22
+ {
23
+ type: 'integer',
24
+ name: 'status',
25
+ },
26
+ {
27
+ type: 'json',
28
+ name: 'result',
29
+ },
30
+ ],
31
+ } as CollectionOptions;
@@ -0,0 +1,88 @@
1
+ import { CollectionOptions } from '@nocobase/database';
2
+
3
+ export default function () {
4
+ return {
5
+ namespace: 'workflow.workflowConfig',
6
+ duplicator: 'required',
7
+ name: 'workflows',
8
+ fields: [
9
+ {
10
+ name: 'key',
11
+ type: 'uid',
12
+ },
13
+ {
14
+ type: 'string',
15
+ name: 'title',
16
+ required: true,
17
+ },
18
+ {
19
+ type: 'boolean',
20
+ name: 'enabled',
21
+ defaultValue: false,
22
+ },
23
+ {
24
+ type: 'text',
25
+ name: 'description',
26
+ },
27
+ {
28
+ type: 'string',
29
+ name: 'type',
30
+ required: true,
31
+ },
32
+ {
33
+ type: 'json',
34
+ name: 'config',
35
+ required: true,
36
+ defaultValue: {},
37
+ },
38
+ {
39
+ type: 'boolean',
40
+ name: 'useTransaction',
41
+ // defaultValue: true,
42
+ },
43
+ {
44
+ type: 'hasMany',
45
+ name: 'nodes',
46
+ target: 'flow_nodes',
47
+ onDelete: 'CASCADE',
48
+ },
49
+ {
50
+ type: 'hasMany',
51
+ name: 'executions',
52
+ onDelete: 'CASCADE',
53
+ },
54
+ {
55
+ type: 'integer',
56
+ name: 'executed',
57
+ defaultValue: 0,
58
+ },
59
+ {
60
+ type: 'integer',
61
+ name: 'allExecuted',
62
+ defaultValue: 0,
63
+ },
64
+ {
65
+ type: 'boolean',
66
+ name: 'current',
67
+ defaultValue: false,
68
+ },
69
+ {
70
+ type: 'hasMany',
71
+ name: 'revisions',
72
+ target: 'workflows',
73
+ foreignKey: 'key',
74
+ sourceKey: 'key',
75
+ // NOTE: no constraints needed here because tricky self-referencing
76
+ constraints: false,
77
+ onDelete: 'NO ACTION',
78
+ },
79
+ ],
80
+ // NOTE: use unique index for avoiding deadlock in mysql when setCurrent
81
+ indexes: [
82
+ {
83
+ unique: true,
84
+ fields: ['key', 'current'],
85
+ },
86
+ ],
87
+ } as CollectionOptions;
88
+ }
@@ -0,0 +1,26 @@
1
+ export const EXECUTION_STATUS = {
2
+ QUEUEING: null,
3
+ STARTED: 0,
4
+ RESOLVED: 1,
5
+ FAILED: -1,
6
+ ERROR: -2,
7
+ ABORTED: -3,
8
+ CANCELED: -4,
9
+ REJECTED: -5,
10
+ };
11
+
12
+ export const JOB_STATUS = {
13
+ PENDING: 0,
14
+ RESOLVED: 1,
15
+ FAILED: -1,
16
+ ERROR: -2,
17
+ ABORTED: -3,
18
+ CANCELED: -4,
19
+ REJECTED: -5,
20
+ };
21
+
22
+ export const BRANCH_INDEX = {
23
+ DEFAULT: null,
24
+ ON_TRUE: 1,
25
+ ON_FALSE: 0,
26
+ };
@@ -0,0 +1,11 @@
1
+ import { BaseFieldOptions, DataTypes, Field } from '@nocobase/database';
2
+
3
+ export interface ExpressionFieldOptions extends BaseFieldOptions {
4
+ type: 'expression';
5
+ }
6
+
7
+ export class ExpressionField extends Field {
8
+ get dataType() {
9
+ return DataTypes.TEXT;
10
+ }
11
+ }
@@ -0,0 +1,7 @@
1
+ import { ExpressionField } from './expression-field';
2
+
3
+ export default function (plugin) {
4
+ plugin.db.registerFieldTypes({
5
+ expression: ExpressionField,
6
+ });
7
+ }
@@ -0,0 +1,16 @@
1
+ import Plugin from '..';
2
+ import type { ExecutionModel, FlowNodeModel } from '../types';
3
+
4
+ export type CustomFunction = (this: { execution: ExecutionModel; node?: FlowNodeModel }) => any;
5
+
6
+ function now() {
7
+ return new Date();
8
+ }
9
+
10
+ export default function ({ functions }: Plugin, more: { [key: string]: CustomFunction } = {}) {
11
+ functions.register('now', now);
12
+
13
+ for (const [name, fn] of Object.entries(more)) {
14
+ functions.register(name, fn);
15
+ }
16
+ }
@@ -0,0 +1,6 @@
1
+ export * from './constants';
2
+ // export * from './instructions';
3
+ export { Trigger } from './triggers';
4
+ export { default as Processor } from './Processor';
5
+ export { default } from './Plugin';
6
+ export * from './types';
@@ -0,0 +1,42 @@
1
+ import { BelongsToManyRepository, DataTypes, HasManyRepository } from '@nocobase/database';
2
+
3
+ import Processor from '../Processor';
4
+ import { JOB_STATUS } from '../constants';
5
+ import type { FlowNodeModel } from '../types';
6
+
7
+ const aggregators = {
8
+ count: 'count',
9
+ sum: 'sum',
10
+ avg: 'avg',
11
+ min: 'min',
12
+ max: 'max',
13
+ };
14
+
15
+ export default {
16
+ async run(node: FlowNodeModel, input, processor: Processor) {
17
+ const { aggregator, associated, collection, association = {}, params = {} } = node.config;
18
+ const options = processor.getParsedValue(params);
19
+ const { database } = <typeof FlowNodeModel>node.constructor;
20
+ const repo = associated
21
+ ? database.getRepository<HasManyRepository | BelongsToManyRepository>(
22
+ `${association?.associatedCollection}.${association.name}`,
23
+ processor.getParsedValue(association?.associatedKey),
24
+ )
25
+ : database.getRepository(collection);
26
+
27
+ if (!options.dataType && aggregator === 'avg') {
28
+ options.dataType = DataTypes.DOUBLE;
29
+ }
30
+
31
+ const result = await repo.aggregate({
32
+ ...options,
33
+ method: aggregators[aggregator],
34
+ transaction: processor.transaction,
35
+ });
36
+
37
+ return {
38
+ result: options.dataType === DataTypes.DOUBLE ? Number(result) : result,
39
+ status: JOB_STATUS.RESOLVED,
40
+ };
41
+ },
42
+ };
@@ -0,0 +1,41 @@
1
+ import { Evaluator, evaluators } from '@nocobase/evaluators';
2
+ import { parse } from '@nocobase/utils';
3
+ import { Instruction } from '.';
4
+ import { Processor } from '..';
5
+ import { JOB_STATUS } from '../constants';
6
+ import type { FlowNodeModel } from '../types';
7
+
8
+ interface CalculationConfig {
9
+ dynamic?: boolean | string;
10
+ engine?: string;
11
+ expression?: string;
12
+ }
13
+
14
+ export default {
15
+ async run(node: FlowNodeModel, prevJob, processor: Processor) {
16
+ const { dynamic = false } = <CalculationConfig>node.config || {};
17
+ let { engine = 'math.js', expression = '' } = node.config;
18
+ let scope = processor.getScope(node);
19
+ if (dynamic) {
20
+ const parsed = parse(dynamic)(scope) ?? {};
21
+ engine = parsed.engine;
22
+ expression = parsed.expression;
23
+ scope = parse(node.config.scope ?? '')(scope) ?? {};
24
+ }
25
+
26
+ const evaluator = <Evaluator | undefined>evaluators.get(engine);
27
+
28
+ try {
29
+ const result = evaluator && expression ? evaluator(expression, scope) : null;
30
+ return {
31
+ result,
32
+ status: JOB_STATUS.RESOLVED,
33
+ };
34
+ } catch (e) {
35
+ return {
36
+ result: e.toString(),
37
+ status: JOB_STATUS.ERROR,
38
+ };
39
+ }
40
+ },
41
+ } as Instruction;