Skip to content

Commit ccd0c88

Browse files
committed
2949 - fix Each nested task insertions
1 parent 4d85581 commit ccd0c88

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

client/src/pages/platform/workflow-editor/utils/getRecursivelyUpdatedTasks.ts

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import {WorkflowTask} from '@/shared/middleware/platform/configuration';
22
import {BranchCaseType} from '@/shared/types';
33

4+
function updateTaskParameter(task: WorkflowTask, parameterKey: string, parameterValue: unknown): WorkflowTask {
5+
return {
6+
...task,
7+
parameters: {
8+
...task.parameters,
9+
[parameterKey]: parameterValue,
10+
},
11+
};
12+
}
13+
14+
function updateTaskArray(task: WorkflowTask, parameterKey: string, taskToReplace: WorkflowTask): WorkflowTask {
15+
const tasks = task.parameters?.[parameterKey] as WorkflowTask[];
16+
17+
if (!tasks) {
18+
return task;
19+
}
20+
21+
return updateTaskParameter(task, parameterKey, getRecursivelyUpdatedTasks(tasks, taskToReplace));
22+
}
23+
424
export default function getRecursivelyUpdatedTasks(
525
tasks: Array<WorkflowTask>,
626
taskToReplace: WorkflowTask
@@ -15,83 +35,60 @@ export default function getRecursivelyUpdatedTasks(
1535
}
1636

1737
if (task.parameters?.caseTrue || task.parameters?.caseFalse) {
18-
const updatedTask = {...task};
38+
let updatedTask = {...task};
1939

2040
if (task.parameters.caseTrue) {
21-
updatedTask.parameters = {
22-
...updatedTask.parameters,
23-
caseTrue: getRecursivelyUpdatedTasks(task.parameters.caseTrue, taskToReplace),
24-
};
41+
updatedTask = updateTaskArray(updatedTask, 'caseTrue', taskToReplace);
2542
}
2643

2744
if (task.parameters.caseFalse) {
28-
updatedTask.parameters = {
29-
...updatedTask.parameters,
30-
caseFalse: getRecursivelyUpdatedTasks(task.parameters.caseFalse, taskToReplace),
31-
};
45+
updatedTask = updateTaskArray(updatedTask, 'caseFalse', taskToReplace);
3246
}
3347

3448
return updatedTask;
3549
}
3650

3751
if (task.parameters?.iteratee) {
38-
return {
39-
...task,
40-
parameters: {
41-
...task.parameters,
42-
iteratee: getRecursivelyUpdatedTasks(task.parameters.iteratee, taskToReplace),
43-
},
44-
};
52+
if (Array.isArray(task.parameters.iteratee)) {
53+
return updateTaskArray(task, 'iteratee', taskToReplace);
54+
} else {
55+
return updateTaskParameter(
56+
task,
57+
'iteratee',
58+
getRecursivelyUpdatedTasks([task.parameters.iteratee], taskToReplace)[0]
59+
);
60+
}
4561
}
4662

4763
if (task.parameters?.cases) {
48-
const updatedTask = {...task};
64+
let updatedTask = {...task};
4965

5066
if (task.parameters.default) {
51-
updatedTask.parameters = {
52-
...updatedTask.parameters,
53-
default: getRecursivelyUpdatedTasks(task.parameters.default, taskToReplace),
54-
};
67+
updatedTask = updateTaskArray(updatedTask, 'default', taskToReplace);
5568
}
5669

5770
if (task.parameters.cases) {
58-
updatedTask.parameters = {
59-
...updatedTask.parameters,
60-
cases: (task.parameters.cases as BranchCaseType[]).map((caseItem) => {
61-
const updatedCaseItem = {...caseItem};
62-
63-
if (caseItem.tasks) {
64-
updatedCaseItem.tasks = getRecursivelyUpdatedTasks(caseItem.tasks, taskToReplace);
65-
}
66-
67-
return updatedCaseItem;
68-
}),
69-
};
71+
const updatedCases = (task.parameters.cases as BranchCaseType[]).map((caseItem) => ({
72+
...caseItem,
73+
tasks: caseItem.tasks ? getRecursivelyUpdatedTasks(caseItem.tasks, taskToReplace) : caseItem.tasks,
74+
}));
75+
76+
updatedTask = updateTaskParameter(updatedTask, 'cases', updatedCases);
7077
}
7178

7279
return updatedTask;
7380
}
7481

7582
if (task.parameters?.tasks) {
76-
return {
77-
...task,
78-
parameters: {
79-
...task.parameters,
80-
tasks: getRecursivelyUpdatedTasks(task.parameters.tasks, taskToReplace),
81-
},
82-
};
83+
return updateTaskArray(task, 'tasks', taskToReplace);
8384
}
8485

8586
if (task.parameters?.branches) {
86-
return {
87-
...task,
88-
parameters: {
89-
...task.parameters,
90-
branches: ((task.parameters?.branches as WorkflowTask[][]) || []).map((branch) =>
91-
getRecursivelyUpdatedTasks(branch, taskToReplace)
92-
),
93-
},
94-
};
87+
const updatedBranches = ((task.parameters?.branches as WorkflowTask[][]) || []).map((branch) =>
88+
getRecursivelyUpdatedTasks(branch, taskToReplace)
89+
);
90+
91+
return updateTaskParameter(task, 'branches', updatedBranches);
9592
}
9693

9794
return task;

client/src/pages/platform/workflow-editor/utils/taskDispatcherConfig.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,12 @@ export function getTaskDispatcherTask({
541541
subtasks = subtasks?.flatMap((branchCase: BranchCaseType) => branchCase.tasks);
542542
} else if (collectionName === 'branches') {
543543
subtasks = subtasks?.flat();
544+
} else if (collectionName === 'iteratee') {
545+
if (subtasks && typeof subtasks === 'object' && !Array.isArray(subtasks)) {
546+
subtasks = [subtasks];
547+
} else if (!Array.isArray(subtasks)) {
548+
subtasks = [];
549+
}
544550
}
545551

546552
if (Array.isArray(subtasks) && subtasks.length > 0) {

0 commit comments

Comments
 (0)