Add enable workflow trigger endpoint (#6443)

Basic endpoint that only returns a boolean currently and overrides the
previous listener.
This commit is contained in:
Thomas Trompette
2024-07-30 14:00:37 +02:00
committed by GitHub
parent c449b4cbfb
commit ee14f25996
13 changed files with 279 additions and 9 deletions

View File

@ -0,0 +1,56 @@
import { Relation } from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/relation.interface';
import { FeatureFlagKeys } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { RelationMetadataType } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
import { WorkspaceEntity } from 'src/engine/twenty-orm/decorators/workspace-entity.decorator';
import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator';
import { WorkspaceGate } from 'src/engine/twenty-orm/decorators/workspace-gate.decorator';
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
import { WorkspaceIsSystem } from 'src/engine/twenty-orm/decorators/workspace-is-system.decorator';
import { WorkspaceJoinColumn } from 'src/engine/twenty-orm/decorators/workspace-join-column.decorator';
import { WorkspaceRelation } from 'src/engine/twenty-orm/decorators/workspace-relation.decorator';
import { WORKFLOW_EVENT_LISTENER_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/standard-objects/workflow.workspace-entity';
@WorkspaceEntity({
standardId: STANDARD_OBJECT_IDS.workflowEventListener,
namePlural: 'workflowEventListeners',
labelSingular: 'WorkflowEventListener',
labelPlural: 'WorkflowEventListeners',
description: 'A workflow event listener',
labelIdentifierStandardId:
WORKFLOW_EVENT_LISTENER_STANDARD_FIELD_IDS.eventName,
})
@WorkspaceGate({
featureFlag: FeatureFlagKeys.IsWorkflowEnabled,
})
@WorkspaceIsSystem()
export class WorkflowEventListenerWorkspaceEntity extends BaseWorkspaceEntity {
@WorkspaceField({
standardId: WORKFLOW_EVENT_LISTENER_STANDARD_FIELD_IDS.eventName,
type: FieldMetadataType.TEXT,
label: 'Name',
description: 'The workflow event listener name',
icon: 'IconPhoneCheck',
})
eventName: string;
// Relations
@WorkspaceRelation({
standardId: WORKFLOW_EVENT_LISTENER_STANDARD_FIELD_IDS.workflow,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Workflow',
description: 'WorkflowEventListener workflow',
icon: 'IconSettingsAutomation',
inverseSideTarget: () => WorkflowWorkspaceEntity,
inverseSideFieldKey: 'eventListeners',
})
@WorkspaceIsNullable()
workflow: Relation<WorkflowWorkspaceEntity>;
@WorkspaceJoinColumn('workflow')
workflowId: string;
}

View File

@ -0,0 +1,14 @@
import { CustomException } from 'src/utils/custom-exception';
export class WorkflowTriggerException extends CustomException {
code: WorkflowTriggerExceptionCode;
constructor(message: string, code: WorkflowTriggerExceptionCode) {
super(message, code);
}
}
export enum WorkflowTriggerExceptionCode {
INVALID_INPUT = 'INVALID_INPUT',
INVALID_WORKFLOW_TRIGGER = 'INVALID_WORKFLOW_TRIGGER',
INVALID_WORKFLOW_VERSION = 'INVALID_WORKFLOW_VERSION',
}

View File

@ -0,0 +1,99 @@
import { Injectable } from '@nestjs/common';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/standard-objects/workflow-event-listener.workspace-entity';
import {
WorkflowTriggerException,
WorkflowTriggerExceptionCode,
} from 'src/modules/workflow/standard-objects/workflow-trigger/workflow-trigger.exception';
import {
WorkflowDatabaseEventTrigger,
WorkflowTrigger,
WorkflowTriggerType,
WorkflowVersionWorkspaceEntity,
} from 'src/modules/workflow/standard-objects/workflow-version.workspace-entity';
@Injectable()
export class WorkflowTriggerService {
constructor(
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {}
async enableWorkflowTrigger(workspaceId: string, workflowVersionId: string) {
const workflowVersionRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowVersionWorkspaceEntity>(
workspaceId,
'workflowVersion',
);
const workflowVersion = await workflowVersionRepository.findOne({
where: {
id: workflowVersionId,
},
});
if (!workflowVersion) {
throw new WorkflowTriggerException(
'Workflow version not found',
WorkflowTriggerExceptionCode.INVALID_INPUT,
);
}
const trigger = workflowVersion.trigger as unknown as WorkflowTrigger;
if (!trigger || !trigger?.type) {
throw new WorkflowTriggerException(
'Workflow version does not contains trigger',
WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION,
);
}
switch (trigger.type) {
case WorkflowTriggerType.DATABASE_EVENT:
await this.upsertWorkflowEventListener(
workspaceId,
workflowVersion.workflowId,
trigger,
);
break;
default:
break;
}
return true;
}
private async upsertWorkflowEventListener(
workspaceId: string,
workflowId: 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 workflowEventListenerRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowEventListenerWorkspaceEntity>(
workspaceId,
'workflowEventListener',
);
// TODO: Use upsert when available for workspace entities
await workflowEventListenerRepository.delete({
workflowId,
eventName,
});
const workflowEventListener = await workflowEventListenerRepository.create({
workflowId,
eventName,
});
await workflowEventListenerRepository.save(workflowEventListener);
}
}

View File

@ -15,6 +15,20 @@ import { WORKFLOW_VERSION_STANDARD_FIELD_IDS } from 'src/engine/workspace-manage
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/standard-objects/workflow.workspace-entity';
export enum WorkflowTriggerType {
DATABASE_EVENT = 'DATABASE_EVENT',
}
export type WorkflowDatabaseEventTrigger = {
type: WorkflowTriggerType.DATABASE_EVENT;
settings: {
eventName: string;
triggerName: string;
};
};
export type WorkflowTrigger = WorkflowDatabaseEventTrigger;
@WorkspaceEntity({
standardId: STANDARD_OBJECT_IDS.workflowVersion,
namePlural: 'workflowVersions',

View File

@ -19,6 +19,7 @@ import { ActivityTargetWorkspaceEntity } from 'src/modules/activity/standard-obj
import { AttachmentWorkspaceEntity } from 'src/modules/attachment/standard-objects/attachment.workspace-entity';
import { FavoriteWorkspaceEntity } from 'src/modules/favorite/standard-objects/favorite.workspace-entity';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/standard-objects/workflow-event-listener.workspace-entity';
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/standard-objects/workflow-version.workspace-entity';
@WorkspaceEntity({
@ -77,6 +78,18 @@ export class WorkflowWorkspaceEntity extends BaseWorkspaceEntity {
@WorkspaceIsNullable()
versions: Relation<WorkflowVersionWorkspaceEntity[]>;
@WorkspaceRelation({
standardId: WORKFLOW_STANDARD_FIELD_IDS.eventListeners,
type: RelationMetadataType.ONE_TO_MANY,
label: 'Event Listeners',
description: 'Workflow event listeners linked to the workflow.',
icon: 'IconVersions',
inverseSideTarget: () => WorkflowEventListenerWorkspaceEntity,
onDelete: RelationOnDeleteAction.SET_NULL,
})
@WorkspaceIsNullable()
eventListeners: Relation<WorkflowEventListenerWorkspaceEntity[]>;
@WorkspaceRelation({
standardId: WORKFLOW_STANDARD_FIELD_IDS.activityTargets,
type: RelationMetadataType.ONE_TO_MANY,