rez_core 3.1.125 → 3.1.127

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": "3.1.125",
3
+ "version": "3.1.127",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -6,6 +6,7 @@ import {
6
6
  HttpStatus,
7
7
  Inject,
8
8
  Post,
9
+ Query,
9
10
  Req,
10
11
  UseGuards,
11
12
  } from '@nestjs/common';
@@ -23,12 +24,14 @@ export class StageController {
23
24
  @Post('/getAllStage')
24
25
  async getAllStage(
25
26
  @Req() req: Request & { user: any },
26
- @Body() body: { stage_group_id: number },
27
+ @Body() body: { stage_group_id: number; mapped_entity_id: number },
28
+ @Query() query: { show_previous: boolean },
27
29
  ) {
28
30
  const { organization_id } = req.user.userData;
29
31
  return this.stageGroupService.getAllStage(
30
- body.stage_group_id,
32
+ body,
31
33
  organization_id,
34
+ query.show_previous,
32
35
  );
33
36
  }
34
37
 
@@ -115,4 +115,58 @@ export class StageRepository {
115
115
 
116
116
  return rows;
117
117
  }
118
+
119
+ async getAllStageGroupAndStageByStageMovement(
120
+ stage_group_id: number,
121
+ organization_id: number,
122
+ ) {
123
+ // 1. Find the current stage group
124
+ const currentStageGroup = await this.dataSource.manager.findOne<any>(
125
+ 'cr_wf_stage_group',
126
+ { where: { id: stage_group_id, organization_id } },
127
+ );
128
+
129
+ if (!currentStageGroup) {
130
+ return [];
131
+ }
132
+
133
+ // 2. Get all stage groups in the same workflow with sequence <= current
134
+ const stageGroups = await this.dataSource.manager.find<any>(
135
+ 'cr_wf_stage_group',
136
+ {
137
+ where: {
138
+ workflow_id: currentStageGroup.workflow_id,
139
+ organization_id,
140
+ },
141
+ order: { sequence: 'ASC' },
142
+ },
143
+ );
144
+
145
+ // Filter only groups with sequence <= current
146
+ const eligibleGroups = stageGroups.filter(
147
+ (g) => g.sequence <= currentStageGroup.sequence,
148
+ );
149
+
150
+ const flatResults: any[] = [];
151
+
152
+ // 3. For each eligible group, fetch its stages
153
+ for (const group of eligibleGroups) {
154
+ const stages = await this.stageRepository.find({
155
+ where: { stage_group_id: group.id, organization_id },
156
+ order: { sequence: 'ASC' },
157
+ });
158
+
159
+ if (!stages || stages.length === 0) continue;
160
+
161
+ // 4. Flatten into result with full_name
162
+ for (const stage of stages) {
163
+ flatResults.push({
164
+ ...stage,
165
+ full_name: `${group.name} - ${stage.name}`,
166
+ });
167
+ }
168
+ }
169
+
170
+ return flatResults;
171
+ }
118
172
  }
@@ -3,6 +3,7 @@ import {
3
3
  BadRequestException,
4
4
  Inject,
5
5
  Injectable,
6
+ Logger,
6
7
  NotFoundException,
7
8
  } from '@nestjs/common';
8
9
  import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service';
@@ -16,6 +17,8 @@ import { StageMovementData } from '../entity/stage-movement-data.entity';
16
17
 
17
18
  @Injectable()
18
19
  export class StageService extends EntityServiceImpl {
20
+ private readonly logger = new Logger(StageService.name);
21
+
19
22
  constructor(
20
23
  private readonly stageRepository: StageRepository,
21
24
  private readonly stageGroupRepository: StageGroupRepository,
@@ -28,29 +31,85 @@ export class StageService extends EntityServiceImpl {
28
31
  super();
29
32
  }
30
33
 
31
- async getAllStage(stage_group_id: number, organization_id: number) {
32
- const allStages = await this.stageRepository.getAllStage(
33
- stage_group_id,
34
- organization_id,
35
- );
34
+ async getAllStage(
35
+ payload: {
36
+ stage_group_id: number;
37
+ mapped_entity_id: number;
38
+ },
39
+ organization_id: number,
40
+ show_previous: boolean,
41
+ ) {
42
+ if (!show_previous) {
43
+ const allStages = await this.stageRepository.getAllStage(
44
+ payload.stage_group_id,
45
+ organization_id,
46
+ );
47
+
48
+ const statusListMaster = await this.dataSource.query(
49
+ `SELECT id, name
50
+ FROM cr_list_master_items
51
+ WHERE listtype = 'STS' AND organization_id = ?`,
52
+ [organization_id],
53
+ );
54
+
55
+ // Build a lookup map: id → name
56
+ const statusMap = new Map(
57
+ statusListMaster.map((s) => [String(s.id), s.name]),
58
+ );
59
+
60
+ for (const stage of allStages) {
61
+ stage.status = statusMap.get(String(stage.status)) as any;
62
+ }
36
63
 
37
- const statusListMaster = await this.dataSource.query(
38
- `SELECT id, name
39
- FROM cr_list_master_items
40
- WHERE listtype = 'STS' AND organization_id = ?`,
41
- [organization_id],
42
- );
64
+ return allStages;
65
+ }
43
66
 
44
- // Build a lookup map: id name
45
- const statusMap = new Map(
46
- statusListMaster.map((s) => [String(s.id), s.name]),
67
+ // 1. Get all stages up to the current stage group
68
+ const allStageWithStageGroup =
69
+ await this.stageRepository.getAllStageGroupAndStageByStageMovement(
70
+ payload.stage_group_id,
71
+ organization_id,
72
+ );
73
+
74
+ // 2. Get stage movement history for this entity
75
+ const allStageMovementData = await this.dataSource.manager.find<{
76
+ id: number;
77
+ stage_group_id: number;
78
+ stage_id: number;
79
+ mapped_entity_id: number;
80
+ mapped_entity_type: string;
81
+ is_current: string;
82
+ }>('cr_wf_stage_movement_data', {
83
+ where: {
84
+ mapped_entity_id: payload.mapped_entity_id,
85
+ mapped_entity_type: 'LEAD',
86
+ },
87
+ order: { id: 'ASC' },
88
+ });
89
+
90
+ if (!allStageMovementData || allStageMovementData.length === 0) {
91
+ return allStageWithStageGroup;
92
+ }
93
+
94
+ // 3. Find the current stage movement (is_current === 'Y')
95
+ const currentMovement = allStageMovementData.find(
96
+ (m) => m.is_current === 'Y',
47
97
  );
48
98
 
49
- for (const stage of allStages) {
50
- stage.status = statusMap.get(String(stage.status)) as any;
99
+ if (!currentMovement) {
100
+ return allStageWithStageGroup;
101
+ }
102
+
103
+ // 4. Filter stages up to and including the current stage_id
104
+ const filteredStages: any[] = [];
105
+ for (const stage of allStageWithStageGroup) {
106
+ filteredStages.push(stage);
107
+ if (stage.id === currentMovement.stage_id) {
108
+ break; // stop once we reach the current stage
109
+ }
51
110
  }
52
111
 
53
- return allStages;
112
+ return filteredStages;
54
113
  }
55
114
 
56
115
  async updateEntity(
@@ -119,7 +178,7 @@ export class StageService extends EntityServiceImpl {
119
178
  allStages.push(...stages);
120
179
  } catch (err) {
121
180
  if (err instanceof NotFoundException) {
122
- console.warn(`No stages for group ID ${groupId}`);
181
+ this.logger.warn(`No stages for group ID ${groupId}`);
123
182
  continue;
124
183
  }
125
184
  throw err;