The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(workflow): dispatch an iterator loop body listed in the iterator nextStepIds (#26301)
Thomas Trompettee99fd1a7683f
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/find-parent-steps.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/find-parent-steps.util.spec.tsindex d97395806be..de5dc655ed8 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/find-parent-steps.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/find-parent-steps.util.spec.ts@@ -1,6 +1,7 @@import {createMockCodeStep,createMockIfElseStep,+ createMockIteratorStep,} from 'src/modules/workflow/workflow-executor/utils/create-mock-workflow-steps.util';import { findParentSteps } from 'src/modules/workflow/workflow-executor/utils/find-parent-steps.util';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';@@ -90,4 +91,25 @@ describe('findParentSteps', () => {expect(result).toEqual([standardParent, ifElseParent]);});++ it('should not treat an iterator as a parent of its own loop body', () => {+ const iterator = createMockIteratorStep('iterator', ['body'], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const steps: WorkflowAction[] = [iterator, body];++ const result = findParentSteps({ step: body, steps });++ expect(result).toEqual([]);+ });++ it('should keep an iterator as a parent of its post-loop step', () => {+ const iterator = createMockIteratorStep('iterator', ['after'], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const after = createMockCodeStep('after');+ const steps: WorkflowAction[] = [iterator, body, after];++ const result = findParentSteps({ step: after, steps });++ expect(result).toEqual([iterator]);+ });});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-step.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-step.util.spec.tsindex 8122da01de6..40312235e51 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-step.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-step.util.spec.ts@@ -4,6 +4,7 @@ import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/import {createMockCodeStep,createMockIfElseStep,+ createMockIteratorStep,} from 'src/modules/workflow/workflow-executor/utils/create-mock-workflow-steps.util';import { shouldExecuteStep } from 'src/modules/workflow/workflow-executor/utils/should-execute-step.util';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';@@ -507,6 +508,96 @@ describe('shouldExecuteStep', () => {workflowRunStatus: WorkflowRunStatus.RUNNING,});+ expect(result).toBe(false);+ });+ it('should return true for a loop body listed in both initialLoopStepIds and the iterator nextStepIds while the iterator is running', () => {+ const iterator = createMockIteratorStep('iterator', ['body'], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const allSteps: WorkflowAction[] = [iterator, body];++ const result = shouldExecuteStep({+ step: body,+ steps: allSteps,+ stepInfos: {+ iterator: { status: StepStatus.RUNNING },+ body: { status: StepStatus.NOT_STARTED },+ },+ workflowRunStatus: WorkflowRunStatus.RUNNING,+ });++ expect(result).toBe(true);+ });++ it('should return true for a loop body listed only in initialLoopStepIds while the iterator is running', () => {+ const iterator = createMockIteratorStep('iterator', [], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const allSteps: WorkflowAction[] = [iterator, body];++ const result = shouldExecuteStep({+ step: body,+ steps: allSteps,+ stepInfos: {+ iterator: { status: StepStatus.RUNNING },+ body: { status: StepStatus.NOT_STARTED },+ },+ workflowRunStatus: WorkflowRunStatus.RUNNING,+ });++ expect(result).toBe(true);+ });++ it('should return false for a post-loop step while the iterator is running', () => {+ const iterator = createMockIteratorStep('iterator', ['after'], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const after = createMockCodeStep('after');+ const allSteps: WorkflowAction[] = [iterator, body, after];++ const result = shouldExecuteStep({+ step: after,+ steps: allSteps,+ stepInfos: {+ iterator: { status: StepStatus.RUNNING },+ body: { status: StepStatus.SUCCESS },+ after: { status: StepStatus.NOT_STARTED },+ },+ workflowRunStatus: WorkflowRunStatus.RUNNING,+ });++ expect(result).toBe(false);+ });+ it('should return true for a loop body whose iterator is skipped, because skip propagation into a loop is dispatched by the iterator and not inferred from parents', () => {+ const iterator = createMockIteratorStep('iterator', ['body'], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const allSteps: WorkflowAction[] = [iterator, body];++ const result = shouldExecuteStep({+ step: body,+ steps: allSteps,+ stepInfos: {+ iterator: { status: StepStatus.SKIPPED },+ body: { status: StepStatus.NOT_STARTED },+ },+ workflowRunStatus: WorkflowRunStatus.RUNNING,+ });++ expect(result).toBe(true);+ });++ it('should return false for a loop body already marked skipped by its iterator', () => {+ const iterator = createMockIteratorStep('iterator', ['body'], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const allSteps: WorkflowAction[] = [iterator, body];++ const result = shouldExecuteStep({+ step: body,+ steps: allSteps,+ stepInfos: {+ iterator: { status: StepStatus.SKIPPED },+ body: { status: StepStatus.SKIPPED },+ },+ workflowRunStatus: WorkflowRunStatus.RUNNING,+ });+expect(result).toBe(false);});});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-skip-step-execution.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-skip-step-execution.util.spec.tsindex fa9b7e0e1f9..e35cdcca13f 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-skip-step-execution.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-skip-step-execution.util.spec.ts@@ -3,6 +3,7 @@ import { StepStatus } from 'twenty-shared/workflow';import {createMockCodeStep,createMockIfElseStep,+ createMockIteratorStep,} from 'src/modules/workflow/workflow-executor/utils/create-mock-workflow-steps.util';import { shouldSkipStepExecution } from 'src/modules/workflow/workflow-executor/utils/should-skip-step-execution.util';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';@@ -359,4 +360,34 @@ describe('shouldSkipStepExecution', () => {expect(result).toBe(false);});+ it('should return false for a loop body whose iterator is skipped, in both the overlapping and the well-formed graph', () => {+ const overlappingIterator = createMockIteratorStep(+ 'iterator',+ ['body'],+ ['body'],+ );+ const wellFormedIterator = createMockIteratorStep('iterator', [], ['body']);+ const body = createMockCodeStep('body', ['iterator']);+ const stepInfos = {+ iterator: { status: StepStatus.SKIPPED },+ body: { status: StepStatus.NOT_STARTED },+ };++ const overlappingSteps: WorkflowAction[] = [overlappingIterator, body];+ const wellFormedSteps: WorkflowAction[] = [wellFormedIterator, body];++ const overlappingResult = shouldSkipStepExecution({+ step: body,+ steps: overlappingSteps,+ stepInfos,+ });+ const wellFormedResult = shouldSkipStepExecution({+ step: body,+ steps: wellFormedSteps,+ stepInfos,+ });++ expect(overlappingResult).toBe(false);+ expect(wellFormedResult).toBe(false);+ });});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 aefe6473a15..389b1e41f59 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,8 @@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 { 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';export const findParentSteps = ({@@ -15,6 +17,13 @@ export const findParentSteps = ({return false;}+ if (+ isWorkflowIteratorAction(candidateParent) &&+ getIteratorInitialLoopStepIds(candidateParent).includes(step.id)+ ) {+ return false;+ }+if (candidateParent.nextStepIds?.includes(step.id)) {return true;}