Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow filtering on exact date on flattened field #1079

Merged
merged 7 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/services/filters-parser.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading