From 49097ea2ab8746d5c8ed0cc5234ea9dfccf52ec2 Mon Sep 17 00:00:00 2001 From: muntaxir4 Date: Sun, 28 Jul 2024 02:27:57 +0530 Subject: [PATCH 1/2] API: Add pagination metadata to Variable module --- .../src/variable/service/variable.service.ts | 25 +++++++++++++++++-- apps/api/src/variable/variable.e2e.spec.ts | 21 +++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/apps/api/src/variable/service/variable.service.ts b/apps/api/src/variable/service/variable.service.ts index 32981ff1..0fc1d83a 100644 --- a/apps/api/src/variable/service/variable.service.ts +++ b/apps/api/src/variable/service/variable.service.ts @@ -28,6 +28,7 @@ import { ChangeNotification, ChangeNotificationEvent } from 'src/socket/socket.types' +import { paginate } from '../../common/paginate' @Injectable() export class VariableService { @@ -520,7 +521,7 @@ export class VariableService { } }, skip: page * limit, - take: limit, + take: Number(limit), orderBy: { [sort]: order } @@ -601,7 +602,27 @@ export class VariableService { } } - return Array.from(variablesWithEnvironmentalValues.values()) + const items = Array.from(variablesWithEnvironmentalValues.values()) + + //calculate metadata + const totalCount = await this.prisma.variable.count({ + where: { + projectId, + name: { + contains: search + } + } + }) + + const metadata = paginate(totalCount, `/variable/${projectId}`, { + page: Number(page), + limit: Number(limit), + sort, + order, + search + }) + + return { items, metadata } } private async variableExists( diff --git a/apps/api/src/variable/variable.e2e.spec.ts b/apps/api/src/variable/variable.e2e.spec.ts index adcd6f3b..3a887322 100644 --- a/apps/api/src/variable/variable.e2e.spec.ts +++ b/apps/api/src/variable/variable.e2e.spec.ts @@ -575,16 +575,16 @@ describe('Variable Controller Tests', () => { it('should be able to fetch all variables', async () => { const response = await app.inject({ method: 'GET', - url: `/variable/${project1.id}`, + url: `/variable/${project1.id}?page=0&limit=10`, headers: { 'x-e2e-user-email': user1.email } }) expect(response.statusCode).toBe(200) - expect(response.json().length).toBe(1) + expect(response.json().items.length).toBe(1) - const { variable, values } = response.json()[0] + const { variable, values } = response.json().items[0] expect(variable).toBeDefined() expect(values).toBeDefined() expect(values.length).toBe(1) @@ -592,6 +592,21 @@ describe('Variable Controller Tests', () => { expect(values[0].environment.id).toBe(environment1.id) expect(variable.id).toBe(variable1.id) expect(variable.name).toBe('Variable 1') + + //check metadata + const metadata = response.json().metadata + expect(metadata.totalCount).toEqual(1) + expect(metadata.links.self).toEqual( + `/variable/${project1.id}?page=0&limit=10&sort=name&order=asc&search=` + ) + expect(metadata.links.first).toEqual( + `/variable/${project1.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( + `/variable/${project1.id}?page=0&limit=10&sort=name&order=asc&search=` + ) }) it('should not be able to fetch all variables if the user has no access to the project', async () => { From fc39b4b8bada2ad37187ac2a7ad244cb04a4dcd3 Mon Sep 17 00:00:00 2001 From: muntaxir4 Date: Sun, 28 Jul 2024 18:35:00 +0530 Subject: [PATCH 2/2] Remove explicit number conversions --- apps/api/src/variable/service/variable.service.ts | 6 +++--- apps/api/src/variable/variable.e2e.spec.ts | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/api/src/variable/service/variable.service.ts b/apps/api/src/variable/service/variable.service.ts index 0fc1d83a..9a932952 100644 --- a/apps/api/src/variable/service/variable.service.ts +++ b/apps/api/src/variable/service/variable.service.ts @@ -521,7 +521,7 @@ export class VariableService { } }, skip: page * limit, - take: Number(limit), + take: limit, orderBy: { [sort]: order } @@ -615,8 +615,8 @@ export class VariableService { }) const metadata = paginate(totalCount, `/variable/${projectId}`, { - page: Number(page), - limit: Number(limit), + page, + limit, sort, order, search diff --git a/apps/api/src/variable/variable.e2e.spec.ts b/apps/api/src/variable/variable.e2e.spec.ts index 3a887322..f4a6c407 100644 --- a/apps/api/src/variable/variable.e2e.spec.ts +++ b/apps/api/src/variable/variable.e2e.spec.ts @@ -36,6 +36,7 @@ import { mockDeep } from 'jest-mock-extended' import { RedisClientType } from 'redis' import { UserService } from '../user/service/user.service' import { UserModule } from '../user/user.module' +import { QueryTransformPipe } from '../common/query.transform.pipe' describe('Variable Controller Tests', () => { let app: NestFastifyApplication @@ -83,6 +84,8 @@ describe('Variable Controller Tests', () => { eventService = moduleRef.get(EventService) userService = moduleRef.get(UserService) + app.useGlobalPipes(new QueryTransformPipe()) + await app.init() await app.getHttpAdapter().getInstance().ready() })