shuttlepro-shared 1.4.21 → 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,26 +281,41 @@ class CallRepository {
280
281
  /* ----------------- Reporting & Analytics ----------------- */
281
282
 
282
283
  // Stats grouped by agent
283
- async getAgentStats({ from, to }, filters = {}) {
284
- // Pakistan offset (+5 hours)
285
- const PAK_OFFSET = 5 * 60 * 60 * 1000;
286
-
287
- // convert input to Date objects
288
- const fromDate = new Date(new Date(from).getTime() - PAK_OFFSET);
289
- const toDate = new Date(new Date(to).getTime() - PAK_OFFSET);
290
-
291
- // set end of day in Pakistan time
292
- toDate.setHours(23, 59, 59, 999);
293
284
 
294
- console.log({ fromDate, toDate }, filters);
295
-
296
- 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 = {
297
305
  createdAt: { $gte: fromDate, $lte: toDate },
298
306
  ...filters,
299
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
+ }
300
314
 
301
- const result = await Call.aggregate([
302
- { $match: matchStage },
315
+ console.log(match, "match");
316
+
317
+ const [stats] = await Call.aggregate([
318
+ { $match: match },
303
319
  {
304
320
  $group: {
305
321
  _id: filters.agentId ? "$agentId" : null,
@@ -312,21 +328,12 @@ class CallRepository {
312
328
  },
313
329
  },
314
330
  {
315
- $project: {
316
- _id: 0,
317
- totalCalls: 1,
318
- pending: 1,
319
- abandoned: 1,
320
- ended: 1,
321
- },
331
+ $project: { _id: 0, totalCalls: 1, pending: 1, abandoned: 1, ended: 1 },
322
332
  },
323
333
  ]);
324
334
 
325
- if (result.length === 0) {
326
- return { totalCalls: 0, pending: 0, abandoned: 0, ended: 0 };
327
- }
328
-
329
- return result[0];
335
+ // Always return a single object
336
+ return stats || { totalCalls: 0, pending: 0, abandoned: 0, ended: 0 };
330
337
  }
331
338
 
332
339
  // Daily call summary
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.4.21",
3
+ "version": "1.4.22",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {