diff --git a/src/structures/services/structures.service.spec.ts b/src/structures/services/structures.service.spec.ts index 9cb53bcb61b0293dfb1d1e842667dd12e8167f20..4c11ff059776f232f5efca0beb1777cc144a6596 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 426fcd81c7028e4449b5d130b6688bcf0c84f4e1..c156e2b2b28df531e0516e97cff3dbe251a07e3f 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 b1b72e6420b806d80d4ad4a5e1e8fae7fc27afa7..5f7d9fcba02f4b63e2b82f4b8aabbbcb7d23682e 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')