Skip to content

Commit

Permalink
fix: allow filtering on exact date on flattened field (#1079)
Browse files Browse the repository at this point in the history
* fix: allow filtering on exact date on flattened field [skip ci]

* ci: force ci with empty commit

* fix: use same separator in filter parser

* ci: force ci with empty commit

* test: fix unit tests

* refactor: review suggestion

Co-authored-by: SteveBunlon <SteveBunlon@users.noreply.github.com>

---------

Co-authored-by: Nicolas Moreau <nicolas.moreau76@gmail.com>
Co-authored-by: SteveBunlon <SteveBunlon@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 5, 2023
1 parent 6fe7ba3 commit 9ad89fe
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/services/filters-parser.js
Expand Up @@ -6,6 +6,8 @@ import Flattener from './flattener';

const AGGREGATOR_OPERATORS = ['and', 'or'];

const { FLATTEN_SEPARATOR } = Flattener;

function FiltersParser(model, timezone, options) {
const modelSchema = Interface.Schemas.schemas[utils.getModelName(model)];

Expand Down Expand Up @@ -95,7 +97,7 @@ function FiltersParser(model, timezone, options) {
throw new InvalidFiltersFormatError(`Field '${fieldName}' not found on collection '${modelSchema.name}'`);
}

const fieldPath = subfieldName ? `${fieldName}.${subfieldName}` : fieldName;
const fieldPath = subfieldName ? `${fieldName}${FLATTEN_SEPARATOR}${subfieldName}` : fieldName;

// NOTICE: either nested or virtual, not both
const fieldType = field.isVirtual
Expand Down
4 changes: 4 additions & 0 deletions src/services/flattener.js
Expand Up @@ -13,6 +13,8 @@ module.exports = class Flattener {
this.lianaOptions = lianaOptions;
}

static FLATTEN_SEPARATOR = FLATTEN_SEPARATOR;

_removeWrongFlattenConfiguration(index) {
this.flatten.splice(index, 1);
}
Expand Down Expand Up @@ -146,6 +148,8 @@ module.exports = class Flattener {
if (!_.isEmpty(request.query?.context)) {
request.query.context.field = Flattener.unflattenFieldName(request.query.context.field);
}
// Note: filter and sorts are not unflattened here because
// they are checked against forest schema later on
return next();
} catch (error) { return next(error); }
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/schema.js
Expand Up @@ -14,10 +14,12 @@ exports.getModelName = (model) => model.modelName;
// TODO: Remove nameOld attribute once the lianas versions older than 2.0.0 are minority
exports.getModelNameOld = (model) => model.collection.name.replace(' ', '');

const { FLATTEN_SEPARATOR } = require('../services/flattener');

const getNestedFieldType = (mongooseSchema, nestedFieldPath) => {
if (!mongooseSchema || !nestedFieldPath) return undefined;

const [currentFieldName, ...deepNestedFieldPath] = nestedFieldPath.split('.');
const [currentFieldName, ...deepNestedFieldPath] = nestedFieldPath.split(FLATTEN_SEPARATOR);

let nestedFieldDeclaration;

Expand All @@ -37,7 +39,7 @@ const getNestedFieldType = (mongooseSchema, nestedFieldPath) => {
return nestedFieldDeclaration.type || nestedFieldDeclaration;
}

return getNestedFieldType(nestedFieldDeclaration, deepNestedFieldPath?.join('.'));
return getNestedFieldType(nestedFieldDeclaration, deepNestedFieldPath?.join(FLATTEN_SEPARATOR));
};

exports.getNestedFieldType = getNestedFieldType;
Expand Down
3 changes: 2 additions & 1 deletion test/tests/services/query-builder.test.js
Expand Up @@ -3,8 +3,9 @@ import loadFixture from 'mongoose-fixture-loader';
import Interface from 'forest-express';
import mongooseConnect from '../../utils/mongoose-connect';
import QueryBuilder from '../../../src/services/query-builder';
import Flattener from '../../../src/services/flattener';

const FLATTEN_SEPARATOR = '@@@';
const { FLATTEN_SEPARATOR } = Flattener;

describe('service > query-builder', () => {
let TreeModel;
Expand Down
26 changes: 13 additions & 13 deletions test/tests/utils/schema.test.js
Expand Up @@ -47,21 +47,21 @@ describe('schema', () => {
it('should correctly detect the type', () => {
expect.assertions(2);
expect(
getNestedFieldType(nestedModelSchema, 'engineSubSchemaAndType._id'),
getNestedFieldType(nestedModelSchema, 'engineSubSchemaAndType@@@_id'),
).toStrictEqual(mongoose.Schema.Types.ObjectId);
expect(
getNestedFieldType(nestedModelSchema, 'engineSubSchemaAndType.horsePower'),
getNestedFieldType(nestedModelSchema, 'engineSubSchemaAndType@@@horsePower'),
).toStrictEqual(String);
});
});
describe('when the nested field is not defined using a type', () => {
it('should correctly detect the type', () => {
expect.assertions(2);
expect(
getNestedFieldType(nestedModelSchema, 'engineSubSchema._id'),
getNestedFieldType(nestedModelSchema, 'engineSubSchema@@@_id'),
).toStrictEqual(mongoose.Schema.Types.ObjectId);
expect(
getNestedFieldType(nestedModelSchema, 'engineSubSchema.horsePower'),
getNestedFieldType(nestedModelSchema, 'engineSubSchema@@@horsePower'),
).toStrictEqual(String);
});
});
Expand All @@ -72,21 +72,21 @@ describe('schema', () => {
it('should correctly detect the type', () => {
expect.assertions(2);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedDocumentAndType._id'),
getNestedFieldType(nestedModelSchema, 'engineNestedDocumentAndType@@@_id'),
).toStrictEqual(mongoose.Schema.Types.ObjectId);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedDocumentAndType.horsePower'),
getNestedFieldType(nestedModelSchema, 'engineNestedDocumentAndType@@@horsePower'),
).toStrictEqual(String);
});
});
describe('when the nested field is not defined using a type', () => {
it('should correctly detect the type', () => {
expect.assertions(2);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedDocument._id'),
getNestedFieldType(nestedModelSchema, 'engineNestedDocument@@@_id'),
).toStrictEqual(mongoose.Schema.Types.ObjectId);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedDocument.horsePower'),
getNestedFieldType(nestedModelSchema, 'engineNestedDocument@@@horsePower'),
).toStrictEqual(String);
});
});
Expand All @@ -98,21 +98,21 @@ describe('schema', () => {
it('should correctly detect the type', () => {
expect.assertions(2);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedPathAndType._id'),
getNestedFieldType(nestedModelSchema, 'engineNestedPathAndType@@@_id'),
).toStrictEqual(mongoose.Schema.Types.ObjectId);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedPathAndType.horsePower'),
getNestedFieldType(nestedModelSchema, 'engineNestedPathAndType@@@horsePower'),
).toStrictEqual(String);
});
});
describe('when the nested field is not defined using a type', () => {
it('should correctly detect the type', () => {
expect.assertions(2);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedPath._id'),
getNestedFieldType(nestedModelSchema, 'engineNestedPath@@@_id'),
).toStrictEqual(mongoose.Schema.Types.ObjectId);
expect(
getNestedFieldType(nestedModelSchema, 'engineNestedPath.horsePower'),
getNestedFieldType(nestedModelSchema, 'engineNestedPath@@@horsePower'),
).toStrictEqual(String);
});
});
Expand All @@ -123,7 +123,7 @@ describe('schema', () => {
expect.assertions(1);

expect(
getNestedFieldType(nestedModelSchema, 'engineNestedPath.notExisting'),
getNestedFieldType(nestedModelSchema, 'engineNestedPath@@@notExisting'),
).toBeUndefined();
});
});
Expand Down

0 comments on commit 9ad89fe

Please sign in to comment.