From d6d19e276fa4dd9ebd911385b754b45930624527 Mon Sep 17 00:00:00 2001 From: lhonRafaat Date: Sat, 21 Sep 2024 02:11:21 +0300 Subject: [PATCH] feat: update query middleware updated the middleware to support multiple ref items & number parsing --- src/common/helper/common-types.ts | 13 ++++++--- src/common/helper/query-middleware.ts | 38 +++++++++++++++++++++++++-- src/modules/users/users.service.ts | 6 ----- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/common/helper/common-types.ts b/src/common/helper/common-types.ts index 6851fd7..666180e 100644 --- a/src/common/helper/common-types.ts +++ b/src/common/helper/common-types.ts @@ -2,14 +2,21 @@ import { Request } from 'express'; import { TUser } from '../../modules/users/user.model'; import { ApiProperty, getSchemaPath } from '@nestjs/swagger'; import { InferSubjects } from '@casl/ability'; +import { ObjectId } from 'mongoose'; export interface queryObj { regular: { - [field: string]: { [operator: string]: string }; + [field: string]: { + [operator: string]: string | string[] | number | ObjectId[]; + }; }; references: { - paths: Array; - value: string; + [reference: string]: { + paths: Array; + value: { + [operator: string]: string | string[] | number | ObjectId[]; + }; + }; }; } diff --git a/src/common/helper/query-middleware.ts b/src/common/helper/query-middleware.ts index cb60c24..59eacb5 100644 --- a/src/common/helper/query-middleware.ts +++ b/src/common/helper/query-middleware.ts @@ -19,6 +19,7 @@ export class QueryMiddleware implements NestMiddleware { parsedQueryObj, ); + this.parseNumbers(queryObj?.regular); req.queryObj = queryObj; next(); } @@ -57,13 +58,25 @@ export class QueryMiddleware implements NestMiddleware { if (this.isNestedField(key)) { if (this.isReference(key)) { const splitByDot = field.replace('-ref', '').split('.'); + const referenceField = splitByDot[0]; + const others = splitByDot.slice(1); + const addOptionsI = operator === '$regex'; parsedQueryObj = { ...parsedQueryObj, references: { ...parsedQueryObj?.references, - paths: [...splitByDot], - value, + [referenceField]: { + paths: [...others], + value: addOptionsI + ? { + [operator]: value, + $options: 'i', + } + : { + [operator]: value, + }, + }, }, }; } else { @@ -150,4 +163,25 @@ export class QueryMiddleware implements NestMiddleware { }); }); } + parseNumbers(obj) { + // Recursively traverse the object + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + const value = obj[key]; + + if (typeof value === 'object' && value !== null) { + // Skip parsing if $regex operator is found + if (key === '$regex') continue; + + // Recursively call parseNumbers on nested objects + this.parseNumbers(value); + } else { + // Convert string to number if possible + if (!isNaN(value) && key !== '$regex') { + obj[key] = Number(value); + } + } + } + } + } } diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 7eb1a14..0f81cd0 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -20,12 +20,6 @@ export class UsersService { .sort({ [req.pagination.sort]: req.pagination.sortBy === 'desc' ? -1 : 1, }); - if (req.queryObj?.references) { - users.populate( - req.queryObj?.references.paths, - req.queryObj?.references.value, - ); - } const total = await users.clone().countDocuments();