Skip to content

Commit

Permalink
fix(smart field): fix filter on smart reference (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentMolinie authored Sep 14, 2021
1 parent a729937 commit 41edb92
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 387 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@babel/runtime": "7.10.1",
"bluebird": "2.9.25",
"core-js": "3.6.5",
"forest-express": "9.2.2",
"forest-express": "9.2.4",
"http-errors": "1.6.1",
"lodash": "4.17.21",
"moment": "2.19.4",
Expand Down
37 changes: 11 additions & 26 deletions src/services/filters-parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BaseFiltersParser, BaseOperatorDateParser, Schemas } from 'forest-express';
import {
BaseFiltersParser, BaseOperatorDateParser, Schemas, SchemaUtils,
} from 'forest-express';
import Operators from '../utils/operators';
import { NoMatchingOperatorError } from './errors';

Expand All @@ -9,27 +11,19 @@ function FiltersParser(modelSchema, timezone, options) {
this.operatorDateParser = new BaseOperatorDateParser({ operators: this.OPERATORS, timezone });

this.perform = async (filtersString) =>
BaseFiltersParser.perform(filtersString, this.formatAggregation, this.formatCondition);
BaseFiltersParser.perform(
filtersString, this.formatAggregation, this.formatCondition, modelSchema,
);

this.formatAggregation = async (aggregator, formattedConditions) => {
const aggregatorOperator = this.formatAggregatorOperator(aggregator);
return { [aggregatorOperator]: formattedConditions };
};

this.formatCondition = async (condition) => {
this.formatCondition = async (condition, isSmartField = false) => {
const isTextField = this.isTextField(condition.field);
if (this.isSmartField(modelSchema, condition.field)) {
const fieldFound = modelSchema.fields.find((field) => field.field === condition.field);

if (!fieldFound.filter) throw new Error(`"filter" method missing on smart field "${fieldFound.field}"`);

const formattedCondition = fieldFound
.filter({
where: this.formatOperatorValue(condition.operator, condition.value, isTextField),
condition,
});
if (!formattedCondition) throw new Error(`"filter" method on smart field "${fieldFound.field}" must return a condition`);
return formattedCondition;
if (isSmartField) {
return this.formatOperatorValue(condition.operator, condition.value, isTextField);
}

const formattedField = this.formatField(condition.field);
Expand Down Expand Up @@ -117,22 +111,13 @@ function FiltersParser(modelSchema, timezone, options) {
Schemas.schemas, modelSchema, associationName, fieldName,
);
if (associationSchema) {
return this.getFieldTypeFromSchema(associationSchema, field) === 'String';
return SchemaUtils.getFieldType(associationSchema, field) === 'String';
}
return false;
}
return this.getFieldTypeFromSchema(modelSchema, field) === 'String';
return SchemaUtils.getFieldType(modelSchema, field) === 'String';
};

this.isSmartField = (schema, fieldName) => {
const fieldFound = schema.fields.find((field) => field.field === fieldName);
return !!fieldFound && !!fieldFound.isVirtual;
};

this.getFieldTypeFromSchema = (schema, fieldName) => {
const fieldFound = schema.fields.find((field) => field.field === fieldName);
return fieldFound && fieldFound.type;
};

// NOTICE: Look for a previous interval condition matching the following:
// - If the filter is a simple condition at the root the check is done right away.
Expand Down
4 changes: 3 additions & 1 deletion src/utils/orm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { SchemaUtils } = require('forest-express');

const semver = require('semver');

const REGEX_VERSION = /(\d+\.)?(\d+\.)?(\*|\d+)/;
Expand Down Expand Up @@ -26,7 +28,7 @@ const findRecord = (model, recordId, options) => {
};

const getColumnName = (schema, fieldName) => {
const schemaField = schema.fields.find((field) => field.field === fieldName);
const schemaField = SchemaUtils.getField(schema, fieldName);
return (schemaField && schemaField.columnName) ? schemaField.columnName : fieldName;
};

Expand Down
Loading

0 comments on commit 41edb92

Please sign in to comment.