From 4b165e9ea1e7ffb417273e1efc57c4e227ab1d6e Mon Sep 17 00:00:00 2001 From: Valya Bullions Date: Wed, 19 Jun 2024 15:54:42 +0100 Subject: [PATCH] fix: Get workflow not returning home project and shared projects --- .../cli/src/workflows/workflow.service.ee.ts | 12 ++++---- packages/cli/src/workflows/workflows.types.ts | 5 ++-- .../workflows/workflows.controller.ee.test.ts | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/workflows/workflow.service.ee.ts b/packages/cli/src/workflows/workflow.service.ee.ts index 959fd412b725f..dd96219bd59b7 100644 --- a/packages/cli/src/workflows/workflow.service.ee.ts +++ b/packages/cli/src/workflows/workflow.service.ee.ts @@ -13,7 +13,6 @@ import { BadRequestError } from '@/errors/response-errors/bad-request.error'; import { NotFoundError } from '@/errors/response-errors/not-found.error'; import { Logger } from '@/Logger'; import type { - CredentialUsedByWorkflow, WorkflowWithSharingsAndCredentials, WorkflowWithSharingsMetaDataAndCredentials, } from './workflows.types'; @@ -101,16 +100,15 @@ export class EnterpriseWorkflowService { const userCredentialIds = userCredentials.map((credential) => credential.id); workflowCredentials.forEach((credential) => { const credentialId = credential.id; - const workflowCredential: CredentialUsedByWorkflow = { + const filledCred = this.ownershipService.addOwnedByAndSharedWith(credential); + workflow.usedCredentials?.push({ id: credentialId, name: credential.name, type: credential.type, currentUserHasAccess: userCredentialIds.includes(credentialId), - sharedWith: [], - ownedBy: null, - }; - credential = this.ownershipService.addOwnedByAndSharedWith(credential); - workflow.usedCredentials?.push(workflowCredential); + homeProject: filledCred.homeProject, + sharedWithProjects: filledCred.sharedWithProjects, + }); }); } diff --git a/packages/cli/src/workflows/workflows.types.ts b/packages/cli/src/workflows/workflows.types.ts index cc9d0ef40f52e..f8c7d8177384e 100644 --- a/packages/cli/src/workflows/workflows.types.ts +++ b/packages/cli/src/workflows/workflows.types.ts @@ -1,4 +1,3 @@ -import type { IUser } from 'n8n-workflow'; import type { SharedWorkflow } from '@db/entities/SharedWorkflow'; import type { WorkflowEntity } from '@db/entities/WorkflowEntity'; import type { SlimProject } from '@/requests'; @@ -21,6 +20,6 @@ export interface CredentialUsedByWorkflow { name: string; type?: string; currentUserHasAccess: boolean; - ownedBy?: IUser | null; - sharedWith?: IUser[]; + homeProject: SlimProject | null; + sharedWithProjects: SlimProject[]; } diff --git a/packages/cli/test/integration/workflows/workflows.controller.ee.test.ts b/packages/cli/test/integration/workflows/workflows.controller.ee.test.ts index b7367d51788e3..ca163c5a16ac1 100644 --- a/packages/cli/test/integration/workflows/workflows.controller.ee.test.ts +++ b/packages/cli/test/integration/workflows/workflows.controller.ee.test.ts @@ -527,6 +527,35 @@ describe('GET /workflows/:workflowId', () => { ]); expect(member2Workflow.sharedWithProjects).toHaveLength(1); }); + + test('should return workflow credentials home project and shared with projects', async () => { + const savedCredential = await saveCredential(randomCredentialPayload(), { user: member }); + // Both users have access to the credential (none is owner) + await shareCredentialWithUsers(savedCredential, [anotherMember]); + + const workflowPayload = makeWorkflow({ + withPinData: false, + withCredential: { id: savedCredential.id, name: savedCredential.name }, + }); + const workflow = await createWorkflow(workflowPayload, member); + await shareWorkflowWithUsers(workflow, [anotherMember]); + + const responseMember1 = await authMemberAgent.get(`/workflows/${workflow.id}`).expect(200); + const member1Workflow: WorkflowWithSharingsMetaDataAndCredentials = responseMember1.body.data; + + expect(member1Workflow.usedCredentials).toMatchObject([ + { + id: savedCredential.id, + name: savedCredential.name, + currentUserHasAccess: true, + homeProject: { + id: memberPersonalProject.id, + }, + sharedWithProjects: [{ id: anotherMemberPersonalProject.id }], + }, + ]); + expect(member1Workflow.sharedWithProjects).toHaveLength(1); + }); }); describe('POST /workflows', () => {