Trigger workflow run manually (#6696)

Fix https://github.com/twentyhq/twenty/issues/6669

- create a commun function `startWorkflowRun` that both create the run
object and the job for executing the workflow
- use it in both the `workflowEventJob` and the `runWorkflowVersion`
endpoint

Bonus:
- use filtering for exceptions instead of a util. It avoids doing a try
catch in all endpoint
This commit is contained in:
Thomas Trompette
2024-08-21 17:41:26 +02:00
committed by GitHub
parent da5dfb7a5b
commit 663acd56e4
43 changed files with 452 additions and 316 deletions

View File

@ -0,0 +1,12 @@
import { CustomException } from 'src/utils/custom-exception';
export class WorkflowActionExecutorException extends CustomException {
code: WorkflowActionExecutorExceptionCode;
constructor(message: string, code: WorkflowActionExecutorExceptionCode) {
super(message, code);
}
}
export enum WorkflowActionExecutorExceptionCode {
SCOPED_WORKSPACE_NOT_FOUND = 'SCOPED_WORKSPACE_NOT_FOUND',
}

View File

@ -0,0 +1,23 @@
import { Injectable } from '@nestjs/common';
import { WorkflowActionType } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowActionExecutor } from 'src/modules/workflow/workflow-action-executor/workflow-action-executor.interface';
import { CodeWorkflowActionExecutor } from 'src/modules/workflow/workflow-action-executor/workflow-action-executors/code-workflow-action-executor';
@Injectable()
export class WorkflowActionExecutorFactory {
constructor(
private readonly codeWorkflowActionExecutor: CodeWorkflowActionExecutor,
) {}
get(actionType: WorkflowActionType): WorkflowActionExecutor {
switch (actionType) {
case WorkflowActionType.CODE:
return this.codeWorkflowActionExecutor;
default:
throw new Error(
`Workflow action executor not found for action type '${actionType}'`,
);
}
}
}

View File

@ -1,7 +1,7 @@
import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type';
export interface WorkflowActionRunner {
export interface WorkflowActionExecutor {
execute({
action,
payload,

View File

@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { ServerlessFunctionModule } from 'src/engine/metadata-modules/serverless-function/serverless-function.module';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowActionExecutorFactory } from 'src/modules/workflow/workflow-action-executor/workflow-action-executor.factory';
import { CodeWorkflowActionExecutor } from 'src/modules/workflow/workflow-action-executor/workflow-action-executors/code-workflow-action-executor';
@Module({
imports: [ServerlessFunctionModule],
providers: [
WorkflowActionExecutorFactory,
CodeWorkflowActionExecutor,
ScopedWorkspaceContextFactory,
],
exports: [WorkflowActionExecutorFactory],
})
export class WorkflowActionExecutorModule {}

View File

@ -5,13 +5,13 @@ import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/s
import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type';
import {
WorkflowActionRunnerException,
WorkflowActionRunnerExceptionCode,
} from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.exception';
import { WorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.interface';
WorkflowActionExecutorException,
WorkflowActionExecutorExceptionCode,
} from 'src/modules/workflow/workflow-action-executor/workflow-action-executor.exception';
import { WorkflowActionExecutor } from 'src/modules/workflow/workflow-action-executor/workflow-action-executor.interface';
@Injectable()
export class CodeWorkflowActionRunner implements WorkflowActionRunner {
export class CodeWorkflowActionExecutor implements WorkflowActionExecutor {
constructor(
private readonly serverlessFunctionService: ServerlessFunctionService,
private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory,
@ -27,9 +27,9 @@ export class CodeWorkflowActionRunner implements WorkflowActionRunner {
const { workspaceId } = this.scopedWorkspaceContextFactory.create();
if (!workspaceId) {
throw new WorkflowActionRunnerException(
throw new WorkflowActionExecutorException(
'Scoped workspace not found',
WorkflowActionRunnerExceptionCode.SCOPED_WORKSPACE_NOT_FOUND,
WorkflowActionExecutorExceptionCode.SCOPED_WORKSPACE_NOT_FOUND,
);
}

View File

@ -1,12 +0,0 @@
import { CustomException } from 'src/utils/custom-exception';
export class WorkflowActionRunnerException extends CustomException {
code: WorkflowActionRunnerExceptionCode;
constructor(message: string, code: WorkflowActionRunnerExceptionCode) {
super(message, code);
}
}
export enum WorkflowActionRunnerExceptionCode {
SCOPED_WORKSPACE_NOT_FOUND = 'SCOPED_WORKSPACE_NOT_FOUND',
}

View File

@ -1,23 +0,0 @@
import { Injectable } from '@nestjs/common';
import { CodeWorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runners/code-workflow-action-runner';
import { WorkflowActionType } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.interface';
@Injectable()
export class WorkflowActionRunnerFactory {
constructor(
private readonly codeWorkflowActionRunner: CodeWorkflowActionRunner,
) {}
get(actionType: WorkflowActionType): WorkflowActionRunner {
switch (actionType) {
case WorkflowActionType.CODE:
return this.codeWorkflowActionRunner;
default:
throw new Error(
`Workflow action executor not found for action type '${actionType}'`,
);
}
}
}

View File

@ -1,17 +0,0 @@
import { Module } from '@nestjs/common';
import { ServerlessFunctionModule } from 'src/engine/metadata-modules/serverless-function/serverless-function.module';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowActionRunnerFactory } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.factory';
import { CodeWorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runners/code-workflow-action-runner';
@Module({
imports: [ServerlessFunctionModule],
providers: [
WorkflowActionRunnerFactory,
CodeWorkflowActionRunner,
ScopedWorkspaceContextFactory,
],
exports: [WorkflowActionRunnerFactory],
})
export class WorkflowActionRunnerModule {}

View File

@ -1,11 +1,11 @@
import { CustomException } from 'src/utils/custom-exception';
export class WorkflowRunnerException extends CustomException {
export class WorkflowExecutorException extends CustomException {
constructor(message: string, code: string) {
super(message, code);
}
}
export enum WorkflowRunnerExceptionCode {
export enum WorkflowExecutorExceptionCode {
WORKFLOW_FAILED = 'WORKFLOW_FAILED',
}

View File

@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { WorkflowActionExecutorModule } from 'src/modules/workflow/workflow-action-executor/workflow-action-executor.module';
import { WorkflowExecutorWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-executor.workspace-service';
@Module({
imports: [WorkflowCommonModule, WorkflowActionExecutorModule],
providers: [WorkflowExecutorWorkspaceService],
exports: [WorkflowExecutorWorkspaceService],
})
export class WorkflowExecutorModule {}

View File

@ -0,0 +1,84 @@
import { Injectable } from '@nestjs/common';
import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowActionExecutorFactory } from 'src/modules/workflow/workflow-action-executor/workflow-action-executor.factory';
import {
WorkflowExecutorException,
WorkflowExecutorExceptionCode,
} from 'src/modules/workflow/workflow-executor/workflow-executor.exception';
const MAX_RETRIES_ON_FAILURE = 3;
export type WorkflowExecutionOutput = {
data?: object;
error?: object;
};
@Injectable()
export class WorkflowExecutorWorkspaceService {
constructor(
private readonly workflowActionExecutorFactory: WorkflowActionExecutorFactory,
) {}
async execute({
action,
payload,
attemptCount = 1,
}: {
action?: WorkflowAction;
payload?: object;
attemptCount?: number;
}): Promise<WorkflowExecutionOutput> {
if (!action) {
return {
data: payload,
};
}
const workflowActionExecutor = this.workflowActionExecutorFactory.get(
action.type,
);
const result = await workflowActionExecutor.execute({
action,
payload,
});
if (result.data) {
return await this.execute({
action: action.nextAction,
payload: result.data,
});
}
if (!result.error) {
throw new WorkflowExecutorException(
'Execution result error, no data or error',
WorkflowExecutorExceptionCode.WORKFLOW_FAILED,
);
}
if (action.settings.errorHandlingOptions.continueOnFailure.value) {
return await this.execute({
action: action.nextAction,
payload,
});
}
if (
action.settings.errorHandlingOptions.retryOnFailure.value &&
attemptCount < MAX_RETRIES_ON_FAILURE
) {
return await this.execute({
action,
payload,
attemptCount: attemptCount + 1,
});
}
throw new WorkflowExecutorException(
`Workflow failed: ${result.error}`,
WorkflowExecutorExceptionCode.WORKFLOW_FAILED,
);
}
}

View File

@ -5,8 +5,8 @@ import { Processor } from 'src/engine/integrations/message-queue/decorators/proc
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';
import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service';
import { WorkflowStatusWorkspaceService } from 'src/modules/workflow/workflow-status/workflow-status.workspace-service';
import { WorkflowExecutorWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-executor.workspace-service';
import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';
export type RunWorkflowJobData = {
workspaceId: string;
@ -16,20 +16,20 @@ export type RunWorkflowJobData = {
};
@Processor({ queueName: MessageQueue.workflowQueue, scope: Scope.REQUEST })
export class WorkflowRunnerJob {
export class RunWorkflowJob {
constructor(
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService,
private readonly workflowStatusWorkspaceService: WorkflowStatusWorkspaceService,
private readonly workflowExecutorWorkspaceService: WorkflowExecutorWorkspaceService,
private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,
) {}
@Process(WorkflowRunnerJob.name)
@Process(RunWorkflowJob.name)
async handle({
workflowVersionId,
workflowRunId,
payload,
}: RunWorkflowJobData): Promise<void> {
await this.workflowStatusWorkspaceService.startWorkflowRun(workflowRunId);
await this.workflowRunWorkspaceService.startWorkflowRun(workflowRunId);
const workflowVersion =
await this.workflowCommonWorkspaceService.getWorkflowVersion(
@ -37,17 +37,17 @@ export class WorkflowRunnerJob {
);
try {
await this.workflowRunnerWorkspaceService.run({
await this.workflowExecutorWorkspaceService.execute({
action: workflowVersion.trigger.nextAction,
payload,
});
await this.workflowStatusWorkspaceService.endWorkflowRun(
await this.workflowRunWorkspaceService.endWorkflowRun(
workflowRunId,
WorkflowRunStatus.COMPLETED,
);
} catch (error) {
await this.workflowStatusWorkspaceService.endWorkflowRun(
await this.workflowRunWorkspaceService.endWorkflowRun(
workflowRunId,
WorkflowRunStatus.FAILED,
);

View File

@ -0,0 +1,13 @@
import { CustomException } from 'src/utils/custom-exception';
export class WorkflowRunException extends CustomException {
code: WorkflowRunExceptionCode;
constructor(message: string, code: WorkflowRunExceptionCode) {
super(message, code);
}
}
export enum WorkflowRunExceptionCode {
WORKFLOW_RUN_NOT_FOUND = 'WORKFLOW_RUN_NOT_FOUND',
INVALID_OPERATION = 'INVALID_OPERATION',
}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';
@Module({
providers: [WorkflowRunWorkspaceService],
exports: [WorkflowRunWorkspaceService],
})
export class WorkflowRunModule {}

View File

@ -7,12 +7,12 @@ import {
WorkflowRunWorkspaceEntity,
} from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import {
WorkflowStatusException,
WorkflowStatusExceptionCode,
} from 'src/modules/workflow/workflow-status/workflow-status.exception';
WorkflowRunException,
WorkflowRunExceptionCode,
} from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.exception';
@Injectable()
export class WorkflowStatusWorkspaceService {
export class WorkflowRunWorkspaceService {
constructor(private readonly twentyORMManager: TwentyORMManager) {}
async createWorkflowRun(workflowVersionId: string, createdBy: ActorMetadata) {
@ -41,16 +41,16 @@ export class WorkflowStatusWorkspaceService {
});
if (!workflowRunToUpdate) {
throw new WorkflowStatusException(
throw new WorkflowRunException(
'No workflow run to start',
WorkflowStatusExceptionCode.WORKFLOW_RUN_NOT_FOUND,
WorkflowRunExceptionCode.WORKFLOW_RUN_NOT_FOUND,
);
}
if (workflowRunToUpdate.status !== WorkflowRunStatus.NOT_STARTED) {
throw new WorkflowStatusException(
throw new WorkflowRunException(
'Workflow run already started',
WorkflowStatusExceptionCode.INVALID_OPERATION,
WorkflowRunExceptionCode.INVALID_OPERATION,
);
}
@ -71,16 +71,16 @@ export class WorkflowStatusWorkspaceService {
});
if (!workflowRunToUpdate) {
throw new WorkflowStatusException(
throw new WorkflowRunException(
'No workflow run to end',
WorkflowStatusExceptionCode.WORKFLOW_RUN_NOT_FOUND,
WorkflowRunExceptionCode.WORKFLOW_RUN_NOT_FOUND,
);
}
if (workflowRunToUpdate.status !== WorkflowRunStatus.RUNNING) {
throw new WorkflowStatusException(
throw new WorkflowRunException(
'Workflow cannot be ended as it is not running',
WorkflowStatusExceptionCode.INVALID_OPERATION,
WorkflowRunExceptionCode.INVALID_OPERATION,
);
}

View File

@ -1,18 +1,14 @@
import { Module } from '@nestjs/common';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { WorkflowActionRunnerModule } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.module';
import { WorkflowRunnerJob } from 'src/modules/workflow/workflow-runner/workflow-runner.job';
import { WorkflowExecutorModule } from 'src/modules/workflow/workflow-executor/workflow-executor.module';
import { RunWorkflowJob } from 'src/modules/workflow/workflow-runner/jobs/run-workflow.job';
import { WorkflowRunModule } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.module';
import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service';
import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workflow-status.module';
@Module({
imports: [
WorkflowCommonModule,
WorkflowActionRunnerModule,
WorkflowStatusModule,
],
providers: [WorkflowRunnerWorkspaceService, WorkflowRunnerJob],
imports: [WorkflowRunModule, WorkflowCommonModule, WorkflowExecutorModule],
providers: [WorkflowRunnerWorkspaceService, RunWorkflowJob],
exports: [WorkflowRunnerWorkspaceService],
})
export class WorkflowRunnerModule {}

View File

@ -1,84 +1,45 @@
import { Injectable } from '@nestjs/common';
import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type';
import { WorkflowActionRunnerFactory } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.factory';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { ActorMetadata } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
import {
WorkflowRunnerException,
WorkflowRunnerExceptionCode,
} from 'src/modules/workflow/workflow-runner/workflow-runner.exception';
const MAX_RETRIES_ON_FAILURE = 3;
export type WorkflowRunOutput = {
data?: object;
error?: object;
};
RunWorkflowJob,
RunWorkflowJobData,
} from 'src/modules/workflow/workflow-runner/jobs/run-workflow.job';
import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';
@Injectable()
export class WorkflowRunnerWorkspaceService {
constructor(
private readonly workflowActionRunnerFactory: WorkflowActionRunnerFactory,
private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,
@InjectMessageQueue(MessageQueue.workflowQueue)
private readonly messageQueueService: MessageQueueService,
) {}
async run({
action,
payload,
attemptCount = 1,
}: {
action?: WorkflowAction;
payload?: object;
attemptCount?: number;
}): Promise<WorkflowRunOutput> {
if (!action) {
return {
data: payload,
};
}
const workflowActionRunner = this.workflowActionRunnerFactory.get(
action.type,
);
const result = await workflowActionRunner.execute({
action,
payload,
});
if (result.data) {
return await this.run({
action: action.nextAction,
payload: result.data,
});
}
if (!result.error) {
throw new WorkflowRunnerException(
'Execution result error, no data or error',
WorkflowRunnerExceptionCode.WORKFLOW_FAILED,
async run(
workspaceId: string,
workflowVersionId: string,
payload: object,
source: ActorMetadata,
) {
const workflowRunId =
await this.workflowRunWorkspaceService.createWorkflowRun(
workflowVersionId,
source,
);
}
if (action.settings.errorHandlingOptions.continueOnFailure.value) {
return await this.run({
action: action.nextAction,
payload,
});
}
if (
action.settings.errorHandlingOptions.retryOnFailure.value &&
attemptCount < MAX_RETRIES_ON_FAILURE
) {
return await this.run({
action,
payload,
attemptCount: attemptCount + 1,
});
}
throw new WorkflowRunnerException(
`Workflow failed: ${result.error}`,
WorkflowRunnerExceptionCode.WORKFLOW_FAILED,
await this.messageQueueService.add<RunWorkflowJobData>(
RunWorkflowJob.name,
{
workspaceId,
workflowVersionId,
payload: payload,
workflowRunId,
},
);
return { workflowRunId };
}
}

View File

@ -1,13 +0,0 @@
import { CustomException } from 'src/utils/custom-exception';
export class WorkflowStatusException extends CustomException {
code: WorkflowStatusExceptionCode;
constructor(message: string, code: WorkflowStatusExceptionCode) {
super(message, code);
}
}
export enum WorkflowStatusExceptionCode {
WORKFLOW_RUN_NOT_FOUND = 'WORKFLOW_RUN_NOT_FOUND',
INVALID_OPERATION = 'INVALID_OPERATION',
}

View File

@ -1,9 +0,0 @@
import { Module } from '@nestjs/common';
import { WorkflowStatusWorkspaceService } from 'src/modules/workflow/workflow-status/workflow-status.workspace-service';
@Module({
providers: [WorkflowStatusWorkspaceService],
exports: [WorkflowStatusWorkspaceService],
})
export class WorkflowStatusModule {}

View File

@ -1,18 +1,16 @@
import { Scope } from '@nestjs/common';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service';
import {
RunWorkflowJobData,
WorkflowRunnerJob,
} from 'src/modules/workflow/workflow-runner/workflow-runner.job';
import { WorkflowStatusWorkspaceService } from 'src/modules/workflow/workflow-status/workflow-status.workspace-service';
WorkflowTriggerException,
WorkflowTriggerExceptionCode,
} from 'src/modules/workflow/workflow-trigger/workflow-trigger.exception';
export type WorkflowEventTriggerJobData = {
workspaceId: string;
@ -23,10 +21,8 @@ export type WorkflowEventTriggerJobData = {
@Processor({ queueName: MessageQueue.workflowQueue, scope: Scope.REQUEST })
export class WorkflowEventTriggerJob {
constructor(
@InjectMessageQueue(MessageQueue.workflowQueue)
private readonly messageQueueService: MessageQueueService,
private readonly twentyORMManager: TwentyORMManager,
private readonly workflowStatusWorkspaceService: WorkflowStatusWorkspaceService,
private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService,
) {}
@Process(WorkflowEventTriggerJob.name)
@ -41,23 +37,20 @@ export class WorkflowEventTriggerJob {
});
if (!workflow.publishedVersionId) {
throw new Error('Workflow has no published version');
throw new WorkflowTriggerException(
'Workflow has no published version',
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
);
}
const workflowRunId =
await this.workflowStatusWorkspaceService.createWorkflowRun(
workflow.publishedVersionId,
{
source: FieldActorSource.WORKFLOW,
name: workflow.name,
},
);
this.messageQueueService.add<RunWorkflowJobData>(WorkflowRunnerJob.name, {
workspaceId: data.workspaceId,
workflowVersionId: workflow.publishedVersionId,
payload: data.payload,
workflowRunId,
});
await this.workflowRunnerWorkspaceService.run(
data.workspaceId,
workflow.publishedVersionId,
data.payload,
{
source: FieldActorSource.WORKFLOW,
name: workflow.name,
},
);
}
}

View File

@ -1,11 +0,0 @@
import { Module } from '@nestjs/common';
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module';
import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workflow-status.module';
import { WorkflowEventTriggerJob } from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job';
@Module({
imports: [WorkflowRunnerModule, WorkflowStatusModule],
providers: [WorkflowEventTriggerJob],
})
export class WorkflowTriggerJobModule {}

View File

@ -1,10 +0,0 @@
import { Module } from '@nestjs/common';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trigger/listeners/database-event-trigger.listener';
@Module({
imports: [FeatureFlagModule],
providers: [DatabaseEventTriggerListener],
})
export class WorkflowTriggerListenerModule {}

View File

@ -1,9 +1,21 @@
import { Module } from '@nestjs/common';
import { WorkflowTriggerJobModule } from 'src/modules/workflow/workflow-trigger/jobs/workflow-trigger-job.module';
import { WorkflowTriggerListenerModule } from 'src/modules/workflow/workflow-trigger/listeners/workflow-trigger-listener.module';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module';
import { WorkflowEventTriggerJob } from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job';
import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trigger/listeners/database-event-trigger.listener';
import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service';
@Module({
imports: [WorkflowTriggerJobModule, WorkflowTriggerListenerModule],
imports: [WorkflowCommonModule, WorkflowRunnerModule, FeatureFlagModule],
providers: [
WorkflowTriggerWorkspaceService,
ScopedWorkspaceContextFactory,
DatabaseEventTriggerListener,
WorkflowEventTriggerJob,
],
exports: [WorkflowTriggerWorkspaceService],
})
export class WorkflowTriggerModule {}

View File

@ -1,5 +1,8 @@
import { Injectable } from '@nestjs/common';
import { buildCreatedByFromWorkspaceMember } from 'src/engine/core-modules/actor/utils/build-created-by-from-workspace-member.util';
import { User } from 'src/engine/core-modules/user/user.entity';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity';
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
@ -19,26 +22,43 @@ export class WorkflowTriggerWorkspaceService {
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory,
private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService,
) {}
async runWorkflowVersion(workflowVersionId: string, payload: object) {
async runWorkflowVersion(
workflowVersionId: string,
payload: object,
workspaceMemberId: string,
user: User,
) {
const workspaceId = this.scopedWorkspaceContextFactory.create().workspaceId;
if (!workspaceId) {
throw new WorkflowTriggerException(
'No workspace id found',
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
);
}
const workflowVersion =
await this.workflowCommonWorkspaceService.getWorkflowVersion(
workflowVersionId,
);
try {
return await this.workflowRunnerWorkspaceService.run({
action: workflowVersion.trigger.nextAction,
payload,
});
} catch (error) {
if (!workflowVersion) {
throw new WorkflowTriggerException(
`Error running workflow version ${error}`,
WorkflowTriggerExceptionCode.INTERNAL_ERROR,
'No workflow version found',
WorkflowTriggerExceptionCode.INVALID_WORKFLOW_VERSION,
);
}
return await this.workflowRunnerWorkspaceService.run(
workspaceId,
workflowVersionId,
payload,
buildCreatedByFromWorkspaceMember(workspaceMemberId, user),
);
}
async enableWorkflowTrigger(workflowVersionId: string) {

View File

@ -1,9 +1,8 @@
import { Module } from '@nestjs/common';
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module';
import { WorkflowTriggerModule } from 'src/modules/workflow/workflow-trigger/workflow-trigger.module';
@Module({
imports: [WorkflowRunnerModule, WorkflowTriggerModule],
imports: [WorkflowTriggerModule],
})
export class WorkflowModule {}