@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,327 @@
1
+ import React, { useCallback } from 'react';
2
+ import { Cascader } from 'antd';
3
+ import { useForm } from '@formily/react';
4
+
5
+ import {
6
+ SchemaInitializerItemOptions,
7
+ useCollectionDataSource,
8
+ useCompile,
9
+ SchemaComponentContext,
10
+ useCollectionManager,
11
+ } from '@nocobase/client';
12
+
13
+ import { collection, filter } from '../schemas/collection';
14
+ import { NAMESPACE, lang } from '../locale';
15
+ import { FilterDynamicComponent } from '../components/FilterDynamicComponent';
16
+ import { BaseTypeSets, defaultFieldNames, nodesOptions, triggerOptions, useWorkflowVariableOptions } from '../variable';
17
+ import { FieldsSelect } from '../components/FieldsSelect';
18
+ import { ValueBlock } from '../components/ValueBlock';
19
+ import { useNodeContext } from '.';
20
+
21
+ function matchToManyField(field, appends): boolean {
22
+ const fieldPrefix = `${field.name}.`;
23
+ return (
24
+ ['hasMany', 'belongsToMany'].includes(field.type) &&
25
+ (appends.includes(field.name) || appends.some((item) => item.startsWith(fieldPrefix)))
26
+ );
27
+ }
28
+
29
+ function AssociatedConfig({ value, onChange, ...props }): JSX.Element {
30
+ const { setValuesIn } = useForm();
31
+ const compile = useCompile();
32
+ const { getCollection } = useCollectionManager();
33
+ const options = [nodesOptions, triggerOptions].map((item) => {
34
+ const children = item.useOptions({ types: [matchToManyField] })?.filter(Boolean);
35
+ return {
36
+ label: compile(item.label),
37
+ value: item.value,
38
+ key: item.value,
39
+ children: compile(children),
40
+ disabled: children && !children.length,
41
+ };
42
+ });
43
+
44
+ const { associatedKey = '', name: fieldName } = value ?? {};
45
+ let p = [];
46
+ const matched = associatedKey.match(/^{{(.*)}}$/);
47
+ if (matched) {
48
+ p = [...matched[1].trim().split('.').slice(0, -1), fieldName];
49
+ }
50
+
51
+ const onSelectChange = useCallback(
52
+ (path, option) => {
53
+ if (!path?.length) {
54
+ setValuesIn('collection', null);
55
+ onChange({});
56
+ return;
57
+ }
58
+
59
+ // const associationFieldName = path.pop();
60
+ const { field } = option.pop();
61
+ // need to get:
62
+ // * source collection (from node.config)
63
+ // * target collection (from field name)
64
+ const { collectionName, target, name } = field;
65
+
66
+ const collection = getCollection(collectionName);
67
+ const primaryKeyField = collection.fields.find((f) => f.primaryKey);
68
+
69
+ setValuesIn('collection', target);
70
+
71
+ onChange({
72
+ name,
73
+ // primary key data path
74
+ associatedKey: `{{${path.slice(0, -1).join('.')}.${primaryKeyField.name}}}`,
75
+ // data associated collection name
76
+ associatedCollection: collectionName,
77
+ });
78
+ },
79
+ [onChange],
80
+ );
81
+
82
+ return <Cascader {...props} value={p} options={options} onChange={onSelectChange} />;
83
+ }
84
+
85
+ // based on collection:
86
+ // { collection, field }
87
+
88
+ // based on data associated collection
89
+ // { key: '{{$context.data.id}}', collection: "collection.association", field }
90
+ // select data based
91
+
92
+ export default {
93
+ title: `{{t("Aggregate", { ns: "${NAMESPACE}" })}}`,
94
+ type: 'aggregate',
95
+ group: 'collection',
96
+ description: `{{t("Counting, summing, finding maximum, minimum, and average values for multiple records of a collection or associated data of a record.", { ns: "${NAMESPACE}" })}}`,
97
+ fieldset: {
98
+ aggregator: {
99
+ type: 'string',
100
+ title: `{{t("Aggregator function", { ns: "${NAMESPACE}" })}}`,
101
+ 'x-decorator': 'FormItem',
102
+ 'x-component': 'Radio.Group',
103
+ enum: [
104
+ { label: 'COUNT', value: 'count' },
105
+ { label: 'SUM', value: 'sum' },
106
+ { label: 'AVG', value: 'avg' },
107
+ { label: 'MIN', value: 'min' },
108
+ { label: 'MAX', value: 'max' },
109
+ ],
110
+ required: true,
111
+ default: 'count',
112
+ },
113
+ associated: {
114
+ type: 'boolean',
115
+ title: `{{t("Target type", { ns: "${NAMESPACE}" })}}`,
116
+ 'x-decorator': 'FormItem',
117
+ 'x-component': 'Radio.Group',
118
+ enum: [
119
+ { label: `{{t("Data of collection", { ns: "${NAMESPACE}" })}}`, value: false },
120
+ { label: `{{t("Data of associated collection", { ns: "${NAMESPACE}" })}}`, value: true },
121
+ ],
122
+ required: true,
123
+ default: false,
124
+ 'x-reactions': [
125
+ {
126
+ target: 'collection',
127
+ effects: ['onFieldValueChange'],
128
+ fulfill: {
129
+ state: {
130
+ value: null,
131
+ },
132
+ },
133
+ },
134
+ {
135
+ target: 'association',
136
+ effects: ['onFieldValueChange'],
137
+ fulfill: {
138
+ state: {
139
+ value: null,
140
+ },
141
+ },
142
+ },
143
+ ],
144
+ },
145
+ collectionField: {
146
+ type: 'void',
147
+ 'x-decorator': 'SchemaComponentContext.Provider',
148
+ 'x-decorator-props': {
149
+ value: { designable: false },
150
+ },
151
+ 'x-component': 'Grid',
152
+ properties: {
153
+ row: {
154
+ type: 'void',
155
+ 'x-component': 'Grid.Row',
156
+ properties: {
157
+ target: {
158
+ type: 'void',
159
+ 'x-component': 'Grid.Col',
160
+ properties: {
161
+ collection: {
162
+ ...collection,
163
+ title: `{{t("Data of collection", { ns: "${NAMESPACE}" })}}`,
164
+ 'x-component-props': {
165
+ ...collection['x-component-props'],
166
+ className: 'full-width',
167
+ },
168
+ 'x-reactions': [
169
+ ...collection['x-reactions'],
170
+ {
171
+ dependencies: ['associated'],
172
+ fulfill: {
173
+ state: {
174
+ display: '{{$deps[0] ? "hidden" : "visible"}}',
175
+ },
176
+ },
177
+ },
178
+ {
179
+ target: 'params.field',
180
+ effects: ['onFieldValueChange'],
181
+ fulfill: {
182
+ state: {
183
+ value: null,
184
+ },
185
+ },
186
+ },
187
+ {
188
+ target: 'params.filter',
189
+ effects: ['onFieldValueChange'],
190
+ fulfill: {
191
+ state: {
192
+ value: null,
193
+ },
194
+ },
195
+ },
196
+ ],
197
+ },
198
+ association: {
199
+ type: 'object',
200
+ title: `{{t("Data of associated collection", { ns: "${NAMESPACE}" })}}`,
201
+ 'x-decorator': 'FormItem',
202
+ 'x-component': 'AssociatedConfig',
203
+ 'x-component-props': {
204
+ className: 'full-width',
205
+ },
206
+ 'x-reactions': [
207
+ {
208
+ dependencies: ['associated'],
209
+ fulfill: {
210
+ state: {
211
+ visible: '{{!!$deps[0]}}',
212
+ },
213
+ },
214
+ },
215
+ ],
216
+ required: true,
217
+ },
218
+ },
219
+ },
220
+ field: {
221
+ type: 'void',
222
+ 'x-component': 'Grid.Col',
223
+ properties: {
224
+ 'params.field': {
225
+ type: 'string',
226
+ title: `{{t("Field to aggregate", { ns: "${NAMESPACE}" })}}`,
227
+ 'x-decorator': 'FormItem',
228
+ 'x-component': 'FieldsSelect',
229
+ 'x-component-props': {
230
+ filter(field) {
231
+ return (
232
+ !field.hidden &&
233
+ field.interface &&
234
+ !['belongsTo', 'hasOne', 'hasMany', 'belongsToMany'].includes(field.type)
235
+ );
236
+ },
237
+ },
238
+ required: true,
239
+ 'x-reactions': [
240
+ {
241
+ dependencies: ['collection'],
242
+ fulfill: {
243
+ state: {
244
+ visible: '{{!!$deps[0]}}',
245
+ },
246
+ },
247
+ },
248
+ ],
249
+ },
250
+ },
251
+ },
252
+ },
253
+ },
254
+ },
255
+ },
256
+ params: {
257
+ type: 'object',
258
+ properties: {
259
+ distinct: {
260
+ type: 'boolean',
261
+ title: `{{t("Distinct", { ns: "${NAMESPACE}" })}}`,
262
+ 'x-decorator': 'FormItem',
263
+ 'x-component': 'Checkbox',
264
+ 'x-reactions': [
265
+ {
266
+ dependencies: ['collection', 'aggregator'],
267
+ fulfill: {
268
+ state: {
269
+ visible: '{{!!$deps[0] && ["count"].includes($deps[1])}}',
270
+ },
271
+ },
272
+ },
273
+ ],
274
+ },
275
+ filter: {
276
+ ...filter,
277
+ 'x-reactions': [
278
+ {
279
+ dependencies: ['collection'],
280
+ fulfill: {
281
+ state: {
282
+ visible: '{{!!$deps[0]}}',
283
+ },
284
+ },
285
+ },
286
+ ],
287
+ },
288
+ },
289
+ },
290
+ },
291
+ view: {},
292
+ scope: {
293
+ useCollectionDataSource,
294
+ },
295
+ components: {
296
+ SchemaComponentContext,
297
+ FilterDynamicComponent,
298
+ FieldsSelect,
299
+ ValueBlock,
300
+ AssociatedConfig,
301
+ },
302
+ useVariables({ id, title }, { types, fieldNames = defaultFieldNames }) {
303
+ if (
304
+ types &&
305
+ !types.some((type) => type in BaseTypeSets || Object.values(BaseTypeSets).some((set) => set.has(type)))
306
+ ) {
307
+ return null;
308
+ }
309
+ return {
310
+ [fieldNames.value]: `${id}`,
311
+ [fieldNames.label]: title,
312
+ };
313
+ },
314
+ useInitializers(node): SchemaInitializerItemOptions | null {
315
+ if (!node.config.collection) {
316
+ return null;
317
+ }
318
+
319
+ return {
320
+ type: 'item',
321
+ title: node.title ?? `#${node.id}`,
322
+ component: ValueBlock.Initializer,
323
+ node,
324
+ resultTitle: lang('Query result'),
325
+ };
326
+ },
327
+ };
@@ -0,0 +1,216 @@
1
+ import { FormItem, FormLayout } from '@formily/antd-v5';
2
+ import { SchemaInitializerItemOptions, Variable, css, defaultFieldNames, useCollectionManager } from '@nocobase/client';
3
+ import { Evaluator, evaluators, getOptions } from '@nocobase/evaluators/client';
4
+ import { parse } from '@nocobase/utils/client';
5
+ import { Radio } from 'antd';
6
+ import React from 'react';
7
+ import { useTranslation } from 'react-i18next';
8
+ import { useFlowContext } from '../FlowContext';
9
+ import { RadioWithTooltip } from '../components/RadioWithTooltip';
10
+ import { ValueBlock } from '../components/ValueBlock';
11
+ import { renderEngineReference } from '../components/renderEngineReference';
12
+ import { NAMESPACE, lang } from '../locale';
13
+ import { BaseTypeSets, useWorkflowVariableOptions } from '../variable';
14
+
15
+ function useDynamicExpressionCollectionFieldMatcher(field): boolean {
16
+ if (!['belongsTo', 'hasOne'].includes(field.type)) {
17
+ return false;
18
+ }
19
+
20
+ if (this.getCollection(field.collectionName)?.template === 'expression') {
21
+ return true;
22
+ }
23
+
24
+ const fields = this.getCollectionFields(field.target);
25
+ return fields.some((f) => f.interface === 'expression');
26
+ }
27
+
28
+ const DynamicConfig = ({ value, onChange }) => {
29
+ const { t } = useTranslation();
30
+ const { getCollectionFields, getCollection } = useCollectionManager();
31
+ const scope = useWorkflowVariableOptions({
32
+ types: [useDynamicExpressionCollectionFieldMatcher.bind({ getCollectionFields, getCollection })],
33
+ });
34
+
35
+ return (
36
+ <FormLayout layout="vertical">
37
+ <FormItem colon label={t('Expression type', { ns: NAMESPACE })}>
38
+ <Radio.Group
39
+ value={value === false ? false : value || ''}
40
+ onChange={(ev) => {
41
+ onChange(ev.target.value);
42
+ }}
43
+ >
44
+ <Radio value={false}>{t('Static', { ns: NAMESPACE })}</Radio>
45
+ <Radio value={value || ''}>{t('Dynamic', { ns: NAMESPACE })}</Radio>
46
+ </Radio.Group>
47
+ </FormItem>
48
+ {value !== false ? (
49
+ <FormItem
50
+ label={t('Select dynamic expression', { ns: NAMESPACE })}
51
+ extra={t(
52
+ 'Select the dynamic expression queried from the upstream node. You need to query it from an expression collection.',
53
+ { ns: NAMESPACE },
54
+ )}
55
+ >
56
+ <Variable.Input value={value || ''} onChange={(v) => onChange(v)} scope={scope} />
57
+ </FormItem>
58
+ ) : null}
59
+ </FormLayout>
60
+ );
61
+ };
62
+
63
+ export default {
64
+ title: `{{t("Calculation", { ns: "${NAMESPACE}" })}}`,
65
+ type: 'calculation',
66
+ group: 'control',
67
+ description: `{{t("Calculate an expression based on a calculation engine and obtain a value as the result. Variables in the upstream nodes can be used in the expression. The expression can be static or dynamic one from an expression collections.", { ns: "${NAMESPACE}" })}}`,
68
+ fieldset: {
69
+ dynamic: {
70
+ type: 'string',
71
+ 'x-component': 'DynamicConfig',
72
+ // description: `{{t("Select the dynamic expression queried from the upstream node. You need to query it from an expression collection.", { ns: "${NAMESPACE}" })}}`,
73
+ default: false,
74
+ },
75
+ engine: {
76
+ type: 'string',
77
+ title: `{{t("Calculation engine", { ns: "${NAMESPACE}" })}}`,
78
+ 'x-decorator': 'FormItem',
79
+ 'x-component': 'RadioWithTooltip',
80
+ 'x-component-props': {
81
+ options: getOptions(),
82
+ },
83
+ required: true,
84
+ default: 'math.js',
85
+ 'x-reactions': {
86
+ dependencies: ['dynamic'],
87
+ fulfill: {
88
+ state: {
89
+ visible: '{{$deps[0] === false}}',
90
+ },
91
+ },
92
+ },
93
+ },
94
+ expression: {
95
+ type: 'string',
96
+ title: `{{t("Calculation expression", { ns: "${NAMESPACE}" })}}`,
97
+ 'x-decorator': 'FormItem',
98
+ 'x-component': 'CalculationExpression',
99
+ // NOTE: can not use Variable.Input and scope directly as below,
100
+ // because the scope will be cached.
101
+ // 'x-component': 'Variable.Input',
102
+ // 'x-component-props': {
103
+ // scope: '{{useWorkflowVariableOptions()}}',
104
+ // },
105
+ ['x-validator'](value, rules, { form }) {
106
+ const { values } = form;
107
+ const { evaluate } = evaluators.get(values.engine) as Evaluator;
108
+ const exp = value.trim().replace(/{{([^{}]+)}}/g, ' 1 ');
109
+ try {
110
+ evaluate(exp);
111
+ return '';
112
+ } catch (e) {
113
+ return lang('Expression syntax error');
114
+ }
115
+ },
116
+ 'x-reactions': [
117
+ {
118
+ dependencies: ['dynamic'],
119
+ fulfill: {
120
+ state: {
121
+ visible: '{{$deps[0] === false}}',
122
+ },
123
+ },
124
+ },
125
+ {
126
+ dependencies: ['engine'],
127
+ fulfill: {
128
+ schema: {
129
+ description: '{{renderEngineReference($deps[0])}}',
130
+ },
131
+ },
132
+ },
133
+ ],
134
+ required: true,
135
+ },
136
+ scope: {
137
+ type: 'string',
138
+ title: `{{t("Variable datasource", { ns: "${NAMESPACE}" })}}`,
139
+ 'x-decorator': 'FormItem',
140
+ 'x-component': 'ScopeSelect',
141
+ 'x-component-props': {
142
+ changeOnSelect: true,
143
+ },
144
+ 'x-reactions': {
145
+ dependencies: ['dynamic'],
146
+ fulfill: {
147
+ state: {
148
+ visible: '{{$deps[0]}}',
149
+ },
150
+ },
151
+ },
152
+ },
153
+ },
154
+ view: {},
155
+ scope: {
156
+ renderEngineReference,
157
+ },
158
+ components: {
159
+ CalculationExpression(props) {
160
+ const scope = useWorkflowVariableOptions();
161
+
162
+ return <Variable.TextArea scope={scope} {...props} />;
163
+ },
164
+ ScopeSelect(props) {
165
+ const scope = useWorkflowVariableOptions({
166
+ types: [{ type: 'reference', options: { collection: '*', entity: true } }],
167
+ });
168
+ return <Variable.Input scope={scope} {...props} />;
169
+ },
170
+ CalculationResult({ dataSource }) {
171
+ const { execution } = useFlowContext();
172
+ if (!execution) {
173
+ return lang('Calculation result');
174
+ }
175
+ const result = parse(dataSource)({
176
+ $jobsMapByNodeId: (execution.jobs ?? []).reduce(
177
+ (map, job) => Object.assign(map, { [job.nodeId]: job.result }),
178
+ {},
179
+ ),
180
+ });
181
+
182
+ return (
183
+ <pre
184
+ className={css`
185
+ margin: 0;
186
+ `}
187
+ >
188
+ {JSON.stringify(result, null, 2)}
189
+ </pre>
190
+ );
191
+ },
192
+ RadioWithTooltip,
193
+ DynamicConfig,
194
+ },
195
+ useVariables({ id, title }, { types, fieldNames = defaultFieldNames }) {
196
+ if (
197
+ types &&
198
+ !types.some((type) => type in BaseTypeSets || Object.values(BaseTypeSets).some((set) => set.has(type)))
199
+ ) {
200
+ return null;
201
+ }
202
+ return {
203
+ [fieldNames.value]: `${id}`,
204
+ [fieldNames.label]: title,
205
+ };
206
+ },
207
+ useInitializers(node): SchemaInitializerItemOptions {
208
+ return {
209
+ type: 'item',
210
+ title: node.title ?? `#${node.id}`,
211
+ component: ValueBlock.Initializer,
212
+ node,
213
+ resultTitle: lang('Calculation result'),
214
+ };
215
+ },
216
+ };