@qrvey/utils 1.15.0-0 → 1.15.0-2

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.
@@ -29,7 +29,8 @@ export declare enum CHART_TYPE {
29
29
  COMBINED_CHART = "COMBINED_CHART",
30
30
  RADAR_CHART = "RADAR_CHART",
31
31
  TREEMAP_CHART = "TREEMAP_CHART",
32
- AREA_CHART = "AREA_CHART"
32
+ AREA_CHART = "AREA_CHART",
33
+ AREA_CHART_MS = "AREA_CHART_MS"
33
34
  }
34
35
  export declare enum CHART_DIMENSION {
35
36
  CATEGORIES = "categories",
@@ -34,6 +34,7 @@ var CHART_TYPE;
34
34
  CHART_TYPE["RADAR_CHART"] = "RADAR_CHART";
35
35
  CHART_TYPE["TREEMAP_CHART"] = "TREEMAP_CHART";
36
36
  CHART_TYPE["AREA_CHART"] = "AREA_CHART";
37
+ CHART_TYPE["AREA_CHART_MS"] = "AREA_CHART_MS";
37
38
  })(CHART_TYPE || (exports.CHART_TYPE = CHART_TYPE = {}));
38
39
  var CHART_DIMENSION;
39
40
  (function (CHART_DIMENSION) {
@@ -1,3 +1,4 @@
1
+ export * from "./transformFilters";
1
2
  export * from "./modelAnswerToFilterValue";
2
3
  export * from "./adaptDateGroupingProperty";
3
4
  export * from "./adaptFilterData";
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./transformFilters"), exports);
17
18
  __exportStar(require("./modelAnswerToFilterValue"), exports);
18
19
  __exportStar(require("./adaptDateGroupingProperty"), exports);
19
20
  __exportStar(require("./adaptFilterData"), exports);
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Transform the old filters structure into the new one
3
+ *
4
+ * @deprecated Since 2020, Added back in 2024 but not support provided (unmantained)
5
+ * @param {Object} oldFiltersObj an object with the old filters structure
6
+ * @param {String} section could be 'ANALYZE', 'PB', 'UM' or any other value. Deafult is 'ANYWHERE'
7
+ *
8
+ * @returns {Object} an object with the new filters structure
9
+ *
10
+ * @example
11
+ *
12
+ * const oldPreferenceFilters = {
13
+ * "WWXHAULtR-_-xYOQAdpqT__ENABLED": true,
14
+ * "WWXHAULtR-_-xYOQAdpqT__ALL--ENABLED": true,
15
+ * "WWXHAULtR-_-xYOQAdpqT__COLLAPSED": false,
16
+ * "WWXHAULtR-_-xYOQAdpqT__LABEL": "GLOBAL",
17
+ * "WWXHAULtR-_-xYOQAdpqT-_-AK4M8UV2": {
18
+ * "formulaId": null,
19
+ * "panelId": null,
20
+ * "values": [
21
+ * {
22
+ * "EQUALS": [
23
+ * {
24
+ * "id": "AK4M8UV2a0",
25
+ * "value": "A",
26
+ * "enabled": true,
27
+ * "imageUrl": null
28
+ * },
29
+ * {
30
+ * "id": "AK4M8UV2a1",
31
+ * "value": "B",
32
+ * "enabled": true,
33
+ * "imageUrl": null
34
+ * }
35
+ * ]
36
+ * }
37
+ * ],
38
+ * "bucketId": null,
39
+ * "text": "MC",
40
+ * "title": "MC",
41
+ * "type": "SINGLE_CHOICE",
42
+ * "qid": "AK4M8UV2",
43
+ * "dataset": {
44
+ * "sourceid": "xYOQAdpqT",
45
+ * "name": "Form All Questions",
46
+ * "qrveyid": "xYOQAdpqT",
47
+ * "text": "Form All Questions",
48
+ * "linkid": 0
49
+ * },
50
+ * "enabled": true,
51
+ * "linked": null
52
+ * }
53
+ * };
54
+ *
55
+ * const filterData = transformFilters(oldPreferenceFilters, 'ANALYZE');
56
+ */
57
+ export declare function transformFilters(oldFiltersObj?: {}, section?: string): {
58
+ enabled: boolean;
59
+ logic: any[];
60
+ section: string;
61
+ };
@@ -0,0 +1,432 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformFilters = void 0;
4
+ /* eslint-disable no-nested-ternary */
5
+ const isTokenLabel_1 = require("../../tokens/isTokenLabel");
6
+ const FILTER_VALIDATOR_1 = require("../constants/common/FILTER_VALIDATOR");
7
+ const FILTER_SCOPE_1 = require("../constants/common/FILTER_SCOPE");
8
+ const FILTER_SECTION_1 = require("../constants/common/FILTER_SECTION");
9
+ const FILTER_BACKEND_VALIDATOR_1 = require("../constants/backend/FILTER_BACKEND_VALIDATOR");
10
+ const get_1 = require("../../general/object/get");
11
+ const capitalize_1 = require("../../general/string/capitalize");
12
+ /**
13
+ * Transform the old filters structure into the new one
14
+ *
15
+ * @deprecated Since 2020, Added back in 2024 but not support provided (unmantained)
16
+ * @param {Object} oldFiltersObj an object with the old filters structure
17
+ * @param {String} section could be 'ANALYZE', 'PB', 'UM' or any other value. Deafult is 'ANYWHERE'
18
+ *
19
+ * @returns {Object} an object with the new filters structure
20
+ *
21
+ * @example
22
+ *
23
+ * const oldPreferenceFilters = {
24
+ * "WWXHAULtR-_-xYOQAdpqT__ENABLED": true,
25
+ * "WWXHAULtR-_-xYOQAdpqT__ALL--ENABLED": true,
26
+ * "WWXHAULtR-_-xYOQAdpqT__COLLAPSED": false,
27
+ * "WWXHAULtR-_-xYOQAdpqT__LABEL": "GLOBAL",
28
+ * "WWXHAULtR-_-xYOQAdpqT-_-AK4M8UV2": {
29
+ * "formulaId": null,
30
+ * "panelId": null,
31
+ * "values": [
32
+ * {
33
+ * "EQUALS": [
34
+ * {
35
+ * "id": "AK4M8UV2a0",
36
+ * "value": "A",
37
+ * "enabled": true,
38
+ * "imageUrl": null
39
+ * },
40
+ * {
41
+ * "id": "AK4M8UV2a1",
42
+ * "value": "B",
43
+ * "enabled": true,
44
+ * "imageUrl": null
45
+ * }
46
+ * ]
47
+ * }
48
+ * ],
49
+ * "bucketId": null,
50
+ * "text": "MC",
51
+ * "title": "MC",
52
+ * "type": "SINGLE_CHOICE",
53
+ * "qid": "AK4M8UV2",
54
+ * "dataset": {
55
+ * "sourceid": "xYOQAdpqT",
56
+ * "name": "Form All Questions",
57
+ * "qrveyid": "xYOQAdpqT",
58
+ * "text": "Form All Questions",
59
+ * "linkid": 0
60
+ * },
61
+ * "enabled": true,
62
+ * "linked": null
63
+ * }
64
+ * };
65
+ *
66
+ * const filterData = transformFilters(oldPreferenceFilters, 'ANALYZE');
67
+ */
68
+ function transformFilters(oldFiltersObj = {}, section = FILTER_SECTION_1.FILTER_SECTION.ANYWHERE) {
69
+ const filterData = { enabled: true, logic: [], section };
70
+ const tempScopes = {};
71
+ Object.keys(oldFiltersObj).forEach((key) => {
72
+ if (key.endsWith("__ALL--ENABLED"))
73
+ filterData.enabled = oldFiltersObj[key];
74
+ if (!key.endsWith("ENABLED") &&
75
+ !key.endsWith("LABEL") &&
76
+ !key.endsWith("COLLAPSED")) {
77
+ const filterSeparator = "-_-";
78
+ const keyArray = key.split(filterSeparator);
79
+ const tempArray = [...keyArray];
80
+ tempArray.pop();
81
+ let enabledKey = "__ENABLED";
82
+ let collapsedKey = "__COLLAPSED";
83
+ let labelKey = "__LABEL";
84
+ if (keyArray.length === 5) {
85
+ tempArray.pop();
86
+ enabledKey = "__PANELS--ENABLED";
87
+ collapsedKey = "__PANELS--COLLAPSED";
88
+ labelKey = "__PANELS--LABEL";
89
+ }
90
+ const keyBase = tempArray.join(filterSeparator);
91
+ const scopeLabel = getScopeLabel(oldFiltersObj[keyBase + labelKey]);
92
+ const scopeId = tempArray[tempArray.length - 1] !== "VIEW"
93
+ ? tempArray[tempArray.length - 1]
94
+ : undefined;
95
+ const scopeKey = `${scopeLabel}|${scopeId}`;
96
+ if (!tempScopes[scopeKey]) {
97
+ tempScopes[scopeKey] = {
98
+ enabled: oldFiltersObj[keyBase + enabledKey],
99
+ scope: scopeLabel,
100
+ filters: [],
101
+ uiExtras: {
102
+ scopeid: scopeId,
103
+ collapsed: oldFiltersObj[keyBase + collapsedKey],
104
+ section,
105
+ },
106
+ };
107
+ }
108
+ const filtersObj = validateComplexTypes(oldFiltersObj[key], Object.keys(oldFiltersObj[key].values[0]));
109
+ const validations = Object.keys(filtersObj.values[0]);
110
+ tempScopes[scopeKey].filters.push({
111
+ operator: "AND",
112
+ expressions: [
113
+ {
114
+ operator: "OR",
115
+ uiExtras: {
116
+ expressionid: `${filtersObj.qid}|${scopeId}|${filtersObj.panelid || ""}`,
117
+ },
118
+ expressions: getExpressions(filtersObj, validations, scopeLabel, scopeId, section),
119
+ },
120
+ ],
121
+ });
122
+ }
123
+ });
124
+ filterData.logic = Object.values(tempScopes);
125
+ return filterData;
126
+ }
127
+ exports.transformFilters = transformFilters;
128
+ function validateComplexTypes(oldFiltersItem, validations) {
129
+ if (![
130
+ "IMAGEUPLOAD",
131
+ "TEXTFIELD",
132
+ "LONGTEXT",
133
+ "NAME",
134
+ "ADDRESS",
135
+ "USADDRESS",
136
+ ].includes(oldFiltersItem.type))
137
+ return oldFiltersItem;
138
+ const complexFiltersItem = Object.assign(Object.assign({}, oldFiltersItem), { values: [{}] });
139
+ validations.forEach((valKey) => {
140
+ oldFiltersItem.values[0][valKey].forEach((value) => {
141
+ const newKey = `${valKey}|${(value.targetFilter || value.property || "")
142
+ .toLowerCase()
143
+ .replace("_text", "")
144
+ .replace("_type", "type")}`;
145
+ if (!complexFiltersItem.values[0][newKey]) {
146
+ complexFiltersItem.values[0][newKey] = [value];
147
+ }
148
+ else {
149
+ complexFiltersItem.values[0][newKey].push(value);
150
+ }
151
+ });
152
+ });
153
+ return complexFiltersItem;
154
+ }
155
+ function getScopeLabel(oldScopeLabel) {
156
+ switch (oldScopeLabel) {
157
+ case "GLOBAL":
158
+ return FILTER_SCOPE_1.FILTER_SCOPE.GLOBAL;
159
+ case "PAGE":
160
+ return FILTER_SCOPE_1.FILTER_SCOPE.PAGE;
161
+ case "TAB":
162
+ return FILTER_SCOPE_1.FILTER_SCOPE.TAB;
163
+ case "PANEL":
164
+ return FILTER_SCOPE_1.FILTER_SCOPE.CHART;
165
+ default:
166
+ return oldScopeLabel;
167
+ }
168
+ }
169
+ function getValidationType(oldValidation, columnType, formulaType) {
170
+ switch (oldValidation) {
171
+ case "EQUALS":
172
+ case "EQUAL":
173
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.EQUALS;
174
+ case "NOT_EQUALS":
175
+ case "NOT_EQUAL":
176
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.NOT_EQUAL;
177
+ case "CONTAINS":
178
+ case "CONTAIN":
179
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.CONTAINS;
180
+ case "NOT_CONTAINS":
181
+ case "NOT_CONTAIN":
182
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.NOT_CONTAIN;
183
+ case "LESS_THAN":
184
+ case "LESS_THAN_OR_EQUAL":
185
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.LESS_THAN_EQUAL;
186
+ case "GREATER_THAN":
187
+ case "GREATER_THAN_OR_EQUAL":
188
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.GREATER_THAN_EQUAL;
189
+ case "BETWEEN":
190
+ case "YEAR":
191
+ case "MONTH_YEAR":
192
+ case "QUARTER":
193
+ case "WEEK":
194
+ case "SECOND":
195
+ case "MINUTE":
196
+ case "HOUR":
197
+ case "DAY":
198
+ return ![columnType, formulaType].includes("DATE")
199
+ ? FILTER_VALIDATOR_1.FILTER_VALIDATOR.BETWEEN_INCLUSIVE
200
+ : FILTER_BACKEND_VALIDATOR_1.FILTER_BACKEND_VALIDATOR.RANGE;
201
+ default:
202
+ return oldValidation;
203
+ }
204
+ }
205
+ function getValidator(oldValidation) {
206
+ switch (oldValidation) {
207
+ case "EQUALS":
208
+ case "EQUAL":
209
+ case "YEAR":
210
+ case "MONTH_YEAR":
211
+ case "QUARTER":
212
+ case "WEEK":
213
+ case "SECOND":
214
+ case "MINUTE":
215
+ case "HOUR":
216
+ case "DAY":
217
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.EQUALS;
218
+ case "NOT_EQUALS":
219
+ case "NOT_EQUAL":
220
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.NOT_EQUAL;
221
+ case "CONTAINS":
222
+ case "CONTAIN":
223
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.CONTAINS;
224
+ case "NOT_CONTAINS":
225
+ case "NOT_CONTAIN":
226
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.NOT_CONTAIN;
227
+ case "BETWEEN":
228
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.BETWEEN_INCLUSIVE;
229
+ case "LESS_THAN":
230
+ case "LESS_THAN_OR_EQUAL":
231
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.LESS_THAN_EQUAL;
232
+ case "GREATER_THAN":
233
+ case "GREATER_THAN_OR_EQUAL":
234
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.GREATER_THAN_EQUAL;
235
+ case "IS_EMPTY":
236
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.IS_NULL;
237
+ case "IS_NOT_EMPTY":
238
+ return FILTER_VALIDATOR_1.FILTER_VALIDATOR.IS_NOT_NULL;
239
+ default:
240
+ return oldValidation;
241
+ }
242
+ }
243
+ function getProperty(filterObjItem, validationKey) {
244
+ if (filterObjItem.type === "IMAGE")
245
+ return "answer_id";
246
+ const value = (0, get_1._get)(filterObjItem, `values[0]['${validationKey}'][0]`, {});
247
+ const property = filterObjItem.property || value.property || value.targetFilter;
248
+ return property && property.toUpperCase();
249
+ }
250
+ function getDateProperty(validationKey, columnType, formulaType) {
251
+ if (![columnType, formulaType].includes("DATE"))
252
+ return;
253
+ return [
254
+ "MONTH_YEAR",
255
+ "YEAR",
256
+ "QUARTER",
257
+ "WEEK",
258
+ "HOUR",
259
+ "MINUTE",
260
+ "SECOND",
261
+ ].includes(validationKey)
262
+ ? validationKey.split("_")[0].toLowerCase()
263
+ : "day";
264
+ }
265
+ function getFilterRangeValue(range, minProp = "min", maxProp = "max", extras = {}) {
266
+ return Object.assign({ [minProp]: range.from || range.gte, [maxProp]: range.to || range.lte }, extras);
267
+ }
268
+ function getUiRangeValue(oldValue) {
269
+ if (oldValue.label)
270
+ return (oldValue.label + "").replace("-", " ");
271
+ const range = oldValue.range || oldValue;
272
+ return `${range.from || range.gte} - ${range.to || range.lte}`;
273
+ }
274
+ function getRankingFBvalues(values) {
275
+ const returnValues = [];
276
+ values
277
+ .map((vItem) => (Object.assign(Object.assign({}, vItem), { position: +vItem.position + 1 })))
278
+ .forEach((vItem) => {
279
+ const groupIndex = returnValues.findIndex((group) => group.every((groupItem) => groupItem.value.value !== vItem.value) &&
280
+ group.every((groupItem) => groupItem.position !== vItem.position));
281
+ const returnItem = {
282
+ position: vItem.position,
283
+ value: { enabled: vItem.enabled, value: vItem.value },
284
+ };
285
+ if (groupIndex === -1)
286
+ returnValues.push([returnItem]);
287
+ else
288
+ returnValues[groupIndex].push(returnItem);
289
+ });
290
+ return returnValues;
291
+ }
292
+ function getValues(values, columnType, oldValidation = "EQUAL", destination = "value", formulaType) {
293
+ switch (oldValidation) {
294
+ case "BETWEEN":
295
+ case "YEAR":
296
+ case "MONTH_YEAR":
297
+ return destination === "value"
298
+ ? values.map((val) => getFilterRangeValue(val.value.range || val.value, "gte", "lte"))
299
+ : destination === "uiValue"
300
+ ? values.map((val) => getFilterRangeValue(val.value.range || val.value, "min", "max", {
301
+ enabled: val.enabled,
302
+ value: getUiRangeValue(val.value),
303
+ validationLabel: oldValidation,
304
+ }))
305
+ : ["YEAR", "MONTH_YEAR", "QUARTER"].includes(oldValidation)
306
+ ? values.map((item) => ({ value: getUiRangeValue(item.value) }))
307
+ : values.map((val) => getFilterRangeValue(val.value.range || val.value));
308
+ case "LESS_THAN":
309
+ case "GREATER_THAN": {
310
+ const prop = oldValidation === "LESS_THAN" ? "lte" : "gte";
311
+ return destination === "value"
312
+ ? values.map((val) => getFilterRangeValue({ [prop]: val.value }, "gte", "lte"))
313
+ : destination === "uiValue"
314
+ ? values.map((val) => getFilterRangeValue({ [prop]: val.value }, "min", "max", {
315
+ enabled: val.enabled,
316
+ value: val.value,
317
+ }))
318
+ : values.map((val) => getFilterRangeValue({ [prop]: val.value }));
319
+ }
320
+ case "IS_EMPTY":
321
+ case "IS_NOT_EMPTY": {
322
+ const valueClean = getValidator(oldValidation)
323
+ .toLowerCase()
324
+ .split("_")
325
+ .map((v) => (0, capitalize_1.capitalize)(v))
326
+ .join(" ");
327
+ return destination === "value"
328
+ ? [valueClean]
329
+ : destination === "uiValue"
330
+ ? values.map((val) => ({ value: valueClean, enabled: val.enabled }))
331
+ : [];
332
+ }
333
+ default:
334
+ return getFilterEqualValues(values, columnType, destination, formulaType);
335
+ }
336
+ }
337
+ function getFilterEqualValues(values, columnType, destination = "value", formulaType) {
338
+ if (columnType === "RANKING") {
339
+ return destination === "value"
340
+ ? values.map((item) => ({ position: +item.position, value: item.value }))
341
+ : destination === "uiValue"
342
+ ? values.map((item) => ({
343
+ value: getUiRangeValue({
344
+ from: item.value,
345
+ to: +item.position + 1,
346
+ }),
347
+ enabled: true,
348
+ }))
349
+ : getRankingFBvalues(values);
350
+ }
351
+ return destination === "value"
352
+ ? values.map((item) => {
353
+ let value = item.value;
354
+ if ([columnType, formulaType].includes("DATE") &&
355
+ !(0, isTokenLabel_1.isTokenLabel)(value)) {
356
+ value = getFilterRangeValue(item.value.range || item.value, "gte", "lte");
357
+ }
358
+ else if (columnType === "YES_NO") {
359
+ value = item.value.toLowerCase();
360
+ }
361
+ else if (columnType === "IMAGE") {
362
+ value = item.id || item.answerid || item.answer_id;
363
+ }
364
+ else {
365
+ value = String(item.value);
366
+ }
367
+ return value;
368
+ })
369
+ : values.map((item) => ({
370
+ value: columnType === "YES_NO"
371
+ ? item.value.toLowerCase()
372
+ : [columnType, formulaType].includes("DATE")
373
+ ? item.value.label || item.value
374
+ : String(item.value),
375
+ valueid: columnType === "EXPRESSION" ? undefined : item.id || item.answerid,
376
+ enabled: item.enabled || true,
377
+ }));
378
+ }
379
+ function getFormulaType(oldFiltersItem) {
380
+ if (oldFiltersItem.formulaType)
381
+ return oldFiltersItem.formulaType;
382
+ if (oldFiltersItem.formulaId)
383
+ return oldFiltersItem.type === "NUMERIC"
384
+ ? "number"
385
+ : oldFiltersItem.type === "DATE"
386
+ ? "date"
387
+ : "string";
388
+ }
389
+ function getExpressions(oldFiltersItem, validations, scopeLabel, scopeid, section) {
390
+ const expressions = [];
391
+ validations.forEach((valKey) => {
392
+ const valKeyArray = valKey.split("|");
393
+ const validationKey = valKeyArray[0];
394
+ const property = valKeyArray[1] || getProperty(oldFiltersItem, validationKey);
395
+ const formulaType = getFormulaType(oldFiltersItem);
396
+ const questionType = formulaType ? "FORMULA" : oldFiltersItem.type;
397
+ const expressionItem = {
398
+ enabled: oldFiltersItem.enabled,
399
+ property: property,
400
+ qrveyid: oldFiltersItem.dataset.qrveyid,
401
+ questionid: oldFiltersItem.qid,
402
+ formulaType: formulaType,
403
+ questionType: questionType,
404
+ type: questionType,
405
+ validationType: getValidationType(validationKey, questionType, formulaType && formulaType.toUpperCase()),
406
+ uiExtras: {
407
+ column: {
408
+ dataset: {
409
+ qrveyid: oldFiltersItem.dataset.qrveyid,
410
+ name: oldFiltersItem.dataset.name,
411
+ },
412
+ formulaType: formulaType,
413
+ id: oldFiltersItem.qid,
414
+ qrveyid: oldFiltersItem.dataset.qrveyid,
415
+ text: oldFiltersItem.text,
416
+ type: questionType,
417
+ },
418
+ operator: "AND",
419
+ property: getDateProperty(validationKey, questionType, formulaType && formulaType.toUpperCase()) || property,
420
+ validator: getValidator(validationKey),
421
+ panelid: oldFiltersItem.panelId,
422
+ panelName: oldFiltersItem.panelId ? oldFiltersItem.title : undefined,
423
+ uiHidden: false,
424
+ scope: scopeLabel,
425
+ scopeid,
426
+ section,
427
+ },
428
+ };
429
+ expressions.push(Object.assign(Object.assign({}, expressionItem), { value: getValues(oldFiltersItem.values[0][valKey], questionType, validationKey, "value", formulaType && formulaType.toUpperCase()), uiExtras: Object.assign(Object.assign({}, expressionItem.uiExtras), { values: getValues(oldFiltersItem.values[0][valKey], questionType, validationKey, "values", formulaType && formulaType.toUpperCase()), uiValue: getValues(oldFiltersItem.values[0][valKey], questionType, validationKey, "uiValue", formulaType && formulaType.toUpperCase()) }) }));
430
+ });
431
+ return expressions;
432
+ }
@@ -29,7 +29,8 @@ export declare enum CHART_TYPE {
29
29
  COMBINED_CHART = "COMBINED_CHART",
30
30
  RADAR_CHART = "RADAR_CHART",
31
31
  TREEMAP_CHART = "TREEMAP_CHART",
32
- AREA_CHART = "AREA_CHART"
32
+ AREA_CHART = "AREA_CHART",
33
+ AREA_CHART_MS = "AREA_CHART_MS"
33
34
  }
34
35
  export declare enum CHART_DIMENSION {
35
36
  CATEGORIES = "categories",
@@ -31,6 +31,7 @@ export var CHART_TYPE;
31
31
  CHART_TYPE["RADAR_CHART"] = "RADAR_CHART";
32
32
  CHART_TYPE["TREEMAP_CHART"] = "TREEMAP_CHART";
33
33
  CHART_TYPE["AREA_CHART"] = "AREA_CHART";
34
+ CHART_TYPE["AREA_CHART_MS"] = "AREA_CHART_MS";
34
35
  })(CHART_TYPE || (CHART_TYPE = {}));
35
36
  export var CHART_DIMENSION;
36
37
  (function (CHART_DIMENSION) {
@@ -1,3 +1,4 @@
1
+ export * from "./transformFilters";
1
2
  export * from "./modelAnswerToFilterValue";
2
3
  export * from "./adaptDateGroupingProperty";
3
4
  export * from "./adaptFilterData";
@@ -1,3 +1,4 @@
1
+ export * from "./transformFilters";
1
2
  export * from "./modelAnswerToFilterValue";
2
3
  export * from "./adaptDateGroupingProperty";
3
4
  export * from "./adaptFilterData";
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Transform the old filters structure into the new one
3
+ *
4
+ * @deprecated Since 2020, Added back in 2024 but not support provided (unmantained)
5
+ * @param {Object} oldFiltersObj an object with the old filters structure
6
+ * @param {String} section could be 'ANALYZE', 'PB', 'UM' or any other value. Deafult is 'ANYWHERE'
7
+ *
8
+ * @returns {Object} an object with the new filters structure
9
+ *
10
+ * @example
11
+ *
12
+ * const oldPreferenceFilters = {
13
+ * "WWXHAULtR-_-xYOQAdpqT__ENABLED": true,
14
+ * "WWXHAULtR-_-xYOQAdpqT__ALL--ENABLED": true,
15
+ * "WWXHAULtR-_-xYOQAdpqT__COLLAPSED": false,
16
+ * "WWXHAULtR-_-xYOQAdpqT__LABEL": "GLOBAL",
17
+ * "WWXHAULtR-_-xYOQAdpqT-_-AK4M8UV2": {
18
+ * "formulaId": null,
19
+ * "panelId": null,
20
+ * "values": [
21
+ * {
22
+ * "EQUALS": [
23
+ * {
24
+ * "id": "AK4M8UV2a0",
25
+ * "value": "A",
26
+ * "enabled": true,
27
+ * "imageUrl": null
28
+ * },
29
+ * {
30
+ * "id": "AK4M8UV2a1",
31
+ * "value": "B",
32
+ * "enabled": true,
33
+ * "imageUrl": null
34
+ * }
35
+ * ]
36
+ * }
37
+ * ],
38
+ * "bucketId": null,
39
+ * "text": "MC",
40
+ * "title": "MC",
41
+ * "type": "SINGLE_CHOICE",
42
+ * "qid": "AK4M8UV2",
43
+ * "dataset": {
44
+ * "sourceid": "xYOQAdpqT",
45
+ * "name": "Form All Questions",
46
+ * "qrveyid": "xYOQAdpqT",
47
+ * "text": "Form All Questions",
48
+ * "linkid": 0
49
+ * },
50
+ * "enabled": true,
51
+ * "linked": null
52
+ * }
53
+ * };
54
+ *
55
+ * const filterData = transformFilters(oldPreferenceFilters, 'ANALYZE');
56
+ */
57
+ export declare function transformFilters(oldFiltersObj?: {}, section?: string): {
58
+ enabled: boolean;
59
+ logic: any[];
60
+ section: string;
61
+ };
@@ -0,0 +1,428 @@
1
+ /* eslint-disable no-nested-ternary */
2
+ import { isTokenLabel } from "../../tokens/isTokenLabel";
3
+ import { FILTER_VALIDATOR } from "../constants/common/FILTER_VALIDATOR";
4
+ import { FILTER_SCOPE } from "../constants/common/FILTER_SCOPE";
5
+ import { FILTER_SECTION } from "../constants/common/FILTER_SECTION";
6
+ import { FILTER_BACKEND_VALIDATOR } from "../constants/backend/FILTER_BACKEND_VALIDATOR";
7
+ import { _get } from "../../general/object/get";
8
+ import { capitalize } from "../../general/string/capitalize";
9
+ /**
10
+ * Transform the old filters structure into the new one
11
+ *
12
+ * @deprecated Since 2020, Added back in 2024 but not support provided (unmantained)
13
+ * @param {Object} oldFiltersObj an object with the old filters structure
14
+ * @param {String} section could be 'ANALYZE', 'PB', 'UM' or any other value. Deafult is 'ANYWHERE'
15
+ *
16
+ * @returns {Object} an object with the new filters structure
17
+ *
18
+ * @example
19
+ *
20
+ * const oldPreferenceFilters = {
21
+ * "WWXHAULtR-_-xYOQAdpqT__ENABLED": true,
22
+ * "WWXHAULtR-_-xYOQAdpqT__ALL--ENABLED": true,
23
+ * "WWXHAULtR-_-xYOQAdpqT__COLLAPSED": false,
24
+ * "WWXHAULtR-_-xYOQAdpqT__LABEL": "GLOBAL",
25
+ * "WWXHAULtR-_-xYOQAdpqT-_-AK4M8UV2": {
26
+ * "formulaId": null,
27
+ * "panelId": null,
28
+ * "values": [
29
+ * {
30
+ * "EQUALS": [
31
+ * {
32
+ * "id": "AK4M8UV2a0",
33
+ * "value": "A",
34
+ * "enabled": true,
35
+ * "imageUrl": null
36
+ * },
37
+ * {
38
+ * "id": "AK4M8UV2a1",
39
+ * "value": "B",
40
+ * "enabled": true,
41
+ * "imageUrl": null
42
+ * }
43
+ * ]
44
+ * }
45
+ * ],
46
+ * "bucketId": null,
47
+ * "text": "MC",
48
+ * "title": "MC",
49
+ * "type": "SINGLE_CHOICE",
50
+ * "qid": "AK4M8UV2",
51
+ * "dataset": {
52
+ * "sourceid": "xYOQAdpqT",
53
+ * "name": "Form All Questions",
54
+ * "qrveyid": "xYOQAdpqT",
55
+ * "text": "Form All Questions",
56
+ * "linkid": 0
57
+ * },
58
+ * "enabled": true,
59
+ * "linked": null
60
+ * }
61
+ * };
62
+ *
63
+ * const filterData = transformFilters(oldPreferenceFilters, 'ANALYZE');
64
+ */
65
+ export function transformFilters(oldFiltersObj = {}, section = FILTER_SECTION.ANYWHERE) {
66
+ const filterData = { enabled: true, logic: [], section };
67
+ const tempScopes = {};
68
+ Object.keys(oldFiltersObj).forEach((key) => {
69
+ if (key.endsWith("__ALL--ENABLED"))
70
+ filterData.enabled = oldFiltersObj[key];
71
+ if (!key.endsWith("ENABLED") &&
72
+ !key.endsWith("LABEL") &&
73
+ !key.endsWith("COLLAPSED")) {
74
+ const filterSeparator = "-_-";
75
+ const keyArray = key.split(filterSeparator);
76
+ const tempArray = [...keyArray];
77
+ tempArray.pop();
78
+ let enabledKey = "__ENABLED";
79
+ let collapsedKey = "__COLLAPSED";
80
+ let labelKey = "__LABEL";
81
+ if (keyArray.length === 5) {
82
+ tempArray.pop();
83
+ enabledKey = "__PANELS--ENABLED";
84
+ collapsedKey = "__PANELS--COLLAPSED";
85
+ labelKey = "__PANELS--LABEL";
86
+ }
87
+ const keyBase = tempArray.join(filterSeparator);
88
+ const scopeLabel = getScopeLabel(oldFiltersObj[keyBase + labelKey]);
89
+ const scopeId = tempArray[tempArray.length - 1] !== "VIEW"
90
+ ? tempArray[tempArray.length - 1]
91
+ : undefined;
92
+ const scopeKey = `${scopeLabel}|${scopeId}`;
93
+ if (!tempScopes[scopeKey]) {
94
+ tempScopes[scopeKey] = {
95
+ enabled: oldFiltersObj[keyBase + enabledKey],
96
+ scope: scopeLabel,
97
+ filters: [],
98
+ uiExtras: {
99
+ scopeid: scopeId,
100
+ collapsed: oldFiltersObj[keyBase + collapsedKey],
101
+ section,
102
+ },
103
+ };
104
+ }
105
+ const filtersObj = validateComplexTypes(oldFiltersObj[key], Object.keys(oldFiltersObj[key].values[0]));
106
+ const validations = Object.keys(filtersObj.values[0]);
107
+ tempScopes[scopeKey].filters.push({
108
+ operator: "AND",
109
+ expressions: [
110
+ {
111
+ operator: "OR",
112
+ uiExtras: {
113
+ expressionid: `${filtersObj.qid}|${scopeId}|${filtersObj.panelid || ""}`,
114
+ },
115
+ expressions: getExpressions(filtersObj, validations, scopeLabel, scopeId, section),
116
+ },
117
+ ],
118
+ });
119
+ }
120
+ });
121
+ filterData.logic = Object.values(tempScopes);
122
+ return filterData;
123
+ }
124
+ function validateComplexTypes(oldFiltersItem, validations) {
125
+ if (![
126
+ "IMAGEUPLOAD",
127
+ "TEXTFIELD",
128
+ "LONGTEXT",
129
+ "NAME",
130
+ "ADDRESS",
131
+ "USADDRESS",
132
+ ].includes(oldFiltersItem.type))
133
+ return oldFiltersItem;
134
+ const complexFiltersItem = Object.assign(Object.assign({}, oldFiltersItem), { values: [{}] });
135
+ validations.forEach((valKey) => {
136
+ oldFiltersItem.values[0][valKey].forEach((value) => {
137
+ const newKey = `${valKey}|${(value.targetFilter || value.property || "")
138
+ .toLowerCase()
139
+ .replace("_text", "")
140
+ .replace("_type", "type")}`;
141
+ if (!complexFiltersItem.values[0][newKey]) {
142
+ complexFiltersItem.values[0][newKey] = [value];
143
+ }
144
+ else {
145
+ complexFiltersItem.values[0][newKey].push(value);
146
+ }
147
+ });
148
+ });
149
+ return complexFiltersItem;
150
+ }
151
+ function getScopeLabel(oldScopeLabel) {
152
+ switch (oldScopeLabel) {
153
+ case "GLOBAL":
154
+ return FILTER_SCOPE.GLOBAL;
155
+ case "PAGE":
156
+ return FILTER_SCOPE.PAGE;
157
+ case "TAB":
158
+ return FILTER_SCOPE.TAB;
159
+ case "PANEL":
160
+ return FILTER_SCOPE.CHART;
161
+ default:
162
+ return oldScopeLabel;
163
+ }
164
+ }
165
+ function getValidationType(oldValidation, columnType, formulaType) {
166
+ switch (oldValidation) {
167
+ case "EQUALS":
168
+ case "EQUAL":
169
+ return FILTER_VALIDATOR.EQUALS;
170
+ case "NOT_EQUALS":
171
+ case "NOT_EQUAL":
172
+ return FILTER_VALIDATOR.NOT_EQUAL;
173
+ case "CONTAINS":
174
+ case "CONTAIN":
175
+ return FILTER_VALIDATOR.CONTAINS;
176
+ case "NOT_CONTAINS":
177
+ case "NOT_CONTAIN":
178
+ return FILTER_VALIDATOR.NOT_CONTAIN;
179
+ case "LESS_THAN":
180
+ case "LESS_THAN_OR_EQUAL":
181
+ return FILTER_VALIDATOR.LESS_THAN_EQUAL;
182
+ case "GREATER_THAN":
183
+ case "GREATER_THAN_OR_EQUAL":
184
+ return FILTER_VALIDATOR.GREATER_THAN_EQUAL;
185
+ case "BETWEEN":
186
+ case "YEAR":
187
+ case "MONTH_YEAR":
188
+ case "QUARTER":
189
+ case "WEEK":
190
+ case "SECOND":
191
+ case "MINUTE":
192
+ case "HOUR":
193
+ case "DAY":
194
+ return ![columnType, formulaType].includes("DATE")
195
+ ? FILTER_VALIDATOR.BETWEEN_INCLUSIVE
196
+ : FILTER_BACKEND_VALIDATOR.RANGE;
197
+ default:
198
+ return oldValidation;
199
+ }
200
+ }
201
+ function getValidator(oldValidation) {
202
+ switch (oldValidation) {
203
+ case "EQUALS":
204
+ case "EQUAL":
205
+ case "YEAR":
206
+ case "MONTH_YEAR":
207
+ case "QUARTER":
208
+ case "WEEK":
209
+ case "SECOND":
210
+ case "MINUTE":
211
+ case "HOUR":
212
+ case "DAY":
213
+ return FILTER_VALIDATOR.EQUALS;
214
+ case "NOT_EQUALS":
215
+ case "NOT_EQUAL":
216
+ return FILTER_VALIDATOR.NOT_EQUAL;
217
+ case "CONTAINS":
218
+ case "CONTAIN":
219
+ return FILTER_VALIDATOR.CONTAINS;
220
+ case "NOT_CONTAINS":
221
+ case "NOT_CONTAIN":
222
+ return FILTER_VALIDATOR.NOT_CONTAIN;
223
+ case "BETWEEN":
224
+ return FILTER_VALIDATOR.BETWEEN_INCLUSIVE;
225
+ case "LESS_THAN":
226
+ case "LESS_THAN_OR_EQUAL":
227
+ return FILTER_VALIDATOR.LESS_THAN_EQUAL;
228
+ case "GREATER_THAN":
229
+ case "GREATER_THAN_OR_EQUAL":
230
+ return FILTER_VALIDATOR.GREATER_THAN_EQUAL;
231
+ case "IS_EMPTY":
232
+ return FILTER_VALIDATOR.IS_NULL;
233
+ case "IS_NOT_EMPTY":
234
+ return FILTER_VALIDATOR.IS_NOT_NULL;
235
+ default:
236
+ return oldValidation;
237
+ }
238
+ }
239
+ function getProperty(filterObjItem, validationKey) {
240
+ if (filterObjItem.type === "IMAGE")
241
+ return "answer_id";
242
+ const value = _get(filterObjItem, `values[0]['${validationKey}'][0]`, {});
243
+ const property = filterObjItem.property || value.property || value.targetFilter;
244
+ return property && property.toUpperCase();
245
+ }
246
+ function getDateProperty(validationKey, columnType, formulaType) {
247
+ if (![columnType, formulaType].includes("DATE"))
248
+ return;
249
+ return [
250
+ "MONTH_YEAR",
251
+ "YEAR",
252
+ "QUARTER",
253
+ "WEEK",
254
+ "HOUR",
255
+ "MINUTE",
256
+ "SECOND",
257
+ ].includes(validationKey)
258
+ ? validationKey.split("_")[0].toLowerCase()
259
+ : "day";
260
+ }
261
+ function getFilterRangeValue(range, minProp = "min", maxProp = "max", extras = {}) {
262
+ return Object.assign({ [minProp]: range.from || range.gte, [maxProp]: range.to || range.lte }, extras);
263
+ }
264
+ function getUiRangeValue(oldValue) {
265
+ if (oldValue.label)
266
+ return (oldValue.label + "").replace("-", " ");
267
+ const range = oldValue.range || oldValue;
268
+ return `${range.from || range.gte} - ${range.to || range.lte}`;
269
+ }
270
+ function getRankingFBvalues(values) {
271
+ const returnValues = [];
272
+ values
273
+ .map((vItem) => (Object.assign(Object.assign({}, vItem), { position: +vItem.position + 1 })))
274
+ .forEach((vItem) => {
275
+ const groupIndex = returnValues.findIndex((group) => group.every((groupItem) => groupItem.value.value !== vItem.value) &&
276
+ group.every((groupItem) => groupItem.position !== vItem.position));
277
+ const returnItem = {
278
+ position: vItem.position,
279
+ value: { enabled: vItem.enabled, value: vItem.value },
280
+ };
281
+ if (groupIndex === -1)
282
+ returnValues.push([returnItem]);
283
+ else
284
+ returnValues[groupIndex].push(returnItem);
285
+ });
286
+ return returnValues;
287
+ }
288
+ function getValues(values, columnType, oldValidation = "EQUAL", destination = "value", formulaType) {
289
+ switch (oldValidation) {
290
+ case "BETWEEN":
291
+ case "YEAR":
292
+ case "MONTH_YEAR":
293
+ return destination === "value"
294
+ ? values.map((val) => getFilterRangeValue(val.value.range || val.value, "gte", "lte"))
295
+ : destination === "uiValue"
296
+ ? values.map((val) => getFilterRangeValue(val.value.range || val.value, "min", "max", {
297
+ enabled: val.enabled,
298
+ value: getUiRangeValue(val.value),
299
+ validationLabel: oldValidation,
300
+ }))
301
+ : ["YEAR", "MONTH_YEAR", "QUARTER"].includes(oldValidation)
302
+ ? values.map((item) => ({ value: getUiRangeValue(item.value) }))
303
+ : values.map((val) => getFilterRangeValue(val.value.range || val.value));
304
+ case "LESS_THAN":
305
+ case "GREATER_THAN": {
306
+ const prop = oldValidation === "LESS_THAN" ? "lte" : "gte";
307
+ return destination === "value"
308
+ ? values.map((val) => getFilterRangeValue({ [prop]: val.value }, "gte", "lte"))
309
+ : destination === "uiValue"
310
+ ? values.map((val) => getFilterRangeValue({ [prop]: val.value }, "min", "max", {
311
+ enabled: val.enabled,
312
+ value: val.value,
313
+ }))
314
+ : values.map((val) => getFilterRangeValue({ [prop]: val.value }));
315
+ }
316
+ case "IS_EMPTY":
317
+ case "IS_NOT_EMPTY": {
318
+ const valueClean = getValidator(oldValidation)
319
+ .toLowerCase()
320
+ .split("_")
321
+ .map((v) => capitalize(v))
322
+ .join(" ");
323
+ return destination === "value"
324
+ ? [valueClean]
325
+ : destination === "uiValue"
326
+ ? values.map((val) => ({ value: valueClean, enabled: val.enabled }))
327
+ : [];
328
+ }
329
+ default:
330
+ return getFilterEqualValues(values, columnType, destination, formulaType);
331
+ }
332
+ }
333
+ function getFilterEqualValues(values, columnType, destination = "value", formulaType) {
334
+ if (columnType === "RANKING") {
335
+ return destination === "value"
336
+ ? values.map((item) => ({ position: +item.position, value: item.value }))
337
+ : destination === "uiValue"
338
+ ? values.map((item) => ({
339
+ value: getUiRangeValue({
340
+ from: item.value,
341
+ to: +item.position + 1,
342
+ }),
343
+ enabled: true,
344
+ }))
345
+ : getRankingFBvalues(values);
346
+ }
347
+ return destination === "value"
348
+ ? values.map((item) => {
349
+ let value = item.value;
350
+ if ([columnType, formulaType].includes("DATE") &&
351
+ !isTokenLabel(value)) {
352
+ value = getFilterRangeValue(item.value.range || item.value, "gte", "lte");
353
+ }
354
+ else if (columnType === "YES_NO") {
355
+ value = item.value.toLowerCase();
356
+ }
357
+ else if (columnType === "IMAGE") {
358
+ value = item.id || item.answerid || item.answer_id;
359
+ }
360
+ else {
361
+ value = String(item.value);
362
+ }
363
+ return value;
364
+ })
365
+ : values.map((item) => ({
366
+ value: columnType === "YES_NO"
367
+ ? item.value.toLowerCase()
368
+ : [columnType, formulaType].includes("DATE")
369
+ ? item.value.label || item.value
370
+ : String(item.value),
371
+ valueid: columnType === "EXPRESSION" ? undefined : item.id || item.answerid,
372
+ enabled: item.enabled || true,
373
+ }));
374
+ }
375
+ function getFormulaType(oldFiltersItem) {
376
+ if (oldFiltersItem.formulaType)
377
+ return oldFiltersItem.formulaType;
378
+ if (oldFiltersItem.formulaId)
379
+ return oldFiltersItem.type === "NUMERIC"
380
+ ? "number"
381
+ : oldFiltersItem.type === "DATE"
382
+ ? "date"
383
+ : "string";
384
+ }
385
+ function getExpressions(oldFiltersItem, validations, scopeLabel, scopeid, section) {
386
+ const expressions = [];
387
+ validations.forEach((valKey) => {
388
+ const valKeyArray = valKey.split("|");
389
+ const validationKey = valKeyArray[0];
390
+ const property = valKeyArray[1] || getProperty(oldFiltersItem, validationKey);
391
+ const formulaType = getFormulaType(oldFiltersItem);
392
+ const questionType = formulaType ? "FORMULA" : oldFiltersItem.type;
393
+ const expressionItem = {
394
+ enabled: oldFiltersItem.enabled,
395
+ property: property,
396
+ qrveyid: oldFiltersItem.dataset.qrveyid,
397
+ questionid: oldFiltersItem.qid,
398
+ formulaType: formulaType,
399
+ questionType: questionType,
400
+ type: questionType,
401
+ validationType: getValidationType(validationKey, questionType, formulaType && formulaType.toUpperCase()),
402
+ uiExtras: {
403
+ column: {
404
+ dataset: {
405
+ qrveyid: oldFiltersItem.dataset.qrveyid,
406
+ name: oldFiltersItem.dataset.name,
407
+ },
408
+ formulaType: formulaType,
409
+ id: oldFiltersItem.qid,
410
+ qrveyid: oldFiltersItem.dataset.qrveyid,
411
+ text: oldFiltersItem.text,
412
+ type: questionType,
413
+ },
414
+ operator: "AND",
415
+ property: getDateProperty(validationKey, questionType, formulaType && formulaType.toUpperCase()) || property,
416
+ validator: getValidator(validationKey),
417
+ panelid: oldFiltersItem.panelId,
418
+ panelName: oldFiltersItem.panelId ? oldFiltersItem.title : undefined,
419
+ uiHidden: false,
420
+ scope: scopeLabel,
421
+ scopeid,
422
+ section,
423
+ },
424
+ };
425
+ expressions.push(Object.assign(Object.assign({}, expressionItem), { value: getValues(oldFiltersItem.values[0][valKey], questionType, validationKey, "value", formulaType && formulaType.toUpperCase()), uiExtras: Object.assign(Object.assign({}, expressionItem.uiExtras), { values: getValues(oldFiltersItem.values[0][valKey], questionType, validationKey, "values", formulaType && formulaType.toUpperCase()), uiValue: getValues(oldFiltersItem.values[0][valKey], questionType, validationKey, "uiValue", formulaType && formulaType.toUpperCase()) }) }));
426
+ });
427
+ return expressions;
428
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qrvey/utils",
3
- "version": "1.15.0-0",
3
+ "version": "1.15.0-2",
4
4
  "description": "Helper, Utils for all Qrvey Projects",
5
5
  "homepage": "https://bitbucket.org/qrvey/qrvey_utils/wiki/Home",
6
6
  "main": "dist/index.js",