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