From c1708ff42520bfb4ccf751eb8ab49dd23478d7ac Mon Sep 17 00:00:00 2001 From: Thomas Trompette Date: Wed, 14 Aug 2024 17:10:58 +0200 Subject: [PATCH] Migrate to workspace services --- .../workflow/core-workflow-trigger.module.ts | 4 +- .../workflow/workflow-trigger.resolver.ts | 14 ++--- .../workflow/common/workflow-common.module.ts | 6 +-- ...s => workflow-common.workspace-service.ts} | 16 ++---- .../workflow-action-runner.exception.ts | 12 +++++ .../workflow-action-runner.interface.ts | 4 +- .../workflow-action-runner.module.ts | 9 +++- .../code-workflow-action-runner.ts | 21 ++++++-- .../workflow-runner/workflow-runner.job.ts | 20 ++++--- .../workflow-runner/workflow-runner.module.ts | 6 +-- ...s => workflow-runner.workspace-service.ts} | 8 +-- ... => workflow-trigger.workspace-service.ts} | 52 ++++++++----------- 12 files changed, 85 insertions(+), 87 deletions(-) rename packages/twenty-server/src/modules/workflow/common/{workflow-common.services.ts => workflow-common.workspace-service.ts} (73%) create mode 100644 packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.exception.ts rename packages/twenty-server/src/modules/workflow/workflow-runner/{workflow-runner.service.ts => workflow-runner.workspace-service.ts} (92%) rename packages/twenty-server/src/modules/workflow/workflow-trigger/{workflow-trigger.service.ts => workflow-trigger.workspace-service.ts} (63%) diff --git a/packages/twenty-server/src/engine/core-modules/workflow/core-workflow-trigger.module.ts b/packages/twenty-server/src/engine/core-modules/workflow/core-workflow-trigger.module.ts index 821b31d9d437..76cefa494ab1 100644 --- a/packages/twenty-server/src/engine/core-modules/workflow/core-workflow-trigger.module.ts +++ b/packages/twenty-server/src/engine/core-modules/workflow/core-workflow-trigger.module.ts @@ -3,10 +3,10 @@ import { Module } from '@nestjs/common'; import { WorkflowTriggerResolver } from 'src/engine/core-modules/workflow/workflow-trigger.resolver'; import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module'; import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module'; -import { WorkflowTriggerService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.service'; +import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service'; @Module({ imports: [WorkflowCommonModule, WorkflowRunnerModule], - providers: [WorkflowTriggerService, WorkflowTriggerResolver], + providers: [WorkflowTriggerWorkspaceService, WorkflowTriggerResolver], }) export class WorkflowTriggerCoreModule {} diff --git a/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts b/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts index 078ac0d7e3d2..35c8a40882ba 100644 --- a/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts +++ b/packages/twenty-server/src/engine/core-modules/workflow/workflow-trigger.resolver.ts @@ -4,26 +4,22 @@ import { Args, Mutation, Resolver } from '@nestjs/graphql'; import { RunWorkflowVersionInput } from 'src/engine/core-modules/workflow/dtos/run-workflow-version-input.dto'; import { WorkflowTriggerResultDTO } from 'src/engine/core-modules/workflow/dtos/workflow-trigger-result.dto'; import { workflowTriggerGraphqlApiExceptionHandler } from 'src/engine/core-modules/workflow/utils/workflow-trigger-graphql-api-exception-handler.util'; -import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; -import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator'; import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard'; -import { WorkflowTriggerService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.service'; +import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service'; @UseGuards(JwtAuthGuard) @Resolver() export class WorkflowTriggerResolver { constructor( - private readonly workflowTriggerService: WorkflowTriggerService, + private readonly workflowTriggerWorkspaceService: WorkflowTriggerWorkspaceService, ) {} @Mutation(() => Boolean) async enableWorkflowTrigger( - @AuthWorkspace() { id: workspaceId }: Workspace, @Args('workflowVersionId') workflowVersionId: string, ) { try { - return await this.workflowTriggerService.enableWorkflowTrigger( - workspaceId, + return await this.workflowTriggerWorkspaceService.enableWorkflowTrigger( workflowVersionId, ); } catch (error) { @@ -33,13 +29,11 @@ export class WorkflowTriggerResolver { @Mutation(() => WorkflowTriggerResultDTO) async runWorkflowVersion( - @AuthWorkspace() { id: workspaceId }: Workspace, @Args('input') { workflowVersionId, payload }: RunWorkflowVersionInput, ) { try { return { - result: await this.workflowTriggerService.runWorkflowVersion( - workspaceId, + result: await this.workflowTriggerWorkspaceService.runWorkflowVersion( workflowVersionId, payload ?? {}, ), diff --git a/packages/twenty-server/src/modules/workflow/common/workflow-common.module.ts b/packages/twenty-server/src/modules/workflow/common/workflow-common.module.ts index 159fb862285a..009da5aa8119 100644 --- a/packages/twenty-server/src/modules/workflow/common/workflow-common.module.ts +++ b/packages/twenty-server/src/modules/workflow/common/workflow-common.module.ts @@ -1,9 +1,9 @@ import { Module } from '@nestjs/common'; -import { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services'; +import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service'; @Module({ - providers: [WorkflowCommonService], - exports: [WorkflowCommonService], + providers: [WorkflowCommonWorkspaceService], + exports: [WorkflowCommonWorkspaceService], }) export class WorkflowCommonModule {} diff --git a/packages/twenty-server/src/modules/workflow/common/workflow-common.services.ts b/packages/twenty-server/src/modules/workflow/common/workflow-common.workspace-service.ts similarity index 73% rename from packages/twenty-server/src/modules/workflow/common/workflow-common.services.ts rename to packages/twenty-server/src/modules/workflow/common/workflow-common.workspace-service.ts index baa23b8e1a16..3db70e89c0d5 100644 --- a/packages/twenty-server/src/modules/workflow/common/workflow-common.services.ts +++ b/packages/twenty-server/src/modules/workflow/common/workflow-common.workspace-service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; -import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; +import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity'; import { WorkflowTrigger } from 'src/modules/workflow/common/types/workflow-trigger.type'; import { @@ -9,22 +9,16 @@ import { } from 'src/modules/workflow/workflow-trigger/workflow-trigger.exception'; @Injectable() -export class WorkflowCommonService { - constructor( - private readonly twentyORMGlobalManager: TwentyORMGlobalManager, - ) {} +export class WorkflowCommonWorkspaceService { + constructor(private readonly twentyORMManager: TwentyORMManager) {} - async getWorkflowVersion( - workspaceId: string, - workflowVersionId: string, - ): Promise< + async getWorkflowVersion(workflowVersionId: string): Promise< Omit & { trigger: WorkflowTrigger; } > { const workflowVersionRepository = - await this.twentyORMGlobalManager.getRepositoryForWorkspace( - workspaceId, + await this.twentyORMManager.getRepository( 'workflowVersion', ); diff --git a/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.exception.ts b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.exception.ts new file mode 100644 index 000000000000..9284ce454dab --- /dev/null +++ b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.exception.ts @@ -0,0 +1,12 @@ +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', +} diff --git a/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.interface.ts b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.interface.ts index b443e7c9c688..1591af06c027 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.interface.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.interface.ts @@ -1,14 +1,12 @@ -import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type'; 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 { execute({ action, - workspaceId, payload, }: { action: WorkflowAction; - workspaceId: string; payload?: object; }): Promise; } diff --git a/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.module.ts b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.module.ts index 890b6885003d..1bafd6cfe396 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.module.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runner.module.ts @@ -1,12 +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 { 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'; -import { ServerlessFunctionModule } from 'src/engine/metadata-modules/serverless-function/serverless-function.module'; @Module({ imports: [ServerlessFunctionModule], - providers: [WorkflowActionRunnerFactory, CodeWorkflowActionRunner], + providers: [ + WorkflowActionRunnerFactory, + CodeWorkflowActionRunner, + ScopedWorkspaceContextFactory, + ], exports: [WorkflowActionRunnerFactory], }) export class WorkflowActionRunnerModule {} diff --git a/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runners/code-workflow-action-runner.ts b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runners/code-workflow-action-runner.ts index 3be348283672..2afedcb21402 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runners/code-workflow-action-runner.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-action-runner/workflow-action-runners/code-workflow-action-runner.ts @@ -1,25 +1,38 @@ import { Injectable } from '@nestjs/common'; -import { WorkflowActionRunner } from 'src/modules/workflow/workflow-action-runner/workflow-action-runner.interface'; -import { WorkflowAction } from 'src/modules/workflow/common/types/workflow-action.type'; import { ServerlessFunctionService } from 'src/engine/metadata-modules/serverless-function/serverless-function.service'; +import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory'; +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'; @Injectable() export class CodeWorkflowActionRunner implements WorkflowActionRunner { constructor( private readonly serverlessFunctionService: ServerlessFunctionService, + private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory, ) {} async execute({ action, - workspaceId, payload, }: { action: WorkflowAction; - workspaceId: string; payload?: object; }): Promise { + const { workspaceId } = this.scopedWorkspaceContextFactory.create(); + + if (!workspaceId) { + throw new WorkflowActionRunnerException( + 'Scoped workspace not found', + WorkflowActionRunnerExceptionCode.SCOPED_WORKSPACE_NOT_FOUND, + ); + } + const result = await this.serverlessFunctionService.executeOne( action.settings.serverlessFunctionId, workspaceId, diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.job.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.job.ts index ab3d13d1a7e8..64f1d6984fa5 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.job.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.job.ts @@ -4,8 +4,8 @@ import { Process } from 'src/engine/integrations/message-queue/decorators/proces import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator'; 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 { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services'; -import { WorkflowRunnerService } from 'src/modules/workflow/workflow-runner/workflow-runner.service'; +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'; export type RunWorkflowJobData = { @@ -18,29 +18,27 @@ export type RunWorkflowJobData = { @Processor({ queueName: MessageQueue.workflowQueue, scope: Scope.REQUEST }) export class WorkflowRunnerJob { constructor( - private readonly workflowCommonService: WorkflowCommonService, - private readonly workflowRunnerService: WorkflowRunnerService, + private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService, + private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService, private readonly workflowStatusWorkspaceService: WorkflowStatusWorkspaceService, ) {} @Process(WorkflowRunnerJob.name) async handle({ - workspaceId, workflowVersionId, workflowRunId, payload, }: RunWorkflowJobData): Promise { await this.workflowStatusWorkspaceService.startWorkflowRun(workflowRunId); - const workflowVersion = await this.workflowCommonService.getWorkflowVersion( - workspaceId, - workflowVersionId, - ); + const workflowVersion = + await this.workflowCommonWorkspaceService.getWorkflowVersion( + workflowVersionId, + ); try { - await this.workflowRunnerService.run({ + await this.workflowRunnerWorkspaceService.run({ action: workflowVersion.trigger.nextAction, - workspaceId, payload, }); diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.ts index cce52e509001..9bd7e69d3876 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.ts @@ -3,7 +3,7 @@ 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 { WorkflowRunnerService } from 'src/modules/workflow/workflow-runner/workflow-runner.service'; +import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service'; import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workflow-status.module'; @Module({ @@ -12,7 +12,7 @@ import { WorkflowStatusModule } from 'src/modules/workflow/workflow-status/workf WorkflowActionRunnerModule, WorkflowStatusModule, ], - providers: [WorkflowRunnerService, WorkflowRunnerJob], - exports: [WorkflowRunnerService], + providers: [WorkflowRunnerWorkspaceService, WorkflowRunnerJob], + exports: [WorkflowRunnerWorkspaceService], }) export class WorkflowRunnerModule {} diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.workspace-service.ts similarity index 92% rename from packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.service.ts rename to packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.workspace-service.ts index e8d37e85dfca..0822bc5d2f34 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.service.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.workspace-service.ts @@ -15,19 +15,17 @@ export type WorkflowRunOutput = { }; @Injectable() -export class WorkflowRunnerService { +export class WorkflowRunnerWorkspaceService { constructor( private readonly workflowActionRunnerFactory: WorkflowActionRunnerFactory, ) {} async run({ action, - workspaceId, payload, attemptCount = 1, }: { action?: WorkflowAction; - workspaceId: string; payload?: object; attemptCount?: number; }): Promise { @@ -43,14 +41,12 @@ export class WorkflowRunnerService { const result = await workflowActionRunner.execute({ action, - workspaceId, payload, }); if (result.data) { return await this.run({ action: action.nextAction, - workspaceId, payload: result.data, }); } @@ -65,7 +61,6 @@ export class WorkflowRunnerService { if (action.settings.errorHandlingOptions.continueOnFailure.value) { return await this.run({ action: action.nextAction, - workspaceId, payload, }); } @@ -76,7 +71,6 @@ export class WorkflowRunnerService { ) { return await this.run({ action, - workspaceId, payload, attemptCount: attemptCount + 1, }); diff --git a/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.service.ts b/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service.ts similarity index 63% rename from packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.service.ts rename to packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service.ts index 8cc8bc606609..ea661862dd98 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.service.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.workspace-service.ts @@ -1,41 +1,36 @@ import { Injectable } from '@nestjs/common'; -import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; +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'; import { WorkflowDatabaseEventTrigger, WorkflowTriggerType, } from 'src/modules/workflow/common/types/workflow-trigger.type'; -import { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services'; -import { WorkflowRunnerService } from 'src/modules/workflow/workflow-runner/workflow-runner.service'; +import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service'; +import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-runner.workspace-service'; import { WorkflowTriggerException, WorkflowTriggerExceptionCode, } from 'src/modules/workflow/workflow-trigger/workflow-trigger.exception'; @Injectable() -export class WorkflowTriggerService { +export class WorkflowTriggerWorkspaceService { constructor( - private readonly twentyORMGlobalManager: TwentyORMGlobalManager, - private readonly workflowCommonService: WorkflowCommonService, - private readonly workflowRunnerService: WorkflowRunnerService, + private readonly twentyORMManager: TwentyORMManager, + private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService, + private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService, ) {} - async runWorkflowVersion( - workspaceId: string, - workflowVersionId: string, - payload: object, - ) { - const workflowVersion = await this.workflowCommonService.getWorkflowVersion( - workspaceId, - workflowVersionId, - ); + async runWorkflowVersion(workflowVersionId: string, payload: object) { + const workflowVersion = + await this.workflowCommonWorkspaceService.getWorkflowVersion( + workflowVersionId, + ); try { - return await this.workflowRunnerService.run({ + return await this.workflowRunnerWorkspaceService.run({ action: workflowVersion.trigger.nextAction, - workspaceId, payload, }); } catch (error) { @@ -46,16 +41,15 @@ export class WorkflowTriggerService { } } - async enableWorkflowTrigger(workspaceId: string, workflowVersionId: string) { - const workflowVersion = await this.workflowCommonService.getWorkflowVersion( - workspaceId, - workflowVersionId, - ); + async enableWorkflowTrigger(workflowVersionId: string) { + const workflowVersion = + await this.workflowCommonWorkspaceService.getWorkflowVersion( + workflowVersionId, + ); switch (workflowVersion.trigger.type) { case WorkflowTriggerType.DATABASE_EVENT: await this.upsertEventListenerAndPublishVersion( - workspaceId, workflowVersion.workflowId, workflowVersionId, workflowVersion.trigger, @@ -69,7 +63,6 @@ export class WorkflowTriggerService { } private async upsertEventListenerAndPublishVersion( - workspaceId: string, workflowId: string, workflowVersionId: string, trigger: WorkflowDatabaseEventTrigger, @@ -84,8 +77,7 @@ export class WorkflowTriggerService { } const workflowEventListenerRepository = - await this.twentyORMGlobalManager.getRepositoryForWorkspace( - workspaceId, + await this.twentyORMManager.getRepository( 'workflowEventListener', ); @@ -94,12 +86,10 @@ export class WorkflowTriggerService { eventName, }); - const workspaceDataSource = - await this.twentyORMGlobalManager.getDataSourceForWorkspace(workspaceId); + const workspaceDataSource = await this.twentyORMManager.getDatasource(); const workflowRepository = - await this.twentyORMGlobalManager.getRepositoryForWorkspace( - workspaceId, + await this.twentyORMManager.getRepository( 'workflow', );