@izara_project/izara-shared-search-and-sort 1.0.1 → 1.0.3

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.
@@ -0,0 +1,1544 @@
1
+ /*
2
+ Copyright (C) 2025 Sven Mason <http://izara.io>
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Affero General Public License as
6
+ published by the Free Software Foundation, either version 3 of the
7
+ License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Affero General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Affero General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ import { objectHash as hash } from '@izara_project/izara-shared-core'; //const hash = require('@izara_project/izara-shared-core').objectHash;
21
+ import lodash from 'lodash';
22
+ const { isEmpty } = lodash;
23
+ import complexFilterShared from '@izara_project/izara-core-library-complex-filter';
24
+
25
+ function validateFilterElement(
26
+ filterElements,
27
+ logicalElements
28
+ ) {
29
+ let validate = false;
30
+ let countsBracket = 0;
31
+ let operationCounts = 0;
32
+ let logicalCounts = 0;
33
+ for (let logicalElement of Object.values(logicalElements)) {
34
+ if (logicalElement.logicalElementType === "openBracket" || logicalElement.logicalElementType === "closeBracket") {
35
+ countsBracket = countsBracket + 1;
36
+ } else if (logicalElement.logicalElementType === "operation") {
37
+ operationCounts = operationCounts + 1;
38
+ } else if (logicalElement.logicalElementType === "logical"
39
+ || logicalElement.logicalElementType === "childComplexFilter"
40
+ || logicalElement.logicalElementType === "identifiers"
41
+ || logicalElement.logicalElementType === "translateIds"
42
+ || logicalElement.logicalElementType === "traversal"
43
+ ) {
44
+ logicalCounts = logicalCounts + 1;
45
+ }
46
+ }
47
+ let logicalElementsLength = logicalCounts + operationCounts;
48
+ let filtersLength = Object.keys(filterElements).length;
49
+ console.log('check count: ', {
50
+ countsBracket,
51
+ operationCounts,
52
+ logicalCounts,
53
+ logicalElementsLength,
54
+ filtersLength
55
+ });
56
+
57
+ if (filtersLength === logicalElementsLength) {
58
+ validate = true;
59
+ }
60
+ return validate
61
+ }
62
+
63
+ function createFiltersRequest(
64
+ _izContext,
65
+ objType,
66
+ initialLogicalElementId,
67
+ logicalElements,
68
+ values,
69
+ ) {
70
+ _izContext.logger.debug('createFiltersRequest: ', {
71
+ objType,
72
+ initialLogicalElementId,
73
+ logicalElements,
74
+ values,
75
+ });
76
+
77
+ let filterMainId = null;
78
+ let filterMainObject = {};
79
+ let operation = null;
80
+
81
+ let errorsObject = {};
82
+ let errorsFound = [];
83
+ let status = 'valid';
84
+
85
+ //* validate combine logicalStructure
86
+ let [validateObject, validateFilterMainId, validateFilterObject, validateErrorsObject, validateErrors] = validateCombinationStructure(
87
+ _izContext,
88
+ initialLogicalElementId,
89
+ logicalElements,
90
+ values
91
+ );
92
+ _izContext.logger.debug('return validateCombinationStructure: ', {
93
+ validateObject,
94
+ validateFilterMainId,
95
+ validateFilterObject,
96
+ validateErrorsObject,
97
+ validateErrors
98
+ });
99
+
100
+ if (!validateObject || validateErrors.length > 0) {
101
+ Object.assign(errorsObject, validateErrorsObject);
102
+ errorsFound = errorsFound.concat(validateErrors);
103
+ return [
104
+ null,
105
+ 'invalid',
106
+ errorsObject,
107
+ errorsFound
108
+ ];
109
+ };
110
+
111
+ let operationErrorsObject = {};
112
+ let operationErrors = [];
113
+
114
+ [
115
+ filterMainId,
116
+ filterMainObject,
117
+ operation,
118
+ operationErrorsObject,
119
+ operationErrors
120
+ ] = filterLogicalElements(
121
+ _izContext,
122
+ objType,
123
+ initialLogicalElementId,
124
+ logicalElements,
125
+ values,
126
+ );
127
+ _izContext.logger.debug('final return filterLogicalElements: ', {
128
+ filterMainId,
129
+ filterMainObject,
130
+ operation,
131
+ operationErrorsObject,
132
+ operationErrors
133
+ });
134
+
135
+ if (errorsFound.length > 0 || filterMainId === null) {
136
+ Object.assign(errorsObject, operationErrorsObject);
137
+ errorsFound = errorsFound.concat(operationErrors);
138
+ return [
139
+ null,
140
+ 'invalid',
141
+ errorsObject,
142
+ errorsFound
143
+ ];
144
+ };
145
+
146
+ let filterElements = {};
147
+ if (filterMainObject.filterElement.hasOwnProperty('filterElements')) {
148
+ filterElements = filterMainObject.filterElement.filterElements;
149
+
150
+ delete filterMainObject.filterElement.filterElements;
151
+ Object.assign(filterElements, { [filterMainId]: filterMainObject });
152
+ };
153
+
154
+ if (isEmpty(filterElements)) {
155
+ filterElements = {
156
+ [filterMainId]: filterMainObject
157
+ };
158
+ };
159
+
160
+ let complexFilterRequest = {
161
+ objType: objType,
162
+ filterMainId: filterMainId,
163
+ filterElements: filterElements
164
+ };
165
+ _izContext.logger.debug('complexFilterRequest: ', complexFilterRequest);
166
+
167
+ // let validate = validateFilterElement(
168
+ // complexFilterRequest.filterElements,
169
+ // logicalElements
170
+ // );
171
+ // _izContext.logger.debug('validate: ', validate);
172
+
173
+ // if (validate === false) {
174
+ // errorsObject[initialLogicalElementId] = 'filterElements has create fail';
175
+ // errorsFound.push("filterElements has create fail");
176
+ // return [null, "error", errorsObject, errorsFound]
177
+ // }
178
+
179
+ return [
180
+ complexFilterRequest,
181
+ status,
182
+ errorsObject,
183
+ errorsFound
184
+ ];
185
+
186
+ };
187
+
188
+ function validateCombinationStructure(
189
+ _izContext,
190
+ initialLogicalElementId,
191
+ logicalElements,
192
+ values
193
+ ) {
194
+ _izContext.logger.debug('validateCombinationStructure: ', {
195
+ initialLogicalElementId,
196
+ logicalElements,
197
+ values
198
+ });
199
+
200
+ let validateObject = true;
201
+ let errorsObject = {};
202
+ let errorsFound = []
203
+
204
+ let runningFilterMainId = null;
205
+ let runningFilterObject = null;
206
+ let currentFilterMainId = null;
207
+ let currentFilterObject = null;
208
+ let lastOperation = null;
209
+
210
+ for (const [logicalElementId, logicalElementObject] of Object.entries(logicalElements)) {
211
+ _izContext.logger.debug('iterate logicalElements: ', { initialLogicalElementId, logicalElementId, logicalElementObject });
212
+
213
+ if (initialLogicalElementId !== logicalElementId) {
214
+ continue;
215
+ };
216
+ _izContext.logger.debug('----------------- start logicalElement --------------');
217
+
218
+ if (!logicalElementObject.hasOwnProperty("logicalElementType")) {
219
+ errorsObject[logicalElementId] = `this logical structure is not set logicalElementType`;
220
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set logicalElementType`);
221
+ };
222
+ if (!logicalElementObject.hasOwnProperty("previousLogicalElementId")) {
223
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
224
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
225
+ };
226
+ if (!logicalElementObject.hasOwnProperty("nextLogicalElementId")) {
227
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
228
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
229
+ };
230
+
231
+ let logicalElementType = logicalElementObject.logicalElementType;
232
+
233
+ if (logicalElementType === 'operation') {
234
+ _izContext.logger.debug('----------------- operation --------------');
235
+ _izContext.logger.debug('logicalElementObject: ', logicalElementObject);
236
+
237
+ lastOperation = logicalElementObject.operation;
238
+ _izContext.logger.debug('lastOperation: ', lastOperation);
239
+
240
+ if (isEmpty(lastOperation)) {
241
+ _izContext.logger.debug('no operation: ', logicalElementObject.operation);
242
+ errorsObject[logicalElementId] = `this logical structure is not set operation parameter`;
243
+ errorsFound.push(`this logical structure type: operation: ${logicalElementId} is not set operation parameter`);
244
+ break;
245
+ };
246
+
247
+ if (logicalElementObject.nextLogicalElementId !== null) {
248
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
249
+ };
250
+ continue;
251
+ };
252
+
253
+ if (logicalElementType === 'openBracket') {
254
+ _izContext.logger.debug('----------------- openBracket --------------');
255
+
256
+ let nextElementId = null;
257
+
258
+ [validateObject, currentFilterMainId, currentFilterObject, errorsObject, errorsFound, nextElementId] = validateCombinationStructure(
259
+ _izContext,
260
+ logicalElementObject.nextLogicalElementId,
261
+ logicalElements,
262
+ values
263
+ );
264
+ _izContext.logger.debug('return validateCombinationStruce for openBracket: ', {
265
+ validateObject,
266
+ currentFilterMainId,
267
+ currentFilterObject,
268
+ errorsObject,
269
+ errorsFound,
270
+ nextElementId
271
+ });
272
+
273
+ if (nextElementId !== null) {
274
+ initialLogicalElementId = nextElementId;
275
+ };
276
+
277
+ } else if (logicalElementType === 'closeBracket') {
278
+ _izContext.logger.debug('----------------- closeBracket --------------');
279
+
280
+ return [
281
+ true,
282
+ runningFilterMainId,
283
+ runningFilterObject,
284
+ errorsObject,
285
+ errorsFound,
286
+ logicalElementObject.nextLogicalElementId
287
+ ];
288
+
289
+ } else if (logicalElementType === 'logical') {
290
+ _izContext.logger.debug('----------------- logical --------------');
291
+ if (!logicalElementObject.hasOwnProperty('objType')) {
292
+ errorsObject[logicalElementId] = `this logical structure is not set objType`;
293
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set objType`);
294
+ };
295
+ if (!logicalElementObject.hasOwnProperty('fieldName')) {
296
+ errorsObject[logicalElementId] = `this logical structure is not set fieldName`;
297
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set fieldName`);
298
+ };
299
+ if (!logicalElementObject.hasOwnProperty('comparison')) {
300
+ errorsObject[logicalElementId] = `this logical structure is not set comparison`;
301
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set comparison`);
302
+ };
303
+ if (!logicalElementObject.hasOwnProperty('value')) {
304
+ errorsObject[logicalElementId] = `this logical structure is not set value`;
305
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set value`);
306
+ };
307
+
308
+ currentFilterMainId = logicalElementId;
309
+ currentFilterObject = logicalElementObject;
310
+
311
+ if (logicalElementObject.nextLogicalElementId !== null) {
312
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
313
+ };
314
+
315
+ } else if (logicalElementType === 'childComplexFilter') {
316
+ _izContext.logger.debug('----------------- childComplexFilter --------------');
317
+ if (!logicalElementObject.hasOwnProperty('objType')) {
318
+ errorsObject[logicalElementId] = `this logical structure is not set objType`;
319
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set objType`);
320
+ };
321
+
322
+ if (!logicalElementObject.hasOwnProperty('pathLinkType')) {
323
+ errorsObject[logicalElementId] = `this logical structure is not set pathLinkType`;
324
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set pathLinkType`);
325
+ } else {
326
+ if (!logicalElementObject.pathLinkType.hasOwnProperty('objType')
327
+ && !logicalElementObject.pathLinkType.hasOwnProperty('relType')
328
+ && !logicalElementObject.pathLinkType.hasOwnProperty('direction')
329
+ ) {
330
+ errorsObject[logicalElementId] = `this logical structure is not set properties in pathLinkType in type: childComplexFilter`;
331
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set {objType | relType | direction} in pathLinkType in type: childComplexFilter`);
332
+ };
333
+ };
334
+
335
+ let parantObjType = logicalElementObject.objType;
336
+ _izContext.logger.debug('check parantObjType: ', {
337
+ parantObjType
338
+ });
339
+
340
+ let [
341
+ childValidate,
342
+ childElementId,
343
+ childElementObject,
344
+ childErrorsObject,
345
+ childErrorsFound
346
+ ] = this.validateCombinationStructure(
347
+ _izContext,
348
+ logicalElementObject.childLogicalElementId,
349
+ logicalElements,
350
+ values
351
+ );
352
+ _izContext.logger.debug('return validateCombinationStruce for childComplexFilter: ', {
353
+ childValidate,
354
+ childElementId,
355
+ childElementObject,
356
+ childErrorsObject,
357
+ childErrorsFound
358
+ });
359
+
360
+ if (!childValidate || childErrorsFound.length > 0) {
361
+ Object.assign(errorsObject, childErrorsObject);
362
+ errorsFound = errorsFound.concat(childErrorsFound);
363
+ };
364
+
365
+ currentFilterMainId = logicalElementId;
366
+ currentFilterObject = logicalElementObject;
367
+
368
+ if (logicalElementObject.nextLogicalElementId !== null) {
369
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
370
+ };
371
+
372
+ } else if (logicalElementType === 'translateIds') {
373
+ _izContext.logger.debug("----------------- translateIds --------------", logicalElementObject);
374
+
375
+ if (!logicalElementObject.hasOwnProperty('objType')) {
376
+ errorsObject[logicalElementId] = `this logical structure is not set objType`;
377
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set objType`);
378
+ };
379
+ if (!logicalElementObject.hasOwnProperty('childIdentifiers')) {
380
+ errorsObject[logicalElementId] = `this logical structure is not set childIdentifiers`;
381
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set childIdentifiers`);
382
+ };
383
+ if (!logicalElementObject.hasOwnProperty('pathLinkType')) {
384
+ errorsObject[logicalElementId] = `this logical structure is not set pathLinkType`;
385
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set pathLinkType`);
386
+ } else {
387
+ if (!logicalElementObject.pathLinkType.hasOwnProperty('objType')
388
+ && !logicalElementObject.pathLinkType.hasOwnProperty('relType')
389
+ && !logicalElementObject.pathLinkType.hasOwnProperty('direction')
390
+ ) {
391
+ errorsObject[logicalElementId] = `this logical structure is not set properties in pathLinkType`;
392
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set {objType | relType | direction} in pathLinkType`);
393
+ };
394
+ };
395
+
396
+ currentFilterMainId = logicalElementId;
397
+ currentFilterObject = logicalElementObject;
398
+
399
+ if (logicalElementObject.nextLogicalElementId !== null) {
400
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
401
+ };
402
+
403
+ } else if (logicalElementType === 'identifiers') {
404
+ _izContext.logger.debug('----------------- identifiers --------------', logicalElementObject);
405
+ if (!logicalElementObject.hasOwnProperty('objType')) {
406
+ errorsObject[logicalElementId] = `this logical structure is not set objType`;
407
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set objType`);
408
+ };
409
+ if (!logicalElementObject.hasOwnProperty('fieldName')) {
410
+ errorsObject[logicalElementId] = `this logical structure is not set fieldName`;
411
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set fieldName`);
412
+ };
413
+ if (!logicalElementObject.hasOwnProperty('comparison')) {
414
+ errorsObject[logicalElementId] = `this logical structure is not set comparison`;
415
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set comparison`);
416
+ };
417
+ if (!logicalElementObject.hasOwnProperty('value')) {
418
+ errorsObject[logicalElementId] = `this logical structure is not set value`;
419
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set value`);
420
+ };
421
+
422
+ currentFilterMainId = logicalElementId;
423
+ currentFilterObject = logicalElementObject;
424
+
425
+ if (logicalElementObject.nextLogicalElementId !== null) {
426
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
427
+ };
428
+
429
+ } else if (logicalElementType === 'traversal') {
430
+ _izContext.logger.debug('----------------- traversal --------------', logicalElementObject);
431
+ if (!logicalElementObject.hasOwnProperty('objType')) {
432
+ errorsObject[logicalElementId] = `this logical structure is not set objType`;
433
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set objType`);
434
+ };
435
+ if (!logicalElementObject.hasOwnProperty('traversals')) {
436
+ errorsObject[logicalElementId] = `this logical structure is not set traversals`;
437
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set traversals`);
438
+ };
439
+
440
+ currentFilterMainId = logicalElementId;
441
+ currentFilterObject = logicalElementObject;
442
+
443
+ if (logicalElementObject.nextLogicalElementId !== null) {
444
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
445
+ };
446
+
447
+ };
448
+
449
+ let operationErrorsFound = {};
450
+ let operationFilterObject = [];
451
+ [
452
+ runningFilterMainId,
453
+ runningFilterObject,
454
+ operationFilterObject,
455
+ operationErrorsFound
456
+ ] = this.validateOperation(
457
+ _izContext,
458
+ runningFilterMainId,
459
+ runningFilterObject,
460
+ lastOperation,
461
+ currentFilterMainId,
462
+ currentFilterObject
463
+ );
464
+ _izContext.logger.debug('return validateOperation: ', {
465
+ runningFilterMainId,
466
+ runningFilterObject,
467
+ operationFilterObject,
468
+ operationErrorsFound
469
+ });
470
+
471
+ if (operationErrorsFound.length > 0) {
472
+ Object.assign(errorsObject, operationFilterObject);
473
+ errorsFound = errorsFound.concat(operationErrorsFound);
474
+ break;
475
+ };
476
+
477
+ };
478
+
479
+ if (errorsFound.length > 0) {
480
+ validateObject = false;
481
+ };
482
+
483
+ return [
484
+ validateObject,
485
+ runningFilterMainId,
486
+ runningFilterObject,
487
+ errorsObject,
488
+ errorsFound
489
+ ];
490
+
491
+ }
492
+
493
+ function validateOperation(
494
+ _izContext,
495
+ runningFilterMainId,
496
+ runningFilterObject,
497
+ lastOperation,
498
+ currentFilterMainId,
499
+ currentFilterObject,
500
+ ) {
501
+ _izContext.logger.debug('validateOperation: ', {
502
+ runningFilterMainId,
503
+ runningFilterObject,
504
+ lastOperation,
505
+ currentFilterMainId,
506
+ currentFilterObject,
507
+ });
508
+
509
+ let errorsObject = {};
510
+ let errorsFound = [];
511
+
512
+ if (!currentFilterObject) {
513
+ errorsObject[currentFilterMainId] = 'cannot found current filterElement object';
514
+ errorsFound.push(`${currentFilterMainId}: cannot found current filterElement object`);
515
+ return [null, null, errorsObject, errorsFound];
516
+ };
517
+
518
+ if (!runningFilterObject) {
519
+ return [currentFilterMainId, currentFilterObject, errorsObject, errorsFound];
520
+ };
521
+
522
+ if (!lastOperation) {
523
+
524
+ let returnNextElementId = null;
525
+ let returnNextElementObject = null;
526
+
527
+ if (runningFilterObject.logicalElementType === 'openBracket') {
528
+ if (currentFilterObject.logicalElementType === 'closeBracket') {
529
+
530
+ errorsObject[currentFilterMainId] = `this path has logicalElementType: ${runningFilterObject.logicalElementType} and ${currentFilterObject.logicalElementType} are error`;
531
+ errorsFound.push(`${currentFilterMainId}: this path has logicalElementType: ${runningFilterObject.logicalElementType} and ${currentFilterObject.logicalElementType} are error`);
532
+
533
+ } else {
534
+
535
+ runningFilterObject.nextLogicalElementId = currentFilterMainId;
536
+ currentFilterObject.previousLogicalElementId = runningFilterMainId;
537
+
538
+ operations = Object.assign(runningFilterObject, currentFilterObject);
539
+
540
+ _izContext.logger.debug('operations: ', operations);
541
+ returnNextElementId = currentFilterMainId;
542
+ returnNextElementObject = currentFilterObject;
543
+
544
+ };
545
+
546
+ } else if (runningFilterObject.logicalElementType === 'closeBracket') {
547
+ if (currentFilterObject.logicalElementType === "closeBracket") {
548
+
549
+ runningFilterObject.nextLogicalElementId = currentFilterMainId;
550
+ currentFilterObject.previousLogicalElementId = runningFilterMainId;
551
+
552
+ operations = Object.assign(runningFilterObject, currentFilterObject);
553
+ _izContext.logger.debug('operations: ', operations);
554
+
555
+ returnNextElementId = currentFilterMainId;
556
+ returnNextElementObject = currentFilterObject;
557
+
558
+ } else {
559
+ errorsObject[currentFilterMainId] = `this path has logicalElementType: ${runningFilterObject.logicalElementType} and ${currentFilterObject.logicalElementType} are error`;
560
+ errorsFound.push(`${currentFilterMainId}: this path has logicalElementType: ${runningFilterObject.logicalElementType} and ${currentFilterObject.logicalElementType} are error`);
561
+ };
562
+
563
+ } else {
564
+
565
+ if (currentFilterObject.logicalElementType === 'closeBracket') {
566
+
567
+ runningFilterObject.nextLogicalElementId = currentFilterMainId;
568
+ currentFilterObject.previousLogicalElementId = runningFilterMainId;
569
+
570
+ operations = Object.assign(runningFilterObject, currentFilterObject);
571
+ _izContext.logger.debug('operations: ', operations);
572
+
573
+ returnNextElementId = currentFilterMainId;
574
+ returnNextElementObject = nextLogicalElementObject;
575
+ } else {
576
+ errorsObject[currentFilterMainId] = `this path has logicalElementType: ${runningFilterObject.logicalElementType} and ${currentFilterObject.logicalElementType} are error`;
577
+ errorsFound.push(`${currentFilterMainId}: this path has logicalElementType: ${runningFilterObject.logicalElementType} and ${currentFilterObject.logicalElementType} are error`);
578
+ };
579
+ };
580
+
581
+ return [returnNextElementId, returnNextElementObject, errorsObject, errorsFound];
582
+
583
+ };
584
+
585
+ if (runningFilterMainId && lastOperation && currentFilterMainId) {
586
+
587
+ // let operationFilterObject = {
588
+ // runningFilterMainId: runningFilterObject,
589
+ // operation: lastOperation,
590
+ // currentFilterMainId: currentFilterObject
591
+ // };
592
+ // // let operationFilterMainId = hash(operationFilterObject);
593
+
594
+ return [currentFilterMainId, currentFilterObject, errorsObject, errorsFound];
595
+
596
+ } else {
597
+ errorsFound.push("found some error that not expect");
598
+ return [null, null, errorsObject, errorsFound];
599
+ };
600
+ };
601
+
602
+ function filterLogicalElements(
603
+ _izContext,
604
+ objType,
605
+ initialLogicalElementId,
606
+ logicalElements,
607
+ values,
608
+ nextLogicalElementId = null,
609
+ runningFilterMainId = null,
610
+ runningFilterObject = null,
611
+ lastLogicalOperator = null,
612
+ currentFilterMainId = null,
613
+ currentFilterObject = null
614
+ ) {
615
+ _izContext.logger.debug('filterLogicalElements: ', {
616
+ objType,
617
+ initialLogicalElementId,
618
+ logicalElements,
619
+ values,
620
+ nextLogicalElementId,
621
+ runningFilterMainId,
622
+ runningFilterObject,
623
+ lastLogicalOperator,
624
+ currentFilterMainId,
625
+ currentFilterObject
626
+ });
627
+
628
+ let errorsObject = {};
629
+ let errorsFound = [];
630
+
631
+ let operation = null;
632
+ let operationErrorsFound = [];
633
+ let lastNextLogicalElementId = null;
634
+ let currentLogicalElementId = initialLogicalElementId;
635
+
636
+ if (nextLogicalElementId !== null) {
637
+ currentLogicalElementId = nextLogicalElementId;
638
+ }
639
+
640
+ let logicalElement = logicalElements[currentLogicalElementId];
641
+ _izContext.logger.debug('logicalElement: ', logicalElement)
642
+
643
+ if (isEmpty(logicalElement)) {
644
+ errorsObject[currentLogicalElementId] = 'this logicalElement not found';
645
+ errorsFound.push(`${currentLogicalElementId}: this logicalElement not found`);
646
+ };
647
+ if (!logicalElement.hasOwnProperty('logicalElementType')) {
648
+ errorsObject[currentLogicalElementId] = 'this logicalElement is not set logicalElementType';
649
+ errorsFound.push(`${currentLogicalElementId}: this logicalElement not set logicalElementType`);
650
+ };
651
+
652
+ let logicalElementType = logicalElement.logicalElementType;
653
+ _izContext.logger.debug('logicalElementType: ', logicalElementType);
654
+
655
+ if (logicalElementType === "openBracket") {
656
+
657
+ let nextElementId = null;
658
+
659
+ [
660
+ currentFilterMainId,
661
+ currentFilterObject,
662
+ operation,
663
+ errorsObject,
664
+ errorsFound,
665
+ nextElementId
666
+ ] = filterLogicalElements(
667
+ _izContext,
668
+ objType,
669
+ initialLogicalElementId,
670
+ logicalElements,
671
+ values,
672
+ logicalElement.nextLogicalElementId
673
+ );
674
+ _izContext.logger.debug('return filterLogicalElement for openBracket: ', {
675
+ currentFilterMainId,
676
+ currentFilterObject,
677
+ operation,
678
+ errorsObject, errorsFound,
679
+ lastNextLogicalElementId
680
+ });
681
+
682
+ if (nextElementId !== null) {
683
+ lastNextLogicalElementId = nextElementId;
684
+ }
685
+
686
+ } else if (logicalElementType === "closeBracket") {
687
+
688
+ return [
689
+ runningFilterMainId,
690
+ runningFilterObject,
691
+ null,
692
+ errorsObject,
693
+ errorsFound,
694
+ logicalElement.nextLogicalElementId
695
+ ]
696
+
697
+ } else if (logicalElementType === "operation") {
698
+
699
+ lastLogicalOperator = logicalElement.operation;
700
+
701
+ if (logicalElement.nextLogicalElementId === null) {
702
+ errorsObject[initialLogicalElementId] = `this operation structure must set nextLogicalElementId`;
703
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} must set nextLogicalElementId`);
704
+ }
705
+
706
+ logicalElement = logicalElements[logicalElement.nextLogicalElementId];
707
+ logicalElementType = logicalElement.logicalElementType;
708
+
709
+ }
710
+
711
+ _izContext.logger.debug("check logicalElement: ", logicalElement);
712
+ _izContext.logger.debug("check logicalElementType: ", logicalElementType);
713
+ _izContext.logger.debug("check filter main id: ", {
714
+ runningFilterMainId,
715
+ lastLogicalOperator,
716
+ currentFilterMainId
717
+ });
718
+
719
+ _izContext.logger.debug("check filter main id: ", {
720
+ runningFilterMainId,
721
+ runningFilterObject,
722
+ lastLogicalOperator,
723
+ currentFilterMainId,
724
+ currentFilterObject
725
+ });
726
+
727
+ if (logicalElementType === "logical") {
728
+
729
+ _izContext.logger.debug('------------------ logicalElementType: logical ----------------');
730
+
731
+ if (!logicalElement.hasOwnProperty('objType')) {
732
+ errorsObject[initialLogicalElementId] = `this logical structure is not set objType`;
733
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} is not set objType`);
734
+ };
735
+ if (hash(objType) !== hash(logicalElement.objType)) {
736
+ errorsObject[currentLogicalElementId] = `it' s not the same level objType to process this logicalElement`;
737
+ errorsFound.push(`it' s not the same level objType to process this logicalElement: ${currentLogicalElementId}`);
738
+ };
739
+ if (!logicalElement.hasOwnProperty('fieldName')) {
740
+ errorsObject[currentLogicalElementId] = `this logical structure is not set fieldName`;
741
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} is not set fieldName`);
742
+ };
743
+ if (!logicalElement.hasOwnProperty('comparison')) {
744
+ errorsObject[currentLogicalElementId] = `this logical structure is not set comparison`;
745
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} is not set comparison`);
746
+ };
747
+ if (!logicalElement.hasOwnProperty('value')) {
748
+ errorsObject[currentLogicalElementId] = `this logical structure is not set value`;
749
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} is not set value`);
750
+ }
751
+
752
+ let value = values[logicalElement.value];
753
+ _izContext.logger.debug('value: ', value);
754
+
755
+ if (isEmpty(value)) {
756
+ errorsObject[currentLogicalElementId] = `value is empty`;
757
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} value is empty`);
758
+ } else {
759
+ if (value.hasOwnProperty('valueSource')) {
760
+
761
+ if (value.valueSource !== 'perParentIdentifier') {
762
+ errorsObject[currentLogicalElementId] = `valueType is not set type: perParentIdentifier`;
763
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} has valueType is not set type: perParentIdentifier`);
764
+ };
765
+
766
+ //* create complexFilter normalize here
767
+ currentFilterObject = {
768
+ objType: logicalElement.objType,
769
+ filterElement: {
770
+ filterType: logicalElementType,
771
+ fieldName: logicalElement.fieldName,
772
+ comparison: logicalElement.comparison,
773
+ valueType: value.valueSource,
774
+ perParentIdentifierFieldname: value.perParentIdentifierFieldname
775
+ }
776
+ };
777
+
778
+ } else {
779
+
780
+ //* create complexFilter normalize here
781
+ currentFilterObject = {
782
+ objType: logicalElement.objType,
783
+ filterElement: {
784
+ filterType: logicalElement.logicalElementType,
785
+ fieldName: logicalElement.fieldName,
786
+ comparison: logicalElement.comparison,
787
+ value: values[logicalElement.value].value
788
+ }
789
+ };
790
+ };
791
+ };
792
+ _izContext.logger.debug('currentFilterObject: ', currentFilterObject);
793
+
794
+ currentFilterMainId = hash(currentFilterObject);
795
+ _izContext.logger.debug('currentFilterMainId: ', currentFilterMainId);
796
+
797
+ if (logicalElement.nextLogicalElementId !== null) {
798
+ lastNextLogicalElementId = logicalElement.nextLogicalElementId;
799
+ }
800
+
801
+ } else if (logicalElementType === "childComplexFilter") {
802
+ _izContext.logger.debug('------------------ logicalElementType: childComplexFilter ----------------');
803
+
804
+ if (!logicalElement.hasOwnProperty('objType')) {
805
+ errorsObject[currentLogicalElementId] = `this logical structure is not set objType`;
806
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} is not set objType`);
807
+ };
808
+ if (hash(objType) !== hash(logicalElement.objType)) {
809
+ errorsObject[currentLogicalElementId] = `it' s not the same level objType to process this logicalElement`
810
+ errorsFound.push(`it' s not the same level objType to process this logicalElement: ${currentLogicalElementId}`)
811
+ };
812
+
813
+ if (!logicalElement.hasOwnProperty('pathLinkType')) {
814
+ errorsObject[currentLogicalElementId] = `this logical structure is not set pathLinkType`;
815
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} is not set pathLinkType`);
816
+ } else {
817
+ if (!logicalElement.pathLinkType.hasOwnProperty('objType')
818
+ || !logicalElement.pathLinkType.hasOwnProperty('relType')
819
+ || !logicalElement.pathLinkType.hasOwnProperty('direction')
820
+ ) {
821
+ errorsObject[currentLogicalElementId] = `this logical structure is not set properties in pathLinkType`;
822
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} is not set {objType | relType | direction} in pathLinkType`);
823
+ };
824
+ };
825
+
826
+ let childLogicalElementId = logicalElement.childLogicalElementId;
827
+
828
+ if (childLogicalElementId === null) {
829
+ errorsObject[currentLogicalElementId] = `this logicalType: childComplexFilter is not set childLogicalElementId`;
830
+ errorsFound.push(`this logical structure logicalElementId: ${currentLogicalElementId} logicalType: childComplexFilter is not set childLogicalElementId`);
831
+ }
832
+
833
+ if (errorsFound.length > 0) {
834
+ return [null, null, null, errorsObject, errorsFound];
835
+ };
836
+
837
+ let [
838
+ childFilterMainId,
839
+ childFilterObject,
840
+ operation,
841
+ childErrorsObject,
842
+ childErrorsFound,
843
+ childNextLogicalElementId
844
+ ] = filterLogicalElements(
845
+ _izContext,
846
+ logicalElement.pathLinkType.objType,
847
+ initialLogicalElementId,
848
+ logicalElements,
849
+ values,
850
+ logicalElement.childLogicalElementId
851
+ );
852
+ _izContext.logger.debug('return child filterLogicalElement for child complexFilter: ', {
853
+ childFilterMainId,
854
+ childFilterObject,
855
+ operation,
856
+ childErrorsObject,
857
+ childErrorsFound,
858
+ childNextLogicalElementId
859
+ });
860
+ if (childErrorsFound.length > 0) {
861
+ Object.assign(errorsObject, childErrorsObject);
862
+ errorsFound = errorsFound.concat(childErrorsFound)
863
+ }
864
+
865
+ let filterElements = splitFilterElements(
866
+ _izContext,
867
+ childFilterMainId,
868
+ childFilterObject
869
+ );
870
+ _izContext.logger.debug('filterElements: ', filterElements);
871
+ let setRequestProperties = {};
872
+
873
+ if (!isEmpty(logicalElement.requestProperties)) {
874
+ _izContext.logger.debug('logicalElement: ', logicalElement);
875
+
876
+ for (const [tag, requestPropertyId] of Object.entries(logicalElement.requestProperties)) {
877
+ _izContext.logger.debug('set requestProperties: ', { tag, requestPropertyId });
878
+
879
+ let requestPropertyObject = values[requestPropertyId];
880
+
881
+ if (requestPropertyObject.hasOwnProperty('valueSource')) {
882
+ errorsObject[initialLogicalElementId] = `requestProperties in filter logicalStructure set valueSource`;
883
+ errorsFound.push(`this filter logical structure logicalElementId: ${initialLogicalElementId} set valueSource`);
884
+ continue;
885
+ };
886
+
887
+ setRequestProperties[tag] = requestPropertyObject.value;
888
+
889
+ };
890
+ };
891
+
892
+ currentFilterObject = {
893
+ objType: logicalElement.objType,
894
+ filterElement: {
895
+ filterType: logicalElementType,
896
+ filterElements: filterElements,
897
+ childFilterElementId: childFilterMainId,
898
+ pathLinkType: logicalElement.pathLinkType,
899
+ requestProperties: setRequestProperties ?? {}
900
+ }
901
+ };
902
+ _izContext.logger.debug('currentFilterObject: ', currentFilterObject);
903
+
904
+ currentFilterMainId = hash(currentFilterObject);
905
+ _izContext.logger.debug('currentFilterMainId: ', currentFilterMainId);
906
+
907
+ if (logicalElement.nextLogicalElementId !== null) {
908
+ lastNextLogicalElementId = logicalElement.nextLogicalElementId;
909
+ }
910
+
911
+ } else if (logicalElementType === "traversal") {
912
+
913
+ _izContext.logger.debug('logicalElementType: ', f);
914
+ } else if (logicalElementType === "translateIds") {
915
+
916
+ _izContext.logger.debug('logicalElementType: ', g);
917
+ } else if (logicalElementType === "identifiers") {
918
+
919
+ _izContext.logger.debug('logicalElementType: ', h);
920
+ }
921
+
922
+ if (errorsFound.length > 0) {
923
+ return [null, null, null, errorsObject, errorsFound, null];
924
+ }
925
+
926
+ if (lastNextLogicalElementId !== null) {
927
+
928
+ [runningFilterMainId, runningFilterObject, operation, operationErrorsFound] = complexFilterShared.validateOperation(
929
+ _izContext,
930
+ objType,
931
+ runningFilterMainId,
932
+ runningFilterObject,
933
+ lastLogicalOperator,
934
+ currentFilterMainId,
935
+ currentFilterObject
936
+ );
937
+ _izContext.logger.debug('return validateOperation for lastNextLogicalElementId == null: ', {
938
+ runningFilterMainId,
939
+ runningFilterObject,
940
+ operation,
941
+ operationErrorsFound
942
+ });
943
+
944
+ if (!isEmpty(operation)) {
945
+ lastLogicalOperator = null;
946
+ }
947
+
948
+ let [
949
+ nextFilterMainId,
950
+ nextFilterObject,
951
+ operations,
952
+ nextErrorObject,
953
+ nextErrorFound,
954
+ nextElementId
955
+ ] = filterLogicalElements(
956
+ _izContext,
957
+ objType,
958
+ initialLogicalElementId,
959
+ logicalElements,
960
+ values,
961
+ lastNextLogicalElementId,
962
+ runningFilterMainId,
963
+ runningFilterObject,
964
+ lastLogicalOperator,
965
+ );
966
+ _izContext.logger.debug('return validateOperation for lastNextLogicalElementId !== null: ', {
967
+ nextFilterMainId,
968
+ nextFilterObject,
969
+ operations,
970
+ nextErrorObject,
971
+ nextErrorFound,
972
+ nextElementId
973
+ });
974
+
975
+ if (nextErrorFound.length > 0) {
976
+ Object.assign(errorsObject, nextErrorObject);
977
+ errorsFound = errorsFound.concat(nextErrorFound);
978
+ }
979
+
980
+ if (nextElementId !== null) {
981
+ let filterErrorObject = {};
982
+ let filterErrorFound = [];
983
+
984
+ [
985
+ runningFilterMainId,
986
+ runningFilterObject,
987
+ operation,
988
+ filterErrorObject,
989
+ filterErrorFound,
990
+ nextElementId
991
+ ] = filterLogicalElements(
992
+ _izContext,
993
+ objType,
994
+ initialLogicalElementId,
995
+ logicalElements,
996
+ values,
997
+ nextElementId,
998
+ nextFilterMainId,
999
+ nextFilterObject,
1000
+ lastLogicalOperator
1001
+ );
1002
+ _izContext.logger.debug('return validateOperation for lastNextLogicalElementId !== null: ', {
1003
+ runningFilterMainId,
1004
+ runningFilterObject,
1005
+ operation,
1006
+ filterErrorObject,
1007
+ filterErrorFound,
1008
+ nextElementId
1009
+ });
1010
+ return [
1011
+ runningFilterMainId,
1012
+ runningFilterObject,
1013
+ operation,
1014
+ filterErrorObject,
1015
+ filterErrorFound,
1016
+ nextElementId
1017
+ ];
1018
+ } else {
1019
+ return [
1020
+ nextFilterMainId,
1021
+ nextFilterObject,
1022
+ operation,
1023
+ errorsObject,
1024
+ errorsFound,
1025
+ nextElementId
1026
+ ];
1027
+ }
1028
+
1029
+ } else {
1030
+
1031
+ [runningFilterMainId, runningFilterObject, operation, operationErrorsFound] = complexFilterShared.validateOperation(
1032
+ _izContext,
1033
+ objType,
1034
+ runningFilterMainId,
1035
+ runningFilterObject,
1036
+ lastLogicalOperator,
1037
+ currentFilterMainId,
1038
+ currentFilterObject
1039
+ );
1040
+ _izContext.logger.debug('return validateOperation for lastNextLogicalElementId == null: ', {
1041
+ runningFilterMainId,
1042
+ runningFilterObject,
1043
+ operation,
1044
+ operationErrorsFound
1045
+ });
1046
+
1047
+ if (operationErrorsFound.length > 0) {
1048
+ errorsFound = errorsFound.concat(operationErrorsFound)
1049
+ }
1050
+
1051
+ return [runningFilterMainId, runningFilterObject, operation, errorsObject, errorsFound, null];
1052
+
1053
+ }
1054
+
1055
+ };
1056
+
1057
+
1058
+
1059
+ function combineLogicalStructure(
1060
+ _izContext,
1061
+ objType,
1062
+ initialLogicalElementId,
1063
+ logicalElements,
1064
+ ) {
1065
+ _izContext.logger.debug('combineLogicalStructure: ', {
1066
+ objType,
1067
+ initialLogicalElementId,
1068
+ logicalElements,
1069
+ });
1070
+
1071
+ let returnInitialElementId = null;
1072
+ let initialLogicalElementObject = logicalElements[initialLogicalElementId]
1073
+ if (initialLogicalElementObject.logicalElementType !== 'logicalStructure') {
1074
+ returnInitialElementId = initialLogicalElementId;
1075
+ } else {
1076
+ returnInitialElementId = logicalElements[initialLogicalElementId].initialLogicalElementId;
1077
+ };
1078
+ _izContext.logger.debug('returnInitialElementId', returnInitialElementId);
1079
+
1080
+ let previousLogicalElementId = null;
1081
+ let previousLogicalElementObject = null;
1082
+
1083
+ let operationLogicalElementId = null;
1084
+ let operationLogicalElementObject = null;
1085
+
1086
+ let nextLogicalElementId = null;
1087
+ let nextLogicalElementObject = null;
1088
+
1089
+ let returnLogicalElements = {};
1090
+
1091
+ let errorsObject = {};
1092
+ let errorsFound = [];
1093
+
1094
+ for (const [logicalElementId, logicalElementObject] of Object.entries(logicalElements)) {
1095
+ _izContext.logger.debug('extract logicalELements: ', { initialLogicalElementId, logicalElementId, logicalElementObject });
1096
+
1097
+ if (initialLogicalElementId !== logicalElementId) {
1098
+ continue;
1099
+ };
1100
+
1101
+ let logicalElementType = logicalElementObject.logicalElementType;
1102
+ _izContext.logger.debug('logicalElementType: ', logicalElementType);
1103
+
1104
+ if (logicalElementType === 'openBracket') {
1105
+ _izContext.logger.debug('----------type: openBracket ----------');
1106
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
1107
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
1108
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
1109
+ };
1110
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
1111
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
1112
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
1113
+ };
1114
+
1115
+ nextLogicalElementId = logicalElementId;
1116
+ nextLogicalElementObject = { [nextLogicalElementId]: logicalElementObject };
1117
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
1118
+
1119
+ } else if (logicalElementType === 'logicalStructure') {
1120
+ _izContext.logger.debug('----------type: logicalStructure ----------');
1121
+
1122
+ if (!logicalElementObject.hasOwnProperty('objType')) {
1123
+ errorsObject[logicalElementId] = `this logical structure is not set objType`;
1124
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set objType`);
1125
+ };
1126
+ if (!logicalElementObject.hasOwnProperty('initialLogicalElementId')) {
1127
+ errorsObject[logicalElementId] = `this logical structure is not set initialLogicalElementId`;
1128
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set initialLogicalElementId`);
1129
+ };
1130
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
1131
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
1132
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
1133
+ };
1134
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
1135
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
1136
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
1137
+ };
1138
+ if (!logicalElementObject.hasOwnProperty('logicalElements')) {
1139
+ errorsObject[logicalElementId] = `this logical structure is not set logicalElements`;
1140
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set logicalElements`);
1141
+ };
1142
+
1143
+ nextLogicalElementId = logicalElementObject.initialLogicalElementId;
1144
+ nextLogicalElementObject = logicalElementObject.logicalElements;
1145
+
1146
+ if (logicalElementObject.nextLogicalElementId !== null) {
1147
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
1148
+ };
1149
+
1150
+ } else if (logicalElementType === 'operation') {
1151
+ _izContext.logger.debug('----------type: operation ----------');
1152
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
1153
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
1154
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
1155
+ };
1156
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
1157
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
1158
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
1159
+ };
1160
+ if (!logicalElementObject.hasOwnProperty('operation')) {
1161
+ errorsObject[logicalElementId] = `this logical structure is not set operation`;
1162
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set operation`);
1163
+ };
1164
+
1165
+ operationLogicalElementId = logicalElementId;
1166
+ operationLogicalElementObject = logicalElementObject;
1167
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
1168
+ continue;
1169
+
1170
+ } else if (logicalElementType === 'closeBracket') {
1171
+ _izContext.logger.debug('----------type: closeBracket ----------');
1172
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
1173
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
1174
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
1175
+ };
1176
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
1177
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
1178
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
1179
+ };
1180
+
1181
+ nextLogicalElementId = logicalElementId;
1182
+ nextLogicalElementObject = { [nextLogicalElementId]: logicalElementObject };
1183
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
1184
+
1185
+ } else {
1186
+ //* error
1187
+ errorsObject[logicalElementId] = `has error on logicalElementType: ${logicalElementType}`;
1188
+ errorsFound.push(`has error on logicalElementType in logicalElementId: ${logicalElementId}`);
1189
+ };
1190
+
1191
+ if (errorsFound.length > 0) {
1192
+ return [null, errorsObject, errorsFound];
1193
+ };
1194
+
1195
+ let operations = null;
1196
+ let operationErrorObjects = {};
1197
+ let operationErrors = [];
1198
+
1199
+ [
1200
+ previousLogicalElementId,
1201
+ previousLogicalElementObject,
1202
+ operations,
1203
+ operationErrorObjects,
1204
+ operationErrors
1205
+ ] = combineOperation(
1206
+ _izContext,
1207
+ previousLogicalElementId,
1208
+ previousLogicalElementObject,
1209
+ operationLogicalElementId,
1210
+ operationLogicalElementObject,
1211
+ nextLogicalElementId,
1212
+ nextLogicalElementObject
1213
+ );
1214
+ _izContext.logger.debug('return combineOperation: ', {
1215
+ previousLogicalElementId,
1216
+ previousLogicalElementObject,
1217
+ operations,
1218
+ operationErrors
1219
+ });
1220
+
1221
+ if (operationErrors.length > 0) {
1222
+ Object.assign(errorsObject, operationErrorObjects);
1223
+ };
1224
+
1225
+ if (operations !== null) {
1226
+ Object.assign(returnLogicalElements, operations);
1227
+ operationLogicalElementId = null;
1228
+ operationLogicalElementObject = null;
1229
+ } else {
1230
+ Object.assign(returnLogicalElements, previousLogicalElementObject);
1231
+ };
1232
+
1233
+ } //end iterate logicalElements
1234
+
1235
+ _izContext.logger.debug('returnLogicalElements: ', { returnLogicalElements });
1236
+
1237
+ let returnComplexLogicalStructure = {
1238
+ objType: objType,
1239
+ initialLogicalElementId: returnInitialElementId,
1240
+ logicalElements: returnLogicalElements
1241
+ };
1242
+
1243
+ return [returnComplexLogicalStructure, errorsObject, errorsFound];
1244
+
1245
+ };
1246
+
1247
+ function combineOperation(
1248
+ _izContext,
1249
+ previousLogicalElementId,
1250
+ previousLogicalElementObject,
1251
+ operationLogicalElementId,
1252
+ operationLogicalElementObject,
1253
+ nextLogicalElementId,
1254
+ nextLogicalElementObject
1255
+ ) {
1256
+ _izContext.logger.debug('combineOperation: ', {
1257
+ previousLogicalElementId,
1258
+ previousLogicalElementObject,
1259
+ operationLogicalElementId,
1260
+ operationLogicalElementObject,
1261
+ nextLogicalElementId,
1262
+ nextLogicalElementObject
1263
+ });
1264
+
1265
+ let errorsObject = {};
1266
+ let errorsFound = []
1267
+
1268
+ if (!nextLogicalElementObject) {
1269
+ errorsFound.push('cannot found current filterElement object')
1270
+ return [null, null, null, errorsObject, errorsFound];
1271
+ };
1272
+
1273
+ if (!previousLogicalElementObject) {
1274
+ return [nextLogicalElementId, nextLogicalElementObject, null, errorsObject, errorsFound];
1275
+ };
1276
+
1277
+ if (!operationLogicalElementObject) {
1278
+ _izContext.logger.debug('------------ previous & current ------------', {
1279
+ previousLogicalElementId,
1280
+ previousLogicalElementObject,
1281
+ operationLogicalElementId,
1282
+ operationLogicalElementObject,
1283
+ nextLogicalElementId,
1284
+ nextLogicalElementObject
1285
+ });
1286
+
1287
+ let operations = {};
1288
+ let returnnextElementId = null;
1289
+ let returnNextElementObject = null;
1290
+
1291
+ if (previousLogicalElementObject[previousLogicalElementId].logicalElementType === 'openBracket') {
1292
+ if (nextLogicalElementObject[nextLogicalElementId].logicalElementType === 'closeBracket') {
1293
+
1294
+ if (Object.entries(previousLogicalElementObject).length <= 2) {
1295
+
1296
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
1297
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
1298
+
1299
+ } else {
1300
+
1301
+ if (previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId === null) {
1302
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
1303
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
1304
+
1305
+ } else {
1306
+
1307
+ let checkNextLogicalElementId = previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId;
1308
+ _izContext.logger.debug('checkNextLogicalElementId:', checkNextLogicalElementId);
1309
+
1310
+ for (let i = 0; i < Object.entries(previousLogicalElementObject).length; i++) {
1311
+ let [logicalElementId, logicalObject] = Object.entries(previousLogicalElementObject)[i];
1312
+ _izContext.logger.debug('extract logical element: ', { logicalElementId, logicalObject });
1313
+
1314
+ if (logicalElementId !== checkNextLogicalElementId) {
1315
+ continue;
1316
+ };
1317
+
1318
+ _izContext.logger.debug('------------- has next logicalElemet to continue -------------');
1319
+
1320
+ if (logicalObject.nextLogicalElementId !== null) {
1321
+ checkNextLogicalElementId = logicalObject.nextLogicalElementId;
1322
+ continue;
1323
+ } else {
1324
+ previousLogicalElementObject[logicalElementId].nextLogicalElementId = nextLogicalElementId;
1325
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = logicalElementId;
1326
+ };
1327
+ };
1328
+ };
1329
+ };
1330
+
1331
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
1332
+ _izContext.logger.debug('operations: ', operations);
1333
+
1334
+ returnnextElementId = nextLogicalElementId;
1335
+ returnNextElementObject = nextLogicalElementObject;
1336
+
1337
+ } else {
1338
+
1339
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
1340
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
1341
+
1342
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
1343
+
1344
+ _izContext.logger.debug('operations: ', operations);
1345
+ returnnextElementId = nextLogicalElementId;
1346
+ returnNextElementObject = nextLogicalElementObject;
1347
+
1348
+ };
1349
+
1350
+ } else if (previousLogicalElementObject[previousLogicalElementId].logicalElementType === 'closeBracket') {
1351
+
1352
+ if (nextLogicalElementObject[nextLogicalElementId].logicalElementType === 'closeBracket') {
1353
+
1354
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
1355
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
1356
+
1357
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
1358
+ _izContext.logger.debug('operations: ', operations);
1359
+
1360
+ returnnextElementId = nextLogicalElementId;
1361
+ returnNextElementObject = nextLogicalElementObject;
1362
+
1363
+ } else {
1364
+ errorsObject[nextLogicalElementId] = 'this path has logical structure error';
1365
+ errorsFound.push(`${nextLogicalElementId}: this path has logical structure error`);
1366
+ };
1367
+
1368
+ } else {
1369
+
1370
+ if (nextLogicalElementObject[nextLogicalElementId].logicalElementType === 'closeBracket') {
1371
+
1372
+ _izContext.logger.debug('previousLogicalElementObject: ', previousLogicalElementObject);
1373
+
1374
+ if (Object.entries(previousLogicalElementObject).length <= 2) {
1375
+
1376
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
1377
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
1378
+
1379
+ } else {
1380
+
1381
+ if (previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId === null) {
1382
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
1383
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
1384
+
1385
+ } else {
1386
+
1387
+ let checkNextLogicalElementId = previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId;
1388
+ _izContext.logger.debug('checkNextLogicalElementId:', checkNextLogicalElementId);
1389
+
1390
+ for (let i = 0; i < Object.entries(previousLogicalElementObject).length; i++) {
1391
+ let [logicalElementId, logicalObject] = Object.entries(previousLogicalElementObject)[i];
1392
+ _izContext.logger.debug('extract logical element: ', { logicalElementId, logicalObject });
1393
+
1394
+ if (logicalElementId !== checkNextLogicalElementId) {
1395
+ continue;
1396
+ };
1397
+
1398
+ _izContext.logger.debug('------------- has next logicalElemet to continue -------------');
1399
+
1400
+ if (logicalObject.nextLogicalElementId !== null) {
1401
+ checkNextLogicalElementId = logicalObject.nextLogicalElementId;
1402
+ continue;
1403
+ } else {
1404
+ previousLogicalElementObject[logicalElementId].nextLogicalElementId = nextLogicalElementId;
1405
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = logicalElementId;
1406
+ };
1407
+ };
1408
+ };
1409
+ };
1410
+
1411
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
1412
+ _izContext.logger.debug("operations: ", operations);
1413
+
1414
+ returnnextElementId = nextLogicalElementId;
1415
+ returnNextElementObject = nextLogicalElementObject;
1416
+
1417
+ } else {
1418
+ errorsObject[nextLogicalElementId] = 'this path has logical structure error';
1419
+ errorsFound.push(`${nextLogicalElementId}: this path has logical structure error`);
1420
+ };
1421
+ };
1422
+ return [returnnextElementId, returnNextElementObject, operations, errorsObject, errorsFound];
1423
+ };
1424
+
1425
+ if (previousLogicalElementObject && operationLogicalElementObject && nextLogicalElementObject) {
1426
+ _izContext.logger.debug('------------ previous & operation & current ------------', {
1427
+ previousLogicalElementId,
1428
+ previousLogicalElementObject,
1429
+ operationLogicalElementId,
1430
+ operationLogicalElementObject,
1431
+ nextLogicalElementId,
1432
+ nextLogicalElementObject
1433
+ });
1434
+
1435
+ let operations = {};
1436
+
1437
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = operationLogicalElementId;
1438
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = operationLogicalElementId;
1439
+
1440
+ operationLogicalElementObject.previousLogicalElementId = previousLogicalElementId;
1441
+ operationLogicalElementObject.nextLogicalElementId = nextLogicalElementId;
1442
+
1443
+ _izContext.logger.debug('after set previous and next: ', {
1444
+ previousLogicalElementObject,
1445
+ operationLogicalElementObject,
1446
+ nextLogicalElementObject
1447
+ });
1448
+
1449
+ operations = Object.assign(previousLogicalElementObject, { [operationLogicalElementId]: operationLogicalElementObject }, nextLogicalElementObject);
1450
+
1451
+ return [nextLogicalElementId, nextLogicalElementObject, operations, errorsObject, errorsFound];
1452
+
1453
+ } else {
1454
+ errorsObject[previousLogicalElementId] = 'found error that incorrect of logical structure';
1455
+ errorsObject[currentLogicalElementId] = 'found error that incorrect of logical structure';
1456
+ errorsObject[operationLogicalElementId] = 'found error that incorrect of logical structure';
1457
+ errorsFound.push('found error that incorrect of logical structure');
1458
+
1459
+ return [null, null, null, errorsObject, errorsFound];
1460
+ };
1461
+
1462
+ };
1463
+
1464
+ function splitFilterElements(
1465
+ _izContext,
1466
+ runningFilterMainId,
1467
+ runningFilterObject,
1468
+ currentFilterMainId = null,
1469
+ currentFilterObject = null
1470
+ ) {
1471
+ _izContext.logger.debug('split FilterElements: ', {
1472
+ runningFilterMainId,
1473
+ runningFilterObject,
1474
+ currentFilterMainId,
1475
+ currentFilterObject
1476
+ });
1477
+
1478
+ let filterElements = {};
1479
+
1480
+ // if (runningFilterObject.filterElement.filterType === 'logical'
1481
+ // || runningFilterObject.filterElement.filterType === 'identifiers'
1482
+ // || runningFilterObject.filterElement.filterType === 'translateIds'
1483
+ // || runningFilterObject.filterElement.filterType === 'traversal'
1484
+ // ) {}
1485
+
1486
+ if (runningFilterObject.filterElement.filterType === 'operation') {
1487
+ _izContext.logger.debug('-------- operation ----------');
1488
+
1489
+ Object.assign(filterElements, runningFilterObject.filterElement.filterElements);
1490
+ delete runningFilterObject.filterElement.filterElements;
1491
+ filterElements[runningFilterMainId] = runningFilterObject;
1492
+
1493
+ } else if (runningFilterObject.filterElement.filterType === 'childComplexFilter') {
1494
+ _izContext.logger.debug('-------- childComplexFilter ----------');
1495
+
1496
+ Object.assign(filterElements, runningFilterObject.filterElement.filterElements);
1497
+ delete runningFilterObject.filterElement.filterElements;
1498
+ filterElements[runningFilterMainId] = runningFilterObject;
1499
+
1500
+ } else {
1501
+ _izContext.logger.debug(` -------- ${runningFilterObject.filterElement.filterType} ----------`);
1502
+ //'logical'| 'identifiers'|'translateIds'|'traversal'
1503
+ filterElements[runningFilterMainId] = runningFilterObject;
1504
+ };
1505
+
1506
+ if (currentFilterObject !== null) {
1507
+ if (currentFilterObject.filterElement.filterType === 'operation') {
1508
+ _izContext.logger.debug('-------- operation ----------');
1509
+
1510
+ Object.assign(filterElements, currentFilterObject.filterElement.filterElements);
1511
+ delete currentFilterObject.filterElement.filterElements;
1512
+ filterElements[currentFilterMainId] = currentFilterObject;
1513
+
1514
+ } else if (currentFilterObject.filterElement.filterType === 'childComplexFilter') {
1515
+ _izContext.logger.debug('-------- childComplexFilter ----------');
1516
+
1517
+ Object.assign(filterElements, currentFilterObject.filterElement.filterElements);
1518
+ delete currentFilterObject.filterElement.filterElements;
1519
+ filterElements[currentFilterMainId] = currentFilterObject;
1520
+
1521
+ } else {
1522
+ _izContext.logger.debug(` -------- ${currentFilterObject.filterElement.filterType} ----------`);
1523
+ //'logical'| 'identifiers'|'translateIds'|'traversal'
1524
+ filterElements[currentFilterMainId] = currentFilterObject;
1525
+ };
1526
+ };
1527
+
1528
+ return filterElements;
1529
+
1530
+ };
1531
+
1532
+
1533
+ export default {
1534
+ //* create filter
1535
+ createFiltersRequest,
1536
+ validateCombinationStructure,
1537
+ // validateOperation,
1538
+ // filterLogicalElements,
1539
+ // splitFilterElements,
1540
+
1541
+ combineLogicalStructure,
1542
+ // combineOperation,
1543
+
1544
+ }