Skip to content

Commit

Permalink
Add pagination metadata to workspace service
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 authored and rajdip-b committed Jul 28, 2024
1 parent 90b2e14 commit 56ae1fc
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 8 deletions.
77 changes: 72 additions & 5 deletions apps/api/src/workspace/service/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
},
Expand All @@ -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']) {
Expand Down
67 changes: 64 additions & 3 deletions apps/api/src/workspace/workspace.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 56ae1fc

Please sign in to comment.