Skip to content

Commit

Permalink
🐛 Handle middle logical operator (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
xsalonx authored Sep 13, 2023
1 parent 429bd4b commit f047f1c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
25 changes: 10 additions & 15 deletions app/lib/server/utilities/filterParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class TransformHelper {
}

handleIntermidiateFilterNode(group, groupOperator) {
if (typeof group !== 'object') {
return group;
}

const transformedSubfilter = this.transformHelper(group);
return this.opts.pruneRedundantANDOperator && groupOperator === defaultGroupOperator ?
transformedSubfilter :
Expand All @@ -51,17 +55,6 @@ class TransformHelper {
return val.split(/,/).map((v) => v.trim());
}

handelFieldFilterTail(fieldGroup, groupOperator) {
const transformedFieldGroup = Object.fromEntries(Object.entries(fieldGroup)
.map(([relOp, val]) => [
Op[relOp] ?? throwWrapper(new Error(`No relational operator <${relOp}>, only <${[...relationalOperators]}> are allowed`)),
arrayRelationalConditions.has(relOp) ? this.handleArrayValues(val) : val,
]));
return this.opts.pruneRedundantANDOperator && groupOperator === defaultGroupOperator ?
transformedFieldGroup :
{ [Op[groupOperator]]: transformedFieldGroup };
}

pullGroupOperator(group) {
const readGroupOperator = group[_groupOperatorName];
const groupOperator = readGroupOperator ?? defaultGroupOperator;
Expand All @@ -75,14 +68,16 @@ class TransformHelper {
const groupOperator = this.pullGroupOperator(group);
if (!reservedNames.has(k)) { // Assumes that k is field from db view
if (typeof group === 'object') {
return [k, this.handelFieldFilterTail(group, groupOperator)];
return [k, this.handleIntermidiateFilterNode(group, groupOperator)];
} else {
return [k, group]; // Assumes that there is not relation operator
}
} else { // Then k stands for logical operator
} else { // Then k stands for operator
return [
Op[k] ?? throwWrapper(new Error(`No logical operator <${k}>, only <${[...logicalOperators]}> are allowed`)),
this.handleIntermidiateFilterNode(group, groupOperator),
Op[k] ?? throwWrapper(new Error(`No operator <${k}> is allowed, only <${[...reservedNames]}> are allowed`)),
arrayRelationalConditions.has(k) ?
this.handleArrayValues(group)
: this.handleIntermidiateFilterNode(group, groupOperator),
];
}
}));
Expand Down
16 changes: 2 additions & 14 deletions test/lib/server/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@ module.exports = () => {
it('should return empty object when provided with undefined or empty object', () => {
assert.deepStrictEqual(filterToSequelizeWhereClause(undefined), {});
assert.deepStrictEqual(filterToSequelizeWhereClause({}), {});
}),

it('should throw error when filter object has an non-existant ', () => {
const srcFilter = {
not: {
nand: {
field3: { eq: 1 },
},
},
};

assert.throws(() => filterToSequelizeWhereClause(srcFilter));
}),
});

it('should handle syntax for default relational operator \'eq\'', () => {
const srcFilter = {
Expand All @@ -48,7 +36,7 @@ module.exports = () => {
};

assert.deepStrictEqual(expectedFilter, filterToSequelizeWhereClause(srcFilter));
}),
});

it('should transform filter object - with pruning', () => {
const srcFilter = {
Expand Down

0 comments on commit f047f1c

Please sign in to comment.