Skip to content

Commit

Permalink
feat: update query middleware
Browse files Browse the repository at this point in the history
updated the middleware to support multiple ref items & number parsing
  • Loading branch information
LhonRafaat committed Sep 20, 2024
1 parent 1bae2c1 commit d6d19e2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/common/helper/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
value: string;
[reference: string]: {
paths: Array<string>;
value: {
[operator: string]: string | string[] | number | ObjectId[];
};
};
};
}

Expand Down
38 changes: 36 additions & 2 deletions src/common/helper/query-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class QueryMiddleware implements NestMiddleware {
parsedQueryObj,
);

this.parseNumbers(queryObj?.regular);
req.queryObj = queryObj;
next();
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
}
}
}
}
6 changes: 0 additions & 6 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit d6d19e2

Please sign in to comment.