rez_core 5.0.5 → 5.0.7
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
|
@@ -63,95 +63,117 @@ export class StageGroupRepository {
|
|
|
63
63
|
|
|
64
64
|
async getAllStageGroupAndStageHierarchy(
|
|
65
65
|
workflow_id: number,
|
|
66
|
-
loggedInUser: any,
|
|
66
|
+
loggedInUser: any,
|
|
67
67
|
lead_id: number,
|
|
68
68
|
) {
|
|
69
69
|
if (!workflow_id) {
|
|
70
70
|
throw new BadRequestException('workflow_id is required');
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
// Step 1: Get
|
|
73
|
+
// Step 1: Get stage groups
|
|
74
74
|
const stageGroups = await this.getAllStageGroupByCondition({
|
|
75
75
|
workflow_id,
|
|
76
76
|
organization_id: loggedInUser.organization_id,
|
|
77
|
-
// level_id: loggedInUser.level_id,
|
|
78
77
|
});
|
|
79
78
|
|
|
80
79
|
if (!stageGroups.length) return [];
|
|
81
80
|
|
|
82
|
-
//
|
|
83
|
-
|
|
81
|
+
// -------------------------------------------------------------
|
|
82
|
+
// STEP 2: Get all stages - FIXED with `$1, $2, $3...`
|
|
83
|
+
// -------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
const stageGroupIds = stageGroups.map((sg) => Number(sg.id));
|
|
86
|
+
const stageGroupPlaceholders = stageGroupIds
|
|
87
|
+
.map((_, idx) => `$${idx + 1}`)
|
|
88
|
+
.join(", ");
|
|
89
|
+
|
|
90
|
+
const params_stage = [...stageGroupIds, Number(loggedInUser.organization_id)];
|
|
91
|
+
|
|
84
92
|
const stagesRaw = await this.dataSource.query(
|
|
85
93
|
`
|
|
86
|
-
SELECT
|
|
87
|
-
frm_wf_stage.*
|
|
94
|
+
SELECT frm_wf_stage.*
|
|
88
95
|
FROM frm_wf_stage
|
|
89
|
-
WHERE frm_wf_stage.stage_group_id IN (${
|
|
90
|
-
AND frm_wf_stage.organization_id =
|
|
96
|
+
WHERE frm_wf_stage.stage_group_id IN (${stageGroupPlaceholders})
|
|
97
|
+
AND frm_wf_stage.organization_id = $${stageGroupIds.length + 1}
|
|
91
98
|
ORDER BY frm_wf_stage.sequence ASC
|
|
92
99
|
`,
|
|
93
|
-
|
|
100
|
+
params_stage,
|
|
94
101
|
);
|
|
95
102
|
|
|
96
|
-
const stageIds = stagesRaw.map((s) => s.id);
|
|
103
|
+
const stageIds = stagesRaw.map((s) => Number(s.id));
|
|
104
|
+
|
|
105
|
+
if (stageIds.length === 0) {
|
|
106
|
+
return stageGroups.map((sg) => ({ ...sg, stages: [] }));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// -------------------------------------------------------------
|
|
110
|
+
// STEP 3: Task Counts (Postgres-safe)
|
|
111
|
+
// -------------------------------------------------------------
|
|
112
|
+
const stagePlaceholders = stageIds
|
|
113
|
+
.map((_, idx) => `$${idx + 1}`)
|
|
114
|
+
.join(", ");
|
|
115
|
+
|
|
116
|
+
const params_task = [...stageIds, Number(lead_id)];
|
|
97
117
|
|
|
98
|
-
// Task counts
|
|
99
118
|
const taskCounts = await this.dataSource.query(
|
|
100
119
|
`
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
120
|
+
SELECT stage_id, COUNT(*) AS task_count
|
|
121
|
+
FROM frm_wf_task_data
|
|
122
|
+
WHERE stage_id IN (${stagePlaceholders})
|
|
123
|
+
AND is_system = 0
|
|
124
|
+
AND mapped_entity_id = $${stageIds.length + 1}
|
|
125
|
+
GROUP BY stage_id
|
|
105
126
|
`,
|
|
106
|
-
|
|
127
|
+
params_task,
|
|
107
128
|
);
|
|
108
129
|
|
|
109
|
-
//
|
|
130
|
+
// -------------------------------------------------------------
|
|
131
|
+
// STEP 4: Meeting Counts
|
|
132
|
+
// -------------------------------------------------------------
|
|
110
133
|
const meetingCounts = await this.dataSource.query(
|
|
111
134
|
`
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
135
|
+
SELECT stage_id, COUNT(*) AS meeting_count
|
|
136
|
+
FROM crm_lead_meeting
|
|
137
|
+
WHERE stage_id IN (${stagePlaceholders})
|
|
138
|
+
AND mapped_entity_id = $${stageIds.length + 1}
|
|
139
|
+
GROUP BY stage_id
|
|
116
140
|
`,
|
|
117
|
-
|
|
141
|
+
params_task,
|
|
118
142
|
);
|
|
119
143
|
|
|
120
|
-
//
|
|
144
|
+
// -------------------------------------------------------------
|
|
145
|
+
// STEP 5: Send Communication Counts
|
|
146
|
+
// -------------------------------------------------------------
|
|
121
147
|
const sendCommCounts = await this.dataSource.query(
|
|
122
148
|
`
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
149
|
+
SELECT stage_id, COUNT(*) AS send_comm_count
|
|
150
|
+
FROM crm_lead_communication
|
|
151
|
+
WHERE stage_id IN (${stagePlaceholders})
|
|
152
|
+
AND mapped_entity_id = $${stageIds.length + 1}
|
|
153
|
+
AND type IN ('SEND','LOG')
|
|
154
|
+
GROUP BY stage_id
|
|
128
155
|
`,
|
|
129
|
-
|
|
156
|
+
params_task,
|
|
130
157
|
);
|
|
131
158
|
|
|
132
|
-
// Convert to maps
|
|
159
|
+
// Convert to maps
|
|
133
160
|
const taskCountMap = new Map(
|
|
134
|
-
taskCounts.map((
|
|
161
|
+
taskCounts.map((r) => [Number(r.stage_id), Number(r.task_count)]),
|
|
135
162
|
);
|
|
136
163
|
const meetingCountMap = new Map(
|
|
137
|
-
meetingCounts.map((
|
|
138
|
-
Number(row.stage_id),
|
|
139
|
-
Number(row.meeting_count),
|
|
140
|
-
]),
|
|
164
|
+
meetingCounts.map((r) => [Number(r.stage_id), Number(r.meeting_count)]),
|
|
141
165
|
);
|
|
142
166
|
const sendCommCountMap = new Map(
|
|
143
|
-
sendCommCounts.map((
|
|
144
|
-
Number(row.stage_id),
|
|
145
|
-
Number(row.send_comm_count),
|
|
146
|
-
]),
|
|
167
|
+
sendCommCounts.map((r) => [Number(r.stage_id), Number(r.send_comm_count)]),
|
|
147
168
|
);
|
|
148
169
|
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
//
|
|
170
|
+
// -------------------------------------------------------------
|
|
171
|
+
// STEP 6: Combine stages per group
|
|
172
|
+
// -------------------------------------------------------------
|
|
152
173
|
const stageMap = new Map<number, any[]>();
|
|
174
|
+
|
|
153
175
|
for (const stage of stagesRaw) {
|
|
154
|
-
const stageId = Number(stage.id);
|
|
176
|
+
const stageId = Number(stage.id);
|
|
155
177
|
|
|
156
178
|
stage.task_count = taskCountMap.get(stageId) || 0;
|
|
157
179
|
stage.meeting_count = meetingCountMap.get(stageId) || 0;
|
|
@@ -160,17 +182,16 @@ export class StageGroupRepository {
|
|
|
160
182
|
if (!stageMap.has(stage.stage_group_id)) {
|
|
161
183
|
stageMap.set(stage.stage_group_id, []);
|
|
162
184
|
}
|
|
163
|
-
stageMap.get(stage.stage_group_id)?.push(stage);
|
|
164
|
-
}
|
|
165
185
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return {
|
|
169
|
-
...stageGroup,
|
|
170
|
-
stages: stageMap.get(Number(stageGroup.id)) || [],
|
|
171
|
-
};
|
|
172
|
-
});
|
|
186
|
+
stageMap.get(stage.stage_group_id)!.push(stage);
|
|
187
|
+
}
|
|
173
188
|
|
|
174
|
-
|
|
189
|
+
// -------------------------------------------------------------
|
|
190
|
+
// STEP 7: Build final response
|
|
191
|
+
// -------------------------------------------------------------
|
|
192
|
+
return stageGroups.map((sg) => ({
|
|
193
|
+
...sg,
|
|
194
|
+
stages: stageMap.get(Number(sg.id)) || [],
|
|
195
|
+
}));
|
|
175
196
|
}
|
|
176
197
|
}
|