Skip to content

Commit

Permalink
Adds order by parameter in search API.
Browse files Browse the repository at this point in the history
Co-authored-by: Chinmoy Chakraborty <[email protected]>
  • Loading branch information
geeky-abhishek and chinmoy12c authored Aug 1, 2023
1 parent eb4377d commit c6fade4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/modules/bot/bot.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!`));
});
});
12 changes: 11 additions & 1 deletion src/modules/bot/bot.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -139,7 +140,7 @@ export class BotController {
"startingMessage",
"name",
"status",
"createdDate",
"createdAt",
"endDate",
"ownerid",
"ownerorgid",
Expand All @@ -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),
Expand All @@ -157,6 +166,7 @@ export class BotController {
body.ownerId,
body.ownerOrgId,
sortBy,
orderBy
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/modules/bot/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const MockPrismaService = {
return JSON.parse(JSON.stringify(mockBotsDb));
}
},
count: () => 10,
update: jest.fn()
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -533,7 +534,8 @@ describe('BotService', () => {
true,
'',
'',
'sortParameter'
'sortParameter',
'desc'
);
expect(resp).toEqual({"data": "sortedBots", "totalCount": 10});
})
Expand Down
9 changes: 7 additions & 2 deletions src/modules/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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) {
Expand Down

0 comments on commit c6fade4

Please sign in to comment.