rez_core 5.0.12 → 5.0.14

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.12",
3
+ "version": "5.0.14",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -28,124 +28,124 @@ export class EntityDynamicService {
28
28
  const organizationId = loggedInUser.organization_id;
29
29
 
30
30
  const tableName = await this.getTableName(entityType, organizationId);
31
- const validAttributes = await this.getAttributeCodes(
32
- entityType,
33
- organizationId,
34
- );
35
-
36
- const statusList = await this.dataSource.query(
37
- `SELECT id
38
- FROM frm_list_master_items
39
- WHERE code = $1
40
- AND organization_id = $2`,
41
- [STATUS_ACTIVE, loggedInUser?.organization_id || 0],
42
- );
31
+ const validAttributes = await this.getAttributeCodes(entityType, organizationId);
43
32
 
33
+ // -------------------------------------------------------
34
+ // AUTO fields
35
+ // -------------------------------------------------------
44
36
  entityData.created_date = new Date();
37
+
45
38
  if (loggedInUser) {
46
39
  entityData.created_by = loggedInUser.id;
40
+
47
41
  if (!entityData.organization_id)
48
42
  entityData.organization_id = loggedInUser.organization_id;
43
+
49
44
  if (!entityData.enterprise_id)
50
45
  entityData.enterprise_id = loggedInUser.enterprise_id;
46
+
51
47
  if (!entityData.level_type)
52
48
  entityData.level_type = loggedInUser.level_type;
53
- if (!entityData.level_id) entityData.level_id = loggedInUser.level_id;
54
- if (!entityData.status) entityData.status = statusList[0].id;
55
- if (!entityData.entity_type) entityData.entity_type = entityType;
49
+
50
+ if (!entityData.level_id)
51
+ entityData.level_id = loggedInUser.level_id;
52
+
53
+ if (!entityData.entity_type)
54
+ entityData.entity_type = entityType;
56
55
  }
57
56
 
58
- if (!entityData.code && loggedInUser) {
57
+ // -------------------------------------------------------
58
+ // STATUS
59
+ // -------------------------------------------------------
60
+ const statusList = await this.dataSource.query(
61
+ `
62
+ SELECT id
63
+ FROM frm_list_master_items
64
+ WHERE code = $1
65
+ AND organization_id = $2
66
+ `,
67
+ [STATUS_ACTIVE, organizationId],
68
+ );
69
+
70
+ if (!entityData.status) entityData.status = statusList[0]?.id;
71
+
72
+ // -------------------------------------------------------
73
+ // AUTO-CODE GENERATION (POSTGRES SAFE)
74
+ // -------------------------------------------------------
75
+ if (!entityData.code && entityData.entity_type) {
76
+ // Extract integer suffix
59
77
  const result = await this.dataSource.query(
60
- `SELECT MAX(CAST(SUBSTRING(code, LENGTH(entity_type) + 1) AS UNSIGNED)) AS max_seq_no
61
- FROM ${tableName}
62
- WHERE entity_type = $1`,
78
+ `
79
+ SELECT MAX(id) AS max_seq_no
80
+ FROM ${tableName}
81
+ WHERE entity_type = $1
82
+ `,
63
83
  [entityData.entity_type],
64
84
  );
65
85
 
66
- // result will be like [ { max_seq_no: 12 } ]
67
- let maxSeqNo = result?.[0]?.max_seq_no ? Number(result[0].max_seq_no) : 0;
68
-
69
- maxSeqNo += 1;
70
- entityData.code = `${entityData.entity_type}${maxSeqNo}`;
86
+ let maxSeq = Number(result?.[0]?.max_seq_no) || 0;
87
+ maxSeq++;
88
+ entityData.code = `${entityData.entity_type}${maxSeq}`;
71
89
  }
72
90
 
73
- // Set parent_id if mainID is provided
91
+ // -------------------------------------------------------
92
+ // Parent ID
93
+ // -------------------------------------------------------
74
94
  if (mainID) {
75
95
  entityData.parent_id = mainID;
76
96
  }
77
97
 
78
- const bypassColumn = [
79
- {
80
- attribute_key: 'created_date',
81
- db_datatype: 'datetime',
82
- element_type: 'date',
83
- is_hidden: false,
84
- },
85
- {
86
- attribute_key: 'created_by',
87
- db_datatype: 'int',
88
- element_type: 'number',
89
- is_hidden: false,
90
- },
91
- {
92
- attribute_key: 'organization_id',
93
- db_datatype: 'datetime',
94
- element_type: 'date',
95
- is_hidden: false,
96
- },
97
- {
98
- attribute_key: 'enterprise_id',
99
- db_datatype: 'int',
100
- element_type: 'number',
101
- is_hidden: false,
102
- },
103
- { attribute_key: 'level_type', db_datatype: 'int', element_type: 'text', is_hidden: false },
104
- { attribute_key: 'level_id', db_datatype: 'int', element_type: 'number', is_hidden: false },
105
- { attribute_key: 'status', db_datatype: 'varchar', element_type: 'text', is_hidden: false },
106
- {
107
- attribute_key: 'entity_type',
108
- db_datatype: 'varchar',
109
- element_type: 'text',
110
- is_hidden: false,
111
- },
112
- { attribute_key: 'code', db_datatype: 'varchar', element_type: 'text', is_hidden: false },
113
- {
114
- attribute_key: 'parent_id',
115
- db_datatype: 'int',
116
- element_type: 'number',
117
- is_hidden: false,
118
- },
98
+ // -------------------------------------------------------
99
+ // BYPASS COLUMNS
100
+ // -------------------------------------------------------
101
+ const bypassColumns = [
102
+ 'created_date',
103
+ 'created_by',
104
+ 'organization_id',
105
+ 'enterprise_id',
106
+ 'level_type',
107
+ 'level_id',
108
+ 'status',
109
+ 'entity_type',
110
+ 'code',
111
+ 'parent_id',
119
112
  ];
120
113
 
121
- // Only push bypassColumn attributes that don't already exist in validAttributes
122
- const existingAttributeKeys = validAttributes.map(
123
- (attr) => attr.attribute_key,
124
- );
125
- const newBypassColumns = bypassColumn.filter(
126
- (bypass) => !existingAttributeKeys.includes(bypass.attribute_key),
127
- );
128
- validAttributes.push(...newBypassColumns);
114
+ for (const col of bypassColumns) {
115
+ if (!validAttributes.some(a => a.attribute_key === col)) {
116
+ validAttributes.push({
117
+ attribute_key: col,
118
+ is_hidden: false,
119
+ db_datatype: 'text',
120
+ element_type: 'text',
121
+ });
122
+ }
123
+ }
129
124
 
125
+ // -------------------------------------------------------
126
+ // BUILD INSERT QUERY (POSTGRES FORMAT)
127
+ // -------------------------------------------------------
130
128
  const columns: string[] = [];
131
129
  const values: any[] = [];
130
+ let idx = 1;
131
+ const placeholders: string[] = [];
132
132
 
133
- validAttributes.forEach((attr) => {
133
+ for (const attr of validAttributes) {
134
134
  columns.push(attr.attribute_key);
135
- values.push(
136
- attr.attribute_key in entityData
137
- ? entityData[attr.attribute_key]
138
- : null,
139
- );
140
- });
135
+ values.push(entityData[attr.attribute_key] ?? null);
136
+ placeholders.push(`$${idx++}`);
137
+ }
141
138
 
142
- const placeholders = columns.map(() => '?').join(', ');
143
- const escapedColumns = columns.map((col) => `\`${col}\``).join(', ');
144
- const insertQuery = `INSERT INTO ${tableName} (${escapedColumns})
145
- VALUES (${placeholders})`;
139
+ const colList = columns.map(c => `"${c}"`).join(', ');
140
+ const placeholderList = placeholders.join(', ');
146
141
 
147
- const result = await this.dataSource.query(insertQuery, values);
148
- return result;
142
+ const sql = `
143
+ INSERT INTO ${tableName} (${colList})
144
+ VALUES (${placeholderList}) RETURNING id
145
+ `;
146
+
147
+ const result = await this.dataSource.query(sql, values);
148
+ return result[0];
149
149
  }
150
150
 
151
151
  // ----------------------------- get entity with relations
@@ -736,25 +736,20 @@ export class EntityDynamicService {
736
736
  const organizationId = loggedInUser.organization_id;
737
737
 
738
738
  // 1. Get db_table_name from entity master
739
- const result = await this.dataSource.query(
740
- `SELECT db_table_name
741
- FROM frm_entity_master
742
- WHERE mapped_entity_type = $1
743
- AND organization_id = $2`,
744
- [entityType, organizationId],
745
- );
746
739
 
747
- if (!result.length) {
740
+ const result = await this.entityMasterRepo.getEntityByMappedEntityType(entityType, organizationId);
741
+
742
+ if (!result) {
748
743
  throw new Error(
749
744
  `Entity type '${entityType}' not found in frm_entity_master for org '${organizationId}'`,
750
745
  );
751
746
  }
752
747
 
753
- const tableName = result[0].db_table_name;
748
+ const tableName = result.db_table_name;
754
749
 
755
750
  // 2. Get current max sequence number from that table
756
751
  const seqResult = await this.dataSource.query(
757
- `SELECT MAX(CAST(SUBSTRING(code, LENGTH(entity_type) + 1) AS UNSIGNED)) AS max_seq_no
752
+ `SELECT MAX(id) AS max_seq_no
758
753
  FROM \`${tableName}\`
759
754
  WHERE entity_type = $1`,
760
755
  [entityType],
@@ -35,7 +35,7 @@ export class ActionTemplateMappingService extends EntityServiceImpl {
35
35
  SELECT id
36
36
  FROM frm_list_master_items
37
37
  WHERE organization_id = $1
38
- AND listtype = "MOD" AND code = $2
38
+ AND listtype = 'MOD' AND code = $2
39
39
  `,
40
40
  [loggedInUser.organization_id, 'email'],
41
41
  );
@@ -46,7 +46,7 @@ export class ActionTemplateMappingService extends EntityServiceImpl {
46
46
  `
47
47
  SELECT code as value, name as label , id
48
48
  FROM frm_wf_comm_template
49
- WHERE mode = $1 AND organization_id = $2 AND level_type = $3 AND level_id = $4 AND status IN (SELECT id FROM frm_list_master_items WHERE code = "STATUS_ACTIVE" AND organization_id = $5)
49
+ WHERE mode = $1 AND organization_id = $2 AND level_type = $3 AND level_id = $4 AND status IN (SELECT id FROM frm_list_master_items WHERE code = 'STATUS_ACTIVE' AND organization_id = $5)
50
50
  `,
51
51
  [
52
52
  modeId,
@@ -89,15 +89,15 @@ export class ActionTemplateMappingService extends EntityServiceImpl {
89
89
  `
90
90
  SELECT code as value, name as label , id
91
91
  FROM frm_wf_comm_template
92
- WHERE code IN ($1) AND mode = $2 AND organization_id = $3 AND level_type = $4 AND level_id = $5 AND status IN (SELECT id FROM frm_list_master_items WHERE code = "STATUS_ACTIVE" AND organization_id = $6)
92
+ WHERE code IN ($1) AND mode = $2 AND organization_id = $3 AND level_type = $4 AND level_id = $5 AND status IN (SELECT id FROM frm_list_master_items WHERE code = 'STATUS_ACTIVE' AND organization_id = $6)
93
93
  `,
94
94
  [
95
95
  templateCodes,
96
96
  mode,
97
97
  loggedInUser.organization_id,
98
98
  loggedInUser.level_type,
99
- loggedInUser.level_id,
100
- loggedInUser.organization_id,
99
+ Number(loggedInUser.level_id),
100
+ Number(loggedInUser.organization_id),
101
101
  ],
102
102
  );
103
103