From b1c0d9ccb52410feb8376c34540f1839aa6a76a1 Mon Sep 17 00:00:00 2001 From: Arnaud Besnier Date: Fri, 21 Jun 2019 19:12:28 +0200 Subject: [PATCH] [*] Filters - Fix potential chart / records retrieval errors --- CHANGELOG.md | 2 ++ src/services/operator-value-parser.js | 44 ++++++++++++--------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e52a8086..e5b6f05c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Change Log ## [Unreleased] +### Fixed +- Filters - Fix potential chart / records retrieval errors (Regression introduced in v3.2.2). ## RELEASE 3.2.2 - 2019-06-20 ### Changed diff --git a/src/services/operator-value-parser.js b/src/services/operator-value-parser.js index 06590c19..4067f514 100644 --- a/src/services/operator-value-parser.js +++ b/src/services/operator-value-parser.js @@ -3,18 +3,16 @@ import { Schemas } from 'forest-express'; import Operators from '../utils/operators'; import OperatorDateIntervalParser from './operator-date-interval-parser'; -class OperatorValueParser { - constructor(options) { - this.OPERATORS = new Operators(options); - this.options = options; - } +function OperatorValueParser(options) { + const OPERATORS = new Operators(options); - perform(model, fieldName, value, timezone) { + this.perform = (model, fieldName, value, timezone) => { const operatorDateIntervalParser = new OperatorDateIntervalParser( value, timezone, - this.options, + options, ); + // NOTICE: Handle boolean for MySQL database let modelName; let field; @@ -47,39 +45,37 @@ class OperatorValueParser { if (value[0] === '!' && value[1] !== '*') { value = value.substring(1); if (fieldBoolean) { - condition[this.OPERATORS.NOT] = _.isUndefined(valueBoolean) ? null : - valueBoolean; + condition[OPERATORS.NOT] = _.isUndefined(valueBoolean) ? null : valueBoolean; } else { - condition[this.OPERATORS.NE] = value; + condition[OPERATORS.NE] = value; } } else if (value[0] === '>') { - condition[this.OPERATORS.GT] = value.substring(1); + condition[OPERATORS.GT] = value.substring(1); } else if (value[0] === '<') { - condition[this.OPERATORS.LT] = value.substring(1); + condition[OPERATORS.LT] = value.substring(1); } else if (value[0] === '*' && value[value.length - 1] === '*') { - condition[this.OPERATORS.LIKE] = `%${value.substring(1, value.length - 1)}%`; - } else if (value[0] === '!' && value[1] === '*' && - value[value.length - 1] === '*') { + condition[OPERATORS.LIKE] = `%${value.substring(1, value.length - 1)}%`; + } else if (value[0] === '!' && value[1] === '*' && value[value.length - 1] === '*') { // TODO : Include null values // return { $or: { $notLike: '%' + value + '%', $eq: null } }; - condition[this.OPERATORS.NOT_LIKE] = `%${value.substring(2, value.length - 1)}%`; + condition[OPERATORS.NOT_LIKE] = `%${value.substring(2, value.length - 1)}%`; } else if (value[0] === '*') { - condition[this.OPERATORS.LIKE] = `%${value.substring(1)}`; + condition[OPERATORS.LIKE] = `%${value.substring(1)}`; } else if (value[value.length - 1] === '*') { - condition[this.OPERATORS.LIKE] = `${value.substring(0, value.length - 1)}%`; + condition[OPERATORS.LIKE] = `${value.substring(0, value.length - 1)}%`; } else if (value === '$present') { - condition[this.OPERATORS.NE] = null; + condition[OPERATORS.NE] = null; } else if (value === '$blank') { - condition[this.OPERATORS.EQ] = null; + condition[OPERATORS.EQ] = null; } else if (operatorDateIntervalParser.isIntervalDateValue()) { return operatorDateIntervalParser.getIntervalDateFilter(); } else if (fieldBoolean) { - condition[this.OPERATORS.EQ] = _.isUndefined(valueBoolean) ? null : valueBoolean; + condition[OPERATORS.EQ] = _.isUndefined(valueBoolean) ? null : valueBoolean; } else { - condition[this.OPERATORS.EQ] = value; + condition[OPERATORS.EQ] = value; } return condition; - } + }; } -export default OperatorValueParser; +module.exports = OperatorValueParser;