Skip to content

Commit 9fad405

Browse files
committed
Refactor countAll method in IdentitiesCrudService to enforce a maximum filter limit and update response structure in IdentitiesCrudController
1 parent 1f9fe02 commit 9fad405

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/management/identities/identities-crud.controller.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,22 @@ export class IdentitiesCrudController extends AbstractController {
207207
[key: string]: FilterSchema;
208208
},
209209
@SearchFilterOptions() searchFilterOptions: FilterOptions,
210-
): Promise<Response<number>> {
211-
const filters = {}
212-
for (const key in body) {
213-
filters[key] = filterSchema(body[key]);
214-
console.log('filters', key, body[key], filters[key]);
215-
}
210+
): Promise<Response<{
211+
statusCode: number;
212+
data: {
213+
[key: string]: number;
214+
};
215+
}>> {
216+
const filters = Object.entries(body).reduce((acc, [key, value]) => {
217+
acc[key] = filterSchema(value);
218+
return acc;
219+
}, {} as Record<string, FilterSchema>);
216220

217-
const totals = await this._service.countAll(filters, searchFilterOptions);
221+
const data = await this._service.countAll(filters, searchFilterOptions);
218222

219223
return res.status(HttpStatus.OK).json({
220224
statusCode: HttpStatus.OK,
221-
data: totals,
225+
data,
222226
});
223227
}
224228

src/management/identities/identities-crud.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { Identities } from '~/management/identities/_schemas/identities.schema';
77
import { BadRequestException, HttpException } from '@nestjs/common';
88
import { CountOptions } from 'mongodb';
99

10+
export const COUNT_ALL_MAX_ITERATIONS = 500;
11+
1012
export class IdentitiesCrudService extends AbstractIdentitiesService {
1113
public async create<T extends AbstractSchema | Document>(
1214
data?: any,
@@ -138,8 +140,8 @@ export class IdentitiesCrudService extends AbstractIdentitiesService {
138140
public async countAll<T extends AbstractSchema | Document>(filters: {
139141
[key: string]: FilterQuery<T>;
140142
}, options?: (CountOptions & MongooseBaseQueryOptions<T>) | null) {
141-
if (Object.keys(filters).length >= 5) {
142-
throw new BadRequestException('Too many filters');
143+
if (Object.keys(filters).length >= COUNT_ALL_MAX_ITERATIONS) {
144+
throw new BadRequestException(`Too many filters (max: ${COUNT_ALL_MAX_ITERATIONS})`);
143145
}
144146

145147
const entries = Object.entries(filters);

0 commit comments

Comments
 (0)