Skip to content

Commit

Permalink
feat: add checks for manual triggers validity and rename type to avai…
Browse files Browse the repository at this point in the history
…lability
  • Loading branch information
Devessier committed Oct 23, 2024
1 parent b800b03 commit 460d1fc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const WorkflowEditTriggerManualForm = ({
label="Available"
fullWidth
disabled={readonly}
value={trigger.settings.type}
value={trigger.settings.availability}
options={[
{
label: 'When record(s) are selected',
Expand Down Expand Up @@ -75,7 +75,7 @@ export const WorkflowEditTriggerManualForm = ({
}}
/>

{trigger.settings.type === 'WHEN_RECORD_SELECTED' ? (
{trigger.settings.availability === 'WHEN_RECORD_SELECTED' ? (
<Select
dropdownId={`${inputRootId}-object`}
label="Object"
Expand All @@ -91,7 +91,7 @@ export const WorkflowEditTriggerManualForm = ({
onTriggerUpdate({
...trigger,
settings: {
type: 'WHEN_RECORD_SELECTED',
availability: 'WHEN_RECORD_SELECTED',
objectType: updatedObject,
},
});
Expand Down
6 changes: 3 additions & 3 deletions packages/twenty-front/src/modules/workflow/types/Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ export type WorkflowManualTrigger = BaseTrigger & {
type: 'MANUAL';
settings:
| {
type: 'EVERYWHERE';
availability: 'EVERYWHERE';
}
| {
type: 'WHEN_RECORD_SELECTED';
availability: 'WHEN_RECORD_SELECTED';
objectType: string;
};
};

export type WorkflowManualTriggerSettings = WorkflowManualTrigger['settings'];

export type WorkflowManualTriggerAvailability =
WorkflowManualTrigger['settings']['type'];
WorkflowManualTrigger['settings']['availability'];

export type WorkflowTrigger =
| WorkflowDatabaseEventTrigger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export const getManualTriggerDefaultSettings = ({
switch (availability) {
case 'EVERYWHERE': {
return {
type: 'EVERYWHERE',
availability: 'EVERYWHERE',
};
}
case 'WHEN_RECORD_SELECTED': {
return {
type: 'WHEN_RECORD_SELECTED',
availability: 'WHEN_RECORD_SELECTED',
objectType: activeObjectMetadataItems[0].nameSingular,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ export type WorkflowManualTrigger = BaseTrigger & {
type: WorkflowTriggerType.MANUAL;
settings:
| {
type: WorkflowManualTriggerAvailability.EVERYWHERE;
availability: WorkflowManualTriggerAvailability.EVERYWHERE;
}
| {
type: WorkflowManualTriggerAvailability.WHEN_RECORD_SELECTED;
availability: WorkflowManualTriggerAvailability.WHEN_RECORD_SELECTED;
objectType: string;
};
};

export type WorkflowManualTriggerSettings = WorkflowManualTrigger['settings'];

export type WorkflowTrigger =
| WorkflowDatabaseEventTrigger
| WorkflowManualTrigger;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
WorkflowTriggerException,
WorkflowTriggerExceptionCode,
} from 'src/modules/workflow/workflow-trigger/exceptions/workflow-trigger.exception';
import { WorkflowTriggerType } from 'src/modules/workflow/workflow-trigger/types/workflow-trigger.type';
import {
WorkflowManualTriggerAvailability,
WorkflowManualTriggerSettings,
WorkflowTriggerType,
} from 'src/modules/workflow/workflow-trigger/types/workflow-trigger.type';

export function assertVersionCanBeActivated(
workflowVersion: WorkflowVersionWorkspaceEntity,
Expand Down Expand Up @@ -68,6 +72,9 @@ function assertTriggerSettingsAreValid(
case WorkflowTriggerType.DATABASE_EVENT:
assertDatabaseEventTriggerSettingsAreValid(settings);
break;
case WorkflowTriggerType.MANUAL:
assertManualTriggerSettingsAreValid(settings);
break;
default:
throw new WorkflowTriggerException(
'Invalid trigger type for enabling workflow trigger',
Expand All @@ -84,3 +91,29 @@ function assertDatabaseEventTriggerSettingsAreValid(settings: any) {
);
}
}

function assertManualTriggerSettingsAreValid(
settings: WorkflowManualTriggerSettings,
) {
switch (settings.availability) {
case WorkflowManualTriggerAvailability.EVERYWHERE: {
break;
}
case WorkflowManualTriggerAvailability.WHEN_RECORD_SELECTED: {
if (typeof settings.objectType !== 'string') {
throw new WorkflowTriggerException(
'No object type in manual trigger',
WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER,
);
}

break;
}
default: {
throw new WorkflowTriggerException(
'Invalid manual trigger availability',
WorkflowTriggerExceptionCode.INVALID_WORKFLOW_TRIGGER,
);
}
}
}

0 comments on commit 460d1fc

Please sign in to comment.