directus
1import type { Flow, FlowRaw, Operation, OperationRaw } from '@directus/types';
2import { omit } from 'lodash-es';
3
4export function constructFlowTree(flow: FlowRaw): Flow {
5const rootOperation = flow.operations.find((operation) => operation.id === flow.operation) ?? null;
6
7const operationTree = constructOperationTree(rootOperation, flow.operations);
8
9const flowTree: Flow = {
10...omit(flow, 'operations'),
11operation: operationTree,
12options: flow.options ?? {},
13};
14
15return flowTree;
16}
17
18function constructOperationTree(root: OperationRaw | null, operations: OperationRaw[]): Operation | null {
19if (root === null) {
20return null;
21}
22
23const resolveOperation = root.resolve !== null ? operations.find((operation) => operation.id === root.resolve) : null;
24const rejectOperation = root.reject !== null ? operations.find((operation) => operation.id === root.reject) : null;
25
26if (resolveOperation === undefined || rejectOperation === undefined) {
27throw new Error('Undefined reference in operations');
28}
29
30const operationTree: Operation = {
31...omit(root, 'flow'),
32resolve: constructOperationTree(resolveOperation, operations),
33reject: constructOperationTree(rejectOperation, operations),
34};
35
36return operationTree;
37}
38