shuttlepro-shared 1.4.20 → 1.4.22

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.
@@ -1,4 +1,5 @@
1
1
  const Call = require("../../models/Call");
2
+ const { Types } = require("mongoose");
2
3
 
3
4
  class CallRepository {
4
5
  constructor() {}
@@ -280,20 +281,41 @@ class CallRepository {
280
281
  /* ----------------- Reporting & Analytics ----------------- */
281
282
 
282
283
  // Stats grouped by agent
283
- async getAgentStats({ from, to }, filters = {}) {
284
- const fromDate = new Date(from);
285
- const toDate = new Date(to);
286
- toDate.setHours(23, 59, 59, 999);
287
284
 
288
- const matchStage = {
285
+ async getAgentStats({ from, to }, filters = {}) {
286
+ // Normalize to UTC day bounds
287
+ const f = new Date(from);
288
+ const t = new Date(to);
289
+ const fromDate = new Date(
290
+ Date.UTC(f.getUTCFullYear(), f.getUTCMonth(), f.getUTCDate(), 0, 0, 0, 0)
291
+ );
292
+ const toDate = new Date(
293
+ Date.UTC(
294
+ t.getUTCFullYear(),
295
+ t.getUTCMonth(),
296
+ t.getUTCDate(),
297
+ 23,
298
+ 59,
299
+ 59,
300
+ 999
301
+ )
302
+ );
303
+
304
+ const match = {
289
305
  createdAt: { $gte: fromDate, $lte: toDate },
290
306
  ...filters,
291
307
  };
308
+ if (match.workspaceId && typeof match.workspaceId === "string") {
309
+ match.workspaceId = new Types.ObjectId(match.workspaceId);
310
+ }
311
+ if (match.agentId && typeof match.agentId === "string") {
312
+ match.agentId = new Types.ObjectId(match.agentId);
313
+ }
292
314
 
293
- console.log(matchStage, "matchStage query");
315
+ console.log(match, "match");
294
316
 
295
- const result = await Call.aggregate([
296
- { $match: matchStage },
317
+ const [stats] = await Call.aggregate([
318
+ { $match: match },
297
319
  {
298
320
  $group: {
299
321
  _id: filters.agentId ? "$agentId" : null,
@@ -306,21 +328,12 @@ class CallRepository {
306
328
  },
307
329
  },
308
330
  {
309
- $project: {
310
- _id: 0,
311
- totalCalls: 1,
312
- pending: 1,
313
- abandoned: 1,
314
- ended: 1,
315
- },
331
+ $project: { _id: 0, totalCalls: 1, pending: 1, abandoned: 1, ended: 1 },
316
332
  },
317
333
  ]);
318
334
 
319
- if (result.length === 0) {
320
- return { totalCalls: 0, pending: 0, abandoned: 0, ended: 0 };
321
- }
322
-
323
- return result[0];
335
+ // Always return a single object
336
+ return stats || { totalCalls: 0, pending: 0, abandoned: 0, ended: 0 };
324
337
  }
325
338
 
326
339
  // Daily call summary
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.4.20",
3
+ "version": "1.4.22",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {