From c6fade4e9b51f723db5546f6aa31a2e8c5aa4c03 Mon Sep 17 00:00:00 2001 From: Saiyan Abhishek <74703491+geeky-abhishek@users.noreply.github.com> Date: Tue, 1 Aug 2023 19:02:28 +0530 Subject: [PATCH] Adds order by parameter in search API. Co-authored-by: Chinmoy Chakraborty --- src/modules/bot/bot.controller.spec.ts | 17 +++++++++++++++++ src/modules/bot/bot.controller.ts | 12 +++++++++++- src/modules/bot/bot.service.spec.ts | 6 ++++-- src/modules/bot/bot.service.ts | 9 +++++++-- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/modules/bot/bot.controller.spec.ts b/src/modules/bot/bot.controller.spec.ts index 4c50908..85c4f26 100644 --- a/src/modules/bot/bot.controller.spec.ts +++ b/src/modules/bot/bot.controller.spec.ts @@ -276,9 +276,26 @@ describe('BotController', () => { '', 'true', 'nonExistent', + 'desc', {} )) .rejects .toThrowError(new BadRequestException(`sorting by 'nonExistent' is not supported!`)); }); + + it('search throws error on unknown orderBy value', async () => { + expect(botController.search( + '1', + '1', + '', + '', + 'true', + 'name', + //@ts-ignore + 'nonExistent', + {} + )) + .rejects + .toThrowError(new BadRequestException(`Only asc | desc values are supported in 'orderBy' field!`)); + }); }); diff --git a/src/modules/bot/bot.controller.ts b/src/modules/bot/bot.controller.ts index d742c91..46b9b1c 100644 --- a/src/modules/bot/bot.controller.ts +++ b/src/modules/bot/bot.controller.ts @@ -125,6 +125,7 @@ export class BotController { @Query('startingMessage') startingMessage: string, @Query('match') match: 'true' | 'false', @Query('sortBy') sortBy: string | undefined, + @Query('orderBy') orderBy: 'asc' | 'desc' | undefined, @Body() body: any, ) { if (!perPage) { @@ -139,7 +140,7 @@ export class BotController { "startingMessage", "name", "status", - "createdDate", + "createdAt", "endDate", "ownerid", "ownerorgid", @@ -148,6 +149,14 @@ export class BotController { this.logger.error(`sorting by '${sortBy}' is not supported!`); throw new BadRequestException(`sorting by '${sortBy}' is not supported!`); } + + const allowedOrderingFields = ['asc', 'desc']; + + if (orderBy && !allowedOrderingFields.includes(orderBy)) { + this.logger.error(`Received invalid orderBy value: ${orderBy}!`); + throw new BadRequestException(`Only asc | desc values are supported in 'orderBy' field!`); + } + return await this.botService.search( parseInt(perPage), parseInt(page), @@ -157,6 +166,7 @@ export class BotController { body.ownerId, body.ownerOrgId, sortBy, + orderBy ); } diff --git a/src/modules/bot/bot.service.spec.ts b/src/modules/bot/bot.service.spec.ts index 780114b..d8b9e7f 100644 --- a/src/modules/bot/bot.service.spec.ts +++ b/src/modules/bot/bot.service.spec.ts @@ -43,6 +43,7 @@ const MockPrismaService = { return JSON.parse(JSON.stringify(mockBotsDb)); } }, + count: () => 10, update: jest.fn() } } @@ -524,7 +525,7 @@ describe('BotService', () => { fetchMock.restore(); }) - it('bot update passes orderBy parameter to search', async () => { + it('bot search passes sortBy parameter to prisma', async () => { const resp = await botService.search( 1, 1, @@ -533,7 +534,8 @@ describe('BotService', () => { true, '', '', - 'sortParameter' + 'sortParameter', + 'desc' ); expect(resp).toEqual({"data": "sortedBots", "totalCount": 10}); }) diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index 71a2855..445681e 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -380,6 +380,7 @@ export class BotService { ownerID: string, ownerOrgID: string, sortBy: string | undefined, + orderBy: string | undefined, ): Promise<{ data: Bot[]; totalCount: number } | null> { const startTime = performance.now(); let filterQuery: any = {}; @@ -402,17 +403,21 @@ export class BotService { if (!sortBy) { sortBy = 'id'; } + if (!orderBy) { + orderBy = 'asc'; + } + const count = await this.prisma.bot.count({ where: filterQuery }); const data = await this.prisma.bot.findMany({ skip: perPage * (page - 1), take: perPage, where: filterQuery, include: this.include, orderBy: { - [sortBy]: 'asc' + [sortBy]: orderBy } }); this.logger.log(`BotService::find: Returning response of find query. Time taken: ${performance.now() - startTime} milliseconds.`); - return { data: data, totalCount: data.length }; + return { data: data, totalCount: count }; } async update(id: string, updateBotDto: any) {