diff --git a/apps/api/src/workspace/service/workspace.service.ts b/apps/api/src/workspace/service/workspace.service.ts index 2a6b9319..f93a3648 100644 --- a/apps/api/src/workspace/service/workspace.service.ts +++ b/apps/api/src/workspace/service/workspace.service.ts @@ -31,6 +31,7 @@ import { v4 } from 'uuid' import createEvent from '../../common/create-event' import createWorkspace from '../../common/create-workspace' import { AuthorityCheckerService } from '../../common/authority-checker.service' +import { paginate } from '../../common/paginate' @Injectable() export class WorkspaceService { @@ -464,10 +465,10 @@ export class WorkspaceService { authority: Authority.READ_USERS, prisma: this.prisma }) - - return this.prisma.workspaceMember.findMany({ + //get all members of workspace for page with limit + const items = await this.prisma.workspaceMember.findMany({ skip: page * limit, - take: limit, + take: Number(limit), orderBy: { workspace: { [sort]: order @@ -514,6 +515,37 @@ export class WorkspaceService { } } }) + + //calculate metadata for pagination + const totalCount = await this.prisma.workspaceMember.count({ + where: { + workspaceId, + user: { + OR: [ + { + name: { + contains: search + } + }, + { + email: { + contains: search + } + } + ] + } + } + }) + + const metadata = paginate(totalCount, `/workspace/${workspaceId}/members`, { + page: Number(page), + limit: Number(limit), + sort, + order, + search + }) + + return { items, metadata } } async acceptInvitation( @@ -728,9 +760,10 @@ export class WorkspaceService { order: string, search: string ) { - return this.prisma.workspace.findMany({ + //get all workspaces of user for page with limit + const items = await this.prisma.workspace.findMany({ skip: page * limit, - take: limit, + take: Number(limit), orderBy: { [sort]: order }, @@ -754,6 +787,40 @@ export class WorkspaceService { ] } }) + + // get total count of workspaces of the user + const totalCount = await this.prisma.workspace.count({ + where: { + members: { + some: { + userId: user.id + } + }, + OR: [ + { + name: { + contains: search + } + }, + { + description: { + contains: search + } + } + ] + } + }) + + //calculate metadata for pagination + const metadata = paginate(totalCount, `/workspace`, { + page: Number(page), + limit: Number(limit), + sort, + order, + search + }) + + return { items, metadata } } async exportData(user: User, workspaceId: Workspace['id']) { diff --git a/apps/api/src/workspace/workspace.e2e.spec.ts b/apps/api/src/workspace/workspace.e2e.spec.ts index e40c546c..5a1c1e44 100644 --- a/apps/api/src/workspace/workspace.e2e.spec.ts +++ b/apps/api/src/workspace/workspace.e2e.spec.ts @@ -1033,8 +1033,23 @@ describe('Workspace Controller Tests', () => { }) expect(response.statusCode).toBe(200) - expect(response.json()).toBeInstanceOf(Array) - expect(response.json()).toHaveLength(1) + expect(response.json().items).toBeInstanceOf(Array) + expect(response.json().items).toHaveLength(1) + + //check metadata + const metadata = response.json().metadata + expect(metadata.totalCount).toEqual(1) + expect(metadata.links.self).toEqual( + `/workspace/${workspace1.id}/members?page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.first).toEqual( + `/workspace/${workspace1.id}/members?page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.previous).toBeNull() + expect(metadata.links.next).toBeNull() + expect(metadata.links.last).toEqual( + `/workspace/${workspace1.id}/members?page=0&limit=10&sort=name&order=asc&search=` + ) }) it('should not be able to get all the members of the workspace if user is not a member', async () => { @@ -1150,7 +1165,53 @@ describe('Workspace Controller Tests', () => { }) expect(response.statusCode).toBe(200) - expect(response.json().length).toEqual(2) + expect(response.json().items.length).toEqual(2) + + //check metadata + const metadata = response.json().metadata + expect(metadata.totalCount).toBe(2) + expect(metadata.links.self).toEqual( + `/workspace?page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.first).toEqual( + `/workspace?page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.previous).toBeNull() + expect(metadata.links.next).toBeNull() + expect(metadata.links.last).toEqual( + `/workspace?page=0&limit=10&sort=name&order=asc&search=` + ) + }) + + it('should be able to fetch the 2nd page of the workspaces the user is a member of', async () => { + await createMembership(memberRole.id, user2.id, workspace1.id, prisma) + const response = await app.inject({ + method: 'GET', + headers: { + 'x-e2e-user-email': user2.email + }, + url: '/workspace?page=1&limit=1' + }) + + expect(response.statusCode).toBe(200) + expect(response.json().items).toHaveLength(1) + + //check metadata + const metadata = response.json().metadata + expect(metadata.totalCount).toEqual(2) + expect(metadata.links.self).toEqual( + `/workspace?page=1&limit=1&sort=name&order=asc&search=` + ) + expect(metadata.links.first).toEqual( + `/workspace?page=0&limit=1&sort=name&order=asc&search=` + ) + expect(metadata.links.previous).toEqual( + `/workspace?page=0&limit=1&sort=name&order=asc&search=` + ) + expect(metadata.links.next).toBeNull() + expect(metadata.links.last).toEqual( + `/workspace?page=1&limit=1&sort=name&order=asc&search=` + ) }) it('should be able to transfer the ownership of the workspace', async () => {