From 7bb2d1799e83ae1a8655444ced972e7096bf8e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Tue, 16 Jan 2024 16:52:21 +0100 Subject: [PATCH] refactor(core): Consolidate executions controllers (no-changelog) (#8349) --- packages/cli/src/Server.ts | 12 +-- .../workflowStatistics.controller.ts | 2 +- .../cli/src/executions/execution.request.ts | 29 +++++++ .../executions/executions.controller.ee.ts | 68 ----------------- .../src/executions/executions.controller.ts | 76 ++++++------------- .../src/executions/executions.service.ee.ts | 4 +- .../cli/src/executions/executions.service.ts | 2 +- packages/cli/src/requests.ts | 33 +------- .../integration/shared/utils/testServer.ts | 4 +- 9 files changed, 64 insertions(+), 166 deletions(-) create mode 100644 packages/cli/src/executions/execution.request.ts delete mode 100644 packages/cli/src/executions/executions.controller.ee.ts diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 1de23822f3..cb11e29c5c 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -39,7 +39,8 @@ import { TEMPLATES_DIR, } from '@/constants'; import { credentialsController } from '@/credentials/credentials.controller'; -import type { CurlHelper, ExecutionRequest } from '@/requests'; +import type { CurlHelper } from '@/requests'; +import type { ExecutionRequest } from '@/executions/execution.request'; import { registerController } from '@/decorators'; import { AuthController } from '@/controllers/auth.controller'; import { BinaryDataController } from '@/controllers/binaryData.controller'; @@ -56,7 +57,7 @@ import { TranslationController } from '@/controllers/translation.controller'; import { UsersController } from '@/controllers/users.controller'; import { WorkflowStatisticsController } from '@/controllers/workflowStatistics.controller'; import { ExternalSecretsController } from '@/ExternalSecrets/ExternalSecrets.controller.ee'; -import { executionsController } from '@/executions/executions.controller'; +import { ExecutionsController } from '@/executions/executions.controller'; import { isApiEnabled, loadPublicApiVersions } from '@/PublicApi'; import type { ICredentialsOverwrite, IDiagnosticInfo, IExecutionsStopData } from '@/Interfaces'; import { ActiveExecutions } from '@/ActiveExecutions'; @@ -247,6 +248,7 @@ export class Server extends AbstractServer { RoleController, ActiveWorkflowsController, WorkflowsController, + ExecutionsController, ]; if (process.env.NODE_ENV !== 'production' && Container.get(MultiMainSetup).isEnabled) { @@ -397,12 +399,6 @@ export class Server extends AbstractServer { }), ); - // ---------------------------------------- - // Executions - // ---------------------------------------- - - this.app.use(`/${this.restEndpoint}/executions`, executionsController); - // ---------------------------------------- // Executing Workflows // ---------------------------------------- diff --git a/packages/cli/src/controllers/workflowStatistics.controller.ts b/packages/cli/src/controllers/workflowStatistics.controller.ts index a5ad163823..1fa97db2d5 100644 --- a/packages/cli/src/controllers/workflowStatistics.controller.ts +++ b/packages/cli/src/controllers/workflowStatistics.controller.ts @@ -4,7 +4,7 @@ import type { WorkflowStatistics } from '@db/entities/WorkflowStatistics'; import { StatisticsNames } from '@db/entities/WorkflowStatistics'; import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository'; import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository'; -import { ExecutionRequest } from '@/requests'; +import { ExecutionRequest } from '@/executions/execution.request'; import type { IWorkflowStatisticsDataLoaded } from '@/Interfaces'; import { Logger } from '@/Logger'; import { NotFoundError } from '@/errors/response-errors/not-found.error'; diff --git a/packages/cli/src/executions/execution.request.ts b/packages/cli/src/executions/execution.request.ts new file mode 100644 index 0000000000..96e1a058ae --- /dev/null +++ b/packages/cli/src/executions/execution.request.ts @@ -0,0 +1,29 @@ +import type { IExecutionDeleteFilter } from '@/Interfaces'; +import type { AuthenticatedRequest } from '@/requests'; + +export declare namespace ExecutionRequest { + namespace QueryParam { + type GetAll = { + filter: string; // '{ waitTill: string; finished: boolean, [other: string]: string }' + limit: string; + lastId: string; + firstId: string; + }; + + type GetAllCurrent = { + filter: string; // '{ workflowId: string }' + }; + } + + type GetAll = AuthenticatedRequest<{}, {}, {}, QueryParam.GetAll>; + + type Get = AuthenticatedRequest<{ id: string }, {}, {}, { unflattedResponse: 'true' | 'false' }>; + + type Delete = AuthenticatedRequest<{}, {}, IExecutionDeleteFilter>; + + type Retry = AuthenticatedRequest<{ id: string }, {}, { loadWorkflow: boolean }, {}>; + + type Stop = AuthenticatedRequest<{ id: string }>; + + type GetAllCurrent = AuthenticatedRequest<{}, {}, {}, QueryParam.GetAllCurrent>; +} diff --git a/packages/cli/src/executions/executions.controller.ee.ts b/packages/cli/src/executions/executions.controller.ee.ts deleted file mode 100644 index 673d74d15c..0000000000 --- a/packages/cli/src/executions/executions.controller.ee.ts +++ /dev/null @@ -1,68 +0,0 @@ -import express from 'express'; -import type { - IExecutionFlattedResponse, - IExecutionResponse, - IExecutionsListResponse, -} from '@/Interfaces'; -import type { ExecutionRequest } from '@/requests'; -import * as ResponseHelper from '@/ResponseHelper'; -import { isSharingEnabled } from '@/UserManagement/UserManagementHelper'; -import { EEExecutionsService } from './executions.service.ee'; - -export const EEExecutionsController = express.Router(); - -EEExecutionsController.use((req, res, next) => { - if (!isSharingEnabled()) { - // skip ee router and use free one - next('router'); - return; - } - // use ee router - next(); -}); - -/** - * GET /executions - */ -EEExecutionsController.get( - '/', - ResponseHelper.send(async (req: ExecutionRequest.GetAll): Promise => { - return EEExecutionsService.getExecutionsList(req); - }), -); - -/** - * GET /executions/:id - */ -EEExecutionsController.get( - '/:id(\\d+)', - ResponseHelper.send( - async ( - req: ExecutionRequest.Get, - ): Promise => { - return EEExecutionsService.getExecution(req); - }, - ), -); - -/** - * POST /executions/:id/retry - */ -EEExecutionsController.post( - '/:id/retry', - ResponseHelper.send(async (req: ExecutionRequest.Retry): Promise => { - return EEExecutionsService.retryExecution(req); - }), -); - -/** - * POST /executions/delete - * INFORMATION: We use POST instead of DELETE to not run into any issues with the query data - * getting too long - */ -EEExecutionsController.post( - '/delete', - ResponseHelper.send(async (req: ExecutionRequest.Delete): Promise => { - await EEExecutionsService.deleteExecutions(req); - }), -); diff --git a/packages/cli/src/executions/executions.controller.ts b/packages/cli/src/executions/executions.controller.ts index fffd493fd1..d068c75ec0 100644 --- a/packages/cli/src/executions/executions.controller.ts +++ b/packages/cli/src/executions/executions.controller.ts @@ -1,59 +1,31 @@ -import express from 'express'; -import type { - IExecutionFlattedResponse, - IExecutionResponse, - IExecutionsListResponse, -} from '@/Interfaces'; -import * as ResponseHelper from '@/ResponseHelper'; -import type { ExecutionRequest } from '@/requests'; -import { EEExecutionsController } from './executions.controller.ee'; +import { ExecutionRequest } from './execution.request'; import { ExecutionsService } from './executions.service'; +import { Authorized, Get, Post, RestController } from '@/decorators'; +import { EnterpriseExecutionsService } from './executions.service.ee'; +import { isSharingEnabled } from '@/UserManagement/UserManagementHelper'; -export const executionsController = express.Router(); -executionsController.use('/', EEExecutionsController); - -/** - * GET /executions - */ -executionsController.get( - '/', - ResponseHelper.send(async (req: ExecutionRequest.GetAll): Promise => { +@Authorized() +@RestController('/executions') +export class ExecutionsController { + @Get('/') + async getExecutionsList(req: ExecutionRequest.GetAll) { return ExecutionsService.getExecutionsList(req); - }), -); + } -/** - * GET /executions/:id - */ -executionsController.get( - '/:id(\\d+)', - ResponseHelper.send( - async ( - req: ExecutionRequest.Get, - ): Promise => { - return ExecutionsService.getExecution(req); - }, - ), -); + @Get('/:id') + async getExecution(req: ExecutionRequest.Get) { + return isSharingEnabled() + ? EnterpriseExecutionsService.getExecution(req) + : ExecutionsService.getExecution(req); + } -/** - * POST /executions/:id/retry - */ -executionsController.post( - '/:id/retry', - ResponseHelper.send(async (req: ExecutionRequest.Retry): Promise => { + @Post('/:id/retry') + async retryExecution(req: ExecutionRequest.Retry) { return ExecutionsService.retryExecution(req); - }), -); + } -/** - * POST /executions/delete - * INFORMATION: We use POST instead of DELETE to not run into any issues with the query data - * getting too long - */ -executionsController.post( - '/delete', - ResponseHelper.send(async (req: ExecutionRequest.Delete): Promise => { - await ExecutionsService.deleteExecutions(req); - }), -); + @Post('/delete') + async deleteExecutions(req: ExecutionRequest.Delete) { + return ExecutionsService.deleteExecutions(req); + } +} diff --git a/packages/cli/src/executions/executions.service.ee.ts b/packages/cli/src/executions/executions.service.ee.ts index 662ce38759..17e8179bfc 100644 --- a/packages/cli/src/executions/executions.service.ee.ts +++ b/packages/cli/src/executions/executions.service.ee.ts @@ -1,14 +1,14 @@ import type { User } from '@db/entities/User'; import { getSharedWorkflowIds } from '@/WorkflowHelpers'; import { ExecutionsService } from './executions.service'; -import type { ExecutionRequest } from '@/requests'; +import type { ExecutionRequest } from './execution.request'; import type { IExecutionResponse, IExecutionFlattedResponse } from '@/Interfaces'; import { EnterpriseWorkflowService } from '../workflows/workflow.service.ee'; import type { WorkflowWithSharingsAndCredentials } from '@/workflows/workflows.types'; import Container from 'typedi'; import { WorkflowRepository } from '@/databases/repositories/workflow.repository'; -export class EEExecutionsService extends ExecutionsService { +export class EnterpriseExecutionsService extends ExecutionsService { /** * Function to get the workflow Ids for a User regardless of role */ diff --git a/packages/cli/src/executions/executions.service.ts b/packages/cli/src/executions/executions.service.ts index 0b39da6ce9..a0a836856a 100644 --- a/packages/cli/src/executions/executions.service.ts +++ b/packages/cli/src/executions/executions.service.ts @@ -21,7 +21,7 @@ import type { } from '@/Interfaces'; import { NodeTypes } from '@/NodeTypes'; import { Queue } from '@/Queue'; -import type { ExecutionRequest } from '@/requests'; +import type { ExecutionRequest } from './execution.request'; import { getSharedWorkflowIds } from '@/WorkflowHelpers'; import { WorkflowRunner } from '@/WorkflowRunner'; import * as GenericHelpers from '@/GenericHelpers'; diff --git a/packages/cli/src/requests.ts b/packages/cli/src/requests.ts index 641d1c72fd..ef89d276b3 100644 --- a/packages/cli/src/requests.ts +++ b/packages/cli/src/requests.ts @@ -13,12 +13,7 @@ import type { import { IsBoolean, IsEmail, IsIn, IsOptional, IsString, Length } from 'class-validator'; import { NoXss } from '@db/utils/customValidators'; -import type { - PublicUser, - IExecutionDeleteFilter, - SecretsProvider, - SecretsProviderState, -} from '@/Interfaces'; +import type { PublicUser, SecretsProvider, SecretsProviderState } from '@/Interfaces'; import type { Role, RoleNames } from '@db/entities/Role'; import type { User } from '@db/entities/User'; import type { UserManagementMailer } from '@/UserManagement/email'; @@ -171,32 +166,6 @@ export declare namespace CredentialRequest { type Share = AuthenticatedRequest<{ credentialId: string }, {}, { shareWithIds: string[] }>; } -// ---------------------------------- -// /executions -// ---------------------------------- - -export declare namespace ExecutionRequest { - namespace QueryParam { - type GetAll = { - filter: string; // '{ waitTill: string; finished: boolean, [other: string]: string }' - limit: string; - lastId: string; - firstId: string; - }; - - type GetAllCurrent = { - filter: string; // '{ workflowId: string }' - }; - } - - type GetAll = AuthenticatedRequest<{}, {}, {}, QueryParam.GetAll>; - type Get = AuthenticatedRequest<{ id: string }, {}, {}, { unflattedResponse: 'true' | 'false' }>; - type Delete = AuthenticatedRequest<{}, {}, IExecutionDeleteFilter>; - type Retry = AuthenticatedRequest<{ id: string }, {}, { loadWorkflow: boolean }, {}>; - type Stop = AuthenticatedRequest<{ id: string }>; - type GetAllCurrent = AuthenticatedRequest<{}, {}, {}, QueryParam.GetAllCurrent>; -} - // ---------------------------------- // /me // ---------------------------------- diff --git a/packages/cli/test/integration/shared/utils/testServer.ts b/packages/cli/test/integration/shared/utils/testServer.ts index cb6dc1355f..496c3711f6 100644 --- a/packages/cli/test/integration/shared/utils/testServer.ts +++ b/packages/cli/test/integration/shared/utils/testServer.ts @@ -131,8 +131,8 @@ export const setupTestServer = ({ break; case 'executions': - const { executionsController } = await import('@/executions/executions.controller'); - app.use(`/${REST_PATH_SEGMENT}/executions`, executionsController); + const { ExecutionsController } = await import('@/executions/executions.controller'); + registerController(app, ExecutionsController); break; case 'variables':