Skip to content

Commit

Permalink
feat(api): Add pagination metadata to Integration module (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 authored Jul 29, 2024
1 parent d4be243 commit 8ad6932
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ export class IntegrationController {
}

/* istanbul ignore next */
// The e2e tests are not working, but the API calls work as expected
@Get('all/:workspaceId')
@RequiredApiKeyAuthorities(Authority.READ_INTEGRATION)
async getAllIntegrations(
@CurrentUser() user: User,
@Param('workspaceId') workspaceId: string,
@Query('page') page: number = 1,
@Query('page') page: number = 0,
@Query('limit') limit: number = 10,
@Query('sort') sort: string = 'name',
@Query('order') order: string = 'asc',
Expand Down
31 changes: 31 additions & 0 deletions apps/api/src/integration/integration.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { MAIL_SERVICE } from '../mail/services/interface.service'
import { MockMailService } from '../mail/services/mock.service'
import { EnvironmentModule } from '../environment/environment.module'
import { EnvironmentService } from '../environment/service/environment.service'
import { QueryTransformPipe } from '../common/query.transform.pipe'

describe('Integration Controller Tests', () => {
let app: NestFastifyApplication
Expand Down Expand Up @@ -66,6 +67,8 @@ describe('Integration Controller Tests', () => {
projectService = moduleRef.get(ProjectService)
environmentService = moduleRef.get(EnvironmentService)

app.useGlobalPipes(new QueryTransformPipe())

await app.init()
await app.getHttpAdapter().getInstance().ready()
})
Expand Down Expand Up @@ -599,6 +602,34 @@ describe('Integration Controller Tests', () => {
expect(result.json().id).toEqual(integration1.id)
})

it('should be able to fetch all integrations on first page', async () => {
const result = await app.inject({
method: 'GET',
url: `/integration/all/${workspace1.id}?page=0&limit=10`,
headers: {
'x-e2e-user-email': user1.email
}
})

expect(result.statusCode).toEqual(200)
expect(result.json().items).toHaveLength(1)

//check metadata
const metadata = result.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/integration/all/${workspace1.id}?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.first).toEqual(
`/integration/all/${workspace1.id}?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/integration/all/${workspace1.id}?page=0&limit=10&sort=name&order=asc&search=`
)
})

it('should not be able to fetch an integration that does not exist', async () => {
const result = await app.inject({
method: 'GET',
Expand Down
30 changes: 29 additions & 1 deletion apps/api/src/integration/service/integration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { UpdateIntegration } from '../dto/update.integration/update.integration'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import createEvent from '../../common/create-event'
import IntegrationFactory from '../plugins/factory/integration.factory'
import { paginate } from '../../common/paginate'

@Injectable()
export class IntegrationService {
Expand Down Expand Up @@ -302,7 +303,34 @@ export class IntegrationService {
}
})

return integrations
//calculate metadata for pagination
const totalCount = await this.prisma.integration.count({
where: {
name: {
contains: search
},
workspaceId,
OR: [
{
projectId: null
},
{
projectId: {
in: projectIds
}
}
]
}
})
const metadata = paginate(totalCount, `/integration/all/${workspaceId}`, {
page,
limit,
sort,
order,
search
})

return { items: integrations, metadata }
}

async deleteIntegration(user: User, integrationId: Integration['id']) {
Expand Down

0 comments on commit 8ad6932

Please sign in to comment.