From be50a6256ff5e1909ee9d5f0cea5256df30a20be Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Wed, 21 Aug 2024 17:56:33 +0200 Subject: [PATCH] Check workflow version is valid before publishing (#6702) Fix https://github.com/twentyhq/twenty/issues/6670 --- ...ow-trigger-graphql-api-exception.filter.ts | 4 +- .../workflow-common.workspace-service.ts | 2 +- .../utils/assert-workflow-version-is-valid.ts | 66 +++++++++++++++++++ .../workflow-trigger.workspace-service.ts | 12 ++-- 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 packages/twenty-server/src/modules/workflow/workflow-trigger/utils/assert-workflow-version-is-valid.ts diff --git a/packages/twenty-server/src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter.ts b/packages/twenty-server/src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter.ts index 13edf52a99ee..bc707d5500ce 100644 --- a/packages/twenty-server/src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter.ts +++ b/packages/twenty-server/src/engine/core-modules/workflow/filters/workflow-trigger-graphql-api-exception.filter.ts @@ -16,10 +16,10 @@ export class WorkflowTriggerGraphqlApiExceptionFilter catch(exception: WorkflowTriggerException) { switch (exception.code) { case WorkflowTriggerExceptionCode.INVALID_INPUT: - throw new UserInputError(exception.message); - case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER: case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION: case WorkflowTriggerExceptionCode.INVALID_ACTION_TYPE: + case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER: + throw new UserInputError(exception.message); default: throw new InternalServerError(exception.message); } diff --git a/packages/twenty-server/src/modules/workflow/common/workflow-common.workspace-service.ts b/packages/twenty-server/src/modules/workflow/common/workflow-common.workspace-service.ts index 3db70e89c0d5..a975ecd42afe 100644 --- a/packages/twenty-server/src/modules/workflow/common/workflow-common.workspace-service.ts +++ b/packages/twenty-server/src/modules/workflow/common/workflow-common.workspace-service.ts @@ -35,7 +35,7 @@ export class WorkflowCommonWorkspaceService { ); } - if (!workflowVersion.trigger || !workflowVersion.trigger?.type) { + if (!workflowVersion.trigger) { throw new WorkflowTriggerException( 'Workflow version does not contains trigger', WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION, diff --git a/packages/twenty-server/src/modules/workflow/workflow-trigger/utils/assert-workflow-version-is-valid.ts b/packages/twenty-server/src/modules/workflow/workflow-trigger/utils/assert-workflow-version-is-valid.ts new file mode 100644 index 000000000000..d38cc8034460 --- /dev/null +++ b/packages/twenty-server/src/modules/workflow/workflow-trigger/utils/assert-workflow-version-is-valid.ts @@ -0,0 +1,66 @@ +import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity'; +import { + WorkflowTrigger, + WorkflowTriggerType, +} from 'src/modules/workflow/common/types/workflow-trigger.type'; +import { + WorkflowTriggerException, + WorkflowTriggerExceptionCode, +} from 'src/modules/workflow/workflow-trigger/workflow-trigger.exception'; + +export function assertWorkflowVersionIsValid( + workflowVersion: Omit & { + trigger: WorkflowTrigger; + }, +) { + if (!workflowVersion.trigger) { + throw new WorkflowTriggerException( + 'Workflow version does not contain trigger', + WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION, + ); + } + + if (!workflowVersion.trigger.type) { + throw new WorkflowTriggerException( + 'No trigger type provided', + WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER, + ); + } + + if (!workflowVersion.trigger.nextAction) { + throw new WorkflowTriggerException( + 'No next action provided in trigger', + WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER, + ); + } + + assertTriggerSettingsAreValid( + workflowVersion.trigger.type, + workflowVersion.trigger.settings, + ); +} + +function assertTriggerSettingsAreValid( + triggerType: WorkflowTriggerType, + settings: any, +) { + switch (triggerType) { + case WorkflowTriggerType.DATABASE_EVENT: + assertDatabaseEventTriggerSettingsAreValid(settings); + break; + default: + throw new WorkflowTriggerException( + 'Invalid trigger type for enabling workflow trigger', + WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER, + ); + } +} + +function assertDatabaseEventTriggerSettingsAreValid(settings: any) { + if (!settings?.eventName) { + throw new WorkflowTriggerException( + 'No event name provided in database event trigger', + WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER, + ); + } +} diff --git a/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service.ts index 54079003ea7d..a7782753028e 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service.ts @@ -12,6 +12,7 @@ import { } from 'src/modules/workflow/common/types/workflow-trigger.type'; import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service'; import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service'; +import { assertWorkflowVersionIsValid } from 'src/modules/workflow/workflow-trigger/utils/assert-workflow-version-is-valid'; import { WorkflowTriggerException, WorkflowTriggerExceptionCode, @@ -67,6 +68,8 @@ export class WorkflowTriggerWorkspaceService { workflowVersionId, ); + assertWorkflowVersionIsValid(workflowVersion); + switch (workflowVersion.trigger.type) { case WorkflowTriggerType.DATABASE_EVENT: await this.upsertEventListenerAndPublishVersion( @@ -87,14 +90,7 @@ export class WorkflowTriggerWorkspaceService { workflowVersionId: string, trigger: WorkflowDatabaseEventTrigger, ) { - const eventName = trigger?.settings?.eventName; - - if (!eventName) { - throw new WorkflowTriggerException( - 'No event name provided in database event trigger', - WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER, - ); - } + const eventName = trigger.settings.eventName; const workflowEventListenerRepository = await this.twentyORMManager.getRepository(