The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix step output for iterators (#14796)
Baptiste Devessierb3153535a603
diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/hooks/useWorkflowRunStepInfo.ts b/packages/twenty-front/src/modules/workflow/workflow-steps/hooks/useWorkflowRunStepInfo.tsindex 1f2761a592f..424c0b38012 100644--- a/packages/twenty-front/src/modules/workflow/workflow-steps/hooks/useWorkflowRunStepInfo.ts+++ b/packages/twenty-front/src/modules/workflow/workflow-steps/hooks/useWorkflowRunStepInfo.ts@@ -2,8 +2,8 @@ import { workflowRunIteratorSubStepIterationIndexComponentState } from '@/commanimport { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';import { useWorkflowRun } from '@/workflow/hooks/useWorkflowRun';import { useWorkflowRunIdOrThrow } from '@/workflow/hooks/useWorkflowRunIdOrThrow';-import { getWorkflowRunAllStepInfoHistory } from '@/workflow/workflow-steps/utils/getWorkflowRunAllStepInfoHistory';import { isDefined } from 'twenty-shared/utils';+import { getStepInfoHistoryItem } from '../utils/getStepInfoHistoryItem';export const useWorkflowRunStepInfo = ({ stepId }: { stepId: string }) => {const workflowRunId = useWorkflowRunIdOrThrow();@@ -15,11 +15,14 @@ export const useWorkflowRunStepInfo = ({ stepId }: { stepId: string }) => {const stepInfo = workflowRun?.state?.stepInfos[stepId];- if (!isDefined(stepInfo)) {+ if (!isDefined(stepInfo) || !isDefined(workflowRun?.state?.flow)) {return undefined;}- const allStepInfoHistory = getWorkflowRunAllStepInfoHistory({ stepInfo });-- return allStepInfoHistory[workflowRunIteratorSubStepIterationIndex];+ return getStepInfoHistoryItem({+ stepInfo,+ steps: workflowRun.state.flow.steps,+ stepId,+ iterationIndex: workflowRunIteratorSubStepIterationIndex,+ });};diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/utils/__tests__/getStepInfoHistoryItem.test.ts b/packages/twenty-front/src/modules/workflow/workflow-steps/utils/__tests__/getStepInfoHistoryItem.test.tsnew file mode 100644index 00000000000..6ff169950d4--- /dev/null+++ b/packages/twenty-front/src/modules/workflow/workflow-steps/utils/__tests__/getStepInfoHistoryItem.test.ts@@ -0,0 +1,132 @@+import { type WorkflowStep } from '@/workflow/types/Workflow';+import { type WorkflowRunStepInfo, StepStatus } from 'twenty-shared/workflow';+import { getStepInfoHistoryItem } from '../getStepInfoHistoryItem';++describe('getStepInfoHistoryItem', () => {+ const iteratorStep: WorkflowStep = {+ id: 'iterator1',+ name: 'Iterator',+ type: 'ITERATOR',+ valid: true,+ nextStepIds: ['step2'],+ settings: {+ input: { initialLoopStepIds: ['step2'] },+ outputSchema: {},+ errorHandlingOptions: {+ retryOnFailure: { value: false },+ continueOnFailure: { value: false },+ },+ },+ };++ const codeStep: WorkflowStep = {+ id: 'step2',+ name: 'Step 2',+ type: 'CODE',+ valid: true,+ nextStepIds: [],+ settings: {+ input: {+ serverlessFunctionId: 'func',+ serverlessFunctionVersion: '1.0.0',+ serverlessFunctionInput: {},+ },+ outputSchema: {},+ errorHandlingOptions: {+ retryOnFailure: { value: false },+ continueOnFailure: { value: false },+ },+ },+ };++ const steps: WorkflowStep[] = [iteratorStep, codeStep];++ it('returns the correct history item for a descendant of iterator', () => {+ const stepInfo: WorkflowRunStepInfo = {+ result: 'final',+ status: StepStatus.SUCCESS,+ history: [+ { result: 'first', status: StepStatus.SUCCESS },+ { result: 'second', status: StepStatus.SUCCESS },+ ],+ };++ expect(+ getStepInfoHistoryItem({+ stepInfo,+ steps,+ stepId: 'step2',+ iterationIndex: 0,+ })?.result,+ ).toBe('first');+ expect(+ getStepInfoHistoryItem({+ stepInfo,+ steps,+ stepId: 'step2',+ iterationIndex: 1,+ })?.result,+ ).toBe('second');+ expect(+ getStepInfoHistoryItem({+ stepInfo,+ steps,+ stepId: 'step2',+ iterationIndex: 2,+ })?.result,+ ).toBe('final');+ });++ it('returns the last history item for a non-descendant', () => {+ const stepInfo: WorkflowRunStepInfo = {+ result: 'final',+ status: StepStatus.SUCCESS,+ history: [+ { result: 'first', status: StepStatus.SUCCESS },+ { result: 'second', status: StepStatus.SUCCESS },+ ],+ };++ expect(+ getStepInfoHistoryItem({+ stepInfo,+ steps,+ stepId: 'iterator1',+ iterationIndex: 0,+ })?.result,+ ).toBe('final');+ });++ it('returns undefined if iterationIndex is out of bounds', () => {+ const stepInfo: WorkflowRunStepInfo = {+ result: 'final',+ status: StepStatus.SUCCESS,+ history: [{ result: 'first', status: StepStatus.SUCCESS }],+ };++ expect(+ getStepInfoHistoryItem({+ stepInfo,+ steps,+ stepId: 'step2',+ iterationIndex: 5,+ }),+ ).toBeUndefined();+ });++ it('returns the only item if no history', () => {+ const stepInfo: WorkflowRunStepInfo = {+ result: 'only',+ status: StepStatus.SUCCESS,+ };++ expect(+ getStepInfoHistoryItem({+ stepInfo,+ steps,+ stepId: 'step2',+ iterationIndex: 0,+ })?.result,+ ).toBe('only');+ });+});diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/utils/getIsDescendantOfIterator.ts b/packages/twenty-front/src/modules/workflow/workflow-steps/utils/getIsDescendantOfIterator.tsindex 32c4ce10f2b..580b7efc25c 100644--- a/packages/twenty-front/src/modules/workflow/workflow-steps/utils/getIsDescendantOfIterator.ts+++ b/packages/twenty-front/src/modules/workflow/workflow-steps/utils/getIsDescendantOfIterator.ts@@ -1,5 +1,6 @@import { type WorkflowStep } from '@/workflow/types/Workflow';import { isParentStep } from '@/workflow/workflow-diagram/utils/isParentStep';+import { TRIGGER_STEP_ID } from 'twenty-shared/workflow';export const getIsDescendantOfIterator = ({stepId,@@ -12,6 +13,10 @@ export const getIsDescendantOfIterator = ({currentStepId: string,visited = new Set<string>(),): boolean => {+ if (currentStepId === TRIGGER_STEP_ID) {+ return false;+ }+const currentStep = steps.find((step) => step.id === currentStepId);if (!currentStep) {throw new Error(`Step with ID ${currentStepId} not found`);diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/utils/getStepInfoHistoryItem.ts b/packages/twenty-front/src/modules/workflow/workflow-steps/utils/getStepInfoHistoryItem.tsnew file mode 100644index 00000000000..61bca23dac8--- /dev/null+++ b/packages/twenty-front/src/modules/workflow/workflow-steps/utils/getStepInfoHistoryItem.ts@@ -0,0 +1,26 @@+import { type WorkflowStep } from '@/workflow/types/Workflow';+import { type WorkflowRunStepInfo } from 'twenty-shared/workflow';+import { getIsDescendantOfIterator } from './getIsDescendantOfIterator';+import { getWorkflowRunAllStepInfoHistory } from './getWorkflowRunAllStepInfoHistory';++export const getStepInfoHistoryItem = ({+ stepInfo,+ steps,+ stepId,+ iterationIndex,+}: {+ stepInfo: WorkflowRunStepInfo;+ steps: WorkflowStep[];+ stepId: string;+ iterationIndex: number;+}) => {+ const allStepInfoHistory = getWorkflowRunAllStepInfoHistory({ stepInfo });++ const isDescendantOfIterator = getIsDescendantOfIterator({ steps, stepId });++ if (isDescendantOfIterator) {+ return allStepInfoHistory[iterationIndex];+ }++ return allStepInfoHistory.at(-1);+};