av6-core 1.5.0 → 1.5.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.
package/dist/index.d.mts CHANGED
@@ -328,6 +328,7 @@ interface ColValue {
328
328
  interface NewFixedSearchRequest extends NewSearchRequest {
329
329
  fixedSearch?: Record<string, ColValue>;
330
330
  fixedNotSearch?: Record<string, ColValue>;
331
+ logic?: LogicNode;
331
332
  }
332
333
  interface NewFixedSearchRequestService<T extends DynamicShortCode> extends NewFixedSearchRequest {
333
334
  shortCodeData: T;
@@ -355,8 +356,9 @@ interface DropdownRequest {
355
356
  shortCode: string;
356
357
  searchColumns: string[];
357
358
  searchText?: string;
358
- fixedSearch?: Record<string, ColValue>;
359
- fixedNotSearch?: Record<string, ColValue>;
359
+ fixedSearch?: FixedMap;
360
+ fixedNotSearch?: FixedMap;
361
+ logic: LogicNode;
360
362
  sortBy?: string;
361
363
  sortDir?: "ASC" | "DESC";
362
364
  selectColumns?: Record<string, string>;
package/dist/index.d.ts CHANGED
@@ -328,6 +328,7 @@ interface ColValue {
328
328
  interface NewFixedSearchRequest extends NewSearchRequest {
329
329
  fixedSearch?: Record<string, ColValue>;
330
330
  fixedNotSearch?: Record<string, ColValue>;
331
+ logic?: LogicNode;
331
332
  }
332
333
  interface NewFixedSearchRequestService<T extends DynamicShortCode> extends NewFixedSearchRequest {
333
334
  shortCodeData: T;
@@ -355,8 +356,9 @@ interface DropdownRequest {
355
356
  shortCode: string;
356
357
  searchColumns: string[];
357
358
  searchText?: string;
358
- fixedSearch?: Record<string, ColValue>;
359
- fixedNotSearch?: Record<string, ColValue>;
359
+ fixedSearch?: FixedMap;
360
+ fixedNotSearch?: FixedMap;
361
+ logic: LogicNode;
360
362
  sortBy?: string;
361
363
  sortDir?: "ASC" | "DESC";
362
364
  selectColumns?: Record<string, string>;
package/dist/index.js CHANGED
@@ -299,28 +299,17 @@ function toDynamicDropdownDto(mapping, row) {
299
299
  function toDynamicDropdownDtoList(mapping, rows) {
300
300
  return rows.map((r) => toDynamicDropdownDto(mapping, r));
301
301
  }
302
- function andMerge(a, b) {
303
- if (!a || isPlainObject(a) && Object.keys(a).length === 0) return b;
304
- if (!b || isPlainObject(b) && Object.keys(b).length === 0) return a;
305
- if (isPlainObject(a) && Array.isArray(a.AND)) return { ...a, AND: [...a.AND, b] };
306
- if (isPlainObject(b) && Array.isArray(b.AND)) return { ...b, AND: [...b.AND, a] };
307
- return { AND: [a, b] };
308
- }
309
302
  async function generateFixedMapsCondition(args) {
310
303
  let out = {};
311
- if (args.fixedSearch) {
312
- for (const [field, defObj] of Object.entries(args.fixedSearch)) {
313
- if (!defObj?.value?.length) continue;
314
- const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, false);
315
- out = andMerge(out, cond);
316
- }
304
+ for (const [field, defObj] of Object.entries(args.fixedSearch ?? {})) {
305
+ if (!defObj?.value?.length) continue;
306
+ const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, false);
307
+ out = smartAndMerge(out, cond);
317
308
  }
318
- if (args.fixedNotSearch) {
319
- for (const [field, defObj] of Object.entries(args.fixedNotSearch)) {
320
- if (!defObj?.value?.length) continue;
321
- const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, true);
322
- out = andMerge(out, cond);
323
- }
309
+ for (const [field, defObj] of Object.entries(args.fixedNotSearch ?? {})) {
310
+ if (!defObj?.value?.length) continue;
311
+ const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, true);
312
+ out = smartAndMerge(out, cond);
324
313
  }
325
314
  return out;
326
315
  }
@@ -333,7 +322,7 @@ async function buildLogicCondition(node) {
333
322
  if (node.AND?.length) {
334
323
  for (const child of node.AND) {
335
324
  const childCond = await buildLogicCondition(child);
336
- self = andMerge(self, childCond);
325
+ self = smartAndMerge(self, childCond);
337
326
  }
338
327
  }
339
328
  if (node.OR?.length) {
@@ -344,13 +333,48 @@ async function buildLogicCondition(node) {
344
333
  orList.push(childCond);
345
334
  }
346
335
  if (orList.length === 1) {
347
- self = andMerge(self, orList[0]);
336
+ self = smartAndMerge(self, orList[0]);
348
337
  } else if (orList.length > 1) {
349
- self = andMerge(self, { OR: orList });
338
+ self = smartAndMerge(self, { OR: orList });
350
339
  }
351
340
  }
352
341
  return self;
353
342
  }
343
+ function tryDeepMergeObjects(a, b) {
344
+ if (!isPlainObject(a) || !isPlainObject(b)) return void 0;
345
+ const out = { ...a };
346
+ for (const k of Object.keys(b)) {
347
+ if (out[k] === void 0) {
348
+ out[k] = b[k];
349
+ continue;
350
+ }
351
+ const av = out[k];
352
+ const bv = b[k];
353
+ if (isPlainObject(av) && isPlainObject(bv)) {
354
+ const merged = tryDeepMergeObjects(av, bv);
355
+ if (merged !== void 0) {
356
+ out[k] = merged;
357
+ continue;
358
+ }
359
+ }
360
+ return void 0;
361
+ }
362
+ return out;
363
+ }
364
+ function andWrap(a, b) {
365
+ if (!a || isPlainObject(a) && Object.keys(a).length === 0) return b;
366
+ if (!b || isPlainObject(b) && Object.keys(b).length === 0) return a;
367
+ if (isPlainObject(a) && Array.isArray(a.AND)) return { ...a, AND: [...a.AND, b] };
368
+ if (isPlainObject(b) && Array.isArray(b.AND)) return { ...b, AND: [...b.AND, a] };
369
+ return { AND: [a, b] };
370
+ }
371
+ function smartAndMerge(a, b) {
372
+ if (!a || isPlainObject(a) && Object.keys(a).length === 0) return b;
373
+ if (!b || isPlainObject(b) && Object.keys(b).length === 0) return a;
374
+ const merged = tryDeepMergeObjects(a, b);
375
+ if (merged !== void 0) return merged;
376
+ return andWrap(a, b);
377
+ }
354
378
 
355
379
  // src/repository/common.repository.ts
356
380
  var import_util2 = require("util");
@@ -438,15 +462,17 @@ var commonRepository = (serviceDeps) => {
438
462
  shortCodeData,
439
463
  fixedSearch,
440
464
  fixedNotSearch,
465
+ logic,
441
466
  sortBy,
442
467
  sortDir,
443
468
  selectColumns
444
469
  }) {
445
470
  logger.info("entering::commonSearch::repository");
446
471
  const tableName = shortCodeData.tableName;
447
- const rootCondition = await this.generateRootCondition({
472
+ const rootCondition = await this.generateRootConditionV2({
448
473
  fixedSearch,
449
- fixedNotSearch
474
+ fixedNotSearch,
475
+ logic
450
476
  });
451
477
  const customWhereClause = { ...rootCondition };
452
478
  const whereClause = {};
@@ -622,7 +648,7 @@ var commonRepository = (serviceDeps) => {
622
648
  fixedNotSearch: args.fixedNotSearch
623
649
  });
624
650
  const logicCond = await buildLogicCondition(args.logic);
625
- root = andMerge(root, logicCond);
651
+ root = smartAndMerge(root, logicCond);
626
652
  if (isPlainObject(root) && Object.keys(root).length === 0) return {};
627
653
  return root;
628
654
  },
@@ -634,13 +660,15 @@ var commonRepository = (serviceDeps) => {
634
660
  // now [{ col: string, type: string }, ...]
635
661
  fixedSearch,
636
662
  // now { [fieldName]: { type: string, value: any[] } }
637
- fixedNotSearch
663
+ fixedNotSearch,
638
664
  // same structure
665
+ logic
639
666
  }) {
640
667
  logger.info("entering::fixedSearch::repository");
641
668
  const rootCondition = await this.generateRootConditionV2({
642
669
  fixedSearch,
643
- fixedNotSearch
670
+ fixedNotSearch,
671
+ logic
644
672
  });
645
673
  let searchOR;
646
674
  if (searchText && searchColumns && searchColumns.length > 0) {
package/dist/index.mjs CHANGED
@@ -249,28 +249,17 @@ function toDynamicDropdownDto(mapping, row) {
249
249
  function toDynamicDropdownDtoList(mapping, rows) {
250
250
  return rows.map((r) => toDynamicDropdownDto(mapping, r));
251
251
  }
252
- function andMerge(a, b) {
253
- if (!a || isPlainObject(a) && Object.keys(a).length === 0) return b;
254
- if (!b || isPlainObject(b) && Object.keys(b).length === 0) return a;
255
- if (isPlainObject(a) && Array.isArray(a.AND)) return { ...a, AND: [...a.AND, b] };
256
- if (isPlainObject(b) && Array.isArray(b.AND)) return { ...b, AND: [...b.AND, a] };
257
- return { AND: [a, b] };
258
- }
259
252
  async function generateFixedMapsCondition(args) {
260
253
  let out = {};
261
- if (args.fixedSearch) {
262
- for (const [field, defObj] of Object.entries(args.fixedSearch)) {
263
- if (!defObj?.value?.length) continue;
264
- const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, false);
265
- out = andMerge(out, cond);
266
- }
254
+ for (const [field, defObj] of Object.entries(args.fixedSearch ?? {})) {
255
+ if (!defObj?.value?.length) continue;
256
+ const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, false);
257
+ out = smartAndMerge(out, cond);
267
258
  }
268
- if (args.fixedNotSearch) {
269
- for (const [field, defObj] of Object.entries(args.fixedNotSearch)) {
270
- if (!defObj?.value?.length) continue;
271
- const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, true);
272
- out = andMerge(out, cond);
273
- }
259
+ for (const [field, defObj] of Object.entries(args.fixedNotSearch ?? {})) {
260
+ if (!defObj?.value?.length) continue;
261
+ const cond = buildFieldCondition(field.split("."), defObj.type, defObj.value, true);
262
+ out = smartAndMerge(out, cond);
274
263
  }
275
264
  return out;
276
265
  }
@@ -283,7 +272,7 @@ async function buildLogicCondition(node) {
283
272
  if (node.AND?.length) {
284
273
  for (const child of node.AND) {
285
274
  const childCond = await buildLogicCondition(child);
286
- self = andMerge(self, childCond);
275
+ self = smartAndMerge(self, childCond);
287
276
  }
288
277
  }
289
278
  if (node.OR?.length) {
@@ -294,13 +283,48 @@ async function buildLogicCondition(node) {
294
283
  orList.push(childCond);
295
284
  }
296
285
  if (orList.length === 1) {
297
- self = andMerge(self, orList[0]);
286
+ self = smartAndMerge(self, orList[0]);
298
287
  } else if (orList.length > 1) {
299
- self = andMerge(self, { OR: orList });
288
+ self = smartAndMerge(self, { OR: orList });
300
289
  }
301
290
  }
302
291
  return self;
303
292
  }
293
+ function tryDeepMergeObjects(a, b) {
294
+ if (!isPlainObject(a) || !isPlainObject(b)) return void 0;
295
+ const out = { ...a };
296
+ for (const k of Object.keys(b)) {
297
+ if (out[k] === void 0) {
298
+ out[k] = b[k];
299
+ continue;
300
+ }
301
+ const av = out[k];
302
+ const bv = b[k];
303
+ if (isPlainObject(av) && isPlainObject(bv)) {
304
+ const merged = tryDeepMergeObjects(av, bv);
305
+ if (merged !== void 0) {
306
+ out[k] = merged;
307
+ continue;
308
+ }
309
+ }
310
+ return void 0;
311
+ }
312
+ return out;
313
+ }
314
+ function andWrap(a, b) {
315
+ if (!a || isPlainObject(a) && Object.keys(a).length === 0) return b;
316
+ if (!b || isPlainObject(b) && Object.keys(b).length === 0) return a;
317
+ if (isPlainObject(a) && Array.isArray(a.AND)) return { ...a, AND: [...a.AND, b] };
318
+ if (isPlainObject(b) && Array.isArray(b.AND)) return { ...b, AND: [...b.AND, a] };
319
+ return { AND: [a, b] };
320
+ }
321
+ function smartAndMerge(a, b) {
322
+ if (!a || isPlainObject(a) && Object.keys(a).length === 0) return b;
323
+ if (!b || isPlainObject(b) && Object.keys(b).length === 0) return a;
324
+ const merged = tryDeepMergeObjects(a, b);
325
+ if (merged !== void 0) return merged;
326
+ return andWrap(a, b);
327
+ }
304
328
 
305
329
  // src/repository/common.repository.ts
306
330
  import { isDeepStrictEqual as isDeepStrictEqual2 } from "util";
@@ -388,15 +412,17 @@ var commonRepository = (serviceDeps) => {
388
412
  shortCodeData,
389
413
  fixedSearch,
390
414
  fixedNotSearch,
415
+ logic,
391
416
  sortBy,
392
417
  sortDir,
393
418
  selectColumns
394
419
  }) {
395
420
  logger.info("entering::commonSearch::repository");
396
421
  const tableName = shortCodeData.tableName;
397
- const rootCondition = await this.generateRootCondition({
422
+ const rootCondition = await this.generateRootConditionV2({
398
423
  fixedSearch,
399
- fixedNotSearch
424
+ fixedNotSearch,
425
+ logic
400
426
  });
401
427
  const customWhereClause = { ...rootCondition };
402
428
  const whereClause = {};
@@ -572,7 +598,7 @@ var commonRepository = (serviceDeps) => {
572
598
  fixedNotSearch: args.fixedNotSearch
573
599
  });
574
600
  const logicCond = await buildLogicCondition(args.logic);
575
- root = andMerge(root, logicCond);
601
+ root = smartAndMerge(root, logicCond);
576
602
  if (isPlainObject(root) && Object.keys(root).length === 0) return {};
577
603
  return root;
578
604
  },
@@ -584,13 +610,15 @@ var commonRepository = (serviceDeps) => {
584
610
  // now [{ col: string, type: string }, ...]
585
611
  fixedSearch,
586
612
  // now { [fieldName]: { type: string, value: any[] } }
587
- fixedNotSearch
613
+ fixedNotSearch,
588
614
  // same structure
615
+ logic
589
616
  }) {
590
617
  logger.info("entering::fixedSearch::repository");
591
618
  const rootCondition = await this.generateRootConditionV2({
592
619
  fixedSearch,
593
- fixedNotSearch
620
+ fixedNotSearch,
621
+ logic
594
622
  });
595
623
  let searchOR;
596
624
  if (searchText && searchColumns && searchColumns.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "av6-core",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",