Skip to content

Commit

Permalink
refactor(api): Add required changes to invitations; Add a test for in…
Browse files Browse the repository at this point in the history
…vitation when workspace ownership is transferred
  • Loading branch information
muntaxir4 committed Dec 11, 2024
1 parent a013dfb commit 6ce3489
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 46 deletions.
6 changes: 2 additions & 4 deletions apps/api/src/workspace/controller/workspace.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ 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,
page,
limit,
sort,
order,
search,
isAccepted ? isAccepted === 'true' : undefined
search
)
}

Expand Down
50 changes: 25 additions & 25 deletions apps/api/src/workspace/service/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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({
Expand All @@ -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
}
}
}
Expand All @@ -440,7 +441,8 @@ export class WorkspaceService {
select: {
role: {
select: {
name: true
name: true,
colorCode: true
}
}
}
Expand All @@ -452,32 +454,30 @@ 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
}
}
}
}
})

//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 }
}
Expand Down
53 changes: 36 additions & 17 deletions apps/api/src/workspace/workspace.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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({})
})
})

Expand Down

0 comments on commit 6ce3489

Please sign in to comment.