rez_core 1.1.2 → 1.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -72,6 +72,7 @@ export class SavedFilterRepositoryService {
72
72
  where: {
73
73
  user_id: userId,
74
74
  mapped_entity_type: entityType,
75
+ status: 'ACTIVE',
75
76
  },
76
77
  order: {
77
78
  modified_date: 'DESC',
@@ -84,9 +85,7 @@ export class SavedFilterRepositoryService {
84
85
  }));
85
86
  }
86
87
 
87
- async getFilterById(
88
- id: number,
89
- ): Promise<
88
+ async getFilterById(id: number): Promise<
90
89
  {
91
90
  filter_attribute: string;
92
91
  filter_operator: string;
@@ -23,28 +23,47 @@ export class FilterService {
23
23
  private async gettab_value_counts(
24
24
  tableName: string,
25
25
  column: string | undefined,
26
- whereClauses: any[],
26
+ whereClauses: { query: string; params: Record<string, any> }[],
27
27
  ) {
28
28
  if (!column) return [];
29
29
 
30
- const qb = this.dataSource
31
- .getRepository(tableName)
32
- .createQueryBuilder('e')
33
- .select(`e.${column}`, 'tab_value')
34
- .addSelect('COUNT(*)', 'tab_value_count')
35
- .groupBy(`e.${column}`);
30
+ let whereSQL = '';
31
+ const values: any[] = [];
32
+
33
+ // Convert whereClauses to SQL
34
+ if (whereClauses.length > 0) {
35
+ const clauseParts = whereClauses.map((clause) => {
36
+ let parsedQuery = clause.query.replace(/\be\./g, ''); // removes e. prefix
37
+ Object.entries(clause.params).forEach(([key, val]) => {
38
+ parsedQuery = parsedQuery.replace(new RegExp(`:${key}`, 'g'), '?');
39
+ values.push(val);
40
+ });
41
+ return parsedQuery;
42
+ });
36
43
 
37
- whereClauses.forEach((clause) => qb.andWhere(clause.query, clause.params));
44
+ whereSQL = `WHERE ${clauseParts.join(' AND ')}`;
45
+ }
38
46
 
39
- const rows = await qb.getRawMany();
47
+ // Construct raw SQL
48
+ const rawSQL = `
49
+ SELECT ${column} AS tab_value, COUNT(*) AS tab_value_count
50
+ FROM ${tableName}
51
+ ${whereSQL}
52
+ GROUP BY ${column}
53
+ `;
40
54
 
41
- const total = rows.reduce((sum, r) => sum + parseInt(r.tab_value_count), 0);
55
+ const rows = await this.dataSource.query(rawSQL, values);
56
+
57
+ const total = rows.reduce(
58
+ (sum, r) => sum + parseInt(r.tab_value_count, 10),
59
+ 0,
60
+ );
42
61
 
43
62
  return [
44
63
  { tab_value: 'All', tab_value_count: total },
45
64
  ...rows.map((r) => ({
46
- tab_value: r.tab_value || 'UNKNOWN',
47
- tab_value_count: parseInt(r.tab_value_count),
65
+ tab_value: r.tab_value ?? 'UNKNOWN',
66
+ tab_value_count: parseInt(r.tab_value_count, 10),
48
67
  })),
49
68
  ];
50
69
  }
@@ -131,10 +150,16 @@ export class FilterService {
131
150
  }
132
151
  }
133
152
 
134
- // Final query for filtered data
135
- // Final query for filtered data
136
- const qb = this.dataSource.getRepository(tableName).createQueryBuilder('e');
137
- dataWhere.forEach((clause) => qb.andWhere(clause.query, clause.params));
153
+ // Build raw SQL base
154
+ const qb = this.dataSource
155
+ .createQueryBuilder()
156
+ .select('e.*')
157
+ .from(tableName, 'e');
158
+
159
+ // Apply WHERE clauses
160
+ dataWhere.forEach((clause) => {
161
+ qb.andWhere(clause.query, clause.params);
162
+ });
138
163
 
139
164
  // Optional sorting
140
165
  if (Array.isArray(sortby)) {
@@ -151,7 +176,19 @@ export class FilterService {
151
176
  const size = dto.size && dto.size > 0 ? dto.size : 10;
152
177
  qb.skip((page - 1) * size).take(size);
153
178
 
154
- const [entity_list, total] = await qb.getManyAndCount();
179
+ // Get paginated data
180
+ const entity_list = await qb.getRawMany();
181
+
182
+ // Count query (without pagination)
183
+ const countQb = this.dataSource
184
+ .createQueryBuilder()
185
+ .select('COUNT(*)', 'count')
186
+ .from(tableName, 'e');
187
+ dataWhere.forEach((clause) => {
188
+ countQb.andWhere(clause.query, clause.params);
189
+ });
190
+ const countResult = await countQb.getRawOne();
191
+ const total = parseInt(countResult.count, 10);
155
192
 
156
193
  return {
157
194
  success: true,
@@ -192,8 +229,6 @@ export class FilterService {
192
229
  if (!savedFilter) {
193
230
  throw new BadRequestException(`Saved filter not found for code: ${code}`);
194
231
  }
195
-
196
- console.log(savedFilter);
197
232
  return savedFilter;
198
233
  }
199
234
 
@@ -300,6 +335,7 @@ export class FilterService {
300
335
  }
301
336
  }
302
337
 
338
+ // this can be inproved
303
339
  private buildSelectCondition(
304
340
  attr: string,
305
341
  op: string,
@@ -308,16 +344,16 @@ export class FilterService {
308
344
  ) {
309
345
  switch (op) {
310
346
  case 'equal':
311
- return { query: `e.${attr} = :${key}`, params: { [key]: val } };
347
+ return { query: `e.${attr}_id = :${key}`, params: { [key]: val } };
312
348
 
313
349
  case 'not_equal':
314
- return { query: `e.${attr} != :${key}`, params: { [key]: val } };
350
+ return { query: `e.${attr}_id != :${key}`, params: { [key]: val } };
315
351
 
316
352
  case 'empty':
317
- return { query: `e.${attr} IS NULL`, params: {} };
353
+ return { query: `e.${attr}_id IS NULL`, params: {} };
318
354
 
319
355
  case 'not_empty':
320
- return { query: `e.${attr} IS NOT NULL`, params: {} };
356
+ return { query: `e.${attr}_id IS NOT NULL`, params: {} };
321
357
 
322
358
  default:
323
359
  throw new BadRequestException(`Unsupported operator for select: ${op}`);
@@ -325,41 +361,41 @@ export class FilterService {
325
361
  }
326
362
 
327
363
  private buildMultiSelectCondition(
328
- attr: string,
329
- op: string,
330
- val: any,
331
- key: string,
332
- ) {
333
- switch (op) {
334
- case 'equal':
335
- if (!Array.isArray(val)) {
336
- throw new BadRequestException(
337
- `Value for multi-select must be an array for operator: ${op}`,
338
- );
339
- }
340
- return { query: `e.${attr} IN (:...${key})`, params: { [key]: val } };
364
+ attr: string,
365
+ op: string,
366
+ val: any,
367
+ key: string,
368
+ ) {
369
+ if ((op === 'equal' || op === 'not_equal') && !Array.isArray(val)) {
370
+ throw new BadRequestException(
371
+ `Value for multi-select must be an array for operator: ${op}`,
372
+ );
373
+ }
341
374
 
342
- case 'not_equal':
343
- if (!Array.isArray(val)) {
344
- throw new BadRequestException(
345
- `Value for multi-select must be an array for operator: ${op}`,
346
- );
347
- }
348
- return {
349
- query: `e.${attr} NOT IN (:...${key})`,
350
- params: { [key]: val },
351
- };
375
+ switch (op) {
376
+ case 'equal':
377
+ return {
378
+ query: `e.${attr}_id IN (:${key})`, // use (:key)
379
+ params: { [key]: val },
380
+ };
352
381
 
353
- case 'empty':
354
- return { query: `e.${attr} IS NULL`, params: {} };
382
+ case 'not_equal':
383
+ return {
384
+ query: `e.${attr}_id NOT IN (:${key})`, // ✅ use (:key)
385
+ params: { [key]: val },
386
+ };
355
387
 
356
- case 'not_empty':
357
- return { query: `e.${attr} IS NOT NULL`, params: {} };
388
+ case 'empty':
389
+ return { query: `e.${attr}_id IS NULL`, params: {} };
358
390
 
359
- default:
360
- throw new BadRequestException(
361
- `Unsupported operator for multiselect: ${op}`,
362
- );
363
- }
391
+ case 'not_empty':
392
+ return { query: `e.${attr}_id IS NOT NULL`, params: {} };
393
+
394
+ default:
395
+ throw new BadRequestException(
396
+ `Unsupported operator for multiselect: ${op}`,
397
+ );
364
398
  }
365
399
  }
400
+
401
+ }