Skip to content

Commit

Permalink
Check workflow version is valid before publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Aug 20, 2024
1 parent db54469 commit a7b6aaa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export const workflowTriggerGraphqlApiExceptionHandler = (error: Error) => {
if (error instanceof WorkflowTriggerException) {
switch (error.code) {
case WorkflowTriggerExceptionCode.INVALID_INPUT:
throw new UserInputError(error.message);
case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER:
case WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION:
case WorkflowTriggerExceptionCode.INVALID_ACTION_TYPE:
throw new UserInputError(error.message);
default:
throw new InternalServerError(error.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 contains 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 database event 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 @@ -9,6 +9,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 @@ -47,6 +48,8 @@ export class WorkflowTriggerWorkspaceService {
workflowVersionId,
);

assertWorkflowVersionIsValid(workflowVersion);

switch (workflowVersion.trigger.type) {
case WorkflowTriggerType.DATABASE_EVENT:
await this.upsertEventListenerAndPublishVersion(
Expand All @@ -67,14 +70,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 a7b6aaa

Please sign in to comment.