diff --git a/packages/cli/src/workflows/workflow-sharing.service.ts b/packages/cli/src/workflows/workflow-sharing.service.ts index 3515b3a4f8..8f8c67335b 100644 --- a/packages/cli/src/workflows/workflow-sharing.service.ts +++ b/packages/cli/src/workflows/workflow-sharing.service.ts @@ -67,6 +67,23 @@ export class WorkflowSharingService { return sharedWorkflows.map(({ workflowId }) => workflowId); } + async getSharedWithMeIds(user: User) { + const sharedWithMeWorkflows = await this.sharedWorkflowRepository.find({ + select: ['workflowId'], + where: { + role: 'workflow:editor', + project: { + projectRelations: { + userId: user.id, + role: 'project:personalOwner', + }, + }, + }, + }); + + return sharedWithMeWorkflows.map(({ workflowId }) => workflowId); + } + async getSharedWorkflowScopes( workflowIds: string[], user: User, diff --git a/packages/cli/src/workflows/workflow.request.ts b/packages/cli/src/workflows/workflow.request.ts index 848d84a61c..a9cba76bf5 100644 --- a/packages/cli/src/workflows/workflow.request.ts +++ b/packages/cli/src/workflows/workflow.request.ts @@ -45,7 +45,11 @@ export declare namespace WorkflowRequest { {}, {}, {}, - ListQuery.Params & { includeScopes?: string; includeFolders?: string } + ListQuery.Params & { + includeScopes?: string; + includeFolders?: string; + onlySharedWithMe?: string; + } > & { listQueryOptions: ListQuery.Options; }; diff --git a/packages/cli/src/workflows/workflow.service.ts b/packages/cli/src/workflows/workflow.service.ts index b7afebc420..15cbf925ff 100644 --- a/packages/cli/src/workflows/workflow.service.ts +++ b/packages/cli/src/workflows/workflow.service.ts @@ -69,6 +69,7 @@ export class WorkflowService { options?: ListQuery.Options, includeScopes?: boolean, includeFolders?: boolean, + onlySharedWithMe?: boolean, ) { let count; let workflows; @@ -86,6 +87,8 @@ export class WorkflowService { if (isPersonalProject) { sharedWorkflowIds = await this.workflowSharingService.getOwnedWorkflowsInPersonalProject(user); + } else if (onlySharedWithMe) { + sharedWorkflowIds = await this.workflowSharingService.getSharedWithMeIds(user); } else { sharedWorkflowIds = await this.workflowSharingService.getSharedWorkflowIds(user, { scopes: ['workflow:read'], diff --git a/packages/cli/src/workflows/workflows.controller.ts b/packages/cli/src/workflows/workflows.controller.ts index 8bbfabc5b4..f30014e9a2 100644 --- a/packages/cli/src/workflows/workflows.controller.ts +++ b/packages/cli/src/workflows/workflows.controller.ts @@ -228,6 +228,7 @@ export class WorkflowsController { req.listQueryOptions, !!req.query.includeScopes, !!req.query.includeFolders, + !!req.query.onlySharedWithMe, ); res.json({ count, data }); diff --git a/packages/cli/test/integration/workflows/workflows.controller.test.ts b/packages/cli/test/integration/workflows/workflows.controller.test.ts index 1b68fa9dae..7084cf0180 100644 --- a/packages/cli/test/integration/workflows/workflows.controller.test.ts +++ b/packages/cli/test/integration/workflows/workflows.controller.test.ts @@ -1313,6 +1313,54 @@ describe('GET /workflows', () => { }); }); +describe('GET /workflows?onlySharedWithMe=true', () => { + test('should return only workflows shared with me', async () => { + const memberPersonalProject = await projectRepository.getPersonalProjectForUserOrFail( + member.id, + ); + + const ownerPersonalProject = await projectRepository.getPersonalProjectForUserOrFail(owner.id); + + await createWorkflow({ name: 'First' }, owner); + await createWorkflow({ name: 'Second' }, member); + const workflow3 = await createWorkflow({ name: 'Third' }, member); + + await shareWorkflowWithUsers(workflow3, [owner]); + + const response = await authOwnerAgent.get('/workflows').query({ onlySharedWithMe: true }); + expect(200); + + expect(response.body).toEqual({ + count: 1, + data: arrayContaining([ + objectContaining({ + id: any(String), + name: 'Third', + active: any(Boolean), + createdAt: any(String), + updatedAt: any(String), + versionId: any(String), + parentFolder: null, + homeProject: { + id: memberPersonalProject.id, + name: member.createPersonalProjectName(), + icon: null, + type: memberPersonalProject.type, + }, + sharedWithProjects: [ + objectContaining({ + id: any(String), + name: ownerPersonalProject.name, + icon: null, + type: ownerPersonalProject.type, + }), + ], + }), + ]), + }); + }); +}); + describe('GET /workflows?includeFolders=true', () => { test('should return zero workflows and folders if none exist', async () => { const response = await authOwnerAgent.get('/workflows').query({ includeFolders: true });