From 6ce3489709c258134be448479ee97c0d6c16ec42 Mon Sep 17 00:00:00 2001 From: muntaxir4 Date: Wed, 11 Dec 2024 23:46:44 +0530 Subject: [PATCH] refactor(api): Add required changes to invitations; Add a test for invitation when workspace ownership is transferred --- .../controller/workspace.controller.ts | 6 +-- .../workspace/service/workspace.service.ts | 50 ++++++++--------- apps/api/src/workspace/workspace.e2e.spec.ts | 53 +++++++++++++------ 3 files changed, 63 insertions(+), 46 deletions(-) diff --git a/apps/api/src/workspace/controller/workspace.controller.ts b/apps/api/src/workspace/controller/workspace.controller.ts index 99ba5cb7..5356caa7 100644 --- a/apps/api/src/workspace/controller/workspace.controller.ts +++ b/apps/api/src/workspace/controller/workspace.controller.ts @@ -52,8 +52,7 @@ export class WorkspaceController { @Query('limit') limit: number = 10, @Query('sort') sort: string = 'name', @Query('order') order: string = 'asc', - @Query('search') search: string = '', - @Query('isAccepted') isAccepted: 'true' | 'false' | undefined = undefined + @Query('search') search: string = '' ) { return this.workspaceService.getAllWorkspaceInvitations( user, @@ -61,8 +60,7 @@ export class WorkspaceController { limit, sort, order, - search, - isAccepted ? isAccepted === 'true' : undefined + search ) } diff --git a/apps/api/src/workspace/service/workspace.service.ts b/apps/api/src/workspace/service/workspace.service.ts index bf900eaa..1c7754f0 100644 --- a/apps/api/src/workspace/service/workspace.service.ts +++ b/apps/api/src/workspace/service/workspace.service.ts @@ -386,14 +386,13 @@ export class WorkspaceService { } /** - * Gets all workspaces of a user, paginated. + * Gets all the invitations a user has to various workspaces, paginated. * @param user The user to get the workspaces for * @param page The page number to get * @param limit The number of items per page to get * @param sort The field to sort by * @param order The order to sort in * @param search The search string to filter by - * @param isAccepted The invitation status to filter by * @returns The workspace invitations of the user, paginated, with metadata */ async getAllWorkspaceInvitations( @@ -402,8 +401,7 @@ export class WorkspaceService { limit: number, sort: string, order: string, - search: string, - isAccepted: boolean | undefined + search: string ) { // fetch all workspaces of user where they are not admin const items = await this.prisma.workspaceMember.findMany({ @@ -416,13 +414,16 @@ export class WorkspaceService { }, where: { userId: user.id, - invitationAccepted: isAccepted, + invitationAccepted: false, + workspace: { + name: { + contains: search + } + }, roles: { none: { role: { - authorities: { - has: Authority.WORKSPACE_ADMIN - } + hasAdminAuthority: true } } } @@ -440,7 +441,8 @@ export class WorkspaceService { select: { role: { select: { - name: true + name: true, + colorCode: true } } } @@ -452,13 +454,16 @@ export class WorkspaceService { const totalCount = await this.prisma.workspaceMember.count({ where: { userId: user.id, - invitationAccepted: isAccepted, + invitationAccepted: false, + workspace: { + name: { + contains: search + } + }, roles: { none: { role: { - authorities: { - has: Authority.WORKSPACE_ADMIN - } + hasAdminAuthority: true } } } @@ -466,18 +471,13 @@ export class WorkspaceService { }) //calculate metadata for pagination - const metadata = paginate( - totalCount, - `/workspace/invitations`, - { - page, - limit: limitMaxItemsPerPage(limit), - sort, - order, - search - }, - isAccepted !== undefined ? { isAccepted } : undefined - ) + const metadata = paginate(totalCount, `/workspace/invitations`, { + page, + limit: limitMaxItemsPerPage(limit), + sort, + order, + search + }) return { items, metadata } } diff --git a/apps/api/src/workspace/workspace.e2e.spec.ts b/apps/api/src/workspace/workspace.e2e.spec.ts index 48ee3b31..eb15bd00 100644 --- a/apps/api/src/workspace/workspace.e2e.spec.ts +++ b/apps/api/src/workspace/workspace.e2e.spec.ts @@ -488,7 +488,7 @@ describe('Workspace Controller Tests', () => { }) describe('Get All Workspace Invitations Tests', () => { - it('should be able to fetch all the workspace invitations of the user', async () => { + it('should be able to fetch all the non accepted invitations of the user', async () => { //invite user2 to workspace1 await createMembership(memberRole.id, user2.id, workspace1.id, prisma) @@ -519,7 +519,7 @@ describe('Workspace Controller Tests', () => { ) }) - it('should be able to fetch all the workspace invitations of the user that are accepted', async () => { + it('should be able to fetch empty list of workspace invitations for the user once all invitations are accepted', async () => { //invite user2 to workspace1 await createMembership(memberRole.id, user2.id, workspace1.id, prisma) @@ -531,26 +531,45 @@ describe('Workspace Controller Tests', () => { headers: { 'x-e2e-user-email': user2.email }, - url: `/workspace/invitations?isAccepted=true` + url: `/workspace/invitations` }) const body = response.json() + expect(body.items).toHaveLength(0) + expect(body.metadata).toEqual({}) + }) - expect(body.items).toHaveLength(1) - expect(body.items[0].workspace.id).toBe(workspace1.id) - expect(body.items[0].workspace.slug).not.toBe(workspace2.slug) - expect(body.metadata.totalCount).toBe(1) - expect(body.metadata.links.self).toEqual( - `/workspace/invitations?isAccepted=true&page=0&limit=10&sort=name&order=asc&search=` - ) - expect(body.metadata.links.first).toEqual( - `/workspace/invitations?isAccepted=true&page=0&limit=10&sort=name&order=asc&search=` - ) - expect(body.metadata.links.previous).toBeNull() - expect(body.metadata.links.next).toBeNull() - expect(body.metadata.links.last).toEqual( - `/workspace/invitations?isAccepted=true&page=0&limit=10&sort=name&order=asc&search=` + it('should be able to fetch empty list of workspace invitations for the user if ownership is transferred', async () => { + //create a new workspace for user 1 + const workspace3 = await workspaceService.createWorkspace(user1, { + name: 'Workspace 3' + }) + + //invite user2 to workspace3 + await createMembership(memberRole.id, user2.id, workspace3.id, prisma) + + //accept the invitation for user2 to workspace3 + await workspaceMembershipService.acceptInvitation(user2, workspace3.slug) + + //transfer ownership of workspace1 to user2 + await workspaceMembershipService.transferOwnership( + user1, + workspace3.slug, + user2.email ) + + const response = await app.inject({ + method: 'GET', + headers: { + 'x-e2e-user-email': user1.email + }, + url: `/workspace/invitations` + }) + + const body = response.json() + console.log(body) + expect(body.items).toHaveLength(0) + expect(body.metadata).toEqual({}) }) })