@talkpilot/core-db 1.1.18 → 1.2.0

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.
Files changed (133) hide show
  1. package/.cursor/rules/development.mdc +65 -65
  2. package/DEVELOPMENT.md +98 -98
  3. package/README.md +160 -160
  4. package/dist/talkpilot/calls/calls.getters.d.ts +2 -1
  5. package/dist/talkpilot/calls/calls.getters.d.ts.map +1 -1
  6. package/dist/talkpilot/calls/calls.getters.js +176 -0
  7. package/dist/talkpilot/calls/calls.getters.js.map +1 -1
  8. package/dist/talkpilot/calls/calls.types.d.ts +49 -2
  9. package/dist/talkpilot/calls/calls.types.d.ts.map +1 -1
  10. package/dist/talkpilot/calls/calls.types.js.map +1 -1
  11. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +1 -0
  12. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
  13. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +13 -0
  14. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
  15. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +2 -0
  16. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
  17. package/jest.config.js +19 -19
  18. package/package.json +45 -45
  19. package/src/__tests__/setup.ts +20 -20
  20. package/src/connection.ts +42 -42
  21. package/src/index.ts +16 -16
  22. package/src/municipal/__tests__/validation.spec.ts +62 -62
  23. package/src/municipal/cities/cities.getters.ts +50 -50
  24. package/src/municipal/cities/cities.types.ts +11 -11
  25. package/src/municipal/cities/index.ts +2 -2
  26. package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +282 -282
  27. package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +72 -72
  28. package/src/municipal/departmentsSubjects/index.ts +9 -9
  29. package/src/municipal/index.ts +21 -21
  30. package/src/municipal/mongodb-client.ts +61 -61
  31. package/src/municipal/streets/index.ts +2 -2
  32. package/src/municipal/streets/streets.getters.ts +125 -125
  33. package/src/municipal/streets/streets.types.ts +18 -18
  34. package/src/municipal/systemInstructions/__tests__/getters.spec.ts +113 -113
  35. package/src/municipal/systemInstructions/__tests__/setters.spec.ts +274 -274
  36. package/src/municipal/systemInstructions/index.ts +7 -7
  37. package/src/municipal/systemInstructions/instructions.getters.ts +57 -57
  38. package/src/municipal/systemInstructions/instructions.setters.ts +119 -119
  39. package/src/municipal/systemInstructions/instructions.types.ts +30 -30
  40. package/src/municipal/tickets/__tests__/tickets.getters.spec.ts +66 -66
  41. package/src/municipal/tickets/index.ts +2 -2
  42. package/src/municipal/tickets/tickets.getters.ts +261 -261
  43. package/src/municipal/tickets/tickets.types.ts +43 -43
  44. package/src/municipal/utils/types.ts +11 -11
  45. package/src/talkpilot/__tests__/db.spec.ts +38 -38
  46. package/src/talkpilot/__tests__/mongodb-client.spec.ts +18 -18
  47. package/src/talkpilot/__tests__/validation.spec.ts +68 -68
  48. package/src/talkpilot/agents/__tests__/agents.getters.spec.ts +29 -29
  49. package/src/talkpilot/agents/agents.getters.ts +34 -34
  50. package/src/talkpilot/agents/agents.types.ts +14 -14
  51. package/src/talkpilot/agents/index.ts +2 -2
  52. package/src/talkpilot/backgroundToolResults/__tests__/backgroundToolResults.getters.spec.ts +147 -147
  53. package/src/talkpilot/backgroundToolResults/backgroundToolResults.getters.ts +65 -65
  54. package/src/talkpilot/backgroundToolResults/backgroundToolResults.types.ts +23 -23
  55. package/src/talkpilot/backgroundToolResults/index.ts +2 -2
  56. package/src/talkpilot/calls/__tests__/callStats.utils.spec.ts +128 -128
  57. package/src/talkpilot/calls/__tests__/calls.spec.ts +252 -252
  58. package/src/talkpilot/calls/calls.getters.ts +446 -248
  59. package/src/talkpilot/calls/calls.types.ts +171 -116
  60. package/src/talkpilot/calls/index.ts +2 -2
  61. package/src/talkpilot/clientAudioBuffers/__tests__/clientAudioBuffer.getters.spec.ts +160 -160
  62. package/src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts +117 -117
  63. package/src/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.ts +25 -25
  64. package/src/talkpilot/clientAudioBuffers/index.ts +2 -2
  65. package/src/talkpilot/clients/clients.getters.ts +16 -16
  66. package/src/talkpilot/clients/clients.types.ts +14 -14
  67. package/src/talkpilot/clients/index.ts +2 -2
  68. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +106 -106
  69. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +44 -22
  70. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +94 -92
  71. package/src/talkpilot/clientsConfig/index.ts +2 -2
  72. package/src/talkpilot/flows/__tests__/flows.schema.spec.ts +67 -67
  73. package/src/talkpilot/flows/flows.getter.ts +14 -14
  74. package/src/talkpilot/flows/flows.schema.ts +153 -153
  75. package/src/talkpilot/flows/flows.types.ts +184 -184
  76. package/src/talkpilot/flows/index.ts +2 -2
  77. package/src/talkpilot/groups/__tests__/groups.spec.ts +90 -90
  78. package/src/talkpilot/groups/__tests__/phone.utils.spec.ts +32 -32
  79. package/src/talkpilot/groups/groups.getters.ts +30 -30
  80. package/src/talkpilot/groups/groups.types.ts +29 -29
  81. package/src/talkpilot/groups/index.ts +3 -3
  82. package/src/talkpilot/groups/phone.utils.ts +46 -46
  83. package/src/talkpilot/index.ts +29 -29
  84. package/src/talkpilot/leads/index.ts +2 -2
  85. package/src/talkpilot/leads/leads.getter.ts +6 -6
  86. package/src/talkpilot/leads/leads.schema.ts +33 -33
  87. package/src/talkpilot/leads/leads.types.ts +20 -20
  88. package/src/talkpilot/mongodb-client.ts +78 -78
  89. package/src/talkpilot/phone_numbers/__tests__/phone_numbers.spec.ts +247 -247
  90. package/src/talkpilot/phone_numbers/index.ts +2 -2
  91. package/src/talkpilot/phone_numbers/phone_numbers.getter.ts +154 -154
  92. package/src/talkpilot/phone_numbers/phone_numbers.schema.ts +17 -17
  93. package/src/talkpilot/phone_numbers/phone_numbers.types.ts +30 -30
  94. package/src/talkpilot/plans/__tests__/plans.spec.ts +70 -70
  95. package/src/talkpilot/plans/index.ts +2 -2
  96. package/src/talkpilot/plans/plans.getters.ts +132 -132
  97. package/src/talkpilot/plans/plans.types.ts +89 -89
  98. package/src/talkpilot/results/index.ts +7 -7
  99. package/src/talkpilot/results/results.getter.ts +35 -35
  100. package/src/talkpilot/results/results.schema.ts +25 -25
  101. package/src/talkpilot/results/results.types.ts +34 -34
  102. package/src/talkpilot/retry_analyze/__tests__/retryAnalyze.getters.spec.ts +156 -156
  103. package/src/talkpilot/retry_analyze/index.ts +2 -2
  104. package/src/talkpilot/retry_analyze/retryAnalyze.getters.ts +75 -75
  105. package/src/talkpilot/retry_analyze/retryAnalyze.types.ts +13 -13
  106. package/src/talkpilot/sessions/__tests__/sessions.spec.ts +147 -147
  107. package/src/talkpilot/sessions/index.ts +2 -2
  108. package/src/talkpilot/sessions/sessions.getter.ts +92 -92
  109. package/src/talkpilot/sessions/sessions.schema.ts +34 -34
  110. package/src/talkpilot/sessions/sessions.types.ts +30 -30
  111. package/src/talkpilot/subscriptions/__tests__/subscriptions.getters.utils.spec.ts +45 -45
  112. package/src/talkpilot/subscriptions/index.ts +3 -3
  113. package/src/talkpilot/subscriptions/subscriptions.getters.ts +146 -146
  114. package/src/talkpilot/subscriptions/subscriptions.getters.utils.ts +33 -33
  115. package/src/talkpilot/subscriptions/subscriptions.types.ts +66 -66
  116. package/src/talkpilot/utils/__tests__/query.utils.spec.ts +49 -49
  117. package/src/talkpilot/utils/query.utils.ts +21 -21
  118. package/src/test-utils/db-utils.ts +24 -24
  119. package/src/test-utils/factories/index.ts +12 -12
  120. package/src/test-utils/factories/municipal/cities.ts +16 -16
  121. package/src/test-utils/factories/municipal/departmentsSubjects.ts +37 -37
  122. package/src/test-utils/factories/municipal/streets.ts +22 -22
  123. package/src/test-utils/factories/municipal/tickets.ts +39 -39
  124. package/src/test-utils/factories/talkpilot/agents.ts +19 -19
  125. package/src/test-utils/factories/talkpilot/calls.ts +37 -37
  126. package/src/test-utils/factories/talkpilot/clientAudioBuffers.ts +20 -20
  127. package/src/test-utils/factories/talkpilot/clientsConfig.ts +18 -18
  128. package/src/test-utils/factories/talkpilot/flows.ts +33 -33
  129. package/src/test-utils/factories/talkpilot/groups.ts +33 -33
  130. package/src/test-utils/factories/talkpilot/phone_numbers.ts +22 -22
  131. package/src/test-utils/factories/talkpilot/sessions.ts +35 -35
  132. package/src/utils/validation.ts +23 -23
  133. package/tsconfig.json +23 -23
@@ -1,248 +1,446 @@
1
- import {
2
- Call,
3
- CallDoc,
4
- CallQueryOptions,
5
- CallUpdateParams,
6
- getDb,
7
- } from "../index";
8
- import type { CallsByHour, CountOpts, DateRange, ToolExecution } from "./calls.types";
9
- import { Filter, ObjectId } from "mongodb";
10
- import * as process from "node:process";
11
- import { applyQueryOptions } from "../utils/query.utils";
12
-
13
- export const getCallsCollection = () => {
14
- return getDb().collection<Call>("calls");
15
- };
16
-
17
- export const getCallByCallSid = (callSid: string) => {
18
- return getCallsCollection().findOne({ callSid });
19
- };
20
-
21
- export const getCallsByPhoneNumber = (phoneNumber: string) => {
22
- return getCallsCollection()
23
- .find({ customerPhoneNumber: phoneNumber })
24
- .toArray();
25
- };
26
-
27
- export const getCallsByClient = (clientId: string) => {
28
- return getCallsCollection().find({ clientId }).toArray();
29
- };
30
-
31
- export const getCallsByFlow = (flowId: ObjectId) => {
32
- return getCallsCollection().find({ flowId }).toArray();
33
- };
34
-
35
- export const createCallDoc = (
36
- call: Omit<Call, "createdAt" | "updatedAt" | "env">,
37
- ) => {
38
- return getCallsCollection().insertOne({
39
- ...call,
40
- createdAt: new Date(),
41
- updatedAt: new Date(),
42
- env: process.env.ENV ?? "unknown",
43
- });
44
- };
45
-
46
- export const updateCallByCallSid = async (
47
- callSid: string,
48
- updates: CallUpdateParams,
49
- ): Promise<CallDoc | null> => {
50
- return await getCallsCollection().findOneAndUpdate(
51
- { callSid: callSid },
52
- {
53
- $set: {
54
- ...updates,
55
- updatedAt: new Date(),
56
- },
57
- },
58
- { returnDocument: "after" },
59
- );
60
- };
61
-
62
- export const pushToolExecution = async (
63
- callSid: string,
64
- execution: ToolExecution
65
- ): Promise<void> => {
66
- await getCallsCollection().updateOne(
67
- { callSid },
68
- { $push: { toolExecutions: execution } }
69
- );
70
- };
71
-
72
- // get calls by client and date range
73
- //client here is the user id
74
- export const getCallsByClientAndDateRange = (
75
- clientId: string,
76
- startDate: Date,
77
- endDate: Date,
78
- ) => {
79
- return getCallsCollection()
80
- .find({
81
- clientId,
82
- createdAt: {
83
- $gte: startDate,
84
- $lte: endDate,
85
- },
86
- })
87
- .toArray();
88
- };
89
-
90
- export const findCallsByQuery = async (
91
- query: Filter<Call>,
92
- options?: CallQueryOptions,
93
- ): Promise<CallDoc[]> => {
94
- const cursor = getCallsCollection().find(query);
95
- return await applyQueryOptions(cursor, options).toArray();
96
- };
97
-
98
- export const countCalls = async (query: Filter<Call>): Promise<number> => {
99
- return getCallsCollection().countDocuments(query);
100
- };
101
-
102
- export async function countCallsByPhoneInRange(
103
- customerPhoneNumber: string,
104
- opts: CountOpts = {},
105
- range: DateRange = {},
106
- ): Promise<number> {
107
- const filter: Partial<Call> & { customerPhoneNumber: string } = {
108
- customerPhoneNumber,
109
- };
110
-
111
- if (opts.isOutgoingCall !== undefined) {
112
- filter.isOutgoingCall = opts.isOutgoingCall;
113
- }
114
- if (opts.isIncomingCall !== undefined) {
115
- filter.isIncomingCall = opts.isIncomingCall;
116
- }
117
-
118
- if (range.since || range.until) {
119
- (filter as Filter<Call>).createdAt = {
120
- ...(range.since ? { $gte: range.since } : {}),
121
- ...(range.until ? { $lt: range.until } : {}),
122
- };
123
- }
124
-
125
- return getCallsCollection().countDocuments(filter as Filter<Call>);
126
- }
127
-
128
- export const range = {
129
- between: (since: Date, until: Date): DateRange => ({ since, until }),
130
- todayToNow: (): DateRange => ({ since: startOfDay(), until: new Date() }),
131
- yesterday: (): DateRange => {
132
- const s = startOfDay(addDays(new Date(), -1));
133
- const e = addDays(s, 1);
134
- return { since: s, until: e };
135
- },
136
- todayFull: (): DateRange => {
137
- const s = startOfDay();
138
- const e = addDays(s, 1);
139
- return { since: s, until: e };
140
- },
141
- startOfDayToNow: (d: Date): DateRange => ({
142
- since: startOfDay(d),
143
- until: new Date(),
144
- }),
145
- };
146
-
147
- export function startOfDay(d: Date = new Date()): Date {
148
- const x = new Date(d);
149
- x.setUTCHours(0, 0, 0, 0);
150
- return x;
151
- }
152
-
153
- export function addDays(d: Date, days: number): Date {
154
- const x = new Date(d);
155
- x.setDate(x.getDate() + days);
156
- return x;
157
- }
158
-
159
- /**
160
- * Aggregate calls stats for a date range (createdAt in [startStr, endStr] in timezone).
161
- * completed = agentHungUp=true or status='completed'.
162
- */
163
- export async function getCallsStatsForDateRange(
164
- clientId: string,
165
- startStr: string,
166
- endStr: string,
167
- timezone: string,
168
- ): Promise<{ count: number; totalLen: number; completed: number }> {
169
- const coll = getCallsCollection();
170
- const out = await coll
171
- .aggregate<{
172
- _id: null;
173
- count: number;
174
- totalLen: number;
175
- completed: number;
176
- }>([
177
- // 1. Restrict to the given client
178
- { $match: { clientId } },
179
- // 2. Derive dateLocal (createdAt as YYYY-MM-DD in timezone) and isCompleted (agentHungUp or status='completed')
180
- {
181
- $addFields: {
182
- dateLocal: {
183
- $dateToString: { format: "%Y-%m-%d", date: "$createdAt", timezone },
184
- },
185
- isCompleted: {
186
- $or: [
187
- { $eq: ["$agentHungUp", true] },
188
- { $eq: ["$status", "completed"] },
189
- ],
190
- },
191
- },
192
- },
193
- // 3. Keep only documents with dateLocal in [startStr, endStr] (inclusive)
194
- { $match: { dateLocal: { $gte: startStr, $lte: endStr } } },
195
- // 4. Single group: count, totalLen, completed
196
- {
197
- $group: {
198
- _id: null,
199
- count: { $sum: 1 },
200
- totalLen: { $sum: "$callLength" },
201
- completed: { $sum: { $cond: ["$isCompleted", 1, 0] } },
202
- },
203
- },
204
- ])
205
- .next();
206
- return {
207
- count: out?.count ?? 0,
208
- totalLen: out?.totalLen ?? 0,
209
- completed: out?.completed ?? 0,
210
- };
211
- }
212
-
213
- /**
214
- * Aggregate calls by hour for a given date (createdAt converted to given timezone). Hour format "HH:mm".
215
- */
216
- export async function getCallsHourlyAggregation(
217
- clientId: string,
218
- dateStr: string,
219
- timezone: string,
220
- ): Promise<CallsByHour[]> {
221
- const coll = getCallsCollection();
222
- const rows = await coll
223
- .aggregate<{ _id: number; calls: number }>([
224
- // 1. Restrict to the given client
225
- { $match: { clientId } },
226
- // 2. Derive dateLocal (createdAt as YYYY-MM-DD in timezone) and hour (0–23 from createdAt in timezone)
227
- {
228
- $addFields: {
229
- dateLocal: {
230
- $dateToString: { format: "%Y-%m-%d", date: "$createdAt", timezone },
231
- },
232
- hour: { $hour: { date: "$createdAt", timezone } },
233
- },
234
- },
235
- // 3. Keep only the requested date
236
- { $match: { dateLocal: dateStr } },
237
- // 4. Group by hour, sum calls per hour
238
- { $group: { _id: "$hour", calls: { $sum: 1 } } },
239
- // 5. Order by hour ascending (0..23)
240
- { $sort: { _id: 1 } },
241
- ])
242
- .toArray();
243
-
244
- return rows.map((r: any) => ({
245
- hour: `${String(r._id).padStart(2, "0")}:00`,
246
- calls: r.calls,
247
- }));
248
- }
1
+ import {
2
+ Call,
3
+ CallDoc,
4
+ CallQueryOptions,
5
+ CallUpdateParams,
6
+ getDb,
7
+ } from "../index";
8
+ import type { CallsByHour, CountOpts, DateRange, ToolExecution,HourlyBucket,DailyBucket,CallLengthBuckets,DashboardKpis,DashboardCharts,RecentCall,DashboardStatsResult, DashboardStatsParams } from "./calls.types";
9
+ import { Filter, ObjectId } from "mongodb";
10
+ import * as process from "node:process";
11
+ import { applyQueryOptions } from "../utils/query.utils";
12
+
13
+ const TIMEZONE = 'UTC';
14
+
15
+ export const getCallsCollection = () => {
16
+ return getDb().collection<Call>("calls");
17
+ };
18
+
19
+ export const getCallByCallSid = (callSid: string) => {
20
+ return getCallsCollection().findOne({ callSid });
21
+ };
22
+
23
+ export const getCallsByPhoneNumber = (phoneNumber: string) => {
24
+ return getCallsCollection()
25
+ .find({ customerPhoneNumber: phoneNumber })
26
+ .toArray();
27
+ };
28
+
29
+ export const getCallsByClient = (clientId: string) => {
30
+ return getCallsCollection().find({ clientId }).toArray();
31
+ };
32
+
33
+ export const getCallsByFlow = (flowId: ObjectId) => {
34
+ return getCallsCollection().find({ flowId }).toArray();
35
+ };
36
+
37
+ export const createCallDoc = (
38
+ call: Omit<Call, "createdAt" | "updatedAt" | "env">,
39
+ ) => {
40
+ return getCallsCollection().insertOne({
41
+ ...call,
42
+ createdAt: new Date(),
43
+ updatedAt: new Date(),
44
+ env: process.env.ENV ?? "unknown",
45
+ });
46
+ };
47
+
48
+ export const updateCallByCallSid = async (
49
+ callSid: string,
50
+ updates: CallUpdateParams,
51
+ ): Promise<CallDoc | null> => {
52
+ return await getCallsCollection().findOneAndUpdate(
53
+ { callSid: callSid },
54
+ {
55
+ $set: {
56
+ ...updates,
57
+ updatedAt: new Date(),
58
+ },
59
+ },
60
+ { returnDocument: "after" },
61
+ );
62
+ };
63
+
64
+ export const pushToolExecution = async (
65
+ callSid: string,
66
+ execution: ToolExecution
67
+ ): Promise<void> => {
68
+ await getCallsCollection().updateOne(
69
+ { callSid },
70
+ { $push: { toolExecutions: execution } }
71
+ );
72
+ };
73
+
74
+ // get calls by client and date range
75
+ //client here is the user id
76
+ export const getCallsByClientAndDateRange = (
77
+ clientId: string,
78
+ startDate: Date,
79
+ endDate: Date,
80
+ ) => {
81
+ return getCallsCollection()
82
+ .find({
83
+ clientId,
84
+ createdAt: {
85
+ $gte: startDate,
86
+ $lte: endDate,
87
+ },
88
+ })
89
+ .toArray();
90
+ };
91
+
92
+ export const findCallsByQuery = async (
93
+ query: Filter<Call>,
94
+ options?: CallQueryOptions,
95
+ ): Promise<CallDoc[]> => {
96
+ const cursor = getCallsCollection().find(query);
97
+ return await applyQueryOptions(cursor, options).toArray();
98
+ };
99
+
100
+ export const countCalls = async (query: Filter<Call>): Promise<number> => {
101
+ return getCallsCollection().countDocuments(query);
102
+ };
103
+
104
+ export async function countCallsByPhoneInRange(
105
+ customerPhoneNumber: string,
106
+ opts: CountOpts = {},
107
+ range: DateRange = {},
108
+ ): Promise<number> {
109
+ const filter: Partial<Call> & { customerPhoneNumber: string } = {
110
+ customerPhoneNumber,
111
+ };
112
+
113
+ if (opts.isOutgoingCall !== undefined) {
114
+ filter.isOutgoingCall = opts.isOutgoingCall;
115
+ }
116
+ if (opts.isIncomingCall !== undefined) {
117
+ filter.isIncomingCall = opts.isIncomingCall;
118
+ }
119
+
120
+ if (range.since || range.until) {
121
+ (filter as Filter<Call>).createdAt = {
122
+ ...(range.since ? { $gte: range.since } : {}),
123
+ ...(range.until ? { $lt: range.until } : {}),
124
+ };
125
+ }
126
+
127
+ return getCallsCollection().countDocuments(filter as Filter<Call>);
128
+ }
129
+
130
+ export const range = {
131
+ between: (since: Date, until: Date): DateRange => ({ since, until }),
132
+ todayToNow: (): DateRange => ({ since: startOfDay(), until: new Date() }),
133
+ yesterday: (): DateRange => {
134
+ const s = startOfDay(addDays(new Date(), -1));
135
+ const e = addDays(s, 1);
136
+ return { since: s, until: e };
137
+ },
138
+ todayFull: (): DateRange => {
139
+ const s = startOfDay();
140
+ const e = addDays(s, 1);
141
+ return { since: s, until: e };
142
+ },
143
+ startOfDayToNow: (d: Date): DateRange => ({
144
+ since: startOfDay(d),
145
+ until: new Date(),
146
+ }),
147
+ };
148
+
149
+ export function startOfDay(d: Date = new Date()): Date {
150
+ const x = new Date(d);
151
+ x.setUTCHours(0, 0, 0, 0);
152
+ return x;
153
+ }
154
+
155
+ export function addDays(d: Date, days: number): Date {
156
+ const x = new Date(d);
157
+ x.setDate(x.getDate() + days);
158
+ return x;
159
+ }
160
+
161
+ /**
162
+ * Aggregate calls stats for a date range (createdAt in [startStr, endStr] in timezone).
163
+ * completed = agentHungUp=true or status='completed'.
164
+ */
165
+ export async function getCallsStatsForDateRange(
166
+ clientId: string,
167
+ startStr: string,
168
+ endStr: string,
169
+ timezone: string,
170
+ ): Promise<{ count: number; totalLen: number; completed: number }> {
171
+ const coll = getCallsCollection();
172
+ const out = await coll
173
+ .aggregate<{
174
+ _id: null;
175
+ count: number;
176
+ totalLen: number;
177
+ completed: number;
178
+ }>([
179
+ // 1. Restrict to the given client
180
+ { $match: { clientId } },
181
+ // 2. Derive dateLocal (createdAt as YYYY-MM-DD in timezone) and isCompleted (agentHungUp or status='completed')
182
+ {
183
+ $addFields: {
184
+ dateLocal: {
185
+ $dateToString: { format: "%Y-%m-%d", date: "$createdAt", timezone },
186
+ },
187
+ isCompleted: {
188
+ $or: [
189
+ { $eq: ["$agentHungUp", true] },
190
+ { $eq: ["$status", "completed"] },
191
+ ],
192
+ },
193
+ },
194
+ },
195
+ // 3. Keep only documents with dateLocal in [startStr, endStr] (inclusive)
196
+ { $match: { dateLocal: { $gte: startStr, $lte: endStr } } },
197
+ // 4. Single group: count, totalLen, completed
198
+ {
199
+ $group: {
200
+ _id: null,
201
+ count: { $sum: 1 },
202
+ totalLen: { $sum: "$callLength" },
203
+ completed: { $sum: { $cond: ["$isCompleted", 1, 0] } },
204
+ },
205
+ },
206
+ ])
207
+ .next();
208
+ return {
209
+ count: out?.count ?? 0,
210
+ totalLen: out?.totalLen ?? 0,
211
+ completed: out?.completed ?? 0,
212
+ };
213
+ }
214
+
215
+ /**
216
+ * Aggregate calls by hour for a given date (createdAt converted to given timezone). Hour format "HH:mm".
217
+ */
218
+ export async function getCallsHourlyAggregation(
219
+ clientId: string,
220
+ dateStr: string,
221
+ timezone: string,
222
+ ): Promise<CallsByHour[]> {
223
+ const coll = getCallsCollection();
224
+ const rows = await coll
225
+ .aggregate<{ _id: number; calls: number }>([
226
+ // 1. Restrict to the given client
227
+ { $match: { clientId } },
228
+ // 2. Derive dateLocal (createdAt as YYYY-MM-DD in timezone) and hour (0–23 from createdAt in timezone)
229
+ {
230
+ $addFields: {
231
+ dateLocal: {
232
+ $dateToString: { format: "%Y-%m-%d", date: "$createdAt", timezone },
233
+ },
234
+ hour: { $hour: { date: "$createdAt", timezone } },
235
+ },
236
+ },
237
+ // 3. Keep only the requested date
238
+ { $match: { dateLocal: dateStr } },
239
+ // 4. Group by hour, sum calls per hour
240
+ { $group: { _id: "$hour", calls: { $sum: 1 } } },
241
+ // 5. Order by hour ascending (0..23)
242
+ { $sort: { _id: 1 } },
243
+ ])
244
+ .toArray();
245
+
246
+ return rows.map((r: any) => ({
247
+ hour: `${String(r._id).padStart(2, "0")}:00`,
248
+ calls: r.calls,
249
+ }));
250
+ }
251
+
252
+ function buildKpisPipeline() {
253
+ return [
254
+ {
255
+ $group: {
256
+ _id: null,
257
+ totalCalls: { $sum: 1 },
258
+ totalDuration: { $sum: '$callLength' },
259
+ completedCount: {
260
+ $sum: { $cond: [{ $eq: ['$status', 'completed'] }, 1, 0] },
261
+ },
262
+ failedCount: {
263
+ $sum: { $cond: [{ $eq: ['$status', 'failed'] }, 1, 0] },
264
+ },
265
+ noAnswerCount: {
266
+ $sum: { $cond: [{ $eq: ['$status', 'no-answer'] }, 1, 0] },
267
+ },
268
+ busyCount: {
269
+ $sum: { $cond: [{ $eq: ['$status', 'busy'] }, 1, 0] },
270
+ },
271
+ },
272
+ },
273
+ ];
274
+ }
275
+
276
+ function buildDailyDataPipeline(timezone: string) {
277
+ return [
278
+ {
279
+ $group: {
280
+ _id: {
281
+ $dateToString: { format: '%Y-%m-%d', date: '$createdAt', timezone: timezone },
282
+ },
283
+ count: { $sum: 1 },
284
+ completed: {
285
+ $sum: { $cond: [{ $eq: ['$status', 'completed'] }, 1, 0] },
286
+ },
287
+ },
288
+ },
289
+ { $sort: { _id: 1 } },
290
+ ];
291
+ }
292
+
293
+ function buildHourlyDataPipeline(timezone: string) {
294
+ return [
295
+ {
296
+ $group: {
297
+ _id: {
298
+ day: { $dateToString: { format: '%Y-%m-%d', date: '$createdAt', timezone: timezone } },
299
+ hour: { $hour: { date: '$createdAt', timezone: timezone } },
300
+ },
301
+ count: { $sum: 1 },
302
+ },
303
+ },
304
+ ];
305
+ }
306
+
307
+ function buildCallLengthBucketsPipeline() {
308
+ return [
309
+ {
310
+ $group: {
311
+ _id: null,
312
+ short: { $sum: { $cond: [{ $lt: ['$callLength', 60] }, 1, 0] } },
313
+ medium: {
314
+ $sum: {
315
+ $cond: [
316
+ { $and: [{ $gte: ['$callLength', 60] }, { $lte: ['$callLength', 180] }] },
317
+ 1,
318
+ 0,
319
+ ],
320
+ },
321
+ },
322
+ long: { $sum: { $cond: [{ $gt: ['$callLength', 180] }, 1, 0] } },
323
+ },
324
+ },
325
+ ];
326
+ }
327
+
328
+ function buildRecentCallsPipeline() {
329
+ return [
330
+ { $sort: { createdAt: -1 } },
331
+ { $limit: 8 },
332
+ {
333
+ $project: {
334
+ callSid: 1,
335
+ customerPhoneNumber: 1,
336
+ status: 1,
337
+ callLength: 1,
338
+ createdAt: 1,
339
+ summary: 1,
340
+ isIncomingCall: 1,
341
+ },
342
+ },
343
+ ];
344
+ }
345
+
346
+ export async function getDashboardStats(
347
+ params: DashboardStatsParams,
348
+ ): Promise<DashboardStatsResult> {
349
+ const { clientId, startDate, endDate } = params;
350
+
351
+ const startDateObj = new Date(startDate);
352
+ const endDateObj = new Date(endDate);
353
+
354
+ const startStr = startDateObj.toISOString().slice(0, 10);
355
+ const endStr = endDateObj.toISOString().slice(0, 10);
356
+
357
+ console.info('[core-db] Fetching dashboard stats', { clientId, startStr, endStr });
358
+ const pipeline = [
359
+ {
360
+ $match: {
361
+ clientId,
362
+ createdAt: { $gte: startDateObj, $lte: endDateObj },
363
+ },
364
+ },
365
+ {
366
+ $facet: {
367
+ kpis: buildKpisPipeline(),
368
+ dailyData: buildDailyDataPipeline(TIMEZONE),
369
+ hourlyData: buildHourlyDataPipeline(TIMEZONE),
370
+ callLengthBuckets: buildCallLengthBucketsPipeline(),
371
+ recentCalls: buildRecentCallsPipeline(),
372
+ },
373
+ },
374
+ ];
375
+
376
+ const callsCollection = getCallsCollection();
377
+ const [aggregatedResult] = await callsCollection.aggregate(pipeline).toArray();
378
+
379
+ const kpiData = aggregatedResult?.kpis?.[0] ?? {
380
+ totalCalls: 0,
381
+ totalDuration: 0,
382
+ completedCount: 0,
383
+ failedCount: 0,
384
+ noAnswerCount: 0,
385
+ busyCount: 0,
386
+ };
387
+ const dailyDataRaw = aggregatedResult?.dailyData ?? [];
388
+ const hourlyDataRaw = aggregatedResult?.hourlyData ?? [];
389
+ const callLengthRaw = aggregatedResult?.callLengthBuckets?.[0] ?? { short: 0, medium: 0, long: 0 };
390
+ const recentCallsRaw: RecentCall[] = aggregatedResult?.recentCalls ?? [];
391
+
392
+ const count = kpiData.totalCalls;
393
+ const totalLen = kpiData.totalDuration;
394
+ const completed = kpiData.completedCount;
395
+
396
+ const kpis: DashboardKpis = {
397
+ totalCalls: count,
398
+ avgDurationSeconds: count > 0 ? Math.round(totalLen / count) : 0,
399
+ timeSavedMinutes: Math.round(totalLen / 60),
400
+ successRate: count > 0 ? Math.round((completed / count) * 1000) / 10 : 0,
401
+ completedCount: completed,
402
+ failedCount: kpiData.failedCount,
403
+ noAnswerCount: kpiData.noAnswerCount,
404
+ busyCount: kpiData.busyCount,
405
+ };
406
+
407
+ const volumeData: DailyBucket[] = dailyDataRaw.map((d: any) => ({
408
+ date: d._id,
409
+ count: d.count,
410
+ completed: d.completed,
411
+ }));
412
+
413
+ const heatmapMap = new Map<string, Map<number, number>>();
414
+ for (const bucket of hourlyDataRaw) {
415
+ const dayKey = bucket._id.day;
416
+ const hour = bucket._id.hour;
417
+ if (!heatmapMap.has(dayKey)) heatmapMap.set(dayKey, new Map());
418
+ heatmapMap.get(dayKey)!.set(hour, bucket.count);
419
+ }
420
+
421
+ const toSortedBuckets = (map: Map<number, number>): HourlyBucket[] =>
422
+ Array.from(map.entries())
423
+ .sort(([a], [b]) => a - b)
424
+ .map(([h, c]) => ({ hour: `${String(h).padStart(2, '0')}:00`, calls: c }));
425
+
426
+ const heatmap: Record<string, HourlyBucket[]> = {};
427
+ for (const [day, hourMap] of Array.from(heatmapMap.entries()).sort()) {
428
+ heatmap[day] = toSortedBuckets(hourMap);
429
+ }
430
+
431
+ return {
432
+ kpis,
433
+ charts: {
434
+ volumeData,
435
+ heatmap,
436
+ callLengthBuckets: {
437
+ short: callLengthRaw.short,
438
+ medium: callLengthRaw.medium,
439
+ long: callLengthRaw.long,
440
+ },
441
+ },
442
+ recentCalls: recentCallsRaw,
443
+ };
444
+ }
445
+
446
+