The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(workflow): skip not-taken if/else branch targets also listed in the step's nextStepIds (#27833)
Thomas Trompette46bf0601368d
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/get-effective-parent-status.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/get-effective-parent-status.util.spec.tsindex 040fd2742da..f519d5fb079 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/get-effective-parent-status.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/get-effective-parent-status.util.spec.ts@@ -28,6 +28,11 @@ const ifElseStep = {},} as unknown as WorkflowAction;+const ifElseStepWithBranchTargetsInNextStepIds = {+ ...ifElseStep,+ nextStepIds: ['ifChild', 'elseChild', 'afterIfElse'],+} as unknown as WorkflowAction;+describe('getEffectiveParentStatus', () => {it('should return the raw status for a non if/else parent', () => {const stepInfos: WorkflowRunStepInfos = {@@ -108,6 +113,48 @@ describe('getEffectiveParentStatus', () => {).toBe(StepStatus.FAILED_SAFELY);});+ it('should read as SKIPPED for a not-taken branch target also listed in the if/else nextStepIds', () => {+ const stepInfos: WorkflowRunStepInfos = {+ ifElse: {+ status: StepStatus.SUCCESS,+ result: { matchingBranchId: 'ifBranch' },+ },+ };++ expect(+ getEffectiveParentStatus({+ parentStep: ifElseStepWithBranchTargetsInNextStepIds,+ childStepId: 'elseChild',+ stepInfos,+ }),+ ).toBe(StepStatus.SKIPPED);++ expect(+ getEffectiveParentStatus({+ parentStep: ifElseStepWithBranchTargetsInNextStepIds,+ childStepId: 'ifChild',+ stepInfos,+ }),+ ).toBe(StepStatus.SUCCESS);+ });++ it('should return the raw status for an if/else nextStepIds child that is not a branch target', () => {+ const stepInfos: WorkflowRunStepInfos = {+ ifElse: {+ status: StepStatus.SUCCESS,+ result: { matchingBranchId: 'ifBranch' },+ },+ };++ expect(+ getEffectiveParentStatus({+ parentStep: ifElseStepWithBranchTargetsInNextStepIds,+ childStepId: 'afterIfElse',+ stepInfos,+ }),+ ).toBe(StepStatus.SUCCESS);+ });+it('should return undefined when the parent has no status yet', () => {expect(getEffectiveParentStatus({diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.tsindex 9df2eff026c..5af6f199d8e 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.ts@@ -557,6 +557,47 @@ describe('shouldExecuteChildStep', () => {expect(result).toBe(false);});+ it('should not execute a not-taken branch target also listed in the if/else nextStepIds', () => {+ const ifElseParent = {+ id: 'if-else',+ type: WorkflowActionType.IF_ELSE,+ nextStepIds: ['if-child', 'else-child'],+ settings: {+ input: {+ branches: [+ { id: 'if-branch', filterGroupId: 'fg', nextStepIds: ['if-child'] },+ { id: 'else-branch', nextStepIds: ['else-child'] },+ ],+ stepFilterGroups: [],+ stepFilters: [],+ },+ },+ } as unknown as WorkflowAction;++ const stepInfos = {+ 'if-else': {+ status: StepStatus.SUCCESS,+ result: { matchingBranchId: 'if-branch' },+ },+ };++ expect(+ shouldExecuteChildStep({+ childStepId: 'else-child',+ parentSteps: [ifElseParent],+ stepInfos,+ }),+ ).toBe(false);++ expect(+ shouldExecuteChildStep({+ childStepId: 'if-child',+ parentSteps: [ifElseParent],+ stepInfos,+ }),+ ).toBe(true);+ });+it('should return true when the only parent failed and continues on failure', () => {const continuingParent = createMockCodeStep('continuing-parent',diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/find-parent-steps.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/find-parent-steps.util.tsindex 389b1e41f59..d6f0f48c029 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/find-parent-steps.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/find-parent-steps.util.ts@@ -1,6 +1,7 @@import { isDefined } from 'twenty-shared/utils';import { isWorkflowIfElseAction } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/guards/is-workflow-if-else-action.guard';+import { isIfElseBranchTarget } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/is-if-else-branch-target.util';import { isWorkflowIteratorAction } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/guards/is-workflow-iterator-action.guard';import { getIteratorInitialLoopStepIds } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-iterator-initial-loop-step-ids.util';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';@@ -29,9 +30,10 @@ export const findParentSteps = ({}if (isWorkflowIfElseAction(candidateParent)) {- return candidateParent.settings.input.branches.some((branch) =>- branch.nextStepIds?.includes(step.id),- );+ return isIfElseBranchTarget({+ ifElseStep: candidateParent,+ stepId: step.id,+ });}return false;diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util.tsindex 31d5847497a..866f64a960a 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util.ts@@ -3,6 +3,7 @@ import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';import { isWorkflowIfElseAction } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/guards/is-workflow-if-else-action.guard';import { type WorkflowIfElseResult } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/types/workflow-if-else-result.type';+import { isIfElseBranchTarget } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/is-if-else-branch-target.util';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';export const getEffectiveParentStatus = ({@@ -16,10 +17,11 @@ export const getEffectiveParentStatus = ({}): StepStatus | undefined => {const parentStatus = stepInfos[parentStep.id]?.status;- if (- !isWorkflowIfElseAction(parentStep) ||- parentStep.nextStepIds?.includes(childStepId)- ) {+ if (!isWorkflowIfElseAction(parentStep)) {+ return parentStatus;+ }++ if (!isIfElseBranchTarget({ ifElseStep: parentStep, stepId: childStepId })) {return parentStatus;}diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/is-if-else-branch-target.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/is-if-else-branch-target.util.tsnew file mode 100644index 00000000000..88c50196680--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/is-if-else-branch-target.util.ts@@ -0,0 +1,12 @@+import { type WorkflowIfElseAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';++export const isIfElseBranchTarget = ({+ ifElseStep,+ stepId,+}: {+ ifElseStep: WorkflowIfElseAction;+ stepId: string;+}): boolean =>+ ifElseStep.settings.input.branches.some((branch) =>+ branch.nextStepIds?.includes(stepId),+ );