Skip to content

Commit

Permalink
1721 - enable operation switching for condition subtasks
Browse files Browse the repository at this point in the history
  • Loading branch information
kresimir-coko committed Nov 18, 2024
1 parent f7a20e6 commit 79f3a62
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TriggerDefinitionApi,
WorkflowConnection,
WorkflowNodeOutput,
WorkflowTask,
} from '@/shared/middleware/platform/configuration';
import {useDeleteWorkflowNodeTestOutputMutation} from '@/shared/mutations/platform/workflowNodeTestOutputs.mutations';
import {
Expand All @@ -33,6 +34,7 @@ import {useGetWorkflowTestConfigurationConnectionsQuery} from '@/shared/queries/
import {
ComponentPropertiesType,
DataPillType,
NodeDataType,
PropertyAllType,
UpdateWorkflowMutationType,
WorkflowDefinitionType,
Expand All @@ -50,6 +52,7 @@ import getAllTaskNames from '../utils/getAllTaskNames';
import getDataPillsFromProperties from '../utils/getDataPillsFromProperties';
import getParametersWithDefaultValues from '../utils/getParametersWithDefaultValues';
import saveWorkflowDefinition from '../utils/saveWorkflowDefinition';
import updateConditionSubtask from '../utils/updateRootConditionNode';
import CurrentOperationSelect from './CurrentOperationSelect';
import ConnectionTab from './node-details-tabs/ConnectionTab';
import DescriptionTab from './node-details-tabs/DescriptionTab';
Expand Down Expand Up @@ -96,7 +99,7 @@ const WorkflowNodeDetailsPanel = ({
const {currentComponent, currentNode, setCurrentComponent, setCurrentNode, workflowNodeDetailsPanelOpen} =
useWorkflowNodeDetailsPanelStore();

const {componentActions, setDataPills, workflow} = useWorkflowDataStore();
const {componentActions, nodes, setDataPills, workflow} = useWorkflowDataStore();

const {data: currentComponentDefinition} = useGetComponentDefinitionQuery(
{
Expand Down Expand Up @@ -269,19 +272,72 @@ const WorkflowNodeDetailsPanel = ({

const {componentName, description, label, workflowNodeName} = currentComponent;

let nodeData = {
componentName,
description,
label,
name: workflowNodeName || currentNode?.name || '',
operationName: newOperationName,
parameters: getParametersWithDefaultValues({
properties: operationData.properties as Array<PropertyAllType>,
}),
trigger: currentNode?.trigger,
type: `${componentName}/v${currentComponentDefinition.version}/${newOperationName}`,
};

if (currentNode?.conditionData) {
const parentConditionNode = nodes.find(
(node) => node.data.name === currentNode?.conditionData?.conditionId
);

if (!parentConditionNode) {
return;
}

const conditionCase = currentNode.conditionData.conditionCase;
const conditionParameters: Array<WorkflowTask> = parentConditionNode.data.parameters[conditionCase];

if (conditionParameters) {
const nodeIndex = conditionParameters.findIndex((subtask) => subtask.name === currentNode.name);

if (nodeIndex !== -1) {
conditionParameters[nodeIndex] = {
...conditionParameters[nodeIndex],
type: `${componentName}/v${currentComponentDefinition.version}/${newOperationName}`,
};

if (!workflow.definition) {
return;
}

const tasks = JSON.parse(workflow.definition).tasks;

const updatedParentConditionTask = workflow.tasks?.find(
(task) => task.name === currentNode.conditionData?.conditionId
);

if (!updatedParentConditionTask) {
return;
}

const updatedRootConditionNode = updateConditionSubtask({
conditionCase,
conditionId: currentNode.conditionData.conditionId,
nodeIndex,
nodes,
tasks,
updatedParentConditionNodeData: parentConditionNode.data,
updatedParentConditionTask,
workflow,
});

nodeData = updatedRootConditionNode.data ?? (updatedRootConditionNode as NodeDataType);
}
}
}

saveWorkflowDefinition({
nodeData: {
componentName,
description,
label,
name: workflowNodeName || currentNode?.name || '',
operationName: newOperationName,
parameters: getParametersWithDefaultValues({
properties: operationData.properties as Array<PropertyAllType>,
}),
trigger: currentNode?.trigger,
type: `${componentName}/v${currentComponentDefinition.version}/${newOperationName}`,
},
nodeData,
onSuccess: () => {
setCurrentComponent({
...currentComponent,
Expand All @@ -295,6 +351,7 @@ const WorkflowNodeDetailsPanel = ({
});
},
queryClient,
subtask: !!currentNode?.conditionData,
updateWorkflowMutation,
workflow,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface SaveWorkflowDefinitionProps {
onSuccess?: () => void;
placeholderId?: string;
queryClient: QueryClient;
subtask?: boolean;
updateWorkflowMutation: UseMutationResult<void, Error, UpdateWorkflowRequestType, unknown>;
workflow: Workflow;
}
Expand All @@ -39,6 +40,7 @@ export default async function saveWorkflowDefinition({
onSuccess,
placeholderId,
queryClient,
subtask,
updateWorkflowMutation,
workflow,
}: SaveWorkflowDefinitionProps) {
Expand Down Expand Up @@ -130,6 +132,7 @@ export default async function saveWorkflowDefinition({
if (
existingWorkflowTask &&
!decorative &&
!subtask &&
(!operationName ||
(existingWorkflowTask.parameters &&
JSON.stringify(existingWorkflowTask.parameters) === JSON.stringify(newTask.parameters))) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {Workflow, WorkflowTask} from '@/shared/middleware/automation/configuration';
import {NodeType} from '@/shared/types';
import {Node} from 'reactflow';

import {WorkflowTaskDataType} from '../stores/useWorkflowDataStore';
import getParentConditionTask from './getParentConditionTask';

interface UpdateRootConditionNodeProps {
conditionCase: string;
conditionId: string;
nodeIndex: number;
tasks: Array<WorkflowTask>;
updatedParentConditionNodeData: NodeType;
updatedParentConditionTask: WorkflowTask;
nodes: Array<Node>;
workflow: Workflow & WorkflowTaskDataType;
}

export default function updateRootConditionNode({
nodeIndex,
nodes,
tasks,
updatedParentConditionNodeData,
updatedParentConditionTask,
workflow,
}: UpdateRootConditionNodeProps): NodeType {
let currentTaskNode = updatedParentConditionNodeData;

let currentTaskNodeConditionData = updatedParentConditionNodeData.conditionData;

while (currentTaskNodeConditionData) {
const parentConditionTask = getParentConditionTask(tasks, currentTaskNodeConditionData.conditionId);

if (!parentConditionTask) {
break;
}

const parentConditionTaskNode = nodes.find((node) => node.id === parentConditionTask.name);

if (!parentConditionTaskNode) {
break;
}

console.log('parentConditionTaskNode: ', parentConditionTaskNode);

const currentConditionCase = currentTaskNodeConditionData.conditionCase;

const parentConditionCaseTasks: Array<WorkflowTask> =
parentConditionTaskNode.data.parameters[currentConditionCase] || [];

const workflowTasks = workflow.tasks;

let currentTask = workflowTasks?.find((task) => task.name === currentTaskNode.id);

if (!currentTask) {
currentTask = updatedParentConditionTask;
}

const currentTaskIndex = parentConditionCaseTasks.findIndex((task) => task.name === currentTask.name);

if (currentTaskIndex > -1) {
parentConditionCaseTasks[currentTaskIndex] = currentTask;
} else {
parentConditionCaseTasks[nodeIndex] = currentTask;
}

parentConditionTaskNode.data.parameters = {
...parentConditionTaskNode.data.parameters,
[currentConditionCase]: parentConditionCaseTasks,
};

currentTaskNode = {
...parentConditionTaskNode,
name: parentConditionTaskNode.id,
type: parentConditionTaskNode.type || 'workflow',
version: parentConditionTaskNode.data.type.split('/v')[1],
workflowNodeName: parentConditionTaskNode.id,
};

currentTaskNodeConditionData = parentConditionTaskNode.data.conditionData;
}

return currentTaskNode;
}
9 changes: 8 additions & 1 deletion client/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '@/shared/middleware/platform/configuration';
import {UseMutationResult} from '@tanstack/react-query';
import {ReactNode} from 'react';
import {Node} from 'reactflow';

export type DataPillType = {
componentName?: string;
Expand Down Expand Up @@ -125,8 +126,14 @@ export type NodeDataType = {

export type NodeType = {
componentName?: string;
conditionData?: {
conditionCase: string;
conditionId: string;
index: number;
};
connections?: Array<WorkflowConnectionType>;
connectionId?: number;
data: NodeDataType;
displayConditions?: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: boolean;
Expand All @@ -151,7 +158,7 @@ export type NodeType = {
type: string;
version: number;
workflowNodeName: string;
};
} & Node;

export type SubPropertyType = PropertyAllType & {custom: boolean};

Expand Down

0 comments on commit 79f3a62

Please sign in to comment.