The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Validate the Delay step duration like Wait for Event (#27556)
Félix Malfait4e3e53767f7e
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/compute-workflow-duration-in-ms.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/compute-workflow-duration-in-ms.util.tsnew file mode 100644index 00000000000..b96ec751816--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/compute-workflow-duration-in-ms.util.ts@@ -0,0 +1,45 @@+import {+ WorkflowStepExecutorException,+ WorkflowStepExecutorExceptionCode,+} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';++const MAX_DURATION_IN_MS = 365 * 24 * 60 * 60 * 1000;++// Variables resolve duration parts to strings, and an empty part means none of that unit+export const computeWorkflowDurationInMs = ({+ days,+ hours,+ minutes,+ seconds,+}: {+ days?: number | string;+ hours?: number | string;+ minutes?: number | string;+ seconds?: number | string;+}): number => {+ const parts = [days, hours, minutes, seconds].map((part) =>+ Number(part || 0),+ );++ // a duration that cannot be read must fail the step, not schedule an invalid date+ if (parts.some((part) => !Number.isFinite(part) || part < 0)) {+ throw new WorkflowStepExecutorException(+ 'Duration must be made of non-negative numbers',+ WorkflowStepExecutorExceptionCode.INVALID_STEP_INPUT,+ );+ }++ const [dayCount, hourCount, minuteCount, secondCount] = parts;+ const durationInMs =+ (((dayCount * 24 + hourCount) * 60 + minuteCount) * 60 + secondCount) *+ 1000;++ if (durationInMs > MAX_DURATION_IN_MS) {+ throw new WorkflowStepExecutorException(+ 'Duration cannot exceed one year',+ WorkflowStepExecutorExceptionCode.INVALID_STEP_INPUT,+ );+ }++ return durationInMs;+};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/delay/__tests__/delay.workflow-action.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/delay/__tests__/delay.workflow-action.spec.tsnew file mode 100644index 00000000000..7de56a6f31c--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/delay/__tests__/delay.workflow-action.spec.ts@@ -0,0 +1,68 @@+import { WorkflowActionType } from 'twenty-shared/workflow';++import { WorkflowStepExecutorExceptionCode } from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';+import { DelayWorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/delay/delay.workflow-action';++const NOW = new Date('2026-10-04T10:00:00.000Z');++const executeWithInput = (input: Record<string, unknown>) =>+ new DelayWorkflowAction().execute({+ currentStepId: 'step-id',+ steps: [+ {+ id: 'step-id',+ name: 'Delay',+ type: WorkflowActionType.DELAY,+ valid: true,+ settings: {+ input,+ outputSchema: {},+ errorHandlingOptions: {+ continueOnFailure: { value: false },+ retryOnFailure: { value: false },+ },+ },+ },+ ] as never,+ context: { trigger: { object: { waitHours: 'soon' } } },+ runInfo: { workflowRunId: 'run-id', workspaceId: 'workspace-id' },+ });++describe('DelayWorkflowAction', () => {+ beforeEach(() => {+ jest.useFakeTimers().setSystemTime(NOW);+ });++ afterEach(() => {+ jest.useRealTimers();+ });++ it('resumes after its duration', async () => {+ await expect(+ executeWithInput({+ delayType: 'DURATION',+ duration: { days: 1, hours: 2, minutes: 3, seconds: 4 },+ }),+ ).resolves.toEqual({+ wait: { type: 'TIME', resumeAt: '2026-10-05T12:03:04.000Z' },+ });+ });++ it('fails as an input error when its duration is not a number', async () => {+ await expect(+ executeWithInput({+ delayType: 'DURATION',+ duration: { hours: '{{trigger.object.waitHours}}' },+ }),+ ).rejects.toMatchObject({+ message: 'Duration must be made of non-negative numbers',+ code: WorkflowStepExecutorExceptionCode.INVALID_STEP_INPUT,+ });+ });++ it('refuses a duration too long to schedule', async () => {+ await expect(+ executeWithInput({ delayType: 'DURATION', duration: { days: 366 } }),+ ).rejects.toThrow('Duration cannot exceed one year');+ });+});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/delay/delay.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/delay/delay.workflow-action.tsindex 8d7cc01cf54..b7696153383 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/delay/delay.workflow-action.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/delay/delay.workflow-action.ts@@ -10,6 +10,7 @@ import {} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input.type';import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';+import { computeWorkflowDurationInMs } from 'src/modules/workflow/workflow-executor/utils/compute-workflow-duration-in-ms.util';import { findStepOrThrow } from 'src/modules/workflow/workflow-executor/utils/find-step-or-throw.util';import { isWorkflowDelayAction } from 'src/modules/workflow/workflow-executor/workflow-actions/delay/guards/is-workflow-delay-action.guard';import { WorkflowDelayActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/delay/types/workflow-delay-action-input.type';@@ -67,18 +68,7 @@ export class DelayWorkflowAction implements WorkflowAction {);}- const {- days = 0,- hours = 0,- minutes = 0,- seconds = 0,- } = workflowActionInput.duration;-- delayInMs =- days * 24 * 60 * 60 * 1000 +- hours * 60 * 60 * 1000 +- minutes * 60 * 1000 +- seconds * 1000;+ delayInMs = computeWorkflowDurationInMs(workflowActionInput.duration);} else {throw new WorkflowStepExecutorException('Invalid delay type',diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/__tests__/wait-for-event.workflow-action.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/__tests__/wait-for-event.workflow-action.spec.tsindex 4188670dba7..10d8defb804 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/__tests__/wait-for-event.workflow-action.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/__tests__/wait-for-event.workflow-action.spec.ts@@ -83,7 +83,7 @@ describe('WaitForEventWorkflowAction', () => {eventName: 'company.updated',timeout: { hours: 'soon' },}),- ).rejects.toThrow('Wait timeout must be made of non-negative numbers');+ ).rejects.toThrow('Duration must be made of non-negative numbers');});it('refuses a timeout too long to schedule', async () => {@@ -92,7 +92,7 @@ describe('WaitForEventWorkflowAction', () => {eventName: 'company.updated',timeout: { days: 100000000 },}),- ).rejects.toThrow('Wait timeout cannot exceed one year');+ ).rejects.toThrow('Duration cannot exceed one year');});it('refuses an event on an object name that cannot exist', async () => {diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/utils/compute-wait-timeout-in-ms.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/utils/compute-wait-timeout-in-ms.util.tsdeleted file mode 100644index 0a5f38d82d6..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/utils/compute-wait-timeout-in-ms.util.ts+++ /dev/null@@ -1,45 +0,0 @@-import { isDefined } from 'twenty-shared/utils';--import {- WorkflowStepExecutorException,- WorkflowStepExecutorExceptionCode,-} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';-import { type WorkflowWaitForEventActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/types/workflow-wait-for-event-action-input.type';--const MS_PER_MINUTE = 60 * 1000;--const MAX_TIMEOUT_IN_MS = 365 * 24 * 60 * MS_PER_MINUTE;--// Variables resolve timeout parts to strings, and an empty part means none of that unit-export const computeWaitTimeoutInMs = (- timeout: WorkflowWaitForEventActionInput['timeout'],-): number | null => {- if (!isDefined(timeout)) {- return null;- }-- const days = Number(timeout.days || 0);- const hours = Number(timeout.hours || 0);- const minutes = Number(timeout.minutes || 0);-- // a timeout that cannot be read must not turn into a wait without a deadline- if (- [days, hours, minutes].some((value) => !Number.isFinite(value) || value < 0)- ) {- throw new WorkflowStepExecutorException(- 'Wait timeout must be made of non-negative numbers',- WorkflowStepExecutorExceptionCode.INVALID_STEP_INPUT,- );- }-- const timeoutInMs = ((days * 24 + hours) * 60 + minutes) * MS_PER_MINUTE;-- if (timeoutInMs > MAX_TIMEOUT_IN_MS) {- throw new WorkflowStepExecutorException(- 'Wait timeout cannot exceed one year',- WorkflowStepExecutorExceptionCode.INVALID_STEP_INPUT,- );- }-- return timeoutInMs > 0 ? timeoutInMs : null;-};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/wait-for-event.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/wait-for-event.workflow-action.tsindex c4720d0ebe8..246f0507099 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/wait-for-event.workflow-action.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/wait-for-event.workflow-action.ts@@ -11,11 +11,11 @@ import {} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input.type';import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';+import { computeWorkflowDurationInMs } from 'src/modules/workflow/workflow-executor/utils/compute-workflow-duration-in-ms.util';import { findStepOrThrow } from 'src/modules/workflow/workflow-executor/utils/find-step-or-throw.util';import { WAIT_FOR_EVENT_NAME_PATTERN } from 'src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/constants/wait-for-event-name-pattern.constant';import { isWorkflowWaitForEventAction } from 'src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/guards/is-workflow-wait-for-event-action.guard';import { type WorkflowWaitForEventActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/types/workflow-wait-for-event-action-input.type';-import { computeWaitTimeoutInMs } from 'src/modules/workflow/workflow-executor/workflow-actions/wait-for-event/utils/compute-wait-timeout-in-ms.util';@Injectable()export class WaitForEventWorkflowAction implements WorkflowAction {@@ -59,7 +59,9 @@ export class WaitForEventWorkflowAction implements WorkflowAction {);}- const timeoutInMs = computeWaitTimeoutInMs(timeout);+ const timeoutInMs = isDefined(timeout)+ ? computeWorkflowDurationInMs(timeout)+ : 0;return {wait: {@@ -67,7 +69,7 @@ export class WaitForEventWorkflowAction implements WorkflowAction {eventName,...(isNonEmptyString(recordId) ? { recordId } : {}),...(isNonEmptyArray(updatedFields) ? { updatedFields } : {}),- ...(isDefined(timeoutInMs)+ ...(timeoutInMs > 0? { expiresAt: new Date(Date.now() + timeoutInMs).toISOString() }: {}),},