Skip to content

Commit

Permalink
Check workflow version is valid before publishing (twentyhq#6702)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp authored Aug 21, 2024
1 parent 663acd5 commit be50a62
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WorkflowVersionWorkspaceEntity, 'trigger'> & {
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,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -67,6 +68,8 @@ export class WorkflowTriggerWorkspaceService {
workflowVersionId,
);

assertWorkflowVersionIsValid(workflowVersion);

switch (workflowVersion.trigger.type) {
case WorkflowTriggerType.DATABASE_EVENT:
await this.upsertEventListenerAndPublishVersion(
Expand All @@ -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<WorkflowEventListenerWorkspaceEntity>(
Expand Down

0 comments on commit be50a62

Please sign in to comment.