From 1579d05fd19abd9438f57a0e4ede5c3feed3c702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 25 Nov 2022 14:20:28 +0100 Subject: [PATCH] refactor: Trim down the response on the Workflows listing endpoint (#4726) * fix: Avoid hashing workflows in the listing page * stop returning full nodes data on the listings page when sharing is enabled * fix the relations array for workflow listing * add a comment explaining the hash skipping hack --- .../src/databases/entities/WorkflowEntity.ts | 3 ++ .../src/workflows/workflows.controller.ee.ts | 16 +++++---- .../src/workflows/workflows.services.ee.ts | 10 ++---- .../cli/src/workflows/workflows.services.ts | 35 ++++++++++--------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/packages/cli/src/databases/entities/WorkflowEntity.ts b/packages/cli/src/databases/entities/WorkflowEntity.ts index 4bb7168b82..06d47e40d1 100644 --- a/packages/cli/src/databases/entities/WorkflowEntity.ts +++ b/packages/cli/src/databases/entities/WorkflowEntity.ts @@ -101,6 +101,9 @@ export class WorkflowEntity extends AbstractEntity implements IWorkflowDb { setHash(): void { const { name, active, nodes, connections, settings, staticData, pinData } = this; + // Workflow listing page does not request the `connections` column, so we can use this for `undefined` to avoid generating hashes for all the workflows. + if (!connections) return; + const state = JSON.stringify({ name, active, diff --git a/packages/cli/src/workflows/workflows.controller.ee.ts b/packages/cli/src/workflows/workflows.controller.ee.ts index 292e1092ec..5403c81445 100644 --- a/packages/cli/src/workflows/workflows.controller.ee.ts +++ b/packages/cli/src/workflows/workflows.controller.ee.ts @@ -97,10 +97,9 @@ EEWorkflowController.get( ); } - return EEWorkflows.addCredentialsToWorkflow( - EEWorkflows.addOwnerAndSharings(workflow), - req.user, - ); + EEWorkflows.addOwnerAndSharings(workflow); + await EEWorkflows.addCredentialsToWorkflow(workflow, req.user); + return workflow; }), ); @@ -200,9 +199,12 @@ EEWorkflowController.get( )) as unknown as WorkflowEntity[]; return Promise.all( - workflows.map(async (workflow) => - EEWorkflows.addCredentialsToWorkflow(EEWorkflows.addOwnerAndSharings(workflow), req.user), - ), + workflows.map(async (workflow) => { + EEWorkflows.addOwnerAndSharings(workflow); + await EEWorkflows.addCredentialsToWorkflow(workflow, req.user); + workflow.nodes = []; + return workflow; + }), ); }), ); diff --git a/packages/cli/src/workflows/workflows.services.ee.ts b/packages/cli/src/workflows/workflows.services.ee.ts index 21ce1cfbd1..fdc68db4da 100644 --- a/packages/cli/src/workflows/workflows.services.ee.ts +++ b/packages/cli/src/workflows/workflows.services.ee.ts @@ -85,9 +85,7 @@ export class EEWorkflowsService extends WorkflowsService { return transaction.save(newSharedWorkflows); } - static addOwnerAndSharings( - workflow: WorkflowWithSharingsAndCredentials, - ): WorkflowWithSharingsAndCredentials { + static addOwnerAndSharings(workflow: WorkflowWithSharingsAndCredentials): void { workflow.ownedBy = null; workflow.sharedWith = []; workflow.usedCredentials = []; @@ -104,14 +102,12 @@ export class EEWorkflowsService extends WorkflowsService { }); delete workflow.shared; - - return workflow; } static async addCredentialsToWorkflow( workflow: WorkflowWithSharingsAndCredentials, currentUser: User, - ): Promise { + ): Promise { workflow.usedCredentials = []; const userCredentials = await EECredentials.getAll(currentUser); const credentialIdsUsedByWorkflow = new Set(); @@ -155,8 +151,6 @@ export class EEWorkflowsService extends WorkflowsService { }); workflow.usedCredentials?.push(workflowCredential); }); - - return workflow; } static validateCredentialPermissionsToUser( diff --git a/packages/cli/src/workflows/workflows.services.ts b/packages/cli/src/workflows/workflows.services.ts index 9b4df6e17d..27dfc2ed62 100644 --- a/packages/cli/src/workflows/workflows.services.ts +++ b/packages/cli/src/workflows/workflows.services.ts @@ -140,26 +140,27 @@ export class WorkflowsService { } const fields: Array = ['id', 'name', 'active', 'createdAt', 'updatedAt']; + const relations: string[] = []; - const query: FindManyOptions = { - select: config.get('enterprise.features.sharing') ? [...fields, 'nodes'] : fields, - relations: config.get('enterprise.features.sharing') - ? ['tags', 'shared', 'shared.user', 'shared.role'] - : ['tags'], - }; - - if (config.getEnv('workflowTagsDisabled')) { - delete query.relations; + if (!config.getEnv('workflowTagsDisabled')) { + relations.push('tags'); } - const workflows = await Db.collections.Workflow.find( - Object.assign(query, { - where: { - id: In(sharedWorkflowIds), - ...filter, - }, - }), - ); + const isSharingEnabled = config.getEnv('enterprise.features.sharing'); + if (isSharingEnabled) { + relations.push('shared', 'shared.user', 'shared.role'); + } + + const query: FindManyOptions = { + select: isSharingEnabled ? [...fields, 'nodes'] : fields, + relations, + where: { + id: In(sharedWorkflowIds), + ...filter, + }, + }; + + const workflows = await Db.collections.Workflow.find(query); return workflows.map((workflow) => { const { id, ...rest } = workflow;