rez_core 2.2.148 → 2.2.149

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.148",
3
+ "version": "2.2.149",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -1,3 +1,4 @@
1
+ import { log } from 'console';
1
2
  import {
2
3
  Controller,
3
4
  Get,
@@ -24,7 +25,10 @@ export class ViewMasterController {
24
25
  @Query('entity_type') entityType: string,
25
26
  @Query('type') type: string,
26
27
  @Res() res: Response,
28
+ @Req() req: Request & { user: any },
27
29
  ) {
30
+ const loggedInUser = req.user.userData;
31
+
28
32
  try {
29
33
  if (!entityType || !type) {
30
34
  return res.status(HttpStatus.BAD_REQUEST).json({
@@ -32,4 +32,7 @@ export class ActionEntity extends BaseEntity {
32
32
 
33
33
  @Column({ nullable: true })
34
34
  user_id: number;
35
+
36
+ @Column({ type: 'varchar', nullable: true })
37
+ assignment_type: string;
35
38
  }
@@ -189,7 +189,7 @@ export class StageMovementRepository {
189
189
  ac.code AS action_category_code,
190
190
  arm.form_id
191
191
  FROM cr_wf_action a
192
- INNER JOIN cr_wf_stage_action_mapping m ON a.id = m.action_id
192
+ LEFT JOIN cr_wf_stage_action_mapping m ON a.id = m.action_id
193
193
  LEFT JOIN cr_wf_action_category ac ON a.action_category = ac.id
194
194
  LEFT JOIN cr_wf_action_resources_mapping arm ON m.id = arm.stg_act_mapping_id
195
195
  WHERE m.stage_id = ?
@@ -257,6 +257,23 @@ export class WorkflowMetaService extends EntityServiceImpl {
257
257
  return 'No actions found for this stage.';
258
258
  }
259
259
 
260
+ // check whether first action's action_category is owner_assignment and assignment_type is AUTO_ASSIGN
261
+ const firstAction = actions[0];
262
+ let actionCategory = await this.dataSource.query(
263
+ `SELECT code FROM cr_wf_action_category WHERE id = ${Number(firstAction.action_category)}`,
264
+ );
265
+
266
+ let assignmentType = await this.dataSource.query(
267
+ `SELECT value FROM cr_list_master_items WHERE id = ${Number(firstAction.assignment_type)}`,
268
+ );
269
+ if (
270
+ actionCategory[0]?.code === 'OWAS' &&
271
+ assignmentType[0]?.value == 'round_robin'
272
+ ) {
273
+ console.log('Auto-assigning owner based on round-robin assignment type');
274
+ await this.assignLead(loggedInUser);
275
+ }
276
+
260
277
  // save in action data
261
278
  await this.actionDataService.saveActionData(
262
279
  actions,
@@ -272,5 +289,101 @@ export class WorkflowMetaService extends EntityServiceImpl {
272
289
  mapped_entity_id,
273
290
  mapped_entity_type,
274
291
  );
292
+
293
+ if (
294
+ actionCategory[0]?.code == 'OWAS' &&
295
+ assignmentType[0]?.value == 'round_robin'
296
+ ) {
297
+ console.log('Auto-assigning owner based on round-robin assignment type');
298
+ await this.assignLead(loggedInUser);
299
+ // move task
300
+ await this.taskService.moveTask(loggedInUser, {
301
+ mapped_entity_type,
302
+ mapped_entity_id,
303
+ stage_id,
304
+ action_id: firstAction.id,
305
+ });
306
+
307
+ // if it has only one action then move next stage
308
+ if (actions.length == 1) {
309
+ console.log(
310
+ 'Only one action present and it is owner assignment. Moving to next stage.',
311
+ );
312
+ await this.moveToNextStage(
313
+ mapped_entity_type,
314
+ mapped_entity_id,
315
+ loggedInUser,
316
+ );
317
+ }
318
+ }
319
+ }
320
+
321
+ async assignLead(loggedInUser: UserData): Promise<number> {
322
+ const { organization_id, level_id, level_type } = loggedInUser;
323
+
324
+ // 1) Get eligible owners (distinct + stable ordering)
325
+ const owners: Array<{ id: number }> = await this.dataSource.query(
326
+ `
327
+ SELECT DISTINCT u.id
328
+ FROM cr_user u
329
+ JOIN cr_user_role_mapping urm ON u.id = urm.user_id
330
+ JOIN cr_role r ON urm.role_id = r.id
331
+ JOIN cr_module_access ma ON ma.role_code = r.code
332
+ WHERE urm.organization_id = ?
333
+ AND urm.level_id = ?
334
+ AND urm.level_type = ?
335
+ AND urm.appcode = 'CRM'
336
+ AND ma.appcode = 'CRM'
337
+ AND ma.module_code = 'lead_crm_sch'
338
+ AND ma.access_flag = 1
339
+ AND ma.action_type = 'LEAD_OWNER'
340
+ ORDER BY u.id ASC
341
+ `,
342
+ [organization_id, Number(level_id), level_type],
343
+ );
344
+
345
+ if (!owners?.length) throw new Error('No eligible users found');
346
+ const userIds = owners.map((o) => Number(o.id)); // normalize to numbers
347
+
348
+ // 2) Find the last assigned *eligible* owner (use IN (...))
349
+ const placeholders = userIds.map(() => '?').join(',');
350
+ const lastRow: Array<{ lead_owner: number }> = await this.dataSource.query(
351
+ `
352
+ SELECT lead_owner
353
+ FROM crm_lead
354
+ WHERE organization_id = ?
355
+ AND level_id = ?
356
+ AND level_type = ?
357
+ AND lead_owner IN (${placeholders})
358
+ ORDER BY created_date DESC
359
+ LIMIT 1
360
+ `,
361
+ [organization_id, Number(level_id), level_type, ...userIds],
362
+ );
363
+
364
+ const lastAssigned = lastRow.length ? Number(lastRow[0].lead_owner) : null;
365
+
366
+ // 3) Compute next user in round-robin
367
+ const lastIdx = lastAssigned != null ? userIds.indexOf(lastAssigned) : -1;
368
+ const nextIdx = (lastIdx + 1) % userIds.length;
369
+ const nextUser = userIds[nextIdx];
370
+
371
+ //4) Update lead owner in the lead table
372
+ await this.dataSource.query(
373
+ `
374
+ UPDATE crm_lead
375
+ SET lead_owner = ?
376
+ WHERE organization_id = ?
377
+ AND level_id = ?
378
+ AND level_type = ?
379
+ AND lead_owner IS NULL
380
+ ORDER BY created_date ASC
381
+ LIMIT 1
382
+ `,
383
+ [nextUser, organization_id, Number(level_id), level_type],
384
+ );
385
+
386
+ console.log(`Assigning lead to user ID: ${nextUser}`);
387
+ return nextUser;
275
388
  }
276
389
  }