@izara_project/izara-shared-search-and-sort 1.0.4 → 1.0.6

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,1152 @@
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
+
21
+ import { objectHash as hash } from '@izara_project/izara-shared-core'; //const hash = require('@izara_project/izara-shared-core').objectHash;
22
+ import lodash from 'lodash';
23
+ const { isEmpty } = lodash;
24
+ import { v4 as uuidV4 } from 'uuid'; //const { v4: uuidv4 } = require('uuid');
25
+ import { getObjectSchema } from '@izara_project/izara-core-library-service-schemas';
26
+ const MAX_ITER = 4;
27
+
28
+
29
+ function combineLogicalStructure(
30
+ objType,
31
+ initialLogicalElementId,
32
+ logicalElements,
33
+ ) {
34
+ console.log('combineLogicalStructure: ', {
35
+ objType,
36
+ initialLogicalElementId,
37
+ logicalElements,
38
+ });
39
+
40
+ let returnInitialElementId = null;
41
+ let initialLogicalElementObject = logicalElements[initialLogicalElementId]
42
+ if (initialLogicalElementObject.logicalElementType !== 'logicalStructure') {
43
+ returnInitialElementId = initialLogicalElementId;
44
+ } else {
45
+ returnInitialElementId = logicalElements[initialLogicalElementId].initialLogicalElementId;
46
+ };
47
+ console.log('returnInitialElementId', returnInitialElementId);
48
+
49
+ let previousLogicalElementId = null;
50
+ let previousLogicalElementObject = null;
51
+
52
+ let operationLogicalElementId = null;
53
+ let operationLogicalElementObject = null;
54
+
55
+ let nextLogicalElementId = null;
56
+ let nextLogicalElementObject = null;
57
+
58
+ let returnLogicalElements = {};
59
+
60
+ let errorsObject = {};
61
+ let errorsFound = [];
62
+
63
+ for (const [logicalElementId, logicalElementObject] of Object.entries(logicalElements)) {
64
+ console.log('extract logicalELements: ', { initialLogicalElementId, logicalElementId, logicalElementObject });
65
+
66
+ if (initialLogicalElementId !== logicalElementId) {
67
+ continue;
68
+ };
69
+
70
+ let logicalElementType = logicalElementObject.logicalElementType;
71
+ console.log('logicalElementType: ', logicalElementType);
72
+
73
+ if (logicalElementType === 'openBracket') {
74
+ console.log('----------type: openBracket ----------');
75
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
76
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
77
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
78
+ };
79
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
80
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
81
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
82
+ };
83
+
84
+ nextLogicalElementId = logicalElementId;
85
+ nextLogicalElementObject = { [nextLogicalElementId]: logicalElementObject };
86
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
87
+
88
+ } else if (logicalElementType === 'logicalStructure') {
89
+ console.log('----------type: logicalStructure ----------');
90
+
91
+ if (!logicalElementObject.hasOwnProperty('objType')) {
92
+ errorsObject[logicalElementId] = `this logical structure is not set objType`;
93
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set objType`);
94
+ };
95
+ if (!logicalElementObject.hasOwnProperty('initialLogicalElementId')) {
96
+ errorsObject[logicalElementId] = `this logical structure is not set initialLogicalElementId`;
97
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set initialLogicalElementId`);
98
+ };
99
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
100
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
101
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
102
+ };
103
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
104
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
105
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
106
+ };
107
+ if (!logicalElementObject.hasOwnProperty('logicalElements')) {
108
+ errorsObject[logicalElementId] = `this logical structure is not set logicalElements`;
109
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set logicalElements`);
110
+ };
111
+
112
+ nextLogicalElementId = logicalElementObject.initialLogicalElementId;
113
+ nextLogicalElementObject = logicalElementObject.logicalElements;
114
+
115
+ if (logicalElementObject.nextLogicalElementId !== null) {
116
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
117
+ };
118
+
119
+ } else if (logicalElementType === 'operation') {
120
+ console.log('----------type: operation ----------');
121
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
122
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
123
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
124
+ };
125
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
126
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
127
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
128
+ };
129
+ if (!logicalElementObject.hasOwnProperty('operation')) {
130
+ errorsObject[logicalElementId] = `this logical structure is not set operation`;
131
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set operation`);
132
+ };
133
+
134
+ operationLogicalElementId = logicalElementId;
135
+ operationLogicalElementObject = logicalElementObject;
136
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
137
+ continue;
138
+
139
+ } else if (logicalElementType === 'closeBracket') {
140
+ console.log('----------type: closeBracket ----------');
141
+ if (!logicalElementObject.hasOwnProperty('previousLogicalElementId')) {
142
+ errorsObject[logicalElementId] = `this logical structure is not set previousLogicalElementId`;
143
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set previousLogicalElementId`);
144
+ };
145
+ if (!logicalElementObject.hasOwnProperty('nextLogicalElementId')) {
146
+ errorsObject[logicalElementId] = `this logical structure is not set nextLogicalElementId`;
147
+ errorsFound.push(`this logical structure logicalElementId: ${logicalElementId} is not set nextLogicalElementId`);
148
+ };
149
+
150
+ nextLogicalElementId = logicalElementId;
151
+ nextLogicalElementObject = { [nextLogicalElementId]: logicalElementObject };
152
+ initialLogicalElementId = logicalElementObject.nextLogicalElementId;
153
+
154
+ } else {
155
+ //* error
156
+ errorsObject[logicalElementId] = `has error on logicalElementType: ${logicalElementType}`;
157
+ errorsFound.push(`has error on logicalElementType in logicalElementId: ${logicalElementId}`);
158
+ };
159
+
160
+ if (errorsFound.length > 0) {
161
+ return [null, errorsObject, errorsFound];
162
+ };
163
+
164
+ let operations = null;
165
+ let operationErrorObjects = {};
166
+ let operationErrors = [];
167
+
168
+ [
169
+ previousLogicalElementId,
170
+ previousLogicalElementObject,
171
+ operations,
172
+ operationErrorObjects,
173
+ operationErrors
174
+ ] = combineOperation(
175
+ previousLogicalElementId,
176
+ previousLogicalElementObject,
177
+ operationLogicalElementId,
178
+ operationLogicalElementObject,
179
+ nextLogicalElementId,
180
+ nextLogicalElementObject
181
+ );
182
+ console.log('return combineOperation: ', {
183
+ previousLogicalElementId,
184
+ previousLogicalElementObject,
185
+ operations,
186
+ operationErrors
187
+ });
188
+
189
+ if (operationErrors.length > 0) {
190
+ Object.assign(errorsObject, operationErrorObjects);
191
+ };
192
+
193
+ if (operations !== null) {
194
+ Object.assign(returnLogicalElements, operations);
195
+ operationLogicalElementId = null;
196
+ operationLogicalElementObject = null;
197
+ } else {
198
+ Object.assign(returnLogicalElements, previousLogicalElementObject);
199
+ };
200
+
201
+ } //end iterate logicalElements
202
+
203
+ console.log('returnLogicalElements: ', { returnLogicalElements });
204
+
205
+ let returnComplexLogicalStructure = {
206
+ objType: objType,
207
+ initialLogicalElementId: returnInitialElementId,
208
+ logicalElements: returnLogicalElements
209
+ };
210
+
211
+ return [returnComplexLogicalStructure, errorsObject, errorsFound];
212
+
213
+ };
214
+
215
+ function combineOperation(
216
+ previousLogicalElementId,
217
+ previousLogicalElementObject,
218
+ operationLogicalElementId,
219
+ operationLogicalElementObject,
220
+ nextLogicalElementId,
221
+ nextLogicalElementObject
222
+ ) {
223
+ console.log('combineOperation: ', {
224
+ previousLogicalElementId,
225
+ previousLogicalElementObject,
226
+ operationLogicalElementId,
227
+ operationLogicalElementObject,
228
+ nextLogicalElementId,
229
+ nextLogicalElementObject
230
+ });
231
+
232
+ let errorsObject = {};
233
+ let errorsFound = []
234
+
235
+ if (!nextLogicalElementObject) {
236
+ errorsFound.push('cannot found current filterElement object')
237
+ return [null, null, null, errorsObject, errorsFound];
238
+ };
239
+
240
+ if (!previousLogicalElementObject) {
241
+ return [nextLogicalElementId, nextLogicalElementObject, null, errorsObject, errorsFound];
242
+ };
243
+
244
+ if (!operationLogicalElementObject) {
245
+ console.log('------------ previous & current ------------', {
246
+ previousLogicalElementId,
247
+ previousLogicalElementObject,
248
+ operationLogicalElementId,
249
+ operationLogicalElementObject,
250
+ nextLogicalElementId,
251
+ nextLogicalElementObject
252
+ });
253
+
254
+ let operations = {};
255
+ let returnnextElementId = null;
256
+ let returnNextElementObject = null;
257
+
258
+ if (previousLogicalElementObject[previousLogicalElementId].logicalElementType === 'openBracket') {
259
+ if (nextLogicalElementObject[nextLogicalElementId].logicalElementType === 'closeBracket') {
260
+
261
+ if (Object.entries(previousLogicalElementObject).length <= 2) {
262
+
263
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
264
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
265
+
266
+ } else {
267
+
268
+ if (previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId === null) {
269
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
270
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
271
+
272
+ } else {
273
+
274
+ let checkNextLogicalElementId = previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId;
275
+ console.log('checkNextLogicalElementId:', checkNextLogicalElementId);
276
+
277
+ for (let i = 0; i < Object.entries(previousLogicalElementObject).length; i++) {
278
+ let [logicalElementId, logicalObject] = Object.entries(previousLogicalElementObject)[i];
279
+ console.log('extract logical element: ', { logicalElementId, logicalObject });
280
+
281
+ if (logicalElementId !== checkNextLogicalElementId) {
282
+ continue;
283
+ };
284
+
285
+ console.log('------------- has next logicalElemet to continue -------------');
286
+
287
+ if (logicalObject.nextLogicalElementId !== null) {
288
+ checkNextLogicalElementId = logicalObject.nextLogicalElementId;
289
+ continue;
290
+ } else {
291
+ previousLogicalElementObject[logicalElementId].nextLogicalElementId = nextLogicalElementId;
292
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = logicalElementId;
293
+ };
294
+ };
295
+ };
296
+ };
297
+
298
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
299
+ console.log('operations: ', operations);
300
+
301
+ returnnextElementId = nextLogicalElementId;
302
+ returnNextElementObject = nextLogicalElementObject;
303
+
304
+ } else {
305
+
306
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
307
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
308
+
309
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
310
+
311
+ console.log('operations: ', operations);
312
+ returnnextElementId = nextLogicalElementId;
313
+ returnNextElementObject = nextLogicalElementObject;
314
+
315
+ };
316
+
317
+ } else if (previousLogicalElementObject[previousLogicalElementId].logicalElementType === 'closeBracket') {
318
+
319
+ if (nextLogicalElementObject[nextLogicalElementId].logicalElementType === 'closeBracket') {
320
+
321
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
322
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
323
+
324
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
325
+ console.log('operations: ', operations);
326
+
327
+ returnnextElementId = nextLogicalElementId;
328
+ returnNextElementObject = nextLogicalElementObject;
329
+
330
+ } else {
331
+ errorsObject[nextLogicalElementId] = 'this path has logical structure error';
332
+ errorsFound.push(`${nextLogicalElementId}: this path has logical structure error`);
333
+ };
334
+
335
+ } else {
336
+
337
+ if (nextLogicalElementObject[nextLogicalElementId].logicalElementType === 'closeBracket') {
338
+
339
+ console.log('previousLogicalElementObject: ', previousLogicalElementObject);
340
+
341
+ if (Object.entries(previousLogicalElementObject).length <= 2) {
342
+
343
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
344
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
345
+
346
+ } else {
347
+
348
+ if (previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId === null) {
349
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = nextLogicalElementId;
350
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = previousLogicalElementId;
351
+
352
+ } else {
353
+
354
+ let checkNextLogicalElementId = previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId;
355
+ console.log('checkNextLogicalElementId:', checkNextLogicalElementId);
356
+
357
+ for (let i = 0; i < Object.entries(previousLogicalElementObject).length; i++) {
358
+ let [logicalElementId, logicalObject] = Object.entries(previousLogicalElementObject)[i];
359
+ console.log('extract logical element: ', { logicalElementId, logicalObject });
360
+
361
+ if (logicalElementId !== checkNextLogicalElementId) {
362
+ continue;
363
+ };
364
+
365
+ console.log('------------- has next logicalElemet to continue -------------');
366
+
367
+ if (logicalObject.nextLogicalElementId !== null) {
368
+ checkNextLogicalElementId = logicalObject.nextLogicalElementId;
369
+ continue;
370
+ } else {
371
+ previousLogicalElementObject[logicalElementId].nextLogicalElementId = nextLogicalElementId;
372
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = logicalElementId;
373
+ };
374
+ };
375
+ };
376
+ };
377
+
378
+ operations = Object.assign(previousLogicalElementObject, nextLogicalElementObject);
379
+ console.log("operations: ", operations);
380
+
381
+ returnnextElementId = nextLogicalElementId;
382
+ returnNextElementObject = nextLogicalElementObject;
383
+
384
+ } else {
385
+ errorsObject[nextLogicalElementId] = 'this path has logical structure error';
386
+ errorsFound.push(`${nextLogicalElementId}: this path has logical structure error`);
387
+ };
388
+ };
389
+ return [returnnextElementId, returnNextElementObject, operations, errorsObject, errorsFound];
390
+ };
391
+
392
+ if (previousLogicalElementObject && operationLogicalElementObject && nextLogicalElementObject) {
393
+ console.log('------------ previous & operation & current ------------', {
394
+ previousLogicalElementId,
395
+ previousLogicalElementObject,
396
+ operationLogicalElementId,
397
+ operationLogicalElementObject,
398
+ nextLogicalElementId,
399
+ nextLogicalElementObject
400
+ });
401
+
402
+ let operations = {};
403
+
404
+ previousLogicalElementObject[previousLogicalElementId].nextLogicalElementId = operationLogicalElementId;
405
+ nextLogicalElementObject[nextLogicalElementId].previousLogicalElementId = operationLogicalElementId;
406
+
407
+ operationLogicalElementObject.previousLogicalElementId = previousLogicalElementId;
408
+ operationLogicalElementObject.nextLogicalElementId = nextLogicalElementId;
409
+
410
+ console.log('after set previous and next: ', {
411
+ previousLogicalElementObject,
412
+ operationLogicalElementObject,
413
+ nextLogicalElementObject
414
+ });
415
+
416
+ operations = Object.assign(previousLogicalElementObject, { [operationLogicalElementId]: operationLogicalElementObject }, nextLogicalElementObject);
417
+
418
+ return [nextLogicalElementId, nextLogicalElementObject, operations, errorsObject, errorsFound];
419
+
420
+ } else {
421
+ errorsObject[previousLogicalElementId] = 'found error that incorrect of logical structure';
422
+ errorsObject[currentLogicalElementId] = 'found error that incorrect of logical structure';
423
+ errorsObject[operationLogicalElementId] = 'found error that incorrect of logical structure';
424
+ errorsFound.push('found error that incorrect of logical structure');
425
+
426
+ return [null, null, null, errorsObject, errorsFound];
427
+ };
428
+
429
+ };
430
+
431
+ // function combineLogicalElements(
432
+ // firstFilterStructure,
433
+ // secondFilterStructure,
434
+ // operator = "AND"
435
+ // ) {
436
+ // console.log('combineLogicalElements: ', {
437
+ // firstFilterStructure,
438
+ // secondFilterStructure,
439
+ // operator
440
+ // });
441
+
442
+ // let objType = firstFilterStructure.objType;
443
+
444
+ // let [firstInitialLogicalElementId, firstLastLogicalElementId, firstFiltersStructureBrcket] = addBrackets(
445
+ // firstFilterStructure
446
+ // );
447
+ // console.log("first filterStructure addBrackets", {
448
+ // firstInitialLogicalElementId,
449
+ // firstLastLogicalElementId,
450
+ // firstFiltersStructureBrcket
451
+ // })
452
+
453
+ // let [secondInitialLogicalElementId, secondLastLogicalElementId, secondFiltersStructureBrcket] = addBrackets(
454
+ // secondFilterStructure
455
+ // );
456
+ // console.log("second filterStructure addBrackets", {
457
+ // secondInitialLogicalElementId,
458
+ // secondLastLogicalElementId,
459
+ // secondFiltersStructureBrcket
460
+ // })
461
+
462
+ // let uuid_operation = uuidV4();
463
+ // console.log('uuid_operation: ', uuid_operation);
464
+
465
+ // let operationObject = {
466
+ // logicalElementType: 'operation',
467
+ // previousLogicalElementId: firstLastLogicalElementId,
468
+ // nextLogicalElementId: secondInitialLogicalElementId,
469
+ // operation: operator
470
+ // };
471
+ // console.log('operationObject: ', operationObject);
472
+
473
+ // firstFiltersStructureBrcket.logicalElements[firstLastLogicalElementId].nextLogicalElementId = uuid_operation;
474
+ // secondFiltersStructureBrcket.logicalElements[secondInitialLogicalElementId].previousLogicalElementId = uuid_operation;
475
+
476
+ // let logicalElements = Object.assign(firstFiltersStructureBrcket.logicalElements, { [uuid_operation]: operationObject }, secondFiltersStructureBrcket.logicalElements);
477
+ // console.log('logicalElements: ', logicalElements);
478
+
479
+ // return {
480
+ // objType: objType,
481
+ // initialLogicalElementId: firstInitialLogicalElementId,
482
+ // logicalElements: logicalElements
483
+ // };
484
+
485
+ // }
486
+
487
+
488
+ //...function check exist bracket
489
+ function checkAndCreateBracket(filterLogicalStucture) {
490
+ console.log("checkAndCreateBracket params::::", filterLogicalStucture);
491
+
492
+ let errorsFound = [];
493
+
494
+ // if >1 element need to add brackets
495
+ if (filterLogicalStucture.logicalElements[filterLogicalStucture.initialLogicalElementId].nextLogicalElementId !== null) {
496
+ console.log("nextLogicalElementId != null", filterLogicalStucture.logicalElements[filterLogicalStucture.initialLogicalElementId].nextLogicalElementId);
497
+ [filterLogicalStucture, errorsFound] = createLogicalElementBracket(filterLogicalStucture);
498
+ }
499
+ console.log("nextLogicalElementId = null");
500
+ return [filterLogicalStucture, errorsFound];
501
+ }
502
+
503
+ function createLogicalElementBracket(filterLogicalStucture) {
504
+ console.log("createLogicalElementBracket params::::", filterLogicalStucture);
505
+
506
+ // ---- add closeBracket
507
+ console.log("------create closeBracket------");
508
+ const MAX_LOGICAL_ELEMENTS = 1000;
509
+
510
+ let working_logicalElementId = filterLogicalStucture.initialLogicalElementId;
511
+
512
+ for (let infiniteCheck = 0; infiniteCheck <= MAX_LOGICAL_ELEMENTS; infiniteCheck++) {
513
+ //// 1000 should = limit CONST MAX_LOGICAL_ELEMENTS
514
+ // console.log("-----working_logicalElementId-----", working_logicalElementId);
515
+ if (filterLogicalStucture.logicalElements[working_logicalElementId].nextLogicalElementId === null) {
516
+ console.log("-----last logicalElement-----", filterLogicalStucture.logicalElements[working_logicalElementId]);
517
+ let closeBracket_uuid = uuidV4(); //uuidV4
518
+ console.log("closeBracket_uuid", closeBracket_uuid);
519
+
520
+ filterLogicalStucture.logicalElements[closeBracket_uuid] = {
521
+ logicalElementType: "closeBracket",
522
+ previousLogicalElementId: working_logicalElementId,
523
+ nextLogicalElementId: null,
524
+ };
525
+
526
+ filterLogicalStucture.logicalElements[working_logicalElementId].nextLogicalElementId = closeBracket_uuid;
527
+ console.log("------filterLogicalStucture before break loop------", filterLogicalStucture);
528
+ break;
529
+ }
530
+ if (infiniteCheck >= MAX_LOGICAL_ELEMENTS) {
531
+ // throw no retry error
532
+ // throw new NoRetryError("Error: max count logicalElement");
533
+ return [null, ["Error: max count logicalElement"]]
534
+ }
535
+
536
+ working_logicalElementId = filterLogicalStucture.logicalElements[working_logicalElementId].nextLogicalElementId;
537
+ }
538
+
539
+ console.log("------filterLogicalStucture before add open bracket------", filterLogicalStucture);
540
+
541
+ // ---- add openBracket
542
+
543
+ console.log("start create openBracket");
544
+ console.log("start logicalElement====>", filterLogicalStucture.logicalElements[filterLogicalStucture.initialLogicalElementId]);
545
+
546
+ let openBracket_uuid = uuidV4(); //uuidV4
547
+ console.log("openBracket_uuid", openBracket_uuid);
548
+ console.log("filterLogicalStucture before return:::::>>>>", filterLogicalStucture);
549
+
550
+ filterLogicalStucture.logicalElements[openBracket_uuid] = {
551
+ logicalElementType: "openBracket",
552
+ previousLogicalElementId: null,
553
+ nextLogicalElementId: filterLogicalStucture.initialLogicalElementId,
554
+ };
555
+
556
+ filterLogicalStucture.logicalElements[filterLogicalStucture.initialLogicalElementId].previousLogicalElementId = openBracket_uuid;
557
+ filterLogicalStucture.initialLogicalElementId = openBracket_uuid;
558
+ return [filterLogicalStucture, []];
559
+ }
560
+
561
+ function checkObjType(
562
+ fromObjType,
563
+ toObjType
564
+ ) {
565
+
566
+ let from = {
567
+ serviceTag: fromObjType.serviceTag,
568
+ objectType: fromObjType.objectType
569
+ };
570
+
571
+ let to = {
572
+ serviceTag: toObjType.serviceTag,
573
+ objectType: toObjType.objectType
574
+ };
575
+
576
+ if (hash(to) === hash(from)) {
577
+ return true;
578
+ };
579
+
580
+ return false;
581
+
582
+ };
583
+
584
+ // validate to changed searchType and filterType
585
+ /**
586
+ * recieved request thougth sortedResult??? case specail filter
587
+ * @param {string} requiredSearchType
588
+ * @param {string[]} filterArrays
589
+ *
590
+ * return [filters]
591
+ */
592
+ async function changeObjTypePath(
593
+ requiredObjType,
594
+ filtersStructure,
595
+ path = null,
596
+ ) {
597
+ try {
598
+ console.log('----- function validateSearchTypeFilterType -----', {
599
+ requiredObjType,
600
+ filtersStructure,
601
+ path
602
+ });
603
+
604
+ let filterLogicalStructure = {};
605
+
606
+ let errorsObject = {};
607
+ let errorsFound = [];
608
+
609
+ if (path !== null) {
610
+ console.log('-------------- continue process path --------------');
611
+
612
+ let logicalElements = {};
613
+ let previousLogicalElementId = null;
614
+ let previousLogicalElement = null;
615
+ let currentLogicalElementId = null;
616
+ let currentLogicalElement = null;
617
+
618
+ if (path.length > 1) {
619
+
620
+ for (let i = 0; i < path.length; i++) {
621
+
622
+ let pathObject = path[i];
623
+ let logicalStructureElements = filtersStructure.logicalElements;
624
+ console.log('logicalStructureElements', logicalStructureElements);
625
+
626
+ let initialLogicalElementId = filtersStructure.initialLogicalElementId;
627
+ console.log('initialLogicalElementId', initialLogicalElementId);
628
+
629
+ if (i + 1 === path.length) {
630
+ console.log(`------------------- final path: ${i} ---------------------`);
631
+
632
+ if (previousLogicalElementId !== null) {
633
+
634
+ if (checkObjType(pathObject.objType, logicalElements[previousLogicalElementId].objType) !== true) {
635
+ errorsFound.push('objType is not equals in structure');
636
+ console.log('before return: ', {
637
+ filterLogicalStructure: {},
638
+ errorsObject,
639
+ errorsFound
640
+ });
641
+ return [{}, errorsObject, errorsFound];
642
+ };
643
+
644
+ filterLogicalStructure = {
645
+ objType: pathObject.objType,
646
+ initialLogicalElementId: previousLogicalElementId,
647
+ logicalElements: logicalElements
648
+ };
649
+ console.log('filterLogicalStructure: ', filterLogicalStructure);
650
+
651
+ } else {
652
+ if (checkObjType(requiredObjType, filtersStructure.objType) === true) {
653
+ filterLogicalStructure = filtersStructure;
654
+ } else {
655
+ errorsFound.push('objType is not equals in structure');
656
+ console.log('before return: ', {
657
+ filterLogicalStructure: {},
658
+ errorsObject,
659
+ errorsFound
660
+ });
661
+ return [{}, errorsObject, errorsFound];
662
+ };
663
+ };
664
+
665
+ } else {
666
+ console.log(`------------------- between path: ${i} ---------------------`);
667
+
668
+ let logicalElementsStructure = logicalStructureElements[initialLogicalElementId];
669
+ console.log('logicalElementsStructure', logicalElementsStructure);
670
+
671
+ if (previousLogicalElement !== null) {
672
+
673
+ currentLogicalElementId = uuidV4();
674
+ console.log('currentLogicalElementId: ', currentLogicalElementId);
675
+
676
+ currentLogicalElement = {
677
+ logicalElementType: 'childComplexFilter',
678
+ previousLogicalElementId: null,
679
+ nextLogicalElementId: null,
680
+ childLogicalElementId: filtersStructure.initialLogicalElementId,
681
+
682
+ objType: pathObject.pathLinkType.objType,
683
+ pathLinkType: {
684
+ objType: pathObject.objType,
685
+ relType: pathObject.pathLinkType.relType,
686
+ direction: pathObject.pathLinkType.direction
687
+ }
688
+ };
689
+ console.log('currentLogicalElement', currentLogicalElement);
690
+
691
+ let logicalStructure;
692
+ [previousLogicalElementId, previousLogicalElement, logicalStructure, errorsFound] = createChildComplexFilterStructure(
693
+ logicalElements,
694
+ previousLogicalElementId,
695
+ previousLogicalElement,
696
+ currentLogicalElementId,
697
+ currentLogicalElement,
698
+ filtersStructure
699
+ );
700
+ console.log('return create child ', {
701
+ previousLogicalElementId,
702
+ previousLogicalElement,
703
+ logicalStructure,
704
+ errorsFound
705
+ });
706
+
707
+ logicalElements = logicalStructure;
708
+
709
+ } else {
710
+
711
+ if (logicalElementsStructure.logicalElementType === 'childComplexFilter') {
712
+
713
+ let childLogicalElementId = logicalElementsStructure.childLogicalElementId;
714
+ console.log('childLogicalElementId', childLogicalElementId);
715
+
716
+ if (childLogicalElementId !== null) {
717
+ console.log('-------- has child filter structure --------');
718
+
719
+ let childLogicalElement = logicalStructureElements[childLogicalElementId];
720
+ console.log('childLogicalElement', childLogicalElement);
721
+
722
+ let pathCheck = false;
723
+
724
+ for (const pathObj of path) {
725
+ if (checkObjType(childLogicalElement.objType, pathObj.objType) === true) {
726
+ pathCheck = true;
727
+ };
728
+ };
729
+
730
+ if (pathCheck) {
731
+
732
+ if (logicalElementsStructure.nextLogicalElementId === null) {
733
+
734
+ //* delete level of objType
735
+ let checkChild = false;
736
+ for (const objType of PATHCONSTANCES.objTypes) {
737
+ if (checkObjType(objType, childLogicalElement.objType) === true) {
738
+ if (!childLogicalElement.hasOwnProperty('childLogicalElementId')) {
739
+
740
+ // let childSchema = await getObjectSchema.getObjSchemaS3(
741
+ let childSchema = await getObjectSchema.getObjSchemaS3WithoutHierarchy(
742
+ {}, //_izContext maybe delete
743
+ childLogicalElement.objType
744
+ );
745
+ console.log('childSchema: ', childSchema);
746
+
747
+ if (childSchema.hasOwnProperty('extendObjType')) {
748
+ if (checkObjType(requiredObjType, childSchema.extendObjType) === true) {
749
+ checkChild = false;
750
+ break;
751
+ };
752
+ };
753
+ };
754
+ checkChild = true;
755
+ break;
756
+ };
757
+ };
758
+
759
+ if (checkChild) {
760
+
761
+ delete filtersStructure.logicalElements[initialLogicalElementId];
762
+ console.log('filtersStructure: ', filtersStructure);
763
+
764
+ filtersStructure.objType = childLogicalElement.objType;
765
+ filtersStructure.initialLogicalElementId = childLogicalElementId;
766
+ filtersStructure.logicalElements[childLogicalElementId].previousLogicalElementId = null;
767
+ console.log('after filtersStructure: ', filtersStructure);
768
+
769
+ logicalElements = filtersStructure.logicalElements;
770
+
771
+ continue;
772
+
773
+ };
774
+ };
775
+ };
776
+
777
+ currentLogicalElementId = uuidV4();
778
+ console.log('currentLogicalElementId: ', currentLogicalElementId)
779
+
780
+ currentLogicalElement = {
781
+ logicalElementType: 'childComplexFilter',
782
+ previousLogicalElementId: null,
783
+ nextLogicalElementId: null,
784
+ childLogicalElementId: filtersStructure.initialLogicalElementId,
785
+
786
+ objType: pathObject.pathLinkType.objType,
787
+ pathLinkType: {
788
+ objType: pathObject.objType,
789
+ relType: pathObject.pathLinkType.relType,
790
+ direction: pathObject.pathLinkType.direction
791
+ }
792
+ };
793
+ console.log('currentLogicalElement', currentLogicalElement);
794
+
795
+ let logicalStructure = {};
796
+ [previousLogicalElementId, previousLogicalElement, logicalStructure, errorsFound] = createChildComplexFilterStructure(
797
+ _izContext,
798
+ logicalElements,
799
+ previousLogicalElementId,
800
+ previousLogicalElement,
801
+ currentLogicalElementId,
802
+ currentLogicalElement,
803
+ filtersStructure
804
+ )
805
+ console.log('return create child', {
806
+ previousLogicalElementId,
807
+ previousLogicalElement,
808
+ logicalStructure,
809
+ errorsFound
810
+ });
811
+
812
+ logicalElements = logicalStructure;
813
+ console.log('logicalElements: ', logicalElements);
814
+
815
+ // if (logicalElementsStructure.nextLogicalElementId !== null) {
816
+ // console.log("-------- no child filter structure --------", vvv);
817
+ // }
818
+
819
+ } else {
820
+ console.log('-------- no child filter structure --------', vvv);
821
+
822
+ }
823
+
824
+ } else {
825
+
826
+ currentLogicalElementId = uuidV4();
827
+ console.log('currentLogicalElementId: ', currentLogicalElementId);
828
+
829
+ currentLogicalElement = {
830
+ logicalElementType: 'childComplexFilter',
831
+ previousLogicalElementId: null,
832
+ nextLogicalElementId: null,
833
+ childLogicalElementId: filtersStructure.initialLogicalElementId,
834
+
835
+ objType: pathObject.pathLinkType.objType,
836
+ pathLinkType: {
837
+ objType: pathObject.objType,
838
+ relType: pathObject.pathLinkType.relType,
839
+ direction: pathObject.pathLinkType.direction
840
+ }
841
+ };
842
+ console.log('currentLogicalElement', currentLogicalElement);
843
+
844
+ let logicalStructure = {};
845
+ // change function name ==> set logicalElementId
846
+ [previousLogicalElementId, previousLogicalElement, logicalStructure, errorsFound] = createChildComplexFilterStructure(
847
+ _izContext,
848
+ logicalElements,
849
+ previousLogicalElementId,
850
+ previousLogicalElement,
851
+ currentLogicalElementId,
852
+ currentLogicalElement,
853
+ filtersStructure
854
+ )
855
+ console.log('return create child', {
856
+ previousLogicalElementId,
857
+ previousLogicalElement,
858
+ logicalStructure,
859
+ errorsFound
860
+ });
861
+ logicalElements = logicalStructure;
862
+ };
863
+ };
864
+ };
865
+ };
866
+ } else {
867
+ console.log('------------ path euals 1 -----------');
868
+ console.log('pathObject', path[0]);
869
+
870
+ if (checkObjType(requiredObjType, filtersStructure.objType) === true) {
871
+ filterLogicalStructure = filtersStructure;
872
+ } else {
873
+ errorsObject = {
874
+ [requiredObjType.objectType]: `is not equals objType in filter structure: objType: ${filtersStructure.objType.objectType}`
875
+ };
876
+ errorsFound.push(`${requiredObjType.objectType} is not equals objType in filter structure: objType: ${filtersStructure.objType.objectType}`);
877
+ return [filterLogicalStructure, errorsObject, errorsFound];
878
+ };
879
+ };
880
+ };
881
+
882
+ console.log('before return filterLogicalStructure: ', {
883
+ path,
884
+ filterLogicalStructure,
885
+ errorsObject,
886
+ errorsFound
887
+ });
888
+ return [filterLogicalStructure, errorsObject, errorsFound];
889
+
890
+ } catch (err) {
891
+ _izContext.logger.error('error ValidateSearchTypeFilterType: ', err)
892
+ throw (err);
893
+ };
894
+
895
+ };
896
+ //###################################################################################################################################################################################
897
+
898
+ function createChildComplexFilterStructure(
899
+ logicalElements,
900
+ previousLogicalElementId,
901
+ previousLogicalElement,
902
+ currentLogicalElementId,
903
+ currentLogicalElement,
904
+ filtersStructure
905
+ ) {
906
+ console.log('createChildComplexFilterStructure: ', {
907
+ logicalElements,
908
+ previousLogicalElementId,
909
+ previousLogicalElement,
910
+ currentLogicalElementId,
911
+ currentLogicalElement,
912
+ filtersStructure
913
+ });
914
+
915
+ let errorsFound = [];
916
+
917
+ if (!currentLogicalElement) {
918
+ errorsFound.push('no current logical element to process');
919
+ return [previousLogicalElementId, previousLogicalElement, logicalElements, errorsFound];
920
+ };
921
+
922
+ if (!previousLogicalElement) {
923
+ filtersStructure.logicalElements[filtersStructure.initialLogicalElementId].previousLogicalElementId = currentLogicalElementId;
924
+ logicalElements = Object.assign({ [currentLogicalElementId]: currentLogicalElement }, logicalElements, filtersStructure.logicalElements);
925
+ } else {
926
+
927
+ currentLogicalElement.childLogicalElementId = previousLogicalElementId;
928
+ console.log('currentLogicalElement', currentLogicalElement);
929
+
930
+ logicalElements[previousLogicalElementId].previousLogicalElementId = currentLogicalElementId;
931
+ console.log('logicalElements', logicalElements);
932
+
933
+ logicalElements = Object.assign({ [currentLogicalElementId]: currentLogicalElement }, logicalElements);
934
+ console.log('after logicalElements', logicalElements);
935
+
936
+ };
937
+
938
+ previousLogicalElementId = currentLogicalElementId;
939
+ previousLogicalElement = currentLogicalElement;
940
+
941
+ return [previousLogicalElementId, previousLogicalElement, logicalElements, errorsFound];
942
+ };
943
+
944
+ async function findObjTypePathToAnotherObjType(
945
+ fromObjType,
946
+ toObjType,
947
+ PATHCONSTANCES
948
+ ) {
949
+ console.log('----- function findObjTypePathToAnotherObjType -----', {
950
+ fromObjType,
951
+ toObjType
952
+ });
953
+
954
+ let [path, errorsObject, errorsFound] = await recursiveFindObjTypeTypePathToAnotherObjType(
955
+ PATHCONSTANCES,
956
+ fromObjType,
957
+ toObjType,
958
+ );
959
+ console.log('return path', {
960
+ path,
961
+ errorsObject,
962
+ errorsFound
963
+ });
964
+
965
+ if (path !== null) {
966
+ console.log('--------------- will return -----------------');
967
+ return [path, errorsObject, errorsFound];
968
+ };
969
+
970
+ return [path, errorsObject, errorsFound];
971
+
972
+ };
973
+
974
+ async function recursiveFindObjTypeTypePathToAnotherObjType(
975
+ PATHCONSTANCES,
976
+ fromObjType,
977
+ toObjType,
978
+ iter = 1
979
+ ) {
980
+ console.log('----- function recursiveFindObjTypeTypePathToAnotherObjType -----', {
981
+ PATHCONSTANCES,
982
+ fromObjType,
983
+ toObjType,
984
+ iter
985
+ });
986
+
987
+ let errorsObject = {};
988
+ let errorsFound = [];
989
+
990
+ let continueProcess = false;
991
+
992
+ for (const objType of PATHCONSTANCES.objTypes) {
993
+ console.log('objType: ', objType);
994
+ if (checkObjType(fromObjType, objType) === true) {
995
+ // if (hash(fromObjType) === hash(objType)) {
996
+ continueProcess = true;
997
+ };
998
+ };
999
+
1000
+ if (continueProcess) {
1001
+ console.log('------------ continue to find path --------------');
1002
+
1003
+ if (checkObjType(fromObjType, toObjType) === true) {
1004
+ // if (hash(fromObjType) === hash(toObjType)) {
1005
+ return [[{ objType: toObjType }], errorsObject, errorsFound];
1006
+ };
1007
+
1008
+ if (iter >= MAX_ITER) {
1009
+ return [null, errorsObject, errorsFound];
1010
+ };
1011
+
1012
+ // let objectRelationship = await getObjectSchema.getObjectRelationshipWithCache(
1013
+ const objectRelationship = await getObjectSchema.getObjectRelationship(
1014
+ {}, //_izContext maybe delete
1015
+ fromObjType
1016
+ );
1017
+ console.log("objectRelationship: ", { fromObjType, objectRelationship });
1018
+
1019
+ let path = null;
1020
+ let previousNextPath = null;
1021
+ let currentNextPath = null;
1022
+
1023
+ for (const relationshipObject of objectRelationship) {
1024
+ console.log('relationshipObject: ', relationshipObject);
1025
+
1026
+ let relType = relationshipObject.relType;
1027
+ console.log('relType: ', relType);
1028
+
1029
+ let otherObjType = relationshipObject.other.objType;
1030
+ console.log('otherObjType: ', otherObjType);
1031
+
1032
+ let relCheck = false;
1033
+
1034
+ for (const relPath of PATHCONSTANCES.relTypes) {
1035
+ if (hash(relType) === hash(relPath)) {
1036
+ relCheck = true;
1037
+ break;
1038
+ };
1039
+ };
1040
+
1041
+ if (!relCheck) {
1042
+ //* no relationshipTag in constance
1043
+ continue;
1044
+ };
1045
+
1046
+ // const objectSchema = await getObjectSchema.getObjSchemaS3WithCache(
1047
+ const objectSchema = await getObjectSchema.getObjSchemaS3WithoutHierarchy(
1048
+ {}, //_izContext maybe delete
1049
+ toObjType,
1050
+ );
1051
+ console.log('objectSchema: ', objectSchema);
1052
+ console.log('before relationshipObject: ', {
1053
+ from: fromObjType,
1054
+ to: toObjType,
1055
+ next: otherObjType,
1056
+ relationshipObject
1057
+ });
1058
+
1059
+ if (objectSchema.hasOwnProperty('extendObjType')) {
1060
+ if (checkObjType(otherObjType, objectSchema.extendObjType) === true) {
1061
+ // if (hash(nextObjType) === hash(objectSchema.extendObjType)) {
1062
+ otherObjType = toObjType;
1063
+ };
1064
+ };
1065
+ console.log('after relationshipObject: ', {
1066
+ from: fromObjType,
1067
+ to: toObjType,
1068
+ next: otherObjType,
1069
+ relationshipObject
1070
+ });
1071
+
1072
+ let pathErrorsObject;
1073
+ let pathErrorsFound;
1074
+
1075
+ //* recurvsive path
1076
+ [currentNextPath, pathErrorsObject, pathErrorsFound] = await recursiveFindObjTypeTypePathToAnotherObjType(
1077
+ PATHCONSTANCES,
1078
+ otherObjType,
1079
+ toObjType,
1080
+ iter + 1
1081
+ );
1082
+ console.log('return recursive path: ', {
1083
+ currentNextPath,
1084
+ pathErrorsObject,
1085
+ pathErrorsFound
1086
+ });
1087
+
1088
+ if (currentNextPath !== null) {
1089
+
1090
+ previousNextPath = currentNextPath;
1091
+
1092
+ let addPath = {
1093
+ objType: fromObjType,
1094
+ pathLinkType: {
1095
+ objType: otherObjType,
1096
+ relType: relType,
1097
+ direction: relationshipObject.other.direction
1098
+ }
1099
+ };
1100
+ console.log('addPath: ', addPath);
1101
+
1102
+ previousNextPath.unshift(addPath);
1103
+ console.log('path: ', path);
1104
+
1105
+ } else {
1106
+ continue;
1107
+ };
1108
+
1109
+ if (previousNextPath !== null) {
1110
+ if (path !== null) {
1111
+ if (path.length > previousNextPath.length) {
1112
+ path = previousNextPath;
1113
+ };
1114
+ console.log('path: ', path);
1115
+ return [path, errorsObject, errorsFound];
1116
+ } else {
1117
+ path = previousNextPath;
1118
+ };
1119
+ };
1120
+ };// end iterate relType
1121
+
1122
+ // if (previousNextPath !== null) {
1123
+ if (path !== null) {
1124
+ if (path.length > previousNextPath.length) {
1125
+ path = previousNextPath;
1126
+ };
1127
+ console.log('path: ', path);
1128
+ return [path, errorsObject, errorsFound];
1129
+ };
1130
+
1131
+ } else {
1132
+ errorsObject = {
1133
+ [fromObjType.objectType]: 'cannot found this objType that can process in path'
1134
+ };
1135
+ errorsFound.push(`${fromObjType.objectType}: cannot found objType that can process in path`);
1136
+ };
1137
+
1138
+ return [null, errorsObject, errorsFound];
1139
+
1140
+ };
1141
+
1142
+
1143
+ export default {
1144
+ combineLogicalStructure,
1145
+ // combineOperation,
1146
+ // combineLogicalElements,
1147
+
1148
+ checkAndCreateBracket,
1149
+
1150
+ changeObjTypePath,
1151
+ findObjTypePathToAnotherObjType
1152
+ }