From ad7ff86155fc15b5bca43f215b8abcb60d848280 Mon Sep 17 00:00:00 2001 From: Guilhem CARRON <gcarron@grandlyon.com> Date: Fri, 2 Dec 2022 14:48:06 +0000 Subject: [PATCH] feat: Add limit number for structure search --- src/structures/services/structures.service.spec.ts | 3 +++ src/structures/services/structures.service.ts | 11 ++++++++++- src/structures/structures.controller.ts | 7 ++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/structures/services/structures.service.spec.ts b/src/structures/services/structures.service.spec.ts index 9cb53bcb6..4c11ff059 100644 --- a/src/structures/services/structures.service.spec.ts +++ b/src/structures/services/structures.service.spec.ts @@ -37,6 +37,7 @@ const mockStructureModel = { countDocuments: jest.fn(), findOne: jest.fn(), findById: jest.fn(), + limit: jest.fn(), findByIdAndUpdate: jest.fn(), exec: jest.fn(), find: jest.fn(), @@ -282,6 +283,7 @@ describe('StructuresService', () => { describe('searchForStructures', () => { jest.setTimeout(30000); mockStructureModel.find.mockReturnThis(); + mockStructureModel.limit.mockReturnThis(); mockStructureModel.populate.mockReturnThis(); mockStructureModel.exec.mockResolvedValue([ { @@ -595,6 +597,7 @@ describe('StructuresService', () => { jest.spyOn(service, 'findAll').mockResolvedValue(mockResinStructures as StructureDocument[]); mockStructureModel.findByIdAndUpdate.mockReturnThis(); mockStructureModel.findById.mockReturnThis(); + mockStructureModel.limit.mockReturnThis(); mockStructureModel.populate.mockReturnThis(); mockStructureModel.exec.mockResolvedValueOnce([]); expect(await service.bindCNFSids()).toBe(`2 structures affected`); diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 426fcd81c..c156e2b2b 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -56,7 +56,12 @@ export class StructuresService { }); } - async searchForStructures(text: string, filters?: Array<any>, fields?: string[]): Promise<StructureDocument[]> { + async searchForStructures( + text: string, + filters?: Array<any>, + fields?: string[], + limit?: number + ): Promise<StructureDocument[]> { this.logger.debug( `searchForStructures : ${text} | filters: ${JSON.stringify(filters)} | fields : ${JSON.stringify(fields)} | ` ); @@ -86,6 +91,7 @@ export class StructuresService { }) .populate('personalOffers') .populate('structureType') + .limit(limit) .exec(); } else if (filters?.length > 0 && multipleFilters?.length > 0) { structures = await this.structureModel @@ -96,6 +102,7 @@ export class StructuresService { }) .populate('personalOffers') .populate('structureType') + .limit(limit) .exec(); } else if (filters?.length == 0 && multipleFilters?.length > 0) { structures = await this.structureModel @@ -105,6 +112,7 @@ export class StructuresService { }) .populate('personalOffers') .populate('structureType') + .limit(limit) .exec(); } else { structures = await this.structureModel @@ -114,6 +122,7 @@ export class StructuresService { }) .populate('personalOffers') .populate('structureType') + .limit(limit) .exec(); } diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index b1b72e642..5f7d9fcba 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -81,7 +81,12 @@ export class StructuresController { @Post('search') public async search(@Query() query: QueryStructure, @Body() body): Promise<Structure[]> { this.logger.debug(`search`); - return this.structureService.searchForStructures(query.query, body ? body.filters : null); + return this.structureService.searchForStructures( + query.query, + body ? body.filters : null, + null, + body?.limit || null + ); } @Post('searchByName') -- GitLab