rez_core 5.0.152 → 5.0.154

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.
Files changed (76) hide show
  1. package/dist/config/bull.config.js +4 -4
  2. package/dist/config/bull.config.js.map +1 -1
  3. package/dist/core.module.js +2 -5
  4. package/dist/core.module.js.map +1 -1
  5. package/dist/module/entity_json/entity_json.module.js +1 -1
  6. package/dist/module/entity_json/entity_json.module.js.map +1 -1
  7. package/dist/module/entity_json/service/entity_json.service.js +1 -1
  8. package/dist/module/entity_json/service/entity_json.service.js.map +1 -1
  9. package/dist/module/filter/controller/filter.controller.d.ts +0 -12
  10. package/dist/module/filter/controller/filter.controller.js +1 -1
  11. package/dist/module/filter/controller/filter.controller.js.map +1 -1
  12. package/dist/module/filter/entity/saved-filter-master.entity.d.ts +2 -1
  13. package/dist/module/filter/entity/saved-filter-master.entity.js +6 -2
  14. package/dist/module/filter/entity/saved-filter-master.entity.js.map +1 -1
  15. package/dist/module/filter/filter.module.js +2 -9
  16. package/dist/module/filter/filter.module.js.map +1 -1
  17. package/dist/module/filter/repository/saved-filter.repository.d.ts +1 -5
  18. package/dist/module/filter/repository/saved-filter.repository.js +1 -5
  19. package/dist/module/filter/repository/saved-filter.repository.js.map +1 -1
  20. package/dist/module/filter/service/filter.service.d.ts +1 -37
  21. package/dist/module/filter/service/filter.service.js +2 -30
  22. package/dist/module/filter/service/filter.service.js.map +1 -1
  23. package/dist/module/integration/service/wrapper.service.d.ts +3 -1
  24. package/dist/module/integration/service/wrapper.service.js +24 -2
  25. package/dist/module/integration/service/wrapper.service.js.map +1 -1
  26. package/dist/module/linked_attributes/controller/linked_attributes.controller.d.ts +0 -19
  27. package/dist/module/linked_attributes/controller/linked_attributes.controller.js +0 -77
  28. package/dist/module/linked_attributes/controller/linked_attributes.controller.js.map +1 -1
  29. package/dist/module/linked_attributes/linked_attributes.module.js +1 -8
  30. package/dist/module/linked_attributes/linked_attributes.module.js.map +1 -1
  31. package/dist/module/linked_attributes/service/linked_attributes.service.d.ts +1 -41
  32. package/dist/module/linked_attributes/service/linked_attributes.service.js +2 -266
  33. package/dist/module/linked_attributes/service/linked_attributes.service.js.map +1 -1
  34. package/dist/module/meta/dto/entity-table.dto.d.ts +1 -4
  35. package/dist/module/meta/dto/entity-table.dto.js.map +1 -1
  36. package/dist/module/meta/service/media-data.service.js +18 -5
  37. package/dist/module/meta/service/media-data.service.js.map +1 -1
  38. package/dist/module/workflow/service/action.service.js +2 -10
  39. package/dist/module/workflow/service/action.service.js.map +1 -1
  40. package/dist/table.config.d.ts +1 -3
  41. package/dist/table.config.js +0 -2
  42. package/dist/table.config.js.map +1 -1
  43. package/dist/tsconfig.build.tsbuildinfo +1 -1
  44. package/package.json +1 -1
  45. package/src/config/bull.config.ts +4 -4
  46. package/src/core.module.ts +2 -5
  47. package/src/module/entity_json/entity_json.module.ts +1 -1
  48. package/src/module/entity_json/service/entity_json.service.ts +1 -1
  49. package/src/module/filter/controller/filter.controller.ts +3 -1
  50. package/src/module/filter/entity/saved-filter-master.entity.ts +5 -2
  51. package/src/module/filter/filter.module.ts +2 -9
  52. package/src/module/filter/repository/saved-filter.repository.ts +9 -5
  53. package/src/module/filter/service/filter.service.ts +0 -49
  54. package/src/module/integration/service/wrapper.service.ts +37 -0
  55. package/src/module/linked_attributes/controller/linked_attributes.controller.ts +0 -86
  56. package/src/module/linked_attributes/linked_attributes.module.ts +2 -9
  57. package/src/module/linked_attributes/service/linked_attributes.service.ts +3 -548
  58. package/src/module/meta/dto/entity-table.dto.ts +3 -1
  59. package/src/module/meta/service/media-data.service.ts +27 -9
  60. package/src/module/workflow/service/action.service.ts +6 -10
  61. package/src/table.config.ts +0 -2
  62. package/dist/migrations/1732612800000-AddEntityJsonGinIndex.d.ts +0 -6
  63. package/dist/migrations/1732612800000-AddEntityJsonGinIndex.js +0 -32
  64. package/dist/migrations/1732612800000-AddEntityJsonGinIndex.js.map +0 -1
  65. package/dist/module/filter/service/flatjson-filter.service.d.ts +0 -30
  66. package/dist/module/filter/service/flatjson-filter.service.js +0 -615
  67. package/dist/module/filter/service/flatjson-filter.service.js.map +0 -1
  68. package/dist/module/linked_attributes/dto/create-linked-attribute-smart.dto.d.ts +0 -13
  69. package/dist/module/linked_attributes/dto/create-linked-attribute-smart.dto.js +0 -64
  70. package/dist/module/linked_attributes/dto/create-linked-attribute-smart.dto.js.map +0 -1
  71. package/src/migrations/1732612800000-AddEntityJsonGinIndex.ts +0 -41
  72. package/src/module/entity_json/docs/FlatJson_Filterin_System.md +0 -2804
  73. package/src/module/filter/service/flatjson-filter.service.ts +0 -888
  74. package/src/module/filter/test/flatjson-filter.service.spec.ts +0 -415
  75. package/src/module/linked_attributes/dto/create-linked-attribute-smart.dto.ts +0 -54
  76. package/src/module/linked_attributes/test/linked-attributes.service.spec.ts +0 -244
@@ -1,888 +0,0 @@
1
- import { Injectable, BadRequestException, Inject } from '@nestjs/common';
2
- import { EntityManager, SelectQueryBuilder } from 'typeorm';
3
- import { EntityMasterService } from 'src/module/meta/service/entity-master.service';
4
- import { AttributeMasterService } from 'src/module/meta/service/attribute-master.service';
5
- import { ResolverService } from 'src/module/meta/service/resolver.service';
6
- import { LoggingService } from 'src/utils/service/loggingUtil.service';
7
- import { ConfigService } from '@nestjs/config';
8
- import {
9
- FilterRequestDto,
10
- FilterCondition,
11
- SortConfig,
12
- } from '../dto/filter-request.dto';
13
- import { EntityJson } from 'src/module/entity_json/entity/entityJson.entity';
14
- import { SavedFilterRepositoryService } from '../repository/saved-filter.repository';
15
-
16
- /**
17
- * JSONB query condition structure
18
- */
19
- interface JsonbCondition {
20
- query: string;
21
- params: Record<string, any>;
22
- }
23
-
24
- @Injectable()
25
- export class FlatjsonFilterService {
26
- constructor(
27
- private readonly entityManager: EntityManager,
28
- private readonly entityMasterService: EntityMasterService,
29
- private readonly attributeMasterService: AttributeMasterService,
30
- private readonly resolverService: ResolverService,
31
- private readonly loggingService: LoggingService,
32
- private readonly configService: ConfigService,
33
- private readonly savedFilterRepositoryService: SavedFilterRepositoryService,
34
- ) {}
35
-
36
- /**
37
- * Main filtering method - queries frm_entity_json table
38
- */
39
- async applyFlatjsonFilter(dto: FilterRequestDto): Promise<any> {
40
- const {
41
- entity_type,
42
- quickFilter = [],
43
- savedFilterCode,
44
- attributeFilter = [],
45
- sortby = [],
46
- tabs,
47
- page = 1,
48
- size = 20,
49
- loggedInUser,
50
- } = dto;
51
-
52
- await this.loggingService.log(
53
- 'info',
54
- 'FlatjsonFilterService',
55
- 'applyFlatjsonFilter',
56
- `Filtering entity: ${entity_type}`,
57
- );
58
-
59
- // Step 1: Load attribute metadata for this entity
60
- const attributes =
61
- await this.attributeMasterService.findAttributesByMappedEntityType(
62
- entity_type,
63
- loggedInUser,
64
- );
65
-
66
- const attributeMetaMap: Record<string, any> = {};
67
- attributes.forEach((attr) => {
68
- attributeMetaMap[attr.attribute_key] = attr;
69
- });
70
-
71
- await this.loggingService.log(
72
- 'debug',
73
- 'FlatjsonFilterService',
74
- 'applyFlatjsonFilter',
75
- `Loaded ${attributes.length} attributes`,
76
- );
77
-
78
- // Step 2: Merge filters (quickFilter + savedFilter + attributeFilter)
79
- let allFilters: FilterCondition[] = [...quickFilter, ...attributeFilter];
80
-
81
- if (savedFilterCode) {
82
- const savedFilterDetails =
83
- await this.savedFilterRepositoryService.getDetailsByCode(
84
- savedFilterCode,
85
- );
86
- allFilters = [...allFilters, ...savedFilterDetails];
87
- }
88
-
89
- await this.loggingService.log(
90
- 'debug',
91
- 'FlatjsonFilterService',
92
- 'applyFlatjsonFilter',
93
- `Total filters: ${allFilters.length}`,
94
- );
95
-
96
- // Step 3: Build JSONB conditions
97
- const jsonbConditions = this.buildJsonbConditions(
98
- allFilters,
99
- attributeMetaMap,
100
- );
101
-
102
- // Step 4: Build base query
103
- const qb = this.entityManager
104
- .getRepository(EntityJson)
105
- .createQueryBuilder('ej')
106
- .where('ej.entity_type = :entity_type', { entity_type })
107
- .andWhere('ej.organization_id = :orgId', {
108
- orgId: loggedInUser.organization_id,
109
- });
110
-
111
- // Step 5: Apply JSONB conditions
112
- jsonbConditions.forEach((condition, index) => {
113
- qb.andWhere(condition.query, condition.params);
114
- });
115
-
116
- // Step 6: Apply tab filter if provided
117
- if (tabs?.columnName && tabs?.value) {
118
- const tabMeta = attributeMetaMap[tabs.columnName];
119
- if (tabMeta) {
120
- const flatJsonKey =
121
- tabMeta.flat_json_key ||
122
- `${entity_type}__${tabs.columnName}`;
123
- qb.andWhere(`json_data->>'${flatJsonKey}' = :tabValue`, {
124
- tabValue: tabs.value,
125
- });
126
- }
127
- }
128
-
129
- // Step 7: Get tab counts if needed (before pagination)
130
- let tabCounts: any[] = [];
131
- if (tabs?.columnName && !tabs?.value) {
132
- const tabMeta = attributeMetaMap[tabs.columnName];
133
- if (tabMeta) {
134
- const flatJsonKey =
135
- tabMeta.flat_json_key ||
136
- `${entity_type}__${tabs.columnName}`;
137
- tabCounts = await this.getJsonbTabCounts(
138
- entity_type,
139
- flatJsonKey,
140
- jsonbConditions,
141
- );
142
- }
143
- }
144
-
145
- // Step 8: Apply sorting
146
- if (sortby && sortby.length > 0) {
147
- this.applyJsonbSorting(qb, sortby, attributeMetaMap);
148
- } else {
149
- // Default sort by created_date DESC
150
- qb.orderBy('ej.created_date', 'DESC');
151
- }
152
-
153
- // Step 9: Get total count (before pagination)
154
- const totalCount = await qb.getCount();
155
-
156
- // Step 10: Apply pagination
157
- if (page && size) {
158
- const skip = (page - 1) * size;
159
- qb.skip(skip).take(size);
160
- }
161
-
162
- // Step 11: Execute query
163
- const results = await qb.getMany();
164
-
165
- await this.loggingService.log(
166
- 'info',
167
- 'FlatjsonFilterService',
168
- 'applyFlatjsonFilter',
169
- `Found ${totalCount} total, returning ${results.length} records`,
170
- );
171
-
172
- // Step 12: Resolve entity IDs to full entity data
173
- const resolvedData = await this.resolveEntityData(
174
- results,
175
- entity_type,
176
- loggedInUser,
177
- );
178
-
179
- return {
180
- data: resolvedData,
181
- total: totalCount,
182
- page: page || 1,
183
- size: size || 20,
184
- totalPages: size ? Math.ceil(totalCount / size) : 1,
185
- tabCounts: tabCounts.length > 0 ? tabCounts : undefined,
186
- };
187
- }
188
-
189
- /**
190
- * Build JSONB where clauses from filter conditions
191
- */
192
- private buildJsonbConditions(
193
- filters: FilterCondition[],
194
- attributeMetaMap: Record<string, any>,
195
- ): JsonbCondition[] {
196
- const conditions: JsonbCondition[] = [];
197
-
198
- for (const filter of filters) {
199
- const meta = attributeMetaMap[filter.filter_attribute];
200
- const condition = this.buildJsonbCondition(filter, meta);
201
-
202
- if (condition) {
203
- conditions.push(condition);
204
- } else {
205
- console.warn(
206
- `Could not build condition for filter: ${filter.filter_attribute}`,
207
- );
208
- }
209
- }
210
-
211
- return conditions;
212
- }
213
-
214
- /**
215
- * Resolve entity IDs to full entity data
216
- */
217
- private async resolveEntityData(
218
- entityJsonRecords: EntityJson[],
219
- entity_type: string,
220
- loggedInUser: any,
221
- ): Promise<any[]> {
222
- if (entityJsonRecords.length === 0) return [];
223
-
224
- const entityIds = entityJsonRecords.map((ej) => ej.entity_id);
225
-
226
- try {
227
- // Use resolver service to get full entity data
228
- const fullEntities = await this.resolverService.getResolvedData(
229
- entity_type,
230
- entityIds,
231
- loggedInUser,
232
- );
233
-
234
- // Return with json_data merged
235
- return entityJsonRecords.map((ej) => {
236
- const fullEntity = fullEntities.find((e) => e.id === ej.entity_id);
237
- return {
238
- ...fullEntity,
239
- _flatjson: ej.json_data, // Include flatjson for debugging
240
- };
241
- });
242
- } catch (error) {
243
- console.error('Error resolving entity data:', error);
244
- // Fallback: return just the entity IDs with json_data
245
- return entityJsonRecords.map((ej) => ({
246
- id: ej.entity_id,
247
- entity_type: ej.entity_type,
248
- ...ej.json_data,
249
- }));
250
- }
251
- }
252
-
253
- /**
254
- * Build a single JSONB condition based on data type
255
- */
256
- private buildJsonbCondition(
257
- filter: FilterCondition,
258
- meta: any,
259
- ): JsonbCondition | null {
260
- if (!meta) return null;
261
-
262
- const flatJsonKey =
263
- meta.flat_json_key ||
264
- `${meta.mapped_entity_type}__${filter.filter_attribute}`;
265
- const op = filter.filter_operator;
266
- const val = filter.filter_value;
267
- const key = `param_${filter.filter_attribute}_${Math.random().toString(36).substring(2, 8)}`;
268
-
269
- switch (meta.data_type) {
270
- case 'text':
271
- return this.buildTextCondition(flatJsonKey, op, val, key);
272
- case 'number':
273
- return this.buildNumberCondition(flatJsonKey, op, val, key);
274
- case 'date':
275
- return this.buildDateCondition(flatJsonKey, op, val, key);
276
- case 'select':
277
- case 'radio':
278
- return this.buildSelectCondition(flatJsonKey, op, val, key);
279
- case 'multiselect':
280
- case 'checkbox':
281
- return this.buildMultiSelectCondition(flatJsonKey, op, val, key);
282
- case 'year':
283
- return this.buildYearCondition(flatJsonKey, op, val, key);
284
- default:
285
- return null;
286
- }
287
- }
288
-
289
- /**
290
- * Build text condition (JSONB ->> operator)
291
- */
292
- private buildTextCondition(
293
- flatJsonKey: string,
294
- operator: string,
295
- value: any,
296
- paramKey: string,
297
- ): JsonbCondition | null {
298
- // Text is already stored lowercase in flatjson
299
- const lowerValue = value ? String(value).toLowerCase() : '';
300
-
301
- switch (operator) {
302
- case 'equal':
303
- return {
304
- query: `json_data->>'${flatJsonKey}' = :${paramKey}`,
305
- params: { [paramKey]: lowerValue },
306
- };
307
-
308
- case 'not_equal':
309
- return {
310
- query: `json_data->>'${flatJsonKey}' != :${paramKey}`,
311
- params: { [paramKey]: lowerValue },
312
- };
313
-
314
- case 'contains':
315
- return {
316
- query: `json_data->>'${flatJsonKey}' LIKE :${paramKey}`,
317
- params: { [paramKey]: `%${lowerValue}%` },
318
- };
319
-
320
- case 'not_contains':
321
- return {
322
- query: `json_data->>'${flatJsonKey}' NOT LIKE :${paramKey}`,
323
- params: { [paramKey]: `%${lowerValue}%` },
324
- };
325
-
326
- case 'starts_with':
327
- return {
328
- query: `json_data->>'${flatJsonKey}' LIKE :${paramKey}`,
329
- params: { [paramKey]: `${lowerValue}%` },
330
- };
331
-
332
- case 'ends_with':
333
- return {
334
- query: `json_data->>'${flatJsonKey}' LIKE :${paramKey}`,
335
- params: { [paramKey]: `%${lowerValue}` },
336
- };
337
-
338
- case 'empty':
339
- return {
340
- query: `(json_data->>'${flatJsonKey}' IS NULL OR json_data->>'${flatJsonKey}' = '')`,
341
- params: {},
342
- };
343
-
344
- case 'not_empty':
345
- return {
346
- query: `(json_data->>'${flatJsonKey}' IS NOT NULL AND json_data->>'${flatJsonKey}' != '')`,
347
- params: {},
348
- };
349
-
350
- default:
351
- console.warn(`Unsupported text operator: ${operator}`);
352
- return null;
353
- }
354
- }
355
-
356
- /**
357
- * Build number condition (JSONB ->> with ::numeric cast)
358
- */
359
- private buildNumberCondition(
360
- flatJsonKey: string,
361
- operator: string,
362
- value: any,
363
- paramKey: string,
364
- ): JsonbCondition | null {
365
- // Cast JSONB text to numeric for comparison
366
- const jsonbField = `(json_data->>'${flatJsonKey}')::numeric`;
367
-
368
- switch (operator) {
369
- case 'equal':
370
- return {
371
- query: `${jsonbField} = :${paramKey}`,
372
- params: { [paramKey]: Number(value) },
373
- };
374
-
375
- case 'not_equal':
376
- return {
377
- query: `${jsonbField} != :${paramKey}`,
378
- params: { [paramKey]: Number(value) },
379
- };
380
-
381
- case 'greater_than':
382
- return {
383
- query: `${jsonbField} > :${paramKey}`,
384
- params: { [paramKey]: Number(value) },
385
- };
386
-
387
- case 'less_than':
388
- return {
389
- query: `${jsonbField} < :${paramKey}`,
390
- params: { [paramKey]: Number(value) },
391
- };
392
-
393
- case 'greater_than_equal_to':
394
- return {
395
- query: `${jsonbField} >= :${paramKey}`,
396
- params: { [paramKey]: Number(value) },
397
- };
398
-
399
- case 'less_than_equal_to':
400
- return {
401
- query: `${jsonbField} <= :${paramKey}`,
402
- params: { [paramKey]: Number(value) },
403
- };
404
-
405
- case 'between': {
406
- // Value should be array [min, max] or string "min,max"
407
- let range: number[];
408
- if (typeof value === 'string') {
409
- range = value.split(',').map((v) => Number(v.trim()));
410
- } else if (Array.isArray(value)) {
411
- range = value.map((v) => Number(v));
412
- } else {
413
- return null;
414
- }
415
-
416
- if (range.length !== 2) return null;
417
-
418
- return {
419
- query: `${jsonbField} BETWEEN :${paramKey}_min AND :${paramKey}_max`,
420
- params: {
421
- [`${paramKey}_min`]: range[0],
422
- [`${paramKey}_max`]: range[1],
423
- },
424
- };
425
- }
426
-
427
- case 'empty':
428
- return {
429
- query: `json_data->>'${flatJsonKey}' IS NULL`,
430
- params: {},
431
- };
432
-
433
- case 'not_empty':
434
- return {
435
- query: `json_data->>'${flatJsonKey}' IS NOT NULL`,
436
- params: {},
437
- };
438
-
439
- default:
440
- console.warn(`Unsupported number operator: ${operator}`);
441
- return null;
442
- }
443
- }
444
-
445
- /**
446
- * Build date condition (JSONB ->> with ::bigint cast for epoch ms)
447
- */
448
- private buildDateCondition(
449
- flatJsonKey: string,
450
- operator: string,
451
- value: any,
452
- paramKey: string,
453
- ): JsonbCondition | null {
454
- // Dates stored as epoch milliseconds (bigint)
455
- const jsonbField = `(json_data->>'${flatJsonKey}')::bigint`;
456
-
457
- // Helper: Convert date string to epoch ms
458
- const toEpochMs = (dateStr: string): number => {
459
- return new Date(dateStr).getTime();
460
- };
461
-
462
- // Helper: Get date N days ago
463
- const daysAgo = (days: number): number => {
464
- const d = new Date();
465
- d.setDate(d.getDate() - days);
466
- return d.getTime();
467
- };
468
-
469
- // Helper: Get date N days from now
470
- const daysFromNow = (days: number): number => {
471
- const d = new Date();
472
- d.setDate(d.getDate() + days);
473
- return d.getTime();
474
- };
475
-
476
- // Helper: Subtract business days (skip weekends)
477
- const subtractBusinessDays = (days: number): number => {
478
- let d = new Date();
479
- let count = 0;
480
-
481
- while (count < days) {
482
- d.setDate(d.getDate() - 1);
483
- const day = d.getDay(); // 0=Sun, 6=Sat
484
- if (day !== 0 && day !== 6) {
485
- count++;
486
- }
487
- }
488
-
489
- return d.getTime();
490
- };
491
-
492
- const numVal = Number(value);
493
-
494
- switch (operator) {
495
- // Basic comparisons
496
- case 'equal':
497
- case 'is':
498
- return {
499
- query: `${jsonbField} = :${paramKey}`,
500
- params: { [paramKey]: toEpochMs(value) },
501
- };
502
-
503
- case 'before':
504
- case 'is_before':
505
- return {
506
- query: `${jsonbField} < :${paramKey}`,
507
- params: { [paramKey]: toEpochMs(value) },
508
- };
509
-
510
- case 'after':
511
- case 'is_after':
512
- return {
513
- query: `${jsonbField} > :${paramKey}`,
514
- params: { [paramKey]: toEpochMs(value) },
515
- };
516
-
517
- case 'is_on_or_before':
518
- return {
519
- query: `${jsonbField} <= :${paramKey}`,
520
- params: { [paramKey]: toEpochMs(value) },
521
- };
522
-
523
- case 'is_on_or_after':
524
- return {
525
- query: `${jsonbField} >= :${paramKey}`,
526
- params: { [paramKey]: toEpochMs(value) },
527
- };
528
-
529
- // Day offset logic
530
- case 'is_day_before':
531
- if (isNaN(numVal)) return null;
532
- return {
533
- query: `${jsonbField} <= :${paramKey}`,
534
- params: { [paramKey]: daysAgo(numVal) },
535
- };
536
-
537
- case 'is_day_after':
538
- if (isNaN(numVal)) return null;
539
- return {
540
- query: `${jsonbField} >= :${paramKey}`,
541
- params: { [paramKey]: daysFromNow(numVal) },
542
- };
543
-
544
- // Business days (skip weekends)
545
- case 'is_before_business_days':
546
- if (isNaN(numVal)) return null;
547
- return {
548
- query: `${jsonbField} <= :${paramKey}`,
549
- params: { [paramKey]: subtractBusinessDays(numVal) },
550
- };
551
-
552
- // Range operators
553
- case 'between': {
554
- let range: string[];
555
- if (typeof value === 'string') {
556
- range = value.split(',').map((v) => v.trim());
557
- } else if (Array.isArray(value)) {
558
- range = value;
559
- } else {
560
- return null;
561
- }
562
-
563
- if (range.length !== 2) return null;
564
-
565
- return {
566
- query: `${jsonbField} BETWEEN :${paramKey}_start AND :${paramKey}_end`,
567
- params: {
568
- [`${paramKey}_start`]: toEpochMs(range[0]),
569
- [`${paramKey}_end`]: toEpochMs(range[1]),
570
- },
571
- };
572
- }
573
-
574
- case 'in_last_day':
575
- if (isNaN(numVal)) return null;
576
- return {
577
- query: `${jsonbField} BETWEEN :${paramKey}_start AND :${paramKey}_end`,
578
- params: {
579
- [`${paramKey}_start`]: daysAgo(numVal),
580
- [`${paramKey}_end`]: Date.now(),
581
- },
582
- };
583
-
584
- case 'in_next_day':
585
- if (isNaN(numVal)) return null;
586
- return {
587
- query: `${jsonbField} BETWEEN :${paramKey}_start AND :${paramKey}_end`,
588
- params: {
589
- [`${paramKey}_start`]: Date.now(),
590
- [`${paramKey}_end`]: daysFromNow(numVal),
591
- },
592
- };
593
-
594
- // Special cases
595
- case 'today': {
596
- const todayStart = new Date().setHours(0, 0, 0, 0);
597
- const todayEnd = new Date().setHours(23, 59, 59, 999);
598
- return {
599
- query: `${jsonbField} BETWEEN :${paramKey}_start AND :${paramKey}_end`,
600
- params: {
601
- [`${paramKey}_start`]: todayStart,
602
- [`${paramKey}_end`]: todayEnd,
603
- },
604
- };
605
- }
606
-
607
- case 'empty':
608
- return {
609
- query: `json_data->>'${flatJsonKey}' IS NULL`,
610
- params: {},
611
- };
612
-
613
- case 'not_empty':
614
- return {
615
- query: `json_data->>'${flatJsonKey}' IS NOT NULL`,
616
- params: {},
617
- };
618
-
619
- default:
620
- console.warn(`Unsupported date operator: ${operator}`);
621
- return null;
622
- }
623
- }
624
-
625
- /**
626
- * Build select condition (single value)
627
- */
628
- private buildSelectCondition(
629
- flatJsonKey: string,
630
- operator: string,
631
- value: any,
632
- paramKey: string,
633
- ): JsonbCondition | null {
634
- switch (operator) {
635
- case 'equal':
636
- if (Array.isArray(value)) {
637
- // IN operator for multiple values
638
- return {
639
- query: `json_data->>'${flatJsonKey}' = ANY(:${paramKey})`,
640
- params: { [paramKey]: value.map(String) },
641
- };
642
- }
643
- return {
644
- query: `json_data->>'${flatJsonKey}' = :${paramKey}`,
645
- params: { [paramKey]: String(value) },
646
- };
647
-
648
- case 'not_equal':
649
- if (Array.isArray(value)) {
650
- // NOT IN operator
651
- return {
652
- query: `json_data->>'${flatJsonKey}' != ALL(:${paramKey})`,
653
- params: { [paramKey]: value.map(String) },
654
- };
655
- }
656
- return {
657
- query: `json_data->>'${flatJsonKey}' != :${paramKey}`,
658
- params: { [paramKey]: String(value) },
659
- };
660
-
661
- case 'in': {
662
- const inValues = Array.isArray(value) ? value : [value];
663
- return {
664
- query: `json_data->>'${flatJsonKey}' = ANY(:${paramKey})`,
665
- params: { [paramKey]: inValues.map(String) },
666
- };
667
- }
668
-
669
- case 'not_in': {
670
- const notInValues = Array.isArray(value) ? value : [value];
671
- return {
672
- query: `json_data->>'${flatJsonKey}' != ALL(:${paramKey})`,
673
- params: { [paramKey]: notInValues.map(String) },
674
- };
675
- }
676
-
677
- case 'empty':
678
- return {
679
- query: `json_data->>'${flatJsonKey}' IS NULL`,
680
- params: {},
681
- };
682
-
683
- case 'not_empty':
684
- return {
685
- query: `json_data->>'${flatJsonKey}' IS NOT NULL`,
686
- params: {},
687
- };
688
-
689
- default:
690
- console.warn(`Unsupported select operator: ${operator}`);
691
- return null;
692
- }
693
- }
694
-
695
- /**
696
- * Build multiselect condition (JSONB -> with ? operators for arrays)
697
- */
698
- private buildMultiSelectCondition(
699
- flatJsonKey: string,
700
- operator: string,
701
- value: any,
702
- paramKey: string,
703
- ): JsonbCondition | null {
704
- // Convert value to array if not already
705
- let arr: string[];
706
- if (Array.isArray(value)) {
707
- arr = value.map(String);
708
- } else if (typeof value === 'string') {
709
- arr = value.split(',').map((v) => v.trim());
710
- } else {
711
- arr = [String(value)];
712
- }
713
-
714
- if (arr.length === 0) {
715
- return { query: '1=1', params: {} }; // Always true for empty array
716
- }
717
-
718
- switch (operator) {
719
- case 'contains':
720
- case 'has':
721
- // Check if JSON array contains this element
722
- // json_data->'LEAD__languages' ? 'hindi'
723
- if (arr.length === 1) {
724
- return {
725
- query: `json_data->'${flatJsonKey}' ? :${paramKey}`,
726
- params: { [paramKey]: arr[0] },
727
- };
728
- }
729
- // For multiple values, check if contains ANY
730
- return {
731
- query: `json_data->'${flatJsonKey}' ?| ARRAY[:...${paramKey}]::text[]`,
732
- params: { [paramKey]: arr },
733
- };
734
-
735
- case 'contains_all':
736
- // Check if JSON array contains ALL elements
737
- // json_data->'LEAD__languages' ?& ARRAY['hindi', 'english']
738
- return {
739
- query: `json_data->'${flatJsonKey}' ?& ARRAY[:...${paramKey}]::text[]`,
740
- params: { [paramKey]: arr },
741
- };
742
-
743
- case 'contains_any':
744
- // Check if JSON array contains ANY element
745
- // json_data->'LEAD__languages' ?| ARRAY['hindi', 'english']
746
- return {
747
- query: `json_data->'${flatJsonKey}' ?| ARRAY[:...${paramKey}]::text[]`,
748
- params: { [paramKey]: arr },
749
- };
750
-
751
- case 'not_contains':
752
- // Check if JSON array does NOT contain element
753
- if (arr.length === 1) {
754
- return {
755
- query: `NOT (json_data->'${flatJsonKey}' ? :${paramKey})`,
756
- params: { [paramKey]: arr[0] },
757
- };
758
- }
759
- return {
760
- query: `NOT (json_data->'${flatJsonKey}' ?| ARRAY[:...${paramKey}]::text[])`,
761
- params: { [paramKey]: arr },
762
- };
763
-
764
- case 'equal':
765
- // Exact array match (order matters in PostgreSQL)
766
- return {
767
- query: `json_data->'${flatJsonKey}'::jsonb = :${paramKey}::jsonb`,
768
- params: { [paramKey]: JSON.stringify(arr) },
769
- };
770
-
771
- case 'empty':
772
- return {
773
- query: `(json_data->>'${flatJsonKey}' IS NULL OR json_data->'${flatJsonKey}' = '[]'::jsonb)`,
774
- params: {},
775
- };
776
-
777
- case 'not_empty':
778
- return {
779
- query: `(json_data->>'${flatJsonKey}' IS NOT NULL AND json_data->'${flatJsonKey}' != '[]'::jsonb)`,
780
- params: {},
781
- };
782
-
783
- default:
784
- console.warn(`Unsupported multiselect operator: ${operator}`);
785
- return null;
786
- }
787
- }
788
-
789
- /**
790
- * Build year condition
791
- */
792
- private buildYearCondition(
793
- flatJsonKey: string,
794
- operator: string,
795
- value: any,
796
- paramKey: string,
797
- ): JsonbCondition | null {
798
- // Similar to number condition
799
- return this.buildNumberCondition(flatJsonKey, operator, value, paramKey);
800
- }
801
-
802
- /**
803
- * Get tab aggregation counts from JSONB
804
- */
805
- private async getJsonbTabCounts(
806
- entity_type: string,
807
- flatJsonKey: string,
808
- whereClauses: JsonbCondition[],
809
- ): Promise<Array<{ tab_value: string; tab_value_count: number }>> {
810
- // Build a query that groups by the tab field
811
- const qb = this.entityManager
812
- .getRepository(EntityJson)
813
- .createQueryBuilder('ej')
814
- .select(`json_data->>'${flatJsonKey}'`, 'tab_value')
815
- .addSelect('COUNT(*)', 'tab_value_count')
816
- .where('ej.entity_type = :entity_type', { entity_type })
817
- .groupBy(`json_data->>'${flatJsonKey}'`);
818
-
819
- // Apply the same filter conditions (but not the tab filter itself)
820
- whereClauses.forEach((condition) => {
821
- qb.andWhere(condition.query, condition.params);
822
- });
823
-
824
- const results = await qb.getRawMany();
825
-
826
- // Filter out NULL/undefined tab values and convert count to number
827
- return results
828
- .filter((r) => r.tab_value != null && r.tab_value !== '')
829
- .map((r) => ({
830
- tab_value: r.tab_value,
831
- tab_value_count: parseInt(r.tab_value_count, 10),
832
- }))
833
- .sort((a, b) => b.tab_value_count - a.tab_value_count); // Sort by count DESC
834
- }
835
-
836
- /**
837
- * Apply sorting on JSONB fields
838
- */
839
- private applyJsonbSorting(
840
- qb: SelectQueryBuilder<EntityJson>,
841
- sortby: SortConfig[],
842
- attributeMetaMap: Record<string, any>,
843
- ): void {
844
- sortby.forEach((sort, index) => {
845
- const meta = attributeMetaMap[sort.sortColum];
846
-
847
- if (!meta) {
848
- console.warn(`No metadata found for sort column: ${sort.sortColum}`);
849
- return;
850
- }
851
-
852
- const flatJsonKey =
853
- meta.flat_json_key || `${meta.mapped_entity_type}__${sort.sortColum}`;
854
- const direction = sort.sortType === 'ASC' ? 'ASC' : 'DESC';
855
-
856
- // Apply type-specific casting for proper sorting
857
- let sortExpression: string;
858
-
859
- switch (meta.data_type) {
860
- case 'number':
861
- case 'year':
862
- // Cast to numeric for number sorting
863
- sortExpression = `(json_data->>'${flatJsonKey}')::numeric`;
864
- break;
865
-
866
- case 'date':
867
- // Cast to bigint for date sorting (epoch ms)
868
- sortExpression = `(json_data->>'${flatJsonKey}')::bigint`;
869
- break;
870
-
871
- case 'text':
872
- case 'select':
873
- case 'radio':
874
- default:
875
- // Text sorting (already lowercase)
876
- sortExpression = `json_data->>'${flatJsonKey}'`;
877
- break;
878
- }
879
-
880
- // Add to ORDER BY clause
881
- if (index === 0) {
882
- qb.orderBy(sortExpression, direction);
883
- } else {
884
- qb.addOrderBy(sortExpression, direction);
885
- }
886
- });
887
- }
888
- }