Skip to content

Commit

Permalink
feat(api): Add pagination metadata to Workspace Role module (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 authored and rajdip-b committed Jul 29, 2024
1 parent c4cc667 commit d8e8f49
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
32 changes: 28 additions & 4 deletions apps/api/src/workspace-role/service/workspace-role.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import createEvent from '../../common/create-event'
import { WorkspaceRoleWithProjects } from '../workspace-role.types'
import { v4 } from 'uuid'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { paginate, PaginatedMetadata } from '../../common/paginate'

@Injectable()
export class WorkspaceRoleService {
Expand Down Expand Up @@ -303,28 +304,51 @@ export class WorkspaceRoleService {
sort: string,
order: string,
search: string
): Promise<WorkspaceRole[]> {
): Promise<{ items: WorkspaceRole[]; metadata: PaginatedMetadata }> {
await this.authorityCheckerService.checkAuthorityOverWorkspace({
userId: user.id,
entity: { id: workspaceId },
authority: Authority.READ_WORKSPACE_ROLE,
prisma: this.prisma
})

return await this.prisma.workspaceRole.findMany({
//get workspace roles of a workspace for given page and limit
const items = await this.prisma.workspaceRole.findMany({
where: {
workspaceId,
name: {
contains: search
}
},

skip: page * limit,
take: limit,
orderBy: {
[sort]: order
}
})

//calculate metadata
const totalCount = await this.prisma.workspaceRole.count({
where: {
workspaceId,
name: {
contains: search
}
}
})

const metadata = paginate(
totalCount,
`/workspace-role/${workspaceId}/all`,
{
page,
limit,
sort,
order,
search
}
)

return { items, metadata }
}

private async getWorkspaceRoleWithAuthority(
Expand Down
37 changes: 35 additions & 2 deletions apps/api/src/workspace-role/workspace-role.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { EventModule } from '../event/event.module'
import { WorkspaceRoleService } from './service/workspace-role.service'
import { UserService } from '../user/service/user.service'
import { UserModule } from '../user/user.module'
import { QueryTransformPipe } from '../common/query.transform.pipe'

describe('Workspace Role Controller Tests', () => {
let app: NestFastifyApplication
Expand Down Expand Up @@ -58,6 +59,8 @@ describe('Workspace Role Controller Tests', () => {
workspaceRoleService = moduleRef.get(WorkspaceRoleService)
userService = moduleRef.get(UserService)

app.useGlobalPipes(new QueryTransformPipe())

await app.init()
await app.getHttpAdapter().getInstance().ready()
})
Expand Down Expand Up @@ -725,7 +728,22 @@ describe('Workspace Role Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json()).toEqual(expect.arrayContaining(roles))
expect(response.json().items).toEqual(expect.arrayContaining(roles))

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toBe(roles.length)
expect(metadata.links.self).toEqual(
`/workspace-role/${workspaceAlice.id}/all?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.first).toEqual(
`/workspace-role/${workspaceAlice.id}/all?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-role/${workspaceAlice.id}/all?page=0&limit=10&sort=name&order=asc&search=`
)
})

it('should be able to fetch all the roles of a workspace with READ_WORKSPACE_ROLE role', async () => {
Expand Down Expand Up @@ -766,7 +784,22 @@ describe('Workspace Role Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json()).toEqual(expect.arrayContaining(roles))
expect(response.json().items).toEqual(expect.arrayContaining(roles))

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toBe(roles.length)
expect(metadata.links.self).toEqual(
`/workspace-role/${workspaceAlice.id}/all?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.first).toEqual(
`/workspace-role/${workspaceAlice.id}/all?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-role/${workspaceAlice.id}/all?page=0&limit=10&sort=name&order=asc&search=`
)
})

it('should not be able to fetch all the roles of a workspace without READ_WORKSPACE_ROLE role', async () => {
Expand Down

0 comments on commit d8e8f49

Please sign in to comment.