shuttlepro-shared 1.4.13 → 1.4.15

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.
@@ -35,6 +35,31 @@ class CallRepository {
35
35
  }
36
36
  }
37
37
 
38
+ async endCall(callId, agentId = null, updateObj = {}, callHistory = {}) {
39
+ try {
40
+ return await Call.findOneAndUpdate(
41
+ { callId },
42
+ {
43
+ $set: {
44
+ ...(updateObj?.status === "ended" ? { endTime: new Date() } : {}),
45
+ ...updateObj,
46
+ },
47
+ $push: {
48
+ callHistory: {
49
+ ...callHistory,
50
+ timestamp: new Date(),
51
+ agentId,
52
+ },
53
+ },
54
+ },
55
+ { new: true }
56
+ );
57
+ } catch (error) {
58
+ console.error(`❌ Failed to end call ${callId}:`, error);
59
+ return null;
60
+ }
61
+ }
62
+
38
63
  async updateCall(callId, updateData) {
39
64
  try {
40
65
  const allowedFields = [
@@ -70,6 +95,7 @@ class CallRepository {
70
95
  agentId: updateData.agentId || null,
71
96
  details:
72
97
  updateData.reason || `Status changed to ${updateData.status}`,
98
+ direction: "Agent Initiated",
73
99
  },
74
100
  };
75
101
  }
@@ -165,46 +191,27 @@ class CallRepository {
165
191
  }
166
192
  }
167
193
 
168
- async endCall(callId, agentId = null, updateObj = {}) {
169
- try {
170
- return await Call.findOneAndUpdate(
171
- { callId },
172
- {
173
- $set: {
174
- status: "ended",
175
- endTime: new Date(),
176
- "connectionMetadata.lastHeartbeat": new Date(),
177
- ...updateObj,
178
- },
179
- $push: {
180
- callHistory: {
181
- timestamp: new Date(),
182
- action: "ended",
183
- agentId,
184
- details: "Call ended",
185
- },
186
- },
187
- },
188
- { new: true }
189
- );
190
- } catch (error) {
191
- console.error(`❌ Failed to end call ${callId}:`, error);
192
- return null;
193
- }
194
- }
195
-
196
194
  /* ----------------- Query Helpers ----------------- */
197
195
 
198
196
  async getCallsByWorkspace(workspaceId, filters = {}) {
199
- return Call.find({ workspaceId, ...filters }).sort({ createdAt: -1 });
197
+ return Call.find({ workspaceId, ...filters })
198
+ .sort({ createdAt: -1 })
199
+ .lean()
200
+ .exec();
200
201
  }
201
202
 
202
203
  async getCallsByAgent(agentId, filters = {}) {
203
- return Call.find({ agentId, ...filters }).sort({ createdAt: -1 });
204
+ return Call.find({ agentId, ...filters })
205
+ .sort({ createdAt: -1 })
206
+ .lean()
207
+ .exec();
204
208
  }
205
209
 
206
210
  async getCallsByReceiver(receiver, filters = {}) {
207
- return Call.find({ receiver, ...filters }).sort({ createdAt: -1 });
211
+ return Call.find({ receiver, ...filters })
212
+ .sort({ createdAt: -1 })
213
+ .lean()
214
+ .exec();
208
215
  }
209
216
 
210
217
  async getActiveCalls(workspaceId) {
@@ -236,8 +243,11 @@ class CallRepository {
236
243
  $group: {
237
244
  _id: "$agentId",
238
245
  totalCalls: { $sum: 1 },
239
- answered: {
240
- $sum: { $cond: [{ $eq: ["$status", "active"] }, 1, 0] },
246
+ abandoned: {
247
+ $sum: { $cond: [{ $eq: ["$status", "pending"] }, 1, 0] },
248
+ },
249
+ abandoned: {
250
+ $sum: { $cond: [{ $eq: ["$status", "abandoned"] }, 1, 0] },
241
251
  },
242
252
  ended: {
243
253
  $sum: { $cond: [{ $eq: ["$status", "ended"] }, 1, 0] },
@@ -248,6 +258,42 @@ class CallRepository {
248
258
  ]);
249
259
  }
250
260
 
261
+ async getAgentStatsByAgentId(workspaceId, agentId, from, to) {
262
+ const result = await Call.aggregate([
263
+ {
264
+ $match: {
265
+ workspaceId,
266
+ agentId,
267
+ createdAt: { $gte: from, $lte: to },
268
+ },
269
+ },
270
+ {
271
+ $group: {
272
+ _id: "$agentId",
273
+ totalCalls: { $sum: 1 },
274
+ pending: {
275
+ $sum: { $cond: [{ $eq: ["$status", "pending"] }, 1, 0] },
276
+ },
277
+ abandoned: {
278
+ $sum: { $cond: [{ $eq: ["$status", "abandoned"] }, 1, 0] },
279
+ },
280
+ ended: {
281
+ $sum: { $cond: [{ $eq: ["$status", "ended"] }, 1, 0] },
282
+ },
283
+ },
284
+ },
285
+ ]);
286
+ return (
287
+ result[0] || {
288
+ _id: agentId,
289
+ totalCalls: 0,
290
+ pending: 0,
291
+ abandoned: 0,
292
+ ended: 0,
293
+ }
294
+ );
295
+ }
296
+
251
297
  // Daily call summary
252
298
  async getDailySummary(workspaceId, from, to) {
253
299
  return Call.aggregate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.4.13",
3
+ "version": "1.4.15",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {