@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,318 @@
1
+ import { useCompile } from '@nocobase/client';
2
+ import { useFlowContext } from './FlowContext';
3
+ import { NAMESPACE, lang } from './locale';
4
+ import { instructions, useAvailableUpstreams, useNodeContext, useUpstreamScopes } from './nodes';
5
+ import { triggers } from './triggers';
6
+
7
+ export type VariableOption = {
8
+ key?: string;
9
+ value?: string;
10
+ label?: string;
11
+ children?: VariableOptions;
12
+ [key: string]: any;
13
+ };
14
+
15
+ export type VariableOptions = VariableOption[] | null;
16
+
17
+ export type VariableDataType =
18
+ string |
19
+ {
20
+ type: string;
21
+ options?: { entity?: boolean; collection?: string }
22
+ } |
23
+ ((field: any, appends?: string[]) => boolean);
24
+
25
+ export type OptionsOfUseVariableOptions = {
26
+ types?: VariableDataType[];
27
+ fieldNames?: {
28
+ label?: string;
29
+ value?: string;
30
+ children?: string;
31
+ };
32
+ }
33
+
34
+ export const defaultFieldNames = { label: 'label', value: 'value', children: 'children' } as const;
35
+
36
+ export const nodesOptions = {
37
+ label: `{{t("Node result", { ns: "${NAMESPACE}" })}}`,
38
+ value: '$jobsMapByNodeId',
39
+ useOptions(options: OptionsOfUseVariableOptions) {
40
+ const current = useNodeContext();
41
+ const upstreams = useAvailableUpstreams(current);
42
+ const result: VariableOption[] = [];
43
+ upstreams.forEach((node) => {
44
+ const instruction = instructions.get(node.type);
45
+ const subOption = instruction.useVariables?.(node, options);
46
+ if (subOption) {
47
+ result.push(subOption);
48
+ }
49
+ });
50
+ return result;
51
+ },
52
+ };
53
+
54
+ export const triggerOptions = {
55
+ label: `{{t("Trigger variables", { ns: "${NAMESPACE}" })}}`,
56
+ value: '$context',
57
+ useOptions(options: OptionsOfUseVariableOptions) {
58
+ const { workflow } = useFlowContext();
59
+ const trigger = triggers.get(workflow.type);
60
+ return trigger?.useVariables?.(workflow.config, options) ?? null;
61
+ },
62
+ };
63
+
64
+ export const scopeOptions = {
65
+ label: `{{t("Scope variables", { ns: "${NAMESPACE}" })}}`,
66
+ value: '$scopes',
67
+ useOptions(options: OptionsOfUseVariableOptions) {
68
+ const { fieldNames = defaultFieldNames } = options;
69
+ const current = useNodeContext();
70
+ const scopes = useUpstreamScopes(current);
71
+ const result: VariableOption[] = [];
72
+ scopes.forEach((node) => {
73
+ const instruction = instructions.get(node.type);
74
+ const subOptions = instruction.useScopeVariables?.(node, options);
75
+ if (subOptions) {
76
+ result.push({
77
+ key: node.id.toString(),
78
+ [fieldNames.value]: node.id.toString(),
79
+ [fieldNames.label]: node.title ?? `#${node.id}`,
80
+ [fieldNames.children]: subOptions,
81
+ });
82
+ }
83
+ });
84
+ return result;
85
+ },
86
+ };
87
+
88
+ export const systemOptions = {
89
+ label: `{{t("System variables", { ns: "${NAMESPACE}" })}}`,
90
+ value: '$system',
91
+ useOptions({ types, fieldNames = defaultFieldNames }: OptionsOfUseVariableOptions) {
92
+ return [
93
+ ...(!types || types.includes('date')
94
+ ? [
95
+ {
96
+ key: 'now',
97
+ [fieldNames.label]: lang('System time'),
98
+ [fieldNames.value]: 'now',
99
+ },
100
+ ]
101
+ : []),
102
+ ];
103
+ },
104
+ };
105
+
106
+ export const BaseTypeSets = {
107
+ boolean: new Set(['checkbox']),
108
+ number: new Set(['integer', 'number', 'percent']),
109
+ string: new Set([
110
+ 'input',
111
+ 'password',
112
+ 'email',
113
+ 'phone',
114
+ 'select',
115
+ 'radioGroup',
116
+ 'text',
117
+ 'markdown',
118
+ 'richText',
119
+ 'expression',
120
+ 'time',
121
+ ]),
122
+ date: new Set(['date', 'createdAt', 'updatedAt']),
123
+ };
124
+
125
+ // { type: 'reference', options: { collection: 'users', multiple: false } }
126
+ // { type: 'reference', options: { collection: 'attachments', multiple: false } }
127
+ // { type: 'reference', options: { collection: 'myExpressions', entity: false } }
128
+
129
+ function matchFieldType(field, type, appends): boolean {
130
+ const inputType = typeof type;
131
+ if (inputType === 'string') {
132
+ return BaseTypeSets[type]?.has(field.interface);
133
+ }
134
+
135
+ if (inputType === 'object' && type.type === 'reference') {
136
+ if (isAssociationField(field)) {
137
+ return (
138
+ type.options?.entity && (field.collectionName === type.options?.collection || type.options?.collection === '*')
139
+ );
140
+ } else if (field.isForeignKey) {
141
+ return (
142
+ (field.collectionName === type.options?.collection && field.name === 'id') ||
143
+ field.target === type.options?.collection
144
+ );
145
+ } else {
146
+ return false;
147
+ }
148
+ }
149
+
150
+ if (inputType === 'function') {
151
+ return type(field, appends);
152
+ }
153
+
154
+ return false;
155
+ }
156
+
157
+ function isAssociationField(field): boolean {
158
+ return ['belongsTo', 'hasOne', 'hasMany', 'belongsToMany'].includes(field.type);
159
+ }
160
+
161
+ function getNextAppends(field, appends: string[]) {
162
+ const fieldPrefix = `${field.name}.`;
163
+ return appends.filter((item) => item.startsWith(fieldPrefix)).map((item) => item.replace(fieldPrefix, ''));
164
+ }
165
+
166
+ function filterTypedFields({ fields, types, appends, compile, getCollectionFields }) {
167
+ return fields.filter((field) => {
168
+ const match = types?.length ? types.some((type) => matchFieldType(field, type, appends)) : true;
169
+ if (isAssociationField(field)) {
170
+ const nextAppends = getNextAppends(field, appends);
171
+ const included = appends.includes(field.name);
172
+ if (match) {
173
+ return included;
174
+ } else {
175
+ return (
176
+ (nextAppends.length || included) &&
177
+ filterTypedFields({
178
+ fields: getNormalizedFields(field.target, { compile, getCollectionFields }),
179
+ types,
180
+ // depth: depth - 1,
181
+ appends: nextAppends,
182
+ compile,
183
+ getCollectionFields,
184
+ }).length
185
+ );
186
+ }
187
+ } else {
188
+ return match;
189
+ }
190
+ });
191
+ }
192
+
193
+ export function useWorkflowVariableOptions(options: OptionsOfUseVariableOptions = {}) {
194
+ const fieldNames = Object.assign({}, defaultFieldNames, options.fieldNames ?? {});
195
+ const opts = Object.assign(options, { fieldNames });
196
+ const compile = useCompile();
197
+ const result = [scopeOptions, nodesOptions, triggerOptions, systemOptions].map((item: any) => {
198
+ const children = item.useOptions(opts).filter(Boolean);
199
+ return {
200
+ [fieldNames.label]: compile(item.label),
201
+ [fieldNames.value]: item.value,
202
+ key: item[fieldNames.value],
203
+ [fieldNames.children]: children,
204
+ disabled: children && !children.length,
205
+ };
206
+ });
207
+
208
+ return result;
209
+ }
210
+
211
+ function getNormalizedFields(collectionName, { compile, getCollectionFields }) {
212
+ const fields = getCollectionFields(collectionName);
213
+ const foreignKeyFields: any[] = [];
214
+ const otherFields: any[] = [];
215
+ fields.forEach((field) => {
216
+ if (field.isForeignKey) {
217
+ foreignKeyFields.push(field);
218
+ } else {
219
+ otherFields.push(field);
220
+ }
221
+ });
222
+ for (let i = otherFields.length - 1; i >= 0; i--) {
223
+ const field = otherFields[i];
224
+ if (field.type === 'belongsTo') {
225
+ const foreignKeyField = foreignKeyFields.find((f) => f.name === field.foreignKey);
226
+ if (foreignKeyField) {
227
+ otherFields.splice(i, 0, {
228
+ ...field,
229
+ ...foreignKeyField,
230
+ uiSchema: {
231
+ ...field.uiSchema,
232
+ title: field.uiSchema?.title ? `${compile(field.uiSchema?.title)} ID` : foreignKeyField.name,
233
+ },
234
+ });
235
+ } else {
236
+ otherFields.splice(i, 0, {
237
+ ...field,
238
+ name: field.foreignKey,
239
+ type: 'bigInt',
240
+ isForeignKey: true,
241
+ interface: field.interface,
242
+ uiSchema: {
243
+ ...field.uiSchema,
244
+ title: field.uiSchema?.title ? `${compile(field.uiSchema?.title)} ID` : field.name,
245
+ },
246
+ });
247
+ }
248
+ } else if (field.type === 'context' && field.collectionName === 'users') {
249
+ const belongsToField =
250
+ otherFields.find((f) => f.type === 'belongsTo' && f.target === 'users' && f.foreignKey === field.name) ?? {};
251
+ otherFields.splice(i, 0, {
252
+ ...field,
253
+ type: field.dataType,
254
+ interface: belongsToField.interface,
255
+ uiSchema: {
256
+ ...belongsToField.uiSchema,
257
+ title: belongsToField.uiSchema?.title ? `${compile(belongsToField.uiSchema?.title)} ID` : field.name,
258
+ },
259
+ });
260
+ }
261
+ }
262
+
263
+ return otherFields.filter((field) => field.interface && !field.hidden);
264
+ }
265
+
266
+ async function loadChildren(option) {
267
+ const result = getCollectionFieldOptions({
268
+ collection: option.field.target,
269
+ types: option.types,
270
+ appends: getNextAppends(option.field, option.appends),
271
+ ...this,
272
+ });
273
+ option.loadChildren = null;
274
+ if (result.length) {
275
+ option.children = result;
276
+ } else {
277
+ option.isLeaf = true;
278
+ const matchingType = option.types?.some((type) => matchFieldType(option.field, type, 0));
279
+ if (!matchingType) {
280
+ option.disabled = true;
281
+ }
282
+ }
283
+ }
284
+
285
+ export function getCollectionFieldOptions(options): VariableOption[] {
286
+ const { fields, collection, types, appends = [], compile, getCollectionFields, fieldNames = defaultFieldNames } = options;
287
+ const normalizedFields = getNormalizedFields(collection, { compile, getCollectionFields });
288
+ const computedFields = fields ?? normalizedFields;
289
+ const boundLoadChildren = loadChildren.bind({ compile, getCollectionFields, fieldNames });
290
+
291
+ const result: VariableOption[] = filterTypedFields({
292
+ fields: computedFields,
293
+ types,
294
+ // depth,
295
+ appends,
296
+ compile,
297
+ getCollectionFields,
298
+ }).map((field) => {
299
+ const label = compile(field.uiSchema?.title || field.name);
300
+ // console.log('===', label, field);
301
+ const nextAppends = getNextAppends(field, appends);
302
+ // TODO: no matching fields in next appends should consider isLeaf as true
303
+ const isLeaf = !isAssociationField(field) || (!nextAppends.length && !appends.includes(field.name));
304
+ return {
305
+ [fieldNames.label]: label,
306
+ key: field.name,
307
+ [fieldNames.value]: field.name,
308
+ isLeaf,
309
+ loadChildren: isLeaf ? null : boundLoadChildren,
310
+ field,
311
+ // depth,
312
+ appends,
313
+ types,
314
+ };
315
+ });
316
+
317
+ return result;
318
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from './server';
@@ -0,0 +1,355 @@
1
+ import path from 'path';
2
+
3
+ import winston from 'winston';
4
+ import LRUCache from 'lru-cache';
5
+
6
+ import { Op } from '@nocobase/database';
7
+ import { Plugin } from '@nocobase/server';
8
+ import { Registry } from '@nocobase/utils';
9
+
10
+ import initFields from './fields';
11
+ import initActions from './actions';
12
+ import { EXECUTION_STATUS } from './constants';
13
+ import initInstructions, { Instruction } from './instructions';
14
+ import Processor from './Processor';
15
+ import initTriggers, { Trigger } from './triggers';
16
+ import initFunctions, { CustomFunction } from './functions';
17
+ import { createLogger, Logger, LoggerOptions, getLoggerLevel, getLoggerFilePath } from '@nocobase/logger';
18
+
19
+ import type { WorkflowModel, ExecutionModel, JobModel } from './types';
20
+
21
+ type Pending = [ExecutionModel, JobModel?];
22
+
23
+ type ID = number | string;
24
+ export default class WorkflowPlugin extends Plugin {
25
+ instructions: Registry<Instruction> = new Registry();
26
+ triggers: Registry<Trigger> = new Registry();
27
+ functions: Registry<CustomFunction> = new Registry();
28
+ private executing = false;
29
+ private pending: Pending[] = [];
30
+ private events: [WorkflowModel, any, { context?: any }][] = [];
31
+
32
+ private loggerCache: LRUCache<string, Logger>;
33
+
34
+ getLogger(workflowId: ID): Logger {
35
+ const now = new Date();
36
+ const date = `${now.getFullYear()}-${`0${now.getMonth() + 1}`.slice(-2)}-${`0${now.getDate()}`.slice(-2)}`;
37
+ const key = `${date}-${workflowId}}`;
38
+ if (this.loggerCache.has(key)) {
39
+ return this.loggerCache.get(key);
40
+ }
41
+
42
+ const logger = createLogger({
43
+ transports: [
44
+ 'console',
45
+ new winston.transports.File({
46
+ filename: getLoggerFilePath('workflows', date, `${workflowId}.log`),
47
+ level: getLoggerLevel(),
48
+ }),
49
+ ],
50
+ } as LoggerOptions);
51
+
52
+ this.loggerCache.set(key, logger);
53
+
54
+ return logger;
55
+ }
56
+
57
+ onBeforeSave = async (instance: WorkflowModel, options) => {
58
+ const Model = <typeof WorkflowModel>instance.constructor;
59
+
60
+ if (instance.enabled) {
61
+ instance.set('current', true);
62
+ } else if (!instance.current) {
63
+ const count = await Model.count({
64
+ where: {
65
+ key: instance.key,
66
+ },
67
+ transaction: options.transaction,
68
+ });
69
+ if (!count) {
70
+ instance.set('current', true);
71
+ }
72
+ }
73
+
74
+ if (!instance.changed('enabled') || !instance.enabled) {
75
+ return;
76
+ }
77
+
78
+ const previous = await Model.findOne({
79
+ where: {
80
+ key: instance.key,
81
+ current: true,
82
+ id: {
83
+ [Op.ne]: instance.id,
84
+ },
85
+ },
86
+ transaction: options.transaction,
87
+ });
88
+
89
+ if (previous) {
90
+ // NOTE: set to `null` but not `false` will not violate the unique index
91
+ await previous.update(
92
+ { enabled: false, current: null },
93
+ {
94
+ transaction: options.transaction,
95
+ hooks: false,
96
+ },
97
+ );
98
+
99
+ this.toggle(previous, false);
100
+ }
101
+ };
102
+
103
+ async load() {
104
+ const { db, options } = this;
105
+
106
+ initFields(this);
107
+ initActions(this);
108
+ initTriggers(this, options.triggers);
109
+ initInstructions(this, options.instructions);
110
+ initFunctions(this, options.functions);
111
+
112
+ this.loggerCache = new LRUCache({
113
+ max: 20,
114
+ updateAgeOnGet: true,
115
+ dispose(logger) {
116
+ (<Logger>logger).end();
117
+ },
118
+ });
119
+
120
+ this.app.acl.registerSnippet({
121
+ name: `pm.${this.name}.workflows`,
122
+ actions: [
123
+ 'workflows:*',
124
+ 'workflows.nodes:*',
125
+ 'executions:list',
126
+ 'executions:get',
127
+ 'flow_nodes:update',
128
+ 'flow_nodes:destroy',
129
+ ],
130
+ });
131
+
132
+ this.app.acl.allow('users_jobs', ['list', 'get', 'submit'], 'loggedIn');
133
+
134
+ await db.import({
135
+ directory: path.resolve(__dirname, 'collections'),
136
+ });
137
+
138
+ this.db.addMigrations({
139
+ namespace: 'workflow',
140
+ directory: path.resolve(__dirname, 'migrations'),
141
+ context: {
142
+ plugin: this,
143
+ },
144
+ });
145
+
146
+ db.on('workflows.beforeSave', this.onBeforeSave);
147
+ db.on('workflows.afterSave', (model: WorkflowModel) => this.toggle(model));
148
+ db.on('workflows.afterDestroy', (model: WorkflowModel) => this.toggle(model, false));
149
+
150
+ // [Life Cycle]:
151
+ // * load all workflows in db
152
+ // * add all hooks for enabled workflows
153
+ // * add hooks for create/update[enabled]/delete workflow to add/remove specific hooks
154
+ this.app.on('beforeStart', async () => {
155
+ const collection = db.getCollection('workflows');
156
+ const workflows = await collection.repository.find({
157
+ filter: { enabled: true },
158
+ });
159
+
160
+ workflows.forEach((workflow: WorkflowModel) => {
161
+ this.toggle(workflow);
162
+ });
163
+ });
164
+
165
+ this.app.on('afterStart', () => {
166
+ // check for not started executions
167
+ this.dispatch();
168
+ });
169
+
170
+ this.app.on('beforeStop', async () => {
171
+ const collection = db.getCollection('workflows');
172
+ const workflows = await collection.repository.find({
173
+ filter: { enabled: true },
174
+ });
175
+
176
+ workflows.forEach((workflow: WorkflowModel) => {
177
+ this.toggle(workflow, false);
178
+ });
179
+ });
180
+ }
181
+
182
+ toggle(workflow: WorkflowModel, enable?: boolean) {
183
+ const type = workflow.get('type');
184
+ const trigger = this.triggers.get(type);
185
+ if (typeof enable !== 'undefined' ? enable : workflow.get('enabled')) {
186
+ // NOTE: remove previous listener if config updated
187
+ const prev = workflow.previous();
188
+ if (prev.config) {
189
+ trigger.off({ ...workflow.get(), ...prev });
190
+ }
191
+ trigger.on(workflow);
192
+ } else {
193
+ trigger.off(workflow);
194
+ }
195
+ }
196
+
197
+ public trigger(workflow: WorkflowModel, context: object, options: { context?: any } = {}): void {
198
+ // `null` means not to trigger
199
+ if (context == null) {
200
+ return;
201
+ }
202
+
203
+ this.events.push([workflow, context, options]);
204
+
205
+ this.getLogger(workflow.id).info(`new event triggered, now events: ${this.events.length}`);
206
+ this.getLogger(workflow.id).debug(`event data:`, {
207
+ data: context,
208
+ });
209
+
210
+ if (this.events.length > 1) {
211
+ return;
212
+ }
213
+
214
+ // NOTE: no await for quick return
215
+ setTimeout(this.prepare);
216
+ }
217
+
218
+ private prepare = async () => {
219
+ const [event] = this.events;
220
+ if (!event) {
221
+ return;
222
+ }
223
+ const [workflow, context, options] = event;
224
+
225
+ let valid = true;
226
+ if (options.context?.executionId) {
227
+ // NOTE: no transaction here for read-uncommitted execution
228
+ const existed = await workflow.countExecutions({
229
+ where: {
230
+ id: options.context.executionId,
231
+ },
232
+ });
233
+
234
+ if (existed) {
235
+ this.getLogger(workflow.id).warn(
236
+ `workflow ${workflow.id} has already been triggered in same execution (${options.context.executionId}), and newly triggering will be skipped.`,
237
+ );
238
+
239
+ valid = false;
240
+ }
241
+ }
242
+
243
+ if (valid) {
244
+ const execution = await this.db.sequelize.transaction(async (transaction) => {
245
+ const execution = await workflow.createExecution(
246
+ {
247
+ context,
248
+ key: workflow.key,
249
+ status: EXECUTION_STATUS.QUEUEING,
250
+ useTransaction: workflow.useTransaction,
251
+ },
252
+ { transaction },
253
+ );
254
+
255
+ await workflow.increment('executed', { transaction });
256
+
257
+ await (<typeof WorkflowModel>workflow.constructor).increment('allExecuted', {
258
+ where: {
259
+ key: workflow.key,
260
+ },
261
+ transaction,
262
+ });
263
+
264
+ execution.workflow = workflow;
265
+
266
+ return execution;
267
+ });
268
+
269
+ this.getLogger(workflow.id).debug(`execution of workflow ${workflow.id} created as ${execution.id}`, {
270
+ data: execution.context,
271
+ });
272
+
273
+ // NOTE: cache first execution for most cases
274
+ if (!this.executing && !this.pending.length) {
275
+ this.pending.push([execution]);
276
+ }
277
+ }
278
+
279
+ this.events.shift();
280
+
281
+ if (this.events.length) {
282
+ await this.prepare();
283
+ } else {
284
+ this.dispatch();
285
+ }
286
+ };
287
+
288
+ public async resume(job) {
289
+ if (!job.execution) {
290
+ job.execution = await job.getExecution();
291
+ }
292
+
293
+ this.pending.push([job.execution, job]);
294
+ this.dispatch();
295
+ }
296
+
297
+ private async dispatch() {
298
+ if (this.executing) {
299
+ return;
300
+ }
301
+
302
+ this.executing = true;
303
+
304
+ let next: Pending | null = null;
305
+ // resuming has high priority
306
+ if (this.pending.length) {
307
+ next = this.pending.shift() as Pending;
308
+ this.getLogger(next[0].workflowId).info(`pending execution (${next[0].id}) ready to process`);
309
+ } else {
310
+ const execution = (await this.db.getRepository('executions').findOne({
311
+ filter: {
312
+ status: EXECUTION_STATUS.QUEUEING,
313
+ },
314
+ appends: ['workflow'],
315
+ sort: 'createdAt',
316
+ })) as ExecutionModel;
317
+ if (execution && execution.workflow.enabled) {
318
+ this.getLogger(execution.workflowId).info(`execution (${execution.id}) fetched from db`);
319
+ next = [execution];
320
+ }
321
+ }
322
+ if (next) {
323
+ await this.process(...next);
324
+ }
325
+
326
+ this.executing = false;
327
+
328
+ if (next) {
329
+ this.dispatch();
330
+ }
331
+ }
332
+
333
+ private async process(execution: ExecutionModel, job?: JobModel) {
334
+ if (execution.status === EXECUTION_STATUS.QUEUEING) {
335
+ await execution.update({ status: EXECUTION_STATUS.STARTED });
336
+ }
337
+
338
+ const processor = this.createProcessor(execution);
339
+
340
+ this.getLogger(execution.workflowId).info(`execution (${execution.id}) ${job ? 'resuming' : 'starting'}...`);
341
+
342
+ try {
343
+ await (job ? processor.resume(job) : processor.start());
344
+ this.getLogger(execution.workflowId).info(
345
+ `execution (${execution.id}) finished with status: ${execution.status}`,
346
+ );
347
+ } catch (err) {
348
+ this.getLogger(execution.workflowId).error(`execution (${execution.id}) error: ${err.message}`, err);
349
+ }
350
+ }
351
+
352
+ public createProcessor(execution: ExecutionModel, options = {}): Processor {
353
+ return new Processor(execution, { ...options, plugin: this });
354
+ }
355
+ }