Skip to content

Commit b2dcb76

Browse files
VincentMoliniearnaudbesnier
authored andcommitted
[*] Technical - Refactorize filters-parser (#280)
1 parent 1b41467 commit b2dcb76

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Changed
88
- Technical - Upgrade `mongoose` devDependency to the latest version.
99
- Performance optimization - In a request with no smart fields, do not return fields that are hidden from the UI.
10+
- Technical - Refactorize `filters-parser`.
1011

1112
### Fixed
1213
- Technical - Exclude from pre-commit-hook linter files that are not committed.

src/services/filters-parser.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ const AGGREGATOR_OPERATORS = ['and', 'or'];
88
function FiltersParser(model, timezone, options) {
99
const schema = Interface.Schemas.schemas[utils.getModelName(model)];
1010

11+
const parseInteger = (value) => Number.parseInt(value, 10);
12+
const parseDate = (value) => new Date(value);
13+
const parseBoolean = (value) => {
14+
if (value === 'true') { return true; }
15+
if (value === 'false') { return false; }
16+
return typeof value === 'boolean' ? value : null;
17+
};
18+
const parseString = (value) => {
19+
// NOTICE: Check if the value is a real ObjectID. By default, the isValid method returns true
20+
// for a random string with length 12 (example: 'Black Friday').
21+
const { ObjectId } = options.mongoose.Types;
22+
if (ObjectId.isValid(value) && ObjectId(value).toString() === value) {
23+
return ObjectId(value);
24+
}
25+
if (_.isArray(value)) {
26+
return value
27+
.map((arrayValue) => (ObjectId.isValid(arrayValue) ? ObjectId(arrayValue) : arrayValue));
28+
}
29+
return value;
30+
};
31+
const parseArray = (value) => ({ $size: value });
32+
const parseOther = (value) => value;
33+
1134
this.operatorDateParser = new BaseOperatorDateParser({
1235
operators: { GTE: '$gte', LTE: '$lte' },
1336
timezone,
@@ -47,10 +70,21 @@ function FiltersParser(model, timezone, options) {
4770
};
4871
};
4972

50-
this.parseFunction = async (key) => {
73+
this.getParserForType = (type) => {
74+
switch (type) {
75+
case 'Number': return parseInteger;
76+
case 'Date': return parseDate;
77+
case 'Boolean': return parseBoolean;
78+
case 'String': return parseString;
79+
case _.isArray(type): return parseArray;
80+
default: return parseOther;
81+
}
82+
};
83+
84+
this.getParserForField = async (key) => {
5185
const [fieldName, subfieldName] = key.split(':');
5286

53-
// Mongoose Aggregate don't parse the value automatically.
87+
// NOTICE: Mongoose Aggregate don't parse the value automatically.
5488
let field = _.find(schema.fields, { field: fieldName });
5589

5690
if (!field) {
@@ -63,37 +97,8 @@ function FiltersParser(model, timezone, options) {
6397
}
6498

6599
if (!field) return (val) => val;
66-
switch (field.type) {
67-
case 'Number':
68-
return parseInt;
69-
case 'Date':
70-
return (val) => new Date(val);
71-
case 'Boolean':
72-
return (val) => {
73-
if (val === 'true') { return true; }
74-
if (val === 'false') { return false; }
75-
return typeof val === 'boolean' ? val : null;
76-
};
77-
case 'String':
78-
return (val) => {
79-
// NOTICE: Check if the value is a real ObjectID. By default, the
80-
// isValid method returns true for a random string with length 12.
81-
// Example: 'Black Friday'.
82-
const { ObjectId } = options.mongoose.Types;
83-
if (ObjectId.isValid(val) && ObjectId(val).toString() === val) {
84-
return ObjectId(val);
85-
}
86-
if (_.isArray(val)) {
87-
return val.map((value) => (ObjectId.isValid(value) ? ObjectId(value) : value));
88-
}
89-
return val;
90-
};
91-
default:
92-
if (_.isArray(field.type)) {
93-
return (val) => ({ $size: val });
94-
}
95-
return (val) => val;
96-
}
100+
101+
return this.getParserForType(field.type);
97102
};
98103

99104
this.formatAggregatorOperator = (aggregatorOperator) => {
@@ -106,7 +111,7 @@ function FiltersParser(model, timezone, options) {
106111
return this.operatorDateParser.getDateFilter(operator, value);
107112
}
108113

109-
const parseFct = await this.parseFunction(field);
114+
const parseFct = await this.getParserForField(field);
110115

111116
switch (operator) {
112117
case 'not':

0 commit comments

Comments
 (0)