rez_core 5.0.11 → 5.0.12

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": "5.0.11",
3
+ "version": "5.0.12",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -1,15 +1,10 @@
1
- import { level } from 'winston';
2
1
  import { BadRequestException, Injectable } from '@nestjs/common';
3
- import {
4
- ENTITYTYPE_ENTITYMASTER,
5
- STATUS_ACTIVE,
6
- } from 'src/constant/global.constant';
2
+ import { STATUS_ACTIVE } from 'src/constant/global.constant';
7
3
  import { DataSource } from 'typeorm';
8
4
  import { UserData } from 'src/module/user/entity/user.entity';
9
5
  import { MediaDataService } from './media-data.service';
10
6
  import { ResolverService } from './resolver.service';
11
7
  import { ConfigService } from '@nestjs/config';
12
- import { EntityMasterService } from './entity-master.service';
13
8
  import { EntityMasterRepository } from '../repository/entity-master.repository';
14
9
 
15
10
  @Injectable()
@@ -20,7 +15,8 @@ export class EntityDynamicService {
20
15
  private readonly resolverService: ResolverService,
21
16
  private readonly configService: ConfigService,
22
17
  private readonly entityMasterRepo: EntityMasterRepository,
23
- ) {}
18
+ ) {
19
+ }
24
20
 
25
21
  // -----------------------------
26
22
  async createEntity(
@@ -38,7 +34,10 @@ export class EntityDynamicService {
38
34
  );
39
35
 
40
36
  const statusList = await this.dataSource.query(
41
- `SELECT id FROM frm_list_master_items WHERE code = $1 AND organization_id = $2`,
37
+ `SELECT id
38
+ FROM frm_list_master_items
39
+ WHERE code = $1
40
+ AND organization_id = $2`,
42
41
  [STATUS_ACTIVE, loggedInUser?.organization_id || 0],
43
42
  );
44
43
 
@@ -59,8 +58,8 @@ export class EntityDynamicService {
59
58
  if (!entityData.code && loggedInUser) {
60
59
  const result = await this.dataSource.query(
61
60
  `SELECT MAX(CAST(SUBSTRING(code, LENGTH(entity_type) + 1) AS UNSIGNED)) AS max_seq_no
62
- FROM ${tableName}
63
- WHERE entity_type = $1`,
61
+ FROM ${tableName}
62
+ WHERE entity_type = $1`,
64
63
  [entityData.entity_type],
65
64
  );
66
65
 
@@ -142,7 +141,8 @@ export class EntityDynamicService {
142
141
 
143
142
  const placeholders = columns.map(() => '?').join(', ');
144
143
  const escapedColumns = columns.map((col) => `\`${col}\``).join(', ');
145
- const insertQuery = `INSERT INTO \`${tableName}\` (${escapedColumns}) VALUES (${placeholders})`;
144
+ const insertQuery = `INSERT INTO ${tableName} (${escapedColumns})
145
+ VALUES (${placeholders})`;
146
146
 
147
147
  const result = await this.dataSource.query(insertQuery, values);
148
148
  return result;
@@ -159,7 +159,10 @@ export class EntityDynamicService {
159
159
  const organizationId = loggedInUser.organization_id;
160
160
 
161
161
  const getRelation = await this.dataSource.query(
162
- `SELECT * FROM frm_entity_relation WHERE organization_id = $1 AND source_entity_type = $2`,
162
+ `SELECT *
163
+ FROM frm_entity_relation
164
+ WHERE organization_id = $1
165
+ AND source_entity_type = $2`,
163
166
  [organizationId, entityType],
164
167
  );
165
168
 
@@ -200,7 +203,9 @@ export class EntityDynamicService {
200
203
  );
201
204
 
202
205
  await this.dataSource.query(
203
- `INSERT INTO frm_entity_relation_data (organization_id, source_entity_id, source_entity_type, target_entity_id, target_entity_type, relation_type) VALUES ($1, $2, $3, $4, $5, $6)`,
206
+ `INSERT INTO frm_entity_relation_data (organization_id, source_entity_id, source_entity_type,
207
+ target_entity_id, target_entity_type, relation_type)
208
+ VALUES ($1, $2, $3, $4, $5, $6)`,
204
209
  [
205
210
  organizationId,
206
211
  mainID,
@@ -236,8 +241,10 @@ export class EntityDynamicService {
236
241
  );
237
242
 
238
243
  const relatedEntities = await this.dataSource.query(
239
- `SELECT * FROM frm_entity_relation_data WHERE source_entity_id = $1 AND target_entity_type IN (
240
- SELECT target_entity_type FROM frm_entity_relation WHERE source_entity_type = $2)`,
244
+ `SELECT *
245
+ FROM frm_entity_relation_data
246
+ WHERE source_entity_id = $1
247
+ AND target_entity_type IN (SELECT target_entity_type FROM frm_entity_relation WHERE source_entity_type = $2)`,
241
248
  [id, entityType],
242
249
  );
243
250
 
@@ -308,7 +315,10 @@ export class EntityDynamicService {
308
315
 
309
316
  if (mappedEntities) {
310
317
  const getRelationDefs = await this.dataSource.query(
311
- `SELECT * FROM frm_entity_relation WHERE organization_id = $1 AND source_entity_type = $2`,
318
+ `SELECT *
319
+ FROM frm_entity_relation
320
+ WHERE organization_id = $1
321
+ AND source_entity_type = $2`,
312
322
  [organizationId, entityType],
313
323
  );
314
324
 
@@ -327,7 +337,11 @@ export class EntityDynamicService {
327
337
 
328
338
  // Delete previous relations and related entities
329
339
  const existingRelationsForType = await this.dataSource.query(
330
- `SELECT * FROM frm_entity_relation_data WHERE source_entity_type = $1 AND source_entity_id = $2 AND target_entity_type = $3`,
340
+ `SELECT *
341
+ FROM frm_entity_relation_data
342
+ WHERE source_entity_type = $1
343
+ AND source_entity_id = $2
344
+ AND target_entity_type = $3`,
331
345
  [entityType, id, targetEntityType],
332
346
  );
333
347
 
@@ -384,9 +398,10 @@ export class EntityDynamicService {
384
398
 
385
399
  // Insert relation as per new entity created
386
400
  await this.dataSource.query(
387
- `INSERT INTO frm_entity_relation_data
388
- (organization_id, source_entity_id, source_entity_type, target_entity_id, target_entity_type, relation_type)
389
- VALUES ($1, $2, $3, $4, $5, $6)`,
401
+ `INSERT INTO frm_entity_relation_data
402
+ (organization_id, source_entity_id, source_entity_type, target_entity_id, target_entity_type,
403
+ relation_type)
404
+ VALUES ($1, $2, $3, $4, $5, $6)`,
390
405
  [
391
406
  organizationId,
392
407
  id,
@@ -435,100 +450,59 @@ export class EntityDynamicService {
435
450
  const organizationId = loggedInUser.organization_id;
436
451
 
437
452
  const tableName = await this.getTableName(entityType, organizationId);
438
- const validAttributes = await this.getAttributeCodes(
439
- entityType,
440
- organizationId,
441
- );
453
+ const validAttributes = await this.getAttributeCodes(entityType, organizationId);
442
454
 
443
455
  const updates: string[] = [];
444
456
  const values: any[] = [];
457
+ let idx = 1;
445
458
 
459
+ // Auto fields
446
460
  entityData.modified_date = new Date();
447
461
  if (loggedInUser) {
448
462
  entityData.modified_by = loggedInUser.id;
463
+
449
464
  if (!entityData.organization_id && entityData.entity_type !== 'ORG')
450
465
  entityData.organization_id = loggedInUser.organization_id;
466
+
451
467
  if (!entityData.enterprise_id)
452
468
  entityData.enterprise_id = loggedInUser.enterprise_id;
453
469
  }
454
470
 
455
- // Set parent_id if mainID is provided
456
471
  if (mainID) {
457
472
  entityData.parent_id = mainID;
458
473
  }
459
474
 
460
- const bypassColumn = [
461
- {
462
- attribute_key: 'created_date',
463
- db_datatype: 'datetime',
464
- element_type: 'date',
465
- is_hidden: false,
466
- },
467
- {
468
- attribute_key: 'created_by',
469
- db_datatype: 'int',
470
- element_type: 'number',
471
- is_hidden: false,
472
- },
473
- {
474
- attribute_key: 'modified_date',
475
- db_datatype: 'datetime',
476
- element_type: 'date',
477
- is_hidden: false,
478
- },
479
- {
480
- attribute_key: 'modified_by',
481
- db_datatype: 'int',
482
- element_type: 'number',
483
- is_hidden: false,
484
- },
485
- {
486
- attribute_key: 'organization_id',
487
- db_datatype: 'int',
488
- element_type: 'number',
489
- is_hidden: false,
490
- },
491
- {
492
- attribute_key: 'enterprise_id',
493
- db_datatype: 'int',
494
- element_type: 'number',
495
- is_hidden: false,
496
- },
497
- {
498
- attribute_key: 'level_type',
499
- db_datatype: 'varchar',
500
- element_type: 'text',
501
- is_hidden: false,
502
- },
503
- { attribute_key: 'level_id', db_datatype: 'int', element_type: 'number', is_hidden: false },
504
- { attribute_key: 'status', db_datatype: 'varchar', element_type: 'text', is_hidden: false },
505
- {
506
- attribute_key: 'entity_type',
507
- db_datatype: 'varchar',
508
- element_type: 'text',
509
- is_hidden: false,
510
- },
511
- { attribute_key: 'code', db_datatype: 'varchar', element_type: 'text', is_hidden: false },
512
- {
513
- attribute_key: 'parent_id',
514
- db_datatype: 'int',
515
- element_type: 'number',
516
- is_hidden: false,
517
- },
475
+ // Add bypass columns if needed
476
+ const bypassColumns = [
477
+ 'created_date',
478
+ 'created_by',
479
+ 'modified_date',
480
+ 'modified_by',
481
+ 'organization_id',
482
+ 'enterprise_id',
483
+ 'level_type',
484
+ 'level_id',
485
+ 'status',
486
+ 'entity_type',
487
+ 'code',
488
+ 'parent_id',
518
489
  ];
519
490
 
520
- // Only push bypassColumn attributes that don't already exist in validAttributes
521
- const existingAttributeKeys = validAttributes.map(
522
- (attr) => attr.attribute_key,
523
- );
524
- const newBypassColumns = bypassColumn.filter(
525
- (bypass) => !existingAttributeKeys.includes(bypass.attribute_key),
526
- );
527
- validAttributes.push(...newBypassColumns);
491
+ for (const col of bypassColumns) {
492
+ if (!validAttributes.some((attr) => attr.attribute_key === col)) {
493
+ validAttributes.push({
494
+ attribute_key: col,
495
+ db_datatype: 'text',
496
+ element_type: 'text',
497
+ is_hidden: false,
498
+ });
499
+ }
500
+ }
528
501
 
502
+ // Build SET clause
529
503
  for (const key of Object.keys(entityData)) {
530
504
  if (validAttributes.some((attr) => attr.attribute_key === key)) {
531
- updates.push(`\`${key}\` = ?`);
505
+ updates.push(`${key} = $${idx++}`);
532
506
  values.push(entityData[key]);
533
507
  }
534
508
  }
@@ -537,11 +511,17 @@ export class EntityDynamicService {
537
511
  throw new Error('No valid attributes to update.');
538
512
  }
539
513
 
540
- const updateQuery = `UPDATE \`${tableName}\` SET ${updates.join(', ')} WHERE id = $1`;
541
- values.push(id); // Add id for WHERE clause
514
+ // WHERE clause placeholder
515
+ const idPlaceholder = `$${idx}`;
516
+ values.push(id);
542
517
 
543
- const result = await this.dataSource.query(updateQuery, values);
544
- return result;
518
+ const updateQuery = `
519
+ UPDATE ${tableName}
520
+ SET ${updates.join(', ')}
521
+ WHERE id = ${idPlaceholder}
522
+ `;
523
+
524
+ return await this.dataSource.query(updateQuery, values);
545
525
  }
546
526
 
547
527
  async getEntityByDataSource(
@@ -564,7 +544,9 @@ export class EntityDynamicService {
564
544
  const columns = validAttributes
565
545
  .map((attr) => `${attr.attribute_key}`)
566
546
  .join(', ');
567
- const selectQuery = `SELECT ${columns} FROM ${dataSource} WHERE id = $1`;
547
+ const selectQuery = `SELECT ${columns}
548
+ FROM ${dataSource}
549
+ WHERE id = $1`;
568
550
 
569
551
  const result = await this.dataSource.query(selectQuery, [id]);
570
552
  if (!result.length) return null;
@@ -585,15 +567,16 @@ export class EntityDynamicService {
585
567
  row[attr.attribute_key] =
586
568
  row[attr.attribute_key] != null
587
569
  ? await this.mediaDataService.getMediaDownloadUrl(
588
- Number(row[attr.attribute_key]),
589
- loggedInUser,
590
- )
570
+ Number(row[attr.attribute_key]),
571
+ loggedInUser,
572
+ )
591
573
  : null;
592
574
  }
593
575
  }
594
576
 
595
577
  return row;
596
578
  }
579
+
597
580
  // -----------------------------
598
581
  //TODO : make it normal getEntity function make another function if for resolve data
599
582
  async getEntity(
@@ -616,7 +599,9 @@ export class EntityDynamicService {
616
599
  const columns = validAttributes
617
600
  .map((attr) => `${attr.attribute_key}`)
618
601
  .join(', ');
619
- const selectQuery = `SELECT ${columns} FROM ${tableName} WHERE id = $1`;
602
+ const selectQuery = `SELECT ${columns}
603
+ FROM ${tableName}
604
+ WHERE id = $1`;
620
605
 
621
606
  const result = await this.dataSource.query(selectQuery, [id]);
622
607
  if (!result.length) return null;
@@ -637,9 +622,9 @@ export class EntityDynamicService {
637
622
  row[attr.attribute_key] =
638
623
  row[attr.attribute_key] != null
639
624
  ? await this.mediaDataService.getMediaDownloadUrl(
640
- Number(row[attr.attribute_key]),
641
- loggedInUser,
642
- )
625
+ Number(row[attr.attribute_key]),
626
+ loggedInUser,
627
+ )
643
628
  : null;
644
629
  }
645
630
  }
@@ -718,7 +703,9 @@ export class EntityDynamicService {
718
703
 
719
704
  const tableName = await this.getTableName(entityType, organizationId);
720
705
 
721
- const deleteQuery = `DELETE FROM \`${tableName}\` WHERE id = $1`;
706
+ const deleteQuery = `DELETE
707
+ FROM \`${tableName}\`
708
+ WHERE id = $1`;
722
709
  const result = await this.dataSource.query(deleteQuery, [id]);
723
710
  return result;
724
711
  }
@@ -731,7 +718,9 @@ export class EntityDynamicService {
731
718
  ): Promise<any> {
732
719
  const organizationId = loggedInUser.organization_id;
733
720
 
734
- let query = `SELECT name as label,mapped_entity_type as value FROM frm_entity_master WHERE organization_id = $1`;
721
+ let query = `SELECT name as label, mapped_entity_type as value
722
+ FROM frm_entity_master
723
+ WHERE organization_id = $1`;
735
724
  const params = [organizationId];
736
725
 
737
726
  if (appcode) {
@@ -748,9 +737,10 @@ export class EntityDynamicService {
748
737
 
749
738
  // 1. Get db_table_name from entity master
750
739
  const result = await this.dataSource.query(
751
- `SELECT db_table_name
752
- FROM frm_entity_master
753
- WHERE mapped_entity_type = $1 AND organization_id = $2`,
740
+ `SELECT db_table_name
741
+ FROM frm_entity_master
742
+ WHERE mapped_entity_type = $1
743
+ AND organization_id = $2`,
754
744
  [entityType, organizationId],
755
745
  );
756
746
 
@@ -765,8 +755,8 @@ export class EntityDynamicService {
765
755
  // 2. Get current max sequence number from that table
766
756
  const seqResult = await this.dataSource.query(
767
757
  `SELECT MAX(CAST(SUBSTRING(code, LENGTH(entity_type) + 1) AS UNSIGNED)) AS max_seq_no
768
- FROM \`${tableName}\`
769
- WHERE entity_type = $1`,
758
+ FROM \`${tableName}\`
759
+ WHERE entity_type = $1`,
770
760
  [entityType],
771
761
  );
772
762
 
@@ -824,7 +814,7 @@ export class EntityDynamicService {
824
814
 
825
815
  async queryWithSchema(sql: string, params: any[] = []) {
826
816
  await this.dataSource.query('BEGIN');
827
- const schema = this.configService.get<string>('DB_SCHEMA')
817
+ const schema = this.configService.get<string>('DB_SCHEMA');
828
818
  await this.dataSource.query(`SET LOCAL search_path TO ${schema}`);
829
819
  const result = await this.dataSource.query(sql, params);
830
820
  await this.dataSource.query('COMMIT');
@@ -102,9 +102,9 @@ export class EntityServiceImpl implements EntityService<BaseEntity> {
102
102
  const statusList = await repo.query(
103
103
  `SELECT id
104
104
  FROM frm_list_master_items
105
- WHERE code = ?
106
- AND organization_id = ?`,
107
- [STATUS_ACTIVE, loggedInUser?.organization_id || -1],
105
+ WHERE code = $1
106
+ AND organization_id = $2`,
107
+ [STATUS_ACTIVE, loggedInUser?.organization_id || 0],
108
108
  );
109
109
 
110
110
  console.log('Status List:', statusList); // Debug log