Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add pagination metadata to Workspace module #387

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 71 additions & 4 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,8 +465,8 @@ 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,
orderBy: {
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,
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
70 changes: 67 additions & 3 deletions apps/api/src/workspace/workspace.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { EventModule } from '../event/event.module'
import { UserModule } from '../user/user.module'
import { UserService } from '../user/service/user.service'
import { WorkspaceService } from './service/workspace.service'
import { QueryTransformPipe } from '../common/query.transform.pipe'

const createMembership = async (
adminRoleId: string,
Expand Down Expand Up @@ -75,6 +76,8 @@ describe('Workspace Controller Tests', () => {
userService = moduleRef.get(UserService)
workspaceService = moduleRef.get(WorkspaceService)

app.useGlobalPipes(new QueryTransformPipe())

await app.init()
await app.getHttpAdapter().getInstance().ready()
})
Expand Down Expand Up @@ -1033,8 +1036,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 +1168,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
Loading