forest-express-sequelize 8.5.15-beta.1 → 8.5.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -2
- package/dist/services/leaderboard-stat-getter.js +6 -7
- package/dist/services/line-stat-getter.js +7 -7
- package/dist/services/live-query-checker.js +25 -0
- package/dist/services/pie-stat-getter.js +10 -10
- package/dist/services/query-options.js +3 -0
- package/dist/services/query-stat-getter.js +3 -0
- package/dist/services/value-stat-getter.js +7 -7
- package/package.json +2 -2
- package/test/databases.js +4 -4
- package/test/databases.test.js +45 -45
- package/test/integration/leaderboard-stat-getter.test.js +10 -10
- package/test/services/filters-parser.test.js +14 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
## [8.5.15
|
|
1
|
+
## [8.5.15](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.5.14...v8.5.15) (2023-08-09)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
### Bug Fixes
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* warn in console when the schema is not updated because of NODE_ENV ([#1093](https://github.com/ForestAdmin/forest-express-sequelize/issues/1093)) ([6c5ad8e](https://github.com/ForestAdmin/forest-express-sequelize/commit/6c5ad8ee9fd6bca9b7e4e8dde106d4ba534eeed0))
|
|
7
7
|
|
|
8
8
|
## [8.5.14](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.5.13...v8.5.14) (2022-10-25)
|
|
9
9
|
|
|
@@ -38,17 +38,16 @@ async function getSequelizeOptionsForModel(model, user, timezone) {
|
|
|
38
38
|
* @param {import('sequelize').Model} childModel
|
|
39
39
|
* @param {import('sequelize').Model} parentModel
|
|
40
40
|
* @param {{
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* limit: number;
|
|
41
|
+
* label_field: string;
|
|
42
|
+
* aggregate: string;
|
|
43
|
+
* aggregate_field: string;
|
|
45
44
|
* }} params
|
|
46
45
|
*/
|
|
47
46
|
|
|
48
47
|
|
|
49
48
|
function LeaderboardStatGetter(childModel, parentModel, params, user) {
|
|
50
|
-
const labelField = params.
|
|
51
|
-
const aggregate = params.
|
|
49
|
+
const labelField = params.label_field;
|
|
50
|
+
const aggregate = params.aggregate.toUpperCase();
|
|
52
51
|
const {
|
|
53
52
|
limit
|
|
54
53
|
} = params;
|
|
@@ -61,7 +60,7 @@ function LeaderboardStatGetter(childModel, parentModel, params, user) {
|
|
|
61
60
|
});
|
|
62
61
|
|
|
63
62
|
const aggregateField = getAggregateField({
|
|
64
|
-
aggregateField: params.
|
|
63
|
+
aggregateField: params.aggregate_field,
|
|
65
64
|
parentSchema,
|
|
66
65
|
parentModel
|
|
67
66
|
});
|
|
@@ -30,16 +30,16 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
30
30
|
|
|
31
31
|
function LineStatGetter(model, params, options, user) {
|
|
32
32
|
const schema = _forestExpress.Schemas.schemas[model.name];
|
|
33
|
-
const timeRange = params.
|
|
33
|
+
const timeRange = params.time_range.toLowerCase();
|
|
34
34
|
|
|
35
35
|
function getAggregateField() {
|
|
36
36
|
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
|
|
37
|
-
const fieldName = params.
|
|
37
|
+
const fieldName = params.aggregate_field || schema.primaryKeys[0] || schema.fields[0].field;
|
|
38
38
|
return `${schema.name}.${_orm.default.getColumnName(schema, fieldName)}`;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
function getGroupByDateField() {
|
|
42
|
-
return `${schema.name}.${_orm.default.getColumnName(schema, params.
|
|
42
|
+
return `${schema.name}.${_orm.default.getColumnName(schema, params.group_by_date_field)}`;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const groupByDateField = getGroupByDateField();
|
|
@@ -128,7 +128,7 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
128
128
|
return [getGroupByDateFieldFormatedForSQLite(timeRange), 'date'];
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
return [options.Sequelize.fn('to_char', options.Sequelize.fn('date_trunc', params.
|
|
131
|
+
return [options.Sequelize.fn('to_char', options.Sequelize.fn('date_trunc', params.time_range, options.Sequelize.literal(`"${getGroupByDateField().replace('.', '"."')}" at time zone '${params.timezone}'`)), 'YYYY-MM-DD 00:00:00'), 'date'];
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
function getFormat() {
|
|
@@ -189,7 +189,7 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
function getAggregate() {
|
|
192
|
-
return [options.Sequelize.fn(params.
|
|
192
|
+
return [options.Sequelize.fn(params.aggregate.toLowerCase(), options.Sequelize.col(getAggregateField())), 'value'];
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
function getGroupBy() {
|
|
@@ -202,14 +202,14 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
202
202
|
|
|
203
203
|
this.perform = async function () {
|
|
204
204
|
const {
|
|
205
|
-
|
|
205
|
+
filters,
|
|
206
206
|
timezone
|
|
207
207
|
} = params;
|
|
208
208
|
const scopeFilters = await _forestExpress.scopeManager.getScopeForUser(user, model.name, true);
|
|
209
209
|
const queryOptions = new _queryOptions.default(model, {
|
|
210
210
|
includeRelations: true
|
|
211
211
|
});
|
|
212
|
-
await queryOptions.filterByConditionTree(
|
|
212
|
+
await queryOptions.filterByConditionTree(filters, timezone);
|
|
213
213
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
214
214
|
|
|
215
215
|
const sequelizeOptions = _objectSpread(_objectSpread({}, queryOptions.sequelizeOptions), {}, {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
ErrorHTTP422
|
|
5
|
+
} = require('./errors');
|
|
6
|
+
|
|
7
|
+
function LiveQueryChecker() {
|
|
8
|
+
const QUERY_SELECT = /^SELECT\s[^]*FROM\s[^]*$/i;
|
|
9
|
+
|
|
10
|
+
this.perform = function perform(query) {
|
|
11
|
+
if (!query) {
|
|
12
|
+
throw new ErrorHTTP422('You cannot execute an empty SQL query.');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (query.includes(';') && query.indexOf(';') < query.length - 1) {
|
|
16
|
+
throw new ErrorHTTP422('You cannot chain SQL queries.');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!QUERY_SELECT.test(query)) {
|
|
20
|
+
throw new ErrorHTTP422('Only SELECT queries are allowed.');
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = LiveQueryChecker;
|
|
@@ -43,12 +43,12 @@ function PieStatGetter(model, params, options, user) {
|
|
|
43
43
|
let associationSchema;
|
|
44
44
|
let field;
|
|
45
45
|
|
|
46
|
-
if (params.
|
|
46
|
+
if (params.group_by_field.indexOf(':') === -1) {
|
|
47
47
|
field = _lodash.default.find(schema.fields, function (currentField) {
|
|
48
|
-
return currentField.field === params.
|
|
48
|
+
return currentField.field === params.group_by_field;
|
|
49
49
|
});
|
|
50
50
|
} else {
|
|
51
|
-
associationSplit = params.
|
|
51
|
+
associationSplit = params.group_by_field.split(':');
|
|
52
52
|
associationCollection = model.associations[associationSplit[0]].target.name;
|
|
53
53
|
[, associationField] = associationSplit;
|
|
54
54
|
associationSchema = _forestExpress.Schemas.schemas[associationCollection];
|
|
@@ -58,23 +58,23 @@ function PieStatGetter(model, params, options, user) {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
function getGroupByField() {
|
|
61
|
-
if (params.
|
|
62
|
-
const [associationName, fieldName] = params.
|
|
61
|
+
if (params.group_by_field.includes(':')) {
|
|
62
|
+
const [associationName, fieldName] = params.group_by_field.split(':');
|
|
63
63
|
return `${associationName}.${_orm.default.getColumnName(associationSchema, fieldName)}`;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
return `${schema.name}.${_orm.default.getColumnName(schema, params.
|
|
66
|
+
return `${schema.name}.${_orm.default.getColumnName(schema, params.group_by_field)}`;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
const groupByField = getGroupByField();
|
|
70
70
|
|
|
71
71
|
function getAggregate() {
|
|
72
|
-
return params.
|
|
72
|
+
return params.aggregate.toLowerCase();
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
function getAggregateField() {
|
|
76
76
|
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
|
|
77
|
-
const fieldName = params.
|
|
77
|
+
const fieldName = params.aggregate_field || schema.primaryKeys[0] || schema.fields[0].field;
|
|
78
78
|
return `${schema.name}.${_orm.default.getColumnName(schema, fieldName)}`;
|
|
79
79
|
}
|
|
80
80
|
|
|
@@ -107,14 +107,14 @@ function PieStatGetter(model, params, options, user) {
|
|
|
107
107
|
|
|
108
108
|
this.perform = async function () {
|
|
109
109
|
const {
|
|
110
|
-
|
|
110
|
+
filters,
|
|
111
111
|
timezone
|
|
112
112
|
} = params;
|
|
113
113
|
const scopeFilters = await _forestExpress.scopeManager.getScopeForUser(user, model.name, true);
|
|
114
114
|
const queryOptions = new _queryOptions.default(model, {
|
|
115
115
|
includeRelations: true
|
|
116
116
|
});
|
|
117
|
-
await queryOptions.filterByConditionTree(
|
|
117
|
+
await queryOptions.filterByConditionTree(filters, timezone);
|
|
118
118
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
119
119
|
|
|
120
120
|
const sequelizeOptions = _objectSpread(_objectSpread({}, queryOptions.sequelizeOptions), {}, {
|
|
@@ -26,6 +26,8 @@ var _errors = require("./errors");
|
|
|
26
26
|
|
|
27
27
|
var _filtersParser = _interopRequireDefault(require("./filters-parser"));
|
|
28
28
|
|
|
29
|
+
var _liveQueryChecker = _interopRequireDefault(require("./live-query-checker"));
|
|
30
|
+
|
|
29
31
|
var _primaryKeysManager = _interopRequireDefault(require("./primary-keys-manager"));
|
|
30
32
|
|
|
31
33
|
var _queryBuilder = _interopRequireDefault(require("./query-builder"));
|
|
@@ -294,6 +296,7 @@ class QueryOptions {
|
|
|
294
296
|
const primaryKey = _lodash.default.values(this._model.primaryKeys)[0].field;
|
|
295
297
|
|
|
296
298
|
const queryToFilterRecords = query.trim();
|
|
299
|
+
new _liveQueryChecker.default().perform(queryToFilterRecords);
|
|
297
300
|
|
|
298
301
|
try {
|
|
299
302
|
const options = {
|
|
@@ -8,6 +8,8 @@ require("core-js/modules/es.string.replace.js");
|
|
|
8
8
|
|
|
9
9
|
require("core-js/modules/es.regexp.constructor.js");
|
|
10
10
|
|
|
11
|
+
const LiveQueryChecker = require('./live-query-checker');
|
|
12
|
+
|
|
11
13
|
function QueryStatGetter(params, opts) {
|
|
12
14
|
const QUERY_OPTIONS_SELECT = {
|
|
13
15
|
type: opts.Sequelize.QueryTypes.SELECT
|
|
@@ -15,6 +17,7 @@ function QueryStatGetter(params, opts) {
|
|
|
15
17
|
|
|
16
18
|
this.perform = function perform() {
|
|
17
19
|
let rawQuery = params.query.trim();
|
|
20
|
+
new LiveQueryChecker().perform(rawQuery);
|
|
18
21
|
|
|
19
22
|
if (params.record_id) {
|
|
20
23
|
rawQuery = rawQuery.replace(new RegExp('\\?', 'g'), params.record_id);
|
|
@@ -41,27 +41,27 @@ class ValueStatGetter {
|
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
get _aggregateFunction() {
|
|
44
|
-
return this._params.
|
|
44
|
+
return this._params.aggregate.toLowerCase();
|
|
45
45
|
}
|
|
46
46
|
/** Column name we're aggregating on */
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
get _aggregateField() {
|
|
50
50
|
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
|
|
51
|
-
const fieldName = this._params.
|
|
51
|
+
const fieldName = this._params.aggregate_field || this._schema.primaryKeys[0] || this._schema.fields[0].field;
|
|
52
52
|
return `${this._schema.name}.${_orm.default.getColumnName(this._schema, fieldName)}`;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
async perform() {
|
|
56
56
|
const {
|
|
57
|
-
|
|
57
|
+
filters,
|
|
58
58
|
timezone
|
|
59
59
|
} = this._params;
|
|
60
60
|
const scopeFilters = await _forestExpress.scopeManager.getScopeForUser(this._user, this._model.name, true);
|
|
61
61
|
const queryOptions = new _queryOptions.default(this._model, {
|
|
62
62
|
includeRelations: true
|
|
63
63
|
});
|
|
64
|
-
await queryOptions.filterByConditionTree(
|
|
64
|
+
await queryOptions.filterByConditionTree(filters, timezone);
|
|
65
65
|
await queryOptions.filterByConditionTree(scopeFilters, timezone); // No attributes should be retrieved from relations for the group by to work.
|
|
66
66
|
|
|
67
67
|
const options = queryOptions.sequelizeOptions;
|
|
@@ -94,16 +94,16 @@ class ValueStatGetter {
|
|
|
94
94
|
|
|
95
95
|
async _getCountPrevious(options) {
|
|
96
96
|
const {
|
|
97
|
-
|
|
97
|
+
filters,
|
|
98
98
|
timezone
|
|
99
99
|
} = this._params;
|
|
100
100
|
|
|
101
|
-
if (!
|
|
101
|
+
if (!filters) {
|
|
102
102
|
return undefined;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
const conditionsParser = new _filtersParser.default(this._schema, timezone, this._options);
|
|
106
|
-
const rawInterval = conditionsParser.getPreviousIntervalCondition(
|
|
106
|
+
const rawInterval = conditionsParser.getPreviousIntervalCondition(filters);
|
|
107
107
|
|
|
108
108
|
if (!rawInterval) {
|
|
109
109
|
return undefined;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "forest-express-sequelize",
|
|
3
3
|
"description": "Official Express/Sequelize Liana for Forest",
|
|
4
|
-
"version": "8.5.15
|
|
4
|
+
"version": "8.5.15",
|
|
5
5
|
"author": "Sandro Munda <sandro@munda.me>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Arnaud Besnier <arnaudibesnier@gmail.com>",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@babel/runtime": "7.15.4",
|
|
29
29
|
"bluebird": "2.9.25",
|
|
30
30
|
"core-js": "3.6.5",
|
|
31
|
-
"forest-express": "
|
|
31
|
+
"forest-express": "9.5.7",
|
|
32
32
|
"http-errors": "1.6.1",
|
|
33
33
|
"lodash": "4.17.21",
|
|
34
34
|
"moment": "2.29.4",
|
package/test/databases.js
CHANGED
|
@@ -42,8 +42,8 @@ class ConnectionManager {
|
|
|
42
42
|
* @type {Record<string, ConnectionManager>}
|
|
43
43
|
*/
|
|
44
44
|
module.exports = {
|
|
45
|
-
sequelizePostgres: new ConnectionManager('Postgresql 9.4', 'postgres://forest:secret@
|
|
46
|
-
sequelizeMySQLMin: new ConnectionManager('MySQL 5.6', 'mysql://forest:secret@
|
|
47
|
-
sequelizeMySQLMax: new ConnectionManager('MySQL 8.0', 'mysql://forest:secret@
|
|
48
|
-
sequelizeMariaDB: new ConnectionManager('MariaDB 10', 'mysql://forest:secret@
|
|
45
|
+
sequelizePostgres: new ConnectionManager('Postgresql 9.4', 'postgres://forest:secret@127.0.0.1:5437/forest-express-sequelize-test'),
|
|
46
|
+
sequelizeMySQLMin: new ConnectionManager('MySQL 5.6', 'mysql://forest:secret@127.0.0.1:8998/forest-express-sequelize-test'),
|
|
47
|
+
sequelizeMySQLMax: new ConnectionManager('MySQL 8.0', 'mysql://forest:secret@127.0.0.1:8999/forest-express-sequelize-test'),
|
|
48
|
+
sequelizeMariaDB: new ConnectionManager('MariaDB 10', 'mysql://forest:secret@127.0.0.1:9000/forest-express-sequelize-test'),
|
|
49
49
|
};
|
package/test/databases.test.js
CHANGED
|
@@ -552,11 +552,11 @@ const user = { renderingId: 1 };
|
|
|
552
552
|
const stat = await new PieStatGetter(models.user, {
|
|
553
553
|
...baseParams,
|
|
554
554
|
type: 'Pie',
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
555
|
+
collection: 'user',
|
|
556
|
+
group_by_field: 'firstName',
|
|
557
|
+
aggregate: 'Count',
|
|
558
|
+
time_range: null,
|
|
559
|
+
filters: null,
|
|
560
560
|
}, options, user).perform();
|
|
561
561
|
expect(stat.value).toHaveLength(3);
|
|
562
562
|
} finally {
|
|
@@ -575,11 +575,11 @@ const user = { renderingId: 1 };
|
|
|
575
575
|
const stat = await new PieStatGetter(models.addressWithUserAlias, {
|
|
576
576
|
...baseParams,
|
|
577
577
|
type: 'Pie',
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
578
|
+
collection: 'addressWithUserAlias',
|
|
579
|
+
group_by_field: 'userAlias:primaryId',
|
|
580
|
+
aggregate: 'Count',
|
|
581
|
+
time_range: null,
|
|
582
|
+
filters: null,
|
|
583
583
|
}, options, user).perform();
|
|
584
584
|
expect(stat.value).toBeEmpty();
|
|
585
585
|
} finally {
|
|
@@ -601,11 +601,11 @@ const user = { renderingId: 1 };
|
|
|
601
601
|
const stat = await new LineStatGetter(models.user, {
|
|
602
602
|
...baseParams,
|
|
603
603
|
type: 'Line',
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
604
|
+
collection: 'user',
|
|
605
|
+
group_by_date_field: 'createdAt',
|
|
606
|
+
aggregate: 'Count',
|
|
607
|
+
time_range: 'Day',
|
|
608
|
+
filters: null,
|
|
609
609
|
}, options, user).perform();
|
|
610
610
|
expect(stat.value).toHaveLength(1);
|
|
611
611
|
} finally {
|
|
@@ -624,11 +624,11 @@ const user = { renderingId: 1 };
|
|
|
624
624
|
const stat = await new LineStatGetter(models.user, {
|
|
625
625
|
...baseParams,
|
|
626
626
|
type: 'Line',
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
627
|
+
collection: 'user',
|
|
628
|
+
group_by_date_field: 'createdAt',
|
|
629
|
+
aggregate: 'Count',
|
|
630
|
+
time_range: 'Week',
|
|
631
|
+
filters: null,
|
|
632
632
|
}, options, user).perform();
|
|
633
633
|
expect(stat.value).toHaveLength(1);
|
|
634
634
|
} finally {
|
|
@@ -647,11 +647,11 @@ const user = { renderingId: 1 };
|
|
|
647
647
|
const stat = await new LineStatGetter(models.user, {
|
|
648
648
|
...baseParams,
|
|
649
649
|
type: 'Line',
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
650
|
+
collection: 'user',
|
|
651
|
+
group_by_date_field: 'createdAt',
|
|
652
|
+
aggregate: 'Count',
|
|
653
|
+
time_range: 'Month',
|
|
654
|
+
filters: null,
|
|
655
655
|
}, options, user).perform();
|
|
656
656
|
expect(stat.value).toHaveLength(1);
|
|
657
657
|
} finally {
|
|
@@ -670,11 +670,11 @@ const user = { renderingId: 1 };
|
|
|
670
670
|
const stat = await new LineStatGetter(models.user, {
|
|
671
671
|
...baseParams,
|
|
672
672
|
type: 'Line',
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
673
|
+
collection: 'user',
|
|
674
|
+
group_by_date_field: 'createdAt',
|
|
675
|
+
aggregate: 'Count',
|
|
676
|
+
time_range: 'Year',
|
|
677
|
+
filters: null,
|
|
678
678
|
}, options, user).perform();
|
|
679
679
|
expect(stat.value).toHaveLength(1);
|
|
680
680
|
} finally {
|
|
@@ -693,11 +693,11 @@ const user = { renderingId: 1 };
|
|
|
693
693
|
const stat = await new LineStatGetter(models.address, {
|
|
694
694
|
...baseParams,
|
|
695
695
|
type: 'Line',
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
696
|
+
collection: 'address',
|
|
697
|
+
group_by_date_field: 'createdAt',
|
|
698
|
+
aggregate: 'Count',
|
|
699
|
+
time_range: 'Year',
|
|
700
|
+
filters: JSON.stringify({ field: 'user:primaryId', operator: 'equal', value: 100 }),
|
|
701
701
|
}, options, user).perform();
|
|
702
702
|
expect(stat.value).toHaveLength(1);
|
|
703
703
|
} finally {
|
|
@@ -718,8 +718,8 @@ const user = { renderingId: 1 };
|
|
|
718
718
|
const stat = await new ValueStatGetter(models.user, {
|
|
719
719
|
...baseParams,
|
|
720
720
|
type: 'Value',
|
|
721
|
-
|
|
722
|
-
|
|
721
|
+
aggregate: 'Sum',
|
|
722
|
+
aggregate_field: 'primaryId',
|
|
723
723
|
}, options, user).perform();
|
|
724
724
|
|
|
725
725
|
expect(stat.value).toStrictEqual({ countCurrent: 305, countPrevious: undefined });
|
|
@@ -738,9 +738,9 @@ const user = { renderingId: 1 };
|
|
|
738
738
|
const stat = await new ValueStatGetter(models.user, {
|
|
739
739
|
...baseParams,
|
|
740
740
|
type: 'Value',
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
741
|
+
aggregate: 'Sum',
|
|
742
|
+
aggregate_field: 'primaryId',
|
|
743
|
+
filters: '{"field":"createdAt","operator":"previous_month_to_date","value":null}',
|
|
744
744
|
}, options, user).perform();
|
|
745
745
|
|
|
746
746
|
expect(stat.value).toStrictEqual({ countCurrent: 305, countPrevious: 0 });
|
|
@@ -759,9 +759,9 @@ const user = { renderingId: 1 };
|
|
|
759
759
|
const stat = await new ValueStatGetter(models.user, {
|
|
760
760
|
...baseParams,
|
|
761
761
|
type: 'Value',
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
762
|
+
aggregate: 'Sum',
|
|
763
|
+
aggregate_field: 'primaryId',
|
|
764
|
+
filters: JSON.stringify({
|
|
765
765
|
aggregator: 'and',
|
|
766
766
|
conditions: [
|
|
767
767
|
{ field: 'createdAt', operator: 'previous_month_to_date', value: null },
|
|
@@ -786,8 +786,8 @@ const user = { renderingId: 1 };
|
|
|
786
786
|
const stat = await new ValueStatGetter(models.address, {
|
|
787
787
|
...baseParams,
|
|
788
788
|
type: 'Value',
|
|
789
|
-
|
|
790
|
-
|
|
789
|
+
aggregate: 'Count',
|
|
790
|
+
filters: '{"field":"user:primaryId","operator":"greater_than","value":0}',
|
|
791
791
|
}, options, user).perform();
|
|
792
792
|
|
|
793
793
|
expect(stat.value).toStrictEqual({ countCurrent: 4, countPrevious: undefined });
|
|
@@ -137,8 +137,8 @@ describe('integration > LeaderboardStatGetter', () => {
|
|
|
137
137
|
const { models } = await setup(sequelize);
|
|
138
138
|
|
|
139
139
|
const params = {
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
label_field: 'firstName',
|
|
141
|
+
aggregate: 'count',
|
|
142
142
|
limit: 10,
|
|
143
143
|
};
|
|
144
144
|
|
|
@@ -169,9 +169,9 @@ describe('integration > LeaderboardStatGetter', () => {
|
|
|
169
169
|
const { models } = await setup(sequelize);
|
|
170
170
|
|
|
171
171
|
const params = {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
172
|
+
label_field: 'firstName',
|
|
173
|
+
aggregate: 'sum',
|
|
174
|
+
aggregate_field: 'sellingAmount',
|
|
175
175
|
limit: 10,
|
|
176
176
|
};
|
|
177
177
|
|
|
@@ -204,8 +204,8 @@ describe('integration > LeaderboardStatGetter', () => {
|
|
|
204
204
|
const { models } = await setup(sequelize);
|
|
205
205
|
|
|
206
206
|
const params = {
|
|
207
|
-
|
|
208
|
-
|
|
207
|
+
label_field: 'firstName',
|
|
208
|
+
aggregate: 'count',
|
|
209
209
|
limit: 10,
|
|
210
210
|
};
|
|
211
211
|
|
|
@@ -236,9 +236,9 @@ describe('integration > LeaderboardStatGetter', () => {
|
|
|
236
236
|
const { models } = await setup(sequelize);
|
|
237
237
|
|
|
238
238
|
const params = {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
239
|
+
label_field: 'firstName',
|
|
240
|
+
aggregate: 'sum',
|
|
241
|
+
aggregate_field: 'objectiveScore',
|
|
242
242
|
limit: 10,
|
|
243
243
|
};
|
|
244
244
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import moment from 'moment';
|
|
2
2
|
import Sequelize from 'sequelize';
|
|
3
|
-
import {
|
|
3
|
+
import { SchemaUtils } from 'forest-express';
|
|
4
4
|
import FiltersParser from '../../src/services/filters-parser';
|
|
5
5
|
import Operators from '../../src/utils/operators';
|
|
6
6
|
import { NoMatchingOperatorError } from '../../src/services/errors';
|
|
@@ -227,8 +227,7 @@ describe('services > filters-parser', () => {
|
|
|
227
227
|
const schemaWithFields = {
|
|
228
228
|
fields: [{ field: 'car', reference: 'car.id' }],
|
|
229
229
|
};
|
|
230
|
-
|
|
231
|
-
const filters = { field: 'car:brandName', operator: 'starts_with', value: 'Ferrari' };
|
|
230
|
+
const filters = '{ "field": "car:brandName", "operator": "starts_with", "value": "Ferrari" }';
|
|
232
231
|
const filtersParser = new FiltersParser(schemaWithFields, timezone, sequelizeOptions);
|
|
233
232
|
|
|
234
233
|
it('should not be null', async () => {
|
|
@@ -247,10 +246,10 @@ describe('services > filters-parser', () => {
|
|
|
247
246
|
describe('getPreviousIntervalCondition function', () => {
|
|
248
247
|
describe('working scenarii', () => {
|
|
249
248
|
describe('with \'and\' aggregator + flat conditions + 1 previous interval', () => {
|
|
250
|
-
const aggregator = {
|
|
249
|
+
const aggregator = JSON.stringify({
|
|
251
250
|
aggregator: 'and',
|
|
252
251
|
conditions: [defaultCondition, defaultCondition2, defaultDateCondition],
|
|
253
|
-
};
|
|
252
|
+
});
|
|
254
253
|
const filtersParser = new FiltersParser(schema, timezone, sequelizeOptions);
|
|
255
254
|
|
|
256
255
|
it('should generate the right condition', () => {
|
|
@@ -261,7 +260,7 @@ describe('services > filters-parser', () => {
|
|
|
261
260
|
});
|
|
262
261
|
|
|
263
262
|
describe('with no aggregator + flat conditions + 1 previous interval', () => {
|
|
264
|
-
const aggregator = defaultDateCondition;
|
|
263
|
+
const aggregator = JSON.stringify(defaultDateCondition);
|
|
265
264
|
const filtersParser = new FiltersParser(schema, timezone, sequelizeOptions);
|
|
266
265
|
|
|
267
266
|
it('should generate the right condition', () => {
|
|
@@ -274,10 +273,10 @@ describe('services > filters-parser', () => {
|
|
|
274
273
|
|
|
275
274
|
describe('not working scenarii', () => {
|
|
276
275
|
describe('with \'and\' aggregator + flat conditions + 2 previous intervals', () => {
|
|
277
|
-
const aggregator = {
|
|
276
|
+
const aggregator = JSON.stringify({
|
|
278
277
|
aggregator: 'and',
|
|
279
278
|
conditions: [defaultCondition, defaultDateCondition, defaultDateCondition],
|
|
280
|
-
};
|
|
279
|
+
});
|
|
281
280
|
const filtersParser = new FiltersParser(schema, timezone, sequelizeOptions);
|
|
282
281
|
|
|
283
282
|
it('should not generate conditions', () => {
|
|
@@ -287,10 +286,10 @@ describe('services > filters-parser', () => {
|
|
|
287
286
|
});
|
|
288
287
|
|
|
289
288
|
describe('with \'or\' aggregator + flat conditions + 1 previous interval', () => {
|
|
290
|
-
const aggregator = {
|
|
289
|
+
const aggregator = JSON.stringify({
|
|
291
290
|
aggregator: 'or',
|
|
292
291
|
conditions: [defaultCondition, defaultCondition2, defaultDateCondition],
|
|
293
|
-
};
|
|
292
|
+
});
|
|
294
293
|
const filtersParser = new FiltersParser(schema, timezone, sequelizeOptions);
|
|
295
294
|
|
|
296
295
|
it('should not generate conditions', () => {
|
|
@@ -300,10 +299,10 @@ describe('services > filters-parser', () => {
|
|
|
300
299
|
});
|
|
301
300
|
|
|
302
301
|
describe('with \'and\' aggregator + flat conditions + 0 previous interval', () => {
|
|
303
|
-
const aggregator = {
|
|
302
|
+
const aggregator = JSON.stringify({
|
|
304
303
|
aggregator: 'and',
|
|
305
304
|
conditions: [defaultCondition, defaultCondition2],
|
|
306
|
-
};
|
|
305
|
+
});
|
|
307
306
|
const filtersParser = new FiltersParser(schema, timezone, sequelizeOptions);
|
|
308
307
|
|
|
309
308
|
it('should not generate conditions', () => {
|
|
@@ -313,10 +312,10 @@ describe('services > filters-parser', () => {
|
|
|
313
312
|
});
|
|
314
313
|
|
|
315
314
|
describe('with \'and\' aggregator + nested conditions + 1 previous interval', () => {
|
|
316
|
-
const aggregator = {
|
|
315
|
+
const aggregator = JSON.stringify({
|
|
317
316
|
aggregator: 'and',
|
|
318
317
|
conditions: [{ aggregator: 'or', conditions: [defaultCondition, defaultCondition2] }, defaultDateCondition],
|
|
319
|
-
};
|
|
318
|
+
});
|
|
320
319
|
const filtersParser = new FiltersParser(schema, timezone, sequelizeOptions);
|
|
321
320
|
|
|
322
321
|
it('should not generate conditions', () => {
|
|
@@ -326,7 +325,7 @@ describe('services > filters-parser', () => {
|
|
|
326
325
|
});
|
|
327
326
|
|
|
328
327
|
describe('with no aggregator + flat conditions + 0 previous interval', () => {
|
|
329
|
-
const aggregator = defaultCondition;
|
|
328
|
+
const aggregator = JSON.stringify(defaultCondition);
|
|
330
329
|
const filtersParser = new FiltersParser(schema, timezone, sequelizeOptions);
|
|
331
330
|
|
|
332
331
|
it('should not generate conditions', () => {
|