rez_core 2.2.146 → 2.2.147

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": "2.2.146",
3
+ "version": "2.2.147",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -17,7 +17,6 @@ export class EntityDynamicService {
17
17
  entityData: Record<string, any>,
18
18
  loggedInUser: any,
19
19
  ): Promise<any> {
20
-
21
20
  const organizationId = loggedInUser.organization_id;
22
21
 
23
22
  const tableName = await this.getTableName(entityType, organizationId);
@@ -42,7 +41,7 @@ export class EntityDynamicService {
42
41
  entityData.level_type = loggedInUser.level_type;
43
42
  if (!entityData.level_id) entityData.level_id = loggedInUser.level_id;
44
43
  if (!entityData.status) entityData.status = statusList[0].id;
45
- if( !entityData.entity_type ) entityData.entity_type = entityType;
44
+ if (!entityData.entity_type) entityData.entity_type = entityType;
46
45
  }
47
46
 
48
47
  if (!entityData.code && loggedInUser) {
@@ -92,76 +91,76 @@ export class EntityDynamicService {
92
91
 
93
92
  // ----------------------------- get entity with relations
94
93
 
95
- // ----------------------------- create entity with relations
96
- async createEntityWithRelation(
97
- entityType: string,
98
- data: Record<string, any>,
99
- loggedInUser: any,
100
- ): Promise<any> {
101
- const organizationId = loggedInUser.organization_id;
102
-
103
- const getRelation = await this.dataSource.query(
104
- `SELECT * FROM cr_entity_relation WHERE organization_id = ? AND source_entity_type = ?`,
105
- [organizationId, entityType],
106
- );
107
-
108
- const { mappedEntities, ...mainData } = data;
109
-
110
- // create main entity
111
- const createdEntityData = await this.createEntity(
112
- entityType,
113
- mainData,
114
- loggedInUser,
115
- );
116
- const mainID = createdEntityData.insertId || createdEntityData.id;
117
-
118
- if (mappedEntities && getRelation.length > 0) {
119
- for (const relation of getRelation) {
120
- const targetEntityType = relation.target_entity_type;
121
- const relationType = relation.relation_type;
122
- const relationId = relation.relation_id;
123
-
124
- if (!mappedEntities[targetEntityType]) continue;
125
-
126
- // normalize: always array
127
- const entityDataArray = Array.isArray(mappedEntities[targetEntityType])
128
- ? mappedEntities[targetEntityType]
129
- : [mappedEntities[targetEntityType]];
130
-
131
- for (const item of entityDataArray) {
132
- const itemWithRef = {
133
- ...item,
134
- entity_type: targetEntityType,
135
- };
136
-
137
- const createdRelatedEntity = await this.createEntity(
138
- targetEntityType,
139
- itemWithRef,
140
- loggedInUser,
141
- );
94
+ // ----------------------------- create entity with relations
95
+ async createEntityWithRelation(
96
+ entityType: string,
97
+ data: Record<string, any>,
98
+ loggedInUser: any,
99
+ ): Promise<any> {
100
+ const organizationId = loggedInUser.organization_id;
101
+
102
+ const getRelation = await this.dataSource.query(
103
+ `SELECT * FROM cr_entity_relation WHERE organization_id = ? AND source_entity_type = ?`,
104
+ [organizationId, entityType],
105
+ );
106
+
107
+ const { mappedEntities, ...mainData } = data;
142
108
 
143
- await this.dataSource.query(
144
- `INSERT INTO cr_entity_relation_data (source_entity_id, source_entity_type, target_entity_id, target_entity_type, relation_type) VALUES (?, ?, ?, ?, ?)`,
145
- [
146
- mainID,
147
- entityType,
148
- createdRelatedEntity.insertId,
109
+ // create main entity
110
+ const createdEntityData = await this.createEntity(
111
+ entityType,
112
+ mainData,
113
+ loggedInUser,
114
+ );
115
+ const mainID = createdEntityData.insertId || createdEntityData.id;
116
+
117
+ if (mappedEntities && getRelation.length > 0) {
118
+ for (const relation of getRelation) {
119
+ const targetEntityType = relation.target_entity_type;
120
+ const relationType = relation.relation_type;
121
+ const relationId = relation.relation_id;
122
+
123
+ if (!mappedEntities[targetEntityType]) continue;
124
+
125
+ // normalize: always array
126
+ const entityDataArray = Array.isArray(mappedEntities[targetEntityType])
127
+ ? mappedEntities[targetEntityType]
128
+ : [mappedEntities[targetEntityType]];
129
+
130
+ for (const item of entityDataArray) {
131
+ const itemWithRef = {
132
+ ...item,
133
+ entity_type: targetEntityType,
134
+ };
135
+
136
+ const createdRelatedEntity = await this.createEntity(
149
137
  targetEntityType,
150
- relationType,
151
- ],
152
- );
138
+ itemWithRef,
139
+ loggedInUser,
140
+ );
141
+
142
+ await this.dataSource.query(
143
+ `INSERT INTO cr_entity_relation_data (source_entity_id, source_entity_type, target_entity_id, target_entity_type, relation_type) VALUES (?, ?, ?, ?, ?)`,
144
+ [
145
+ mainID,
146
+ entityType,
147
+ createdRelatedEntity.insertId,
148
+ targetEntityType,
149
+ relationType,
150
+ ],
151
+ );
152
+ }
153
153
  }
154
154
  }
155
- }
156
155
 
157
- return {
158
- mainEntity: {
159
- id: mainID,
160
- entityType,
161
- data: createdEntityData,
162
- },
163
- };
164
- }
156
+ return {
157
+ mainEntity: {
158
+ id: mainID,
159
+ entityType,
160
+ data: createdEntityData,
161
+ },
162
+ };
163
+ }
165
164
 
166
165
  // ----------------------------- get entity with relations
167
166
  async getEntityWithRelation(
@@ -193,132 +192,164 @@ async createEntityWithRelation(
193
192
  return response;
194
193
  }
195
194
 
196
- // ----------------------------- formatMappedEntities
197
- async formatMappedEntities(
198
- relatedEntities: any[],
199
- loggedInUser: any,
200
- ): Promise<any> {
201
- const mappedEntities: any = {};
195
+ // ----------------------------- formatMappedEntities
196
+ async formatMappedEntities(
197
+ relatedEntities: any[],
198
+ loggedInUser: any,
199
+ ): Promise<any> {
200
+ const mappedEntities: any = {};
202
201
 
203
- for (const relation of relatedEntities) {
204
- const targetEntityType = relation.target_entity_type;
205
- const targetEntityId = relation.target_entity_id;
202
+ for (const relation of relatedEntities) {
203
+ const targetEntityType = relation.target_entity_type;
204
+ const targetEntityId = relation.target_entity_id;
206
205
 
207
- const entityData = await this.getEntity(
208
- targetEntityType,
209
- targetEntityId,
210
- loggedInUser,
211
- );
206
+ const entityData = await this.getEntity(
207
+ targetEntityType,
208
+ targetEntityId,
209
+ loggedInUser,
210
+ );
211
+
212
+ if (!mappedEntities[targetEntityType]) {
213
+ mappedEntities[targetEntityType] = [];
214
+ }
212
215
 
213
- if (!mappedEntities[targetEntityType]) {
214
- mappedEntities[targetEntityType] = [];
216
+ mappedEntities[targetEntityType].push(entityData);
215
217
  }
216
218
 
217
- mappedEntities[targetEntityType].push(entityData);
219
+ return mappedEntities;
218
220
  }
219
221
 
220
- return mappedEntities;
221
- }
222
-
223
222
  // ----------------------------- update with relations
224
223
 
225
- // ----------------------------- update entity with relations
226
- async updateEntityWithRelations(
227
- entityType: string,
228
- id: number | string,
229
- data: Record<string, any>,
230
- loggedInUser: any,
231
- ): Promise<any> {
232
- const organizationId = loggedInUser.organization_id;
233
- const { mappedEntities, ...mainData } = data;
234
-
235
- // Update main entity
236
- const updatedMainEntity = await this.updateEntity(entityType, id, mainData, loggedInUser);
237
-
238
- const updatedRelations: Record<string, any> = {};
224
+ // ----------------------------- update entity with relations
225
+ async updateEntityWithRelations(
226
+ entityType: string,
227
+ id: number | string,
228
+ data: Record<string, any>,
229
+ loggedInUser: any,
230
+ ): Promise<any> {
231
+ const organizationId = loggedInUser.organization_id;
232
+ const { mappedEntities, ...mainData } = data;
239
233
 
240
- if (mappedEntities) {
241
- const getRelationDefs = await this.dataSource.query(
242
- `SELECT * FROM cr_entity_relation WHERE organization_id = ? AND source_entity_type = ?`,
243
- [organizationId, entityType],
234
+ // Update main entity
235
+ const updatedMainEntity = await this.updateEntity(
236
+ entityType,
237
+ id,
238
+ mainData,
239
+ loggedInUser,
244
240
  );
245
241
 
246
- for (const [targetEntityType, rawEntityData] of Object.entries(mappedEntities)) {
247
- const relationDef = getRelationDefs.find(
248
- (r) => r.target_entity_type === targetEntityType,
249
- );
250
- if (!relationDef) continue;
251
-
252
- const relationType = relationDef.relation_type;
253
- const entityArray = Array.isArray(rawEntityData) ? rawEntityData : [rawEntityData];
242
+ const updatedRelations: Record<string, any> = {};
254
243
 
255
- // Delete previous relations and related entities
256
- const existingRelationsForType = await this.dataSource.query(
257
- `SELECT * FROM cr_entity_relation_data WHERE source_entity_type = ? AND source_entity_id = ? AND target_entity_type = ?`,
258
- [entityType, id, targetEntityType],
244
+ if (mappedEntities) {
245
+ const getRelationDefs = await this.dataSource.query(
246
+ `SELECT * FROM cr_entity_relation WHERE organization_id = ? AND source_entity_type = ?`,
247
+ [organizationId, entityType],
259
248
  );
260
249
 
261
- for (const existingRel of existingRelationsForType) {
262
- // Delete the related entity
263
- await this.deleteEntity(existingRel.target_entity_type, existingRel.target_entity_id, loggedInUser);
250
+ for (const [targetEntityType, rawEntityData] of Object.entries(
251
+ mappedEntities,
252
+ )) {
253
+ const relationDef = getRelationDefs.find(
254
+ (r) => r.target_entity_type === targetEntityType,
255
+ );
256
+ if (!relationDef) continue;
257
+
258
+ const relationType = relationDef.relation_type;
259
+ const entityArray = Array.isArray(rawEntityData)
260
+ ? rawEntityData
261
+ : [rawEntityData];
264
262
 
265
- // Delete the relation row
266
- await this.dataSource.query(
267
- `DELETE FROM cr_entity_relation_data WHERE id = ?`,
268
- [existingRel.id],
263
+ // Delete previous relations and related entities
264
+ const existingRelationsForType = await this.dataSource.query(
265
+ `SELECT * FROM cr_entity_relation_data WHERE source_entity_type = ? AND source_entity_id = ? AND target_entity_type = ?`,
266
+ [entityType, id, targetEntityType],
269
267
  );
270
- }
271
268
 
272
- // Create/update new entities & relations
273
- const updatedEntities: any[] = [];
274
-
275
- for (const item of entityArray) {
276
- let targetEntityId;
277
- let entityData;
278
-
279
- if (item.id) {
280
- // Update existing entity
281
- await this.updateEntity(targetEntityType, item.id, item, loggedInUser);
282
- targetEntityId = item.id;
283
- entityData = await this.getEntity(targetEntityType, targetEntityId, loggedInUser);
284
- } else {
285
- // Create new entity
286
- const createdEntity = await this.createEntity(targetEntityType, item, loggedInUser);
287
- targetEntityId = createdEntity.insertId || createdEntity.id;
288
- entityData = await this.getEntity(targetEntityType, targetEntityId, loggedInUser);
269
+ for (const existingRel of existingRelationsForType) {
270
+ // Delete the related entity
271
+ await this.deleteEntity(
272
+ existingRel.target_entity_type,
273
+ existingRel.target_entity_id,
274
+ loggedInUser,
275
+ );
276
+
277
+ // Delete the relation row
278
+ await this.dataSource.query(
279
+ `DELETE FROM cr_entity_relation_data WHERE id = ?`,
280
+ [existingRel.id],
281
+ );
289
282
  }
290
283
 
291
- // Insert relation
292
- await this.dataSource.query(
293
- `INSERT INTO cr_entity_relation_data
284
+ // Create/update new entities & relations
285
+ const updatedEntities: any[] = [];
286
+
287
+ for (const item of entityArray) {
288
+ let targetEntityId;
289
+ let entityData;
290
+
291
+ if (item.id) {
292
+ // Update existing entity
293
+ await this.updateEntity(
294
+ targetEntityType,
295
+ item.id,
296
+ item,
297
+ loggedInUser,
298
+ );
299
+ targetEntityId = item.id;
300
+ entityData = await this.getEntity(
301
+ targetEntityType,
302
+ targetEntityId,
303
+ loggedInUser,
304
+ );
305
+ } else {
306
+ // Create new entity
307
+ const createdEntity = await this.createEntity(
308
+ targetEntityType,
309
+ item,
310
+ loggedInUser,
311
+ );
312
+ targetEntityId = createdEntity.insertId || createdEntity.id;
313
+ entityData = await this.getEntity(
314
+ targetEntityType,
315
+ targetEntityId,
316
+ loggedInUser,
317
+ );
318
+ }
319
+
320
+ // Insert relation
321
+ await this.dataSource.query(
322
+ `INSERT INTO cr_entity_relation_data
294
323
  (source_entity_id, source_entity_type, target_entity_id, target_entity_type, relation_type)
295
324
  VALUES (?, ?, ?, ?, ?)`,
296
- [id, entityType, targetEntityId, targetEntityType, relationType],
297
- );
298
-
299
- if (relationType === 'ONE_TO_MANY') {
300
- updatedEntities.push(entityData);
301
- } else if (relationType === 'ONE_TO_ONE' || relationType === 'MANY_TO_ONE') {
302
- updatedEntities[0] = entityData; // single object
325
+ [id, entityType, targetEntityId, targetEntityType, relationType],
326
+ );
327
+
328
+ if (relationType === 'ONE_TO_MANY') {
329
+ updatedEntities.push(entityData);
330
+ } else if (
331
+ relationType === 'ONE_TO_ONE' ||
332
+ relationType === 'MANY_TO_ONE'
333
+ ) {
334
+ updatedEntities[0] = entityData; // single object
335
+ }
303
336
  }
304
- }
305
337
 
306
- // Assign to response mappedEntities
307
- updatedRelations[targetEntityType] =
308
- relationType === 'ONE_TO_MANY' ? updatedEntities : updatedEntities[0];
338
+ // Assign to response mappedEntities
339
+ updatedRelations[targetEntityType] =
340
+ relationType === 'ONE_TO_MANY' ? updatedEntities : updatedEntities[0];
341
+ }
309
342
  }
310
- }
311
-
312
- return {
313
- mainEntity: {
314
- id,
315
- entityType,
316
- data: updatedMainEntity,
317
- },
318
- relatedEntities: updatedRelations,
319
- };
320
- }
321
343
 
344
+ return {
345
+ mainEntity: {
346
+ id,
347
+ entityType,
348
+ data: updatedMainEntity,
349
+ },
350
+ relatedEntities: updatedRelations,
351
+ };
352
+ }
322
353
 
323
354
  // -----------------------------
324
355
  async updateEntity(
@@ -485,4 +516,41 @@ async updateEntityWithRelations(
485
516
  const dropdown = await this.dataSource.query(query, params);
486
517
  return dropdown;
487
518
  }
519
+
520
+ async getCode(entityType: string, loggedInUser: any): Promise<string> {
521
+ const organizationId = loggedInUser.organization_id;
522
+
523
+ // 1. Get db_table_name from entity master
524
+ const result = await this.dataSource.query(
525
+ `SELECT db_table_name
526
+ FROM cr_entity_master
527
+ WHERE mapped_entity_type = ? AND organization_id = ?`,
528
+ [entityType, organizationId],
529
+ );
530
+
531
+ if (!result.length) {
532
+ throw new Error(
533
+ `Entity type '${entityType}' not found in cr_entity_master for org '${organizationId}'`,
534
+ );
535
+ }
536
+
537
+ const tableName = result[0].db_table_name;
538
+
539
+ // 2. Get current max sequence number from that table
540
+ const seqResult = await this.dataSource.query(
541
+ `SELECT MAX(CAST(SUBSTRING(code, LENGTH(entity_type) + 1) AS UNSIGNED)) AS max_seq_no
542
+ FROM \`${tableName}\`
543
+ WHERE entity_type = ?`,
544
+ [entityType],
545
+ );
546
+
547
+ let maxSeqNo = seqResult?.[0]?.max_seq_no
548
+ ? Number(seqResult[0].max_seq_no)
549
+ : 0;
550
+
551
+ maxSeqNo += 1;
552
+
553
+ // 3. Return generated code
554
+ return `${entityType}${maxSeqNo}`;
555
+ }
488
556
  }
@@ -317,6 +317,7 @@ export class TaskService extends EntityServiceImpl {
317
317
  action_id: number;
318
318
  reason_code?: string | number;
319
319
  remark?: string;
320
+ stage_group_id?: number;
320
321
  },
321
322
  ): Promise<any> {
322
323
  // Logic to move task based on the provided body parameters
@@ -346,6 +347,9 @@ export class TaskService extends EntityServiceImpl {
346
347
  reason_code: body.reason_code,
347
348
  remark: body.remark,
348
349
  mapped_entity_id: body.mapped_entity_id,
350
+ stage_id: body.stage_id,
351
+ action_id: body.action_id,
352
+ stage_group_id: body.stage_group_id,
349
353
  },
350
354
  loggedInUser,
351
355
  );
@@ -406,6 +410,9 @@ export class TaskService extends EntityServiceImpl {
406
410
  reason_code: string | number;
407
411
  remark: string;
408
412
  mapped_entity_id: number;
413
+ stage_id: number;
414
+ action_id: number;
415
+ stage_group_id?: number;
409
416
  },
410
417
  loggedInUser: any,
411
418
  ) {
@@ -423,6 +430,9 @@ export class TaskService extends EntityServiceImpl {
423
430
  mapped_entity_type: 'LEAD',
424
431
  entity_type: 'LNO',
425
432
  mapped_entity_id: entity.mapped_entity_id,
433
+ stage_id: entity.stage_id,
434
+ action_id: entity.action_id,
435
+ stage_group_id: entity.stage_group_id,
426
436
  } as any;
427
437
 
428
438
  return await super.createEntity(notePayload, loggedInUser);