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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "5.0.5",
3
+ "version": "5.0.7",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -63,95 +63,117 @@ export class StageGroupRepository {
63
63
 
64
64
  async getAllStageGroupAndStageHierarchy(
65
65
  workflow_id: number,
66
- loggedInUser: any, // required for media service
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 all stage groups
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
- // Step 2: Get all stages for these stage groups
83
- const stageGroupIds = stageGroups.map((sg) => sg.id);
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 (${stageGroupIds.map(() => '?').join(',')})
90
- AND frm_wf_stage.organization_id = $2
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
- [...stageGroupIds, loggedInUser.organization_id],
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
- SELECT stage_id, COUNT(*) AS task_count
102
- FROM frm_wf_task_data
103
- WHERE stage_id IN (${stageIds.map(() => '?').join(',')}) AND is_system=0 AND mapped_entity_id = $2
104
- GROUP BY stage_id
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
- [...stageIds, lead_id],
127
+ params_task,
107
128
  );
108
129
 
109
- // Meeting counts
130
+ // -------------------------------------------------------------
131
+ // STEP 4: Meeting Counts
132
+ // -------------------------------------------------------------
110
133
  const meetingCounts = await this.dataSource.query(
111
134
  `
112
- SELECT stage_id, COUNT(*) AS meeting_count
113
- FROM crm_lead_meeting
114
- WHERE stage_id IN (${stageIds.map(() => '?').join(',')}) AND mapped_entity_id = $2
115
- GROUP BY stage_id
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
- [...stageIds, lead_id],
141
+ params_task,
118
142
  );
119
143
 
120
- // Send communication counts
144
+ // -------------------------------------------------------------
145
+ // STEP 5: Send Communication Counts
146
+ // -------------------------------------------------------------
121
147
  const sendCommCounts = await this.dataSource.query(
122
148
  `
123
- SELECT stage_id, COUNT(*) AS send_comm_count
124
- FROM crm_lead_communication
125
- WHERE stage_id IN (${stageIds.map(() => '?').join(',')}) AND mapped_entity_id = $2
126
- AND type IN ('SEND', 'LOG')
127
- GROUP BY stage_id
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
- [...stageIds, lead_id],
156
+ params_task,
130
157
  );
131
158
 
132
- // Convert to maps for quick lookup
159
+ // Convert to maps
133
160
  const taskCountMap = new Map(
134
- taskCounts.map((row) => [Number(row.stage_id), Number(row.task_count)]),
161
+ taskCounts.map((r) => [Number(r.stage_id), Number(r.task_count)]),
135
162
  );
136
163
  const meetingCountMap = new Map(
137
- meetingCounts.map((row) => [
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((row) => [
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
- // Step 4: Add counts & profile to each stage
150
-
151
- // Step 5: Group stages by stage_group_id
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); // ensure number
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
- // Step 5: Attach stages with counts to stage groups
167
- const result = stageGroups.map((stageGroup) => {
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
- return result;
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
  }