From 02a4dbd9438768d590e14f6ef7b57a289a6f1a64 Mon Sep 17 00:00:00 2001 From: Yazeed Loonat Date: Thu, 14 Mar 2024 19:32:11 -0700 Subject: [PATCH] fix: afs needs to be paginating (#3955) --- .../services/application-flagged-set.service.ts | 17 ++++++++++++++++- .../application-flagged-set.service.spec.ts | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/api/src/services/application-flagged-set.service.ts b/api/src/services/application-flagged-set.service.ts index 848aa9b917..22bd030d89 100644 --- a/api/src/services/application-flagged-set.service.ts +++ b/api/src/services/application-flagged-set.service.ts @@ -24,7 +24,11 @@ import { AfsQueryParams } from '../dtos/application-flagged-sets/afs-query-param import { AfsMeta } from '../dtos/application-flagged-sets/afs-meta.dto'; import { OrderByEnum } from '../enums/shared/order-by-enum'; import { View } from '../enums/application-flagged-sets/view'; -import { buildPaginationMetaInfo } from '../utilities/pagination-helpers'; +import { + buildPaginationMetaInfo, + calculateSkip, + calculateTake, +} from '../utilities/pagination-helpers'; import { AfsResolve } from '../dtos/application-flagged-sets/afs-resolve.dto'; import { User } from '../dtos/users/user.dto'; import { Application } from '../dtos/applications/application.dto'; @@ -67,6 +71,15 @@ export class ApplicationFlaggedSetService implements OnModuleInit { where: whereClause, }); + // if passed in page and limit would result in no results because there aren't that many listings + // revert back to the first page + let page = params.page; + if (count && params.limit && params.limit !== 'all' && params.page > 1) { + if (Math.ceil(count / params.limit) < params.page) { + page = 1; + } + } + const rawAfs = await this.prisma.applicationFlaggedSet.findMany({ include: { listings: true, @@ -80,6 +93,8 @@ export class ApplicationFlaggedSetService implements OnModuleInit { orderBy: { id: OrderByEnum.DESC, }, + skip: calculateSkip(params.limit, page), + take: calculateTake(params.limit), }); const totalFlagged = await this.prisma.applicationFlaggedSet.count({ diff --git a/api/test/unit/services/application-flagged-set.service.spec.ts b/api/test/unit/services/application-flagged-set.service.spec.ts index dfa0cd8f24..8e1e34f8b6 100644 --- a/api/test/unit/services/application-flagged-set.service.spec.ts +++ b/api/test/unit/services/application-flagged-set.service.spec.ts @@ -409,6 +409,7 @@ describe('Testing application flagged set service', () => { orderBy: { id: OrderByEnum.DESC, }, + skip: 0, }); expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(1, {