@talkpilot/core-db 1.3.50 → 1.3.52

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 (335) hide show
  1. package/.cursor/rules/development.mdc +65 -65
  2. package/AGENTS.md +49 -49
  3. package/DEVELOPMENT.md +141 -141
  4. package/README.md +797 -778
  5. package/dist/configuration/models/models.constants.d.ts +1 -1
  6. package/dist/configuration/models/models.constants.d.ts.map +1 -1
  7. package/dist/configuration/models/models.constants.js +1 -6
  8. package/dist/configuration/models/models.constants.js.map +1 -1
  9. package/dist/talkpilot/auditLogs/auditLogs.getters.d.ts +8 -0
  10. package/dist/talkpilot/auditLogs/auditLogs.getters.d.ts.map +1 -0
  11. package/dist/talkpilot/auditLogs/auditLogs.getters.js +26 -0
  12. package/dist/talkpilot/auditLogs/auditLogs.getters.js.map +1 -0
  13. package/dist/talkpilot/auditLogs/auditLogs.setters.d.ts +3 -0
  14. package/dist/talkpilot/auditLogs/auditLogs.setters.d.ts.map +1 -0
  15. package/dist/talkpilot/auditLogs/auditLogs.setters.js +9 -0
  16. package/dist/talkpilot/auditLogs/auditLogs.setters.js.map +1 -0
  17. package/dist/talkpilot/auditLogs/auditLogs.types.d.ts +12 -0
  18. package/dist/talkpilot/auditLogs/auditLogs.types.d.ts.map +1 -0
  19. package/dist/talkpilot/auditLogs/auditLogs.types.js +3 -0
  20. package/dist/talkpilot/auditLogs/auditLogs.types.js.map +1 -0
  21. package/dist/talkpilot/auditLogs/index.d.ts +3 -0
  22. package/dist/talkpilot/auditLogs/index.d.ts.map +1 -0
  23. package/dist/talkpilot/auditLogs/index.js +19 -0
  24. package/dist/talkpilot/auditLogs/index.js.map +1 -0
  25. package/dist/talkpilot/calls/calls.constants.d.ts +1 -1
  26. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.getters.d.ts +4 -0
  27. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.getters.d.ts.map +1 -0
  28. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.getters.js +9 -0
  29. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.getters.js.map +1 -0
  30. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.setters.d.ts +3 -0
  31. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.setters.d.ts.map +1 -0
  32. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.setters.js +9 -0
  33. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.setters.js.map +1 -0
  34. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.types.d.ts +10 -0
  35. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.types.d.ts.map +1 -0
  36. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.types.js +3 -0
  37. package/dist/talkpilot/clientDeletionLogs/clientDeletionLogs.types.js.map +1 -0
  38. package/dist/talkpilot/clientDeletionLogs/index.d.ts +4 -0
  39. package/dist/talkpilot/clientDeletionLogs/index.d.ts.map +1 -0
  40. package/dist/talkpilot/clientDeletionLogs/index.js +20 -0
  41. package/dist/talkpilot/clientDeletionLogs/index.js.map +1 -0
  42. package/dist/talkpilot/clients/clients.constants.d.ts +3 -0
  43. package/dist/talkpilot/clients/clients.constants.d.ts.map +1 -0
  44. package/dist/talkpilot/clients/clients.constants.js +6 -0
  45. package/dist/talkpilot/clients/clients.constants.js.map +1 -0
  46. package/dist/talkpilot/clients/clients.getters.d.ts +1 -0
  47. package/dist/talkpilot/clients/clients.getters.d.ts.map +1 -1
  48. package/dist/talkpilot/clients/clients.getters.js +8 -1
  49. package/dist/talkpilot/clients/clients.getters.js.map +1 -1
  50. package/dist/talkpilot/clients/clients.lifecycle.d.ts +10 -0
  51. package/dist/talkpilot/clients/clients.lifecycle.d.ts.map +1 -0
  52. package/dist/talkpilot/clients/clients.lifecycle.js +95 -0
  53. package/dist/talkpilot/clients/clients.lifecycle.js.map +1 -0
  54. package/dist/talkpilot/clients/clients.types.d.ts +21 -0
  55. package/dist/talkpilot/clients/clients.types.d.ts.map +1 -1
  56. package/dist/talkpilot/clients/index.d.ts +2 -0
  57. package/dist/talkpilot/clients/index.d.ts.map +1 -1
  58. package/dist/talkpilot/clients/index.js +2 -0
  59. package/dist/talkpilot/clients/index.js.map +1 -1
  60. package/dist/talkpilot/flows/flows.getter.d.ts +1 -4
  61. package/dist/talkpilot/flows/flows.getter.d.ts.map +1 -1
  62. package/dist/talkpilot/flows/flows.getter.js +1 -12
  63. package/dist/talkpilot/flows/flows.getter.js.map +1 -1
  64. package/dist/talkpilot/flows/flows.schema.d.ts +0 -31
  65. package/dist/talkpilot/flows/flows.schema.d.ts.map +1 -1
  66. package/dist/talkpilot/flows/flows.schema.js +0 -25
  67. package/dist/talkpilot/flows/flows.schema.js.map +1 -1
  68. package/dist/talkpilot/flows/flows.types.d.ts +0 -14
  69. package/dist/talkpilot/flows/flows.types.d.ts.map +1 -1
  70. package/dist/talkpilot/flows/index.d.ts +1 -1
  71. package/dist/talkpilot/flows/index.d.ts.map +1 -1
  72. package/dist/talkpilot/flows/index.js +1 -3
  73. package/dist/talkpilot/flows/index.js.map +1 -1
  74. package/dist/talkpilot/index.d.ts +1 -0
  75. package/dist/talkpilot/index.d.ts.map +1 -1
  76. package/dist/talkpilot/index.js +1 -0
  77. package/dist/talkpilot/index.js.map +1 -1
  78. package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts +11 -0
  79. package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts.map +1 -1
  80. package/dist/talkpilot/phone_numbers/phone_numbers.getter.js +60 -1
  81. package/dist/talkpilot/phone_numbers/phone_numbers.getter.js.map +1 -1
  82. package/dist/talkpilot/results/index.d.ts +3 -0
  83. package/dist/talkpilot/results/index.d.ts.map +1 -1
  84. package/dist/talkpilot/results/index.js +22 -1
  85. package/dist/talkpilot/results/index.js.map +1 -1
  86. package/dist/talkpilot/results/results.constants.d.ts +9 -0
  87. package/dist/talkpilot/results/results.constants.d.ts.map +1 -0
  88. package/dist/talkpilot/results/results.constants.js +12 -0
  89. package/dist/talkpilot/results/results.constants.js.map +1 -0
  90. package/dist/talkpilot/results/results.getter.d.ts.map +1 -1
  91. package/dist/talkpilot/results/results.getter.js +3 -2
  92. package/dist/talkpilot/results/results.getter.js.map +1 -1
  93. package/dist/talkpilot/results/results.statistics.getters.d.ts +7 -0
  94. package/dist/talkpilot/results/results.statistics.getters.d.ts.map +1 -0
  95. package/dist/talkpilot/results/results.statistics.getters.js +244 -0
  96. package/dist/talkpilot/results/results.statistics.getters.js.map +1 -0
  97. package/dist/talkpilot/results/results.statistics.types.d.ts +42 -0
  98. package/dist/talkpilot/results/results.statistics.types.d.ts.map +1 -0
  99. package/dist/talkpilot/results/results.statistics.types.js +3 -0
  100. package/dist/talkpilot/results/results.statistics.types.js.map +1 -0
  101. package/dist/test-utils/factories/index.d.ts +2 -0
  102. package/dist/test-utils/factories/index.d.ts.map +1 -1
  103. package/dist/test-utils/factories/index.js +2 -0
  104. package/dist/test-utils/factories/index.js.map +1 -1
  105. package/dist/test-utils/factories/talkpilot/clients.d.ts +14 -0
  106. package/dist/test-utils/factories/talkpilot/clients.d.ts.map +1 -0
  107. package/dist/test-utils/factories/talkpilot/clients.js +19 -0
  108. package/dist/test-utils/factories/talkpilot/clients.js.map +1 -0
  109. package/dist/test-utils/factories/talkpilot/results.d.ts +16 -0
  110. package/dist/test-utils/factories/talkpilot/results.d.ts.map +1 -0
  111. package/dist/test-utils/factories/talkpilot/results.js +40 -0
  112. package/dist/test-utils/factories/talkpilot/results.js.map +1 -0
  113. package/jest.config.js +20 -20
  114. package/package.json +46 -46
  115. package/src/__tests__/setup.ts +20 -20
  116. package/src/bulkWrite/__tests__/bulkWrite.spec.ts +65 -65
  117. package/src/bulkWrite/bulkWrite.ops.ts +45 -45
  118. package/src/bulkWrite/bulkWrite.types.ts +4 -4
  119. package/src/bulkWrite/index.ts +2 -2
  120. package/src/config.ts +9 -9
  121. package/src/configuration/index.ts +17 -17
  122. package/src/configuration/internalModels/index.ts +3 -3
  123. package/src/configuration/internalModels/internalModels.constants.ts +1 -1
  124. package/src/configuration/internalModels/internalModels.getters.ts +33 -33
  125. package/src/configuration/internalModels/internalModels.types.ts +12 -12
  126. package/src/configuration/models/index.ts +25 -25
  127. package/src/configuration/models/models.constants.ts +13 -18
  128. package/src/configuration/models/models.getters.ts +53 -53
  129. package/src/configuration/models/models.types.ts +79 -79
  130. package/src/configuration/mongodb-client.ts +62 -62
  131. package/src/configuration/prompts/index.ts +2 -2
  132. package/src/configuration/prompts/prompts.getters.ts +49 -49
  133. package/src/configuration/prompts/prompts.types.ts +11 -11
  134. package/src/connection.ts +81 -81
  135. package/src/index.ts +46 -46
  136. package/src/municipal/__tests__/validation.spec.ts +62 -62
  137. package/src/municipal/cities/cities.getters.ts +50 -50
  138. package/src/municipal/cities/cities.types.ts +11 -11
  139. package/src/municipal/cities/index.ts +2 -2
  140. package/src/municipal/departmentsSubjects/__tests__/departmentsSubjects.spec.ts +272 -272
  141. package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +317 -317
  142. package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +73 -73
  143. package/src/municipal/departmentsSubjects/index.ts +9 -9
  144. package/src/municipal/index.ts +22 -22
  145. package/src/municipal/mongodb-client.ts +61 -61
  146. package/src/municipal/muniIssues/__tests__/muniIssues.setters.spec.ts +94 -94
  147. package/src/municipal/muniIssues/__tests__/muniIssues.setup.spec.ts +92 -92
  148. package/src/municipal/muniIssues/index.ts +17 -17
  149. package/src/municipal/muniIssues/muniIssues.constants.ts +18 -18
  150. package/src/municipal/muniIssues/muniIssues.getters.ts +24 -24
  151. package/src/municipal/muniIssues/muniIssues.schema.ts +66 -66
  152. package/src/municipal/muniIssues/muniIssues.setters.ts +24 -24
  153. package/src/municipal/muniIssues/muniIssues.setup.ts +65 -65
  154. package/src/municipal/muniIssues/muniIssues.types.ts +37 -37
  155. package/src/municipal/streets/__tests__/streets.spec.ts +253 -253
  156. package/src/municipal/streets/index.ts +2 -2
  157. package/src/municipal/streets/streets.getters.ts +140 -140
  158. package/src/municipal/streets/streets.types.ts +19 -19
  159. package/src/municipal/systemInstructions/__tests__/getters.spec.ts +113 -113
  160. package/src/municipal/systemInstructions/__tests__/setters.spec.ts +274 -274
  161. package/src/municipal/systemInstructions/index.ts +7 -7
  162. package/src/municipal/systemInstructions/instructions.getters.ts +57 -57
  163. package/src/municipal/systemInstructions/instructions.setters.ts +119 -119
  164. package/src/municipal/systemInstructions/instructions.types.ts +30 -30
  165. package/src/municipal/tickets/__tests__/tickets.getters.spec.ts +30 -30
  166. package/src/municipal/tickets/__tests__/tickets.statistics.spec.ts +99 -99
  167. package/src/municipal/tickets/index.ts +9 -9
  168. package/src/municipal/tickets/tickets.constants.ts +8 -8
  169. package/src/municipal/tickets/tickets.getters.ts +121 -121
  170. package/src/municipal/tickets/tickets.statistics.aggregation.ts +110 -110
  171. package/src/municipal/tickets/tickets.statistics.getters.ts +145 -145
  172. package/src/municipal/tickets/tickets.types.ts +54 -54
  173. package/src/municipal/utils/types.ts +11 -11
  174. package/src/talkpilot/__tests__/db.spec.ts +38 -38
  175. package/src/talkpilot/__tests__/mongodb-client.spec.ts +18 -18
  176. package/src/talkpilot/__tests__/validation.spec.ts +68 -68
  177. package/src/talkpilot/agents/__tests__/agents.getters.spec.ts +29 -29
  178. package/src/talkpilot/agents/agents.getters.ts +34 -34
  179. package/src/talkpilot/agents/agents.types.ts +14 -14
  180. package/src/talkpilot/agents/index.ts +2 -2
  181. package/src/talkpilot/auditLogs/__tests__/auditLogs.spec.ts +92 -0
  182. package/src/talkpilot/auditLogs/auditLogs.getters.ts +26 -0
  183. package/src/talkpilot/auditLogs/auditLogs.types.ts +13 -0
  184. package/src/talkpilot/auditLogs/index.ts +2 -0
  185. package/src/talkpilot/backgroundToolResults/__tests__/backgroundToolResults.getters.spec.ts +147 -147
  186. package/src/talkpilot/backgroundToolResults/backgroundToolResults.getters.ts +65 -65
  187. package/src/talkpilot/backgroundToolResults/backgroundToolResults.types.ts +23 -23
  188. package/src/talkpilot/backgroundToolResults/index.ts +2 -2
  189. package/src/talkpilot/calls/__tests__/callStats.utils.spec.ts +128 -128
  190. package/src/talkpilot/calls/__tests__/calls.dashboard.spec.ts +229 -229
  191. package/src/talkpilot/calls/__tests__/calls.spec.ts +471 -471
  192. package/src/talkpilot/calls/__tests__/calls.statistics.spec.ts +483 -483
  193. package/src/talkpilot/calls/calls.constants.ts +49 -49
  194. package/src/talkpilot/calls/calls.getters.ts +277 -277
  195. package/src/talkpilot/calls/calls.statistics.getters.ts +668 -668
  196. package/src/talkpilot/calls/calls.statistics.types.ts +48 -48
  197. package/src/talkpilot/calls/calls.types.ts +149 -149
  198. package/src/talkpilot/calls/dashboard/calls.dashboard.ts +295 -295
  199. package/src/talkpilot/calls/dashboard/calls.dashboard.types.ts +57 -57
  200. package/src/talkpilot/calls/index.ts +6 -6
  201. package/src/talkpilot/clientAudioBuffers/__tests__/clientAudioBuffer.getters.spec.ts +160 -160
  202. package/src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts +117 -117
  203. package/src/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.ts +25 -25
  204. package/src/talkpilot/clientAudioBuffers/index.ts +2 -2
  205. package/src/talkpilot/clients/__tests__/clients.getters.spec.ts +33 -0
  206. package/src/talkpilot/clients/__tests__/clients.lifecycle.spec.ts +166 -0
  207. package/src/talkpilot/clients/__tests__/clients.quota.spec.ts +123 -123
  208. package/src/talkpilot/clients/clients.constants.ts +2 -0
  209. package/src/talkpilot/clients/clients.getters.ts +23 -16
  210. package/src/talkpilot/clients/clients.lifecycle.ts +116 -0
  211. package/src/talkpilot/clients/clients.quota.getters.ts +119 -119
  212. package/src/talkpilot/clients/clients.quota.types.ts +27 -27
  213. package/src/talkpilot/clients/clients.types.ts +37 -18
  214. package/src/talkpilot/clients/index.ts +6 -4
  215. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.getters.spec.ts +423 -423
  216. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +212 -212
  217. package/src/talkpilot/clientsConfig/clientsConfig.constants.ts +2 -2
  218. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +247 -247
  219. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +136 -136
  220. package/src/talkpilot/clientsConfig/index.ts +3 -3
  221. package/src/talkpilot/contextNotes/__tests__/contextNotes.getters.spec.ts +217 -217
  222. package/src/talkpilot/contextNotes/contextNotes.getters.ts +85 -85
  223. package/src/talkpilot/contextNotes/contextNotes.types.ts +20 -20
  224. package/src/talkpilot/contextNotes/index.ts +2 -2
  225. package/src/talkpilot/flows/__tests__/flows.schema.spec.ts +99 -124
  226. package/src/talkpilot/flows/flows.getter.ts +14 -34
  227. package/src/talkpilot/flows/flows.schema.ts +155 -180
  228. package/src/talkpilot/flows/flows.types.ts +189 -211
  229. package/src/talkpilot/flows/index.ts +2 -7
  230. package/src/talkpilot/groups/__tests__/groups.spec.ts +90 -90
  231. package/src/talkpilot/groups/__tests__/phone.utils.spec.ts +32 -32
  232. package/src/talkpilot/groups/groups.getters.ts +30 -30
  233. package/src/talkpilot/groups/groups.types.ts +29 -29
  234. package/src/talkpilot/groups/index.ts +3 -3
  235. package/src/talkpilot/groups/phone.utils.ts +46 -46
  236. package/src/talkpilot/index.ts +32 -31
  237. package/src/talkpilot/leads/index.ts +2 -2
  238. package/src/talkpilot/leads/leads.getter.ts +6 -6
  239. package/src/talkpilot/leads/leads.schema.ts +33 -33
  240. package/src/talkpilot/leads/leads.types.ts +20 -20
  241. package/src/talkpilot/mongodb-client.ts +78 -78
  242. package/src/talkpilot/phone_numbers/__tests__/phone_numbers.spec.ts +496 -282
  243. package/src/talkpilot/phone_numbers/index.ts +2 -2
  244. package/src/talkpilot/phone_numbers/phone_numbers.getter.ts +251 -176
  245. package/src/talkpilot/phone_numbers/phone_numbers.schema.ts +17 -17
  246. package/src/talkpilot/phone_numbers/phone_numbers.types.ts +30 -30
  247. package/src/talkpilot/plans/__tests__/plans.spec.ts +70 -70
  248. package/src/talkpilot/plans/index.ts +2 -2
  249. package/src/talkpilot/plans/plans.getters.ts +132 -132
  250. package/src/talkpilot/plans/plans.types.ts +89 -89
  251. package/src/talkpilot/products/__tests__/products.getters.spec.ts +44 -44
  252. package/src/talkpilot/products/index.ts +2 -2
  253. package/src/talkpilot/products/products.getters.ts +12 -12
  254. package/src/talkpilot/products/products.types.ts +9 -9
  255. package/src/talkpilot/results/__tests__/results.statistics.spec.ts +226 -0
  256. package/src/talkpilot/results/index.ts +16 -7
  257. package/src/talkpilot/results/results.constants.ts +11 -0
  258. package/src/talkpilot/results/results.getter.ts +40 -39
  259. package/src/talkpilot/results/results.schema.ts +25 -25
  260. package/src/talkpilot/results/results.statistics.getters.ts +329 -0
  261. package/src/talkpilot/results/results.statistics.types.ts +45 -0
  262. package/src/talkpilot/results/results.types.ts +34 -34
  263. package/src/talkpilot/retry_analyze/__tests__/retryAnalyze.getters.spec.ts +156 -156
  264. package/src/talkpilot/retry_analyze/index.ts +2 -2
  265. package/src/talkpilot/retry_analyze/retryAnalyze.getters.ts +84 -84
  266. package/src/talkpilot/retry_analyze/retryAnalyze.types.ts +13 -13
  267. package/src/talkpilot/sessions/__tests__/sessions.spec.ts +147 -147
  268. package/src/talkpilot/sessions/index.ts +2 -2
  269. package/src/talkpilot/sessions/sessions.getter.ts +92 -92
  270. package/src/talkpilot/sessions/sessions.schema.ts +34 -34
  271. package/src/talkpilot/sessions/sessions.types.ts +30 -30
  272. package/src/talkpilot/subscriptions/__tests__/subscriptions.getters.utils.spec.ts +45 -45
  273. package/src/talkpilot/subscriptions/index.ts +3 -3
  274. package/src/talkpilot/subscriptions/subscriptions.getters.ts +146 -146
  275. package/src/talkpilot/subscriptions/subscriptions.getters.utils.ts +33 -33
  276. package/src/talkpilot/subscriptions/subscriptions.types.ts +66 -66
  277. package/src/talkpilot/utils/__tests__/query.utils.spec.ts +49 -49
  278. package/src/talkpilot/utils/query.utils.ts +21 -21
  279. package/src/test-utils/db-utils.ts +33 -33
  280. package/src/test-utils/factories/index.ts +20 -18
  281. package/src/test-utils/factories/municipal/cities.ts +16 -16
  282. package/src/test-utils/factories/municipal/departmentsSubjects.ts +37 -37
  283. package/src/test-utils/factories/municipal/muniIssues.ts +32 -32
  284. package/src/test-utils/factories/municipal/streets.ts +22 -22
  285. package/src/test-utils/factories/municipal/tickets.ts +39 -39
  286. package/src/test-utils/factories/talkpilot/agents.ts +19 -19
  287. package/src/test-utils/factories/talkpilot/calls.ts +37 -37
  288. package/src/test-utils/factories/talkpilot/clientAudioBuffers.ts +20 -20
  289. package/src/test-utils/factories/talkpilot/clients.ts +21 -0
  290. package/src/test-utils/factories/talkpilot/clientsConfig.ts +18 -18
  291. package/src/test-utils/factories/talkpilot/contextNotes.ts +40 -40
  292. package/src/test-utils/factories/talkpilot/flows.ts +33 -33
  293. package/src/test-utils/factories/talkpilot/groups.ts +33 -33
  294. package/src/test-utils/factories/talkpilot/phone_numbers.ts +22 -22
  295. package/src/test-utils/factories/talkpilot/results.ts +43 -0
  296. package/src/test-utils/factories/talkpilot/sessions.ts +35 -35
  297. package/src/test-utils/factories/websitalk/knowledgeDocuments.ts +29 -29
  298. package/src/test-utils/factories/websitalk/scanResources.ts +26 -26
  299. package/src/test-utils/factories/websitalk/scans.ts +29 -29
  300. package/src/test-utils/factories/websitalk/websiteUrls.ts +17 -17
  301. package/src/utils/date.utils.ts +126 -126
  302. package/src/utils/shared.types.ts +4 -4
  303. package/src/utils/validation.ts +23 -23
  304. package/src/websitalk/index.ts +18 -18
  305. package/src/websitalk/knowledgeDocuments/__tests__/knowledgeDocuments.spec.ts +186 -186
  306. package/src/websitalk/knowledgeDocuments/index.ts +3 -3
  307. package/src/websitalk/knowledgeDocuments/knowledgeDocuments.constants.ts +17 -17
  308. package/src/websitalk/knowledgeDocuments/knowledgeDocuments.getters.ts +78 -78
  309. package/src/websitalk/knowledgeDocuments/knowledgeDocuments.types.ts +62 -62
  310. package/src/websitalk/mongodb-client.ts +61 -61
  311. package/src/websitalk/scanResources/__tests__/scanResources.spec.ts +193 -193
  312. package/src/websitalk/scanResources/index.ts +4 -4
  313. package/src/websitalk/scanResources/scanResources.constants.ts +47 -47
  314. package/src/websitalk/scanResources/scanResources.getters.ts +102 -102
  315. package/src/websitalk/scanResources/scanResources.types.ts +90 -90
  316. package/src/websitalk/scanResources/scanResources.utils.ts +31 -31
  317. package/src/websitalk/scans/__tests__/scans.spec.ts +313 -313
  318. package/src/websitalk/scans/index.ts +4 -4
  319. package/src/websitalk/scans/scans.constants.ts +15 -15
  320. package/src/websitalk/scans/scans.getters.ts +166 -166
  321. package/src/websitalk/scans/scans.types.ts +54 -54
  322. package/src/websitalk/scans/scans.utils.ts +5 -5
  323. package/src/websitalk/websiteUrls/__tests__/websiteUrls.spec.ts +77 -77
  324. package/src/websitalk/websiteUrls/index.ts +2 -2
  325. package/src/websitalk/websiteUrls/websiteUrls.getters.ts +65 -65
  326. package/src/websitalk/websiteUrls/websiteUrls.types.ts +13 -13
  327. package/tsconfig.json +23 -23
  328. package/dist/talkpilot/clients/clients.quota.constants.d.ts +0 -2
  329. package/dist/talkpilot/clients/clients.quota.constants.d.ts.map +0 -1
  330. package/dist/talkpilot/clients/clients.quota.constants.js +0 -5
  331. package/dist/talkpilot/clients/clients.quota.constants.js.map +0 -1
  332. package/dist/talkpilot/clients/clients.quota.utils.d.ts +0 -8
  333. package/dist/talkpilot/clients/clients.quota.utils.d.ts.map +0 -1
  334. package/dist/talkpilot/clients/clients.quota.utils.js +0 -45
  335. package/dist/talkpilot/clients/clients.quota.utils.js.map +0 -1
@@ -1,282 +1,496 @@
1
- import {
2
- getPhoneDataByPhoneNumber,
3
- getPhoneNumbersCollection,
4
- getPhoneNumbersForFlows,
5
- getClientPhoneNumber,
6
- getClientPhoneData,
7
- createPhoneNumberEntity,
8
- createPurchasedPhoneNumber,
9
- getPrimaryPhoneFlowId,
10
- } from "../phone_numbers.getter";
11
- import { getFlowsCollection } from "../../flows/flows.getter";
12
- import { createFlow, createPhoneNumber } from "../../../test-utils/factories";
13
- import { ObjectId } from "mongodb";
14
-
15
- describe("db.phoneNumbers", () => {
16
- describe("getPhoneDataByPhoneNumber", () => {
17
- it("return phone number data with flow", async () => {
18
- const flow = createFlow({
19
- clientId: "test-client-id",
20
- conversationSettings: {
21
- interruptions: {
22
- enableInterruptionDetection: true,
23
- interruptionWindowSeconds: 60,
24
- interruptionThresholdSeconds: 10,
25
- interruptionInstruction:
26
- "I am hearing background noise that makes it hard for me to focus on what you are saying. Please try to move to a quieter place or reduce the noise so I can better assist you.",
27
- },
28
- silence: {
29
- enableSilenceDetection: true,
30
- firstWarningSilenceSeconds: 5,
31
- firstWarningInstruction:
32
- "I have not heard you for a few seconds. If you are still on the line, please say something so we can continue.",
33
- secondWarningSilenceSeconds: 10,
34
- secondWarningInstruction:
35
- "I still have not heard anything from you. If you do not respond in the next few seconds, I will have to end this call.",
36
- disconnectSilenceSeconds: 30,
37
- },
38
- },
39
- });
40
- const phoneData = createPhoneNumber({
41
- flow_id: flow._id,
42
- client_id: "test-client-id",
43
- });
44
-
45
- await getFlowsCollection().insertOne(flow);
46
- await getPhoneNumbersCollection().insertOne(phoneData);
47
-
48
- const result = await getPhoneDataByPhoneNumber(phoneData.phone_number);
49
-
50
- expect(result).toBeDefined();
51
- expect(result).toMatchObject({
52
- client_id: phoneData.client_id,
53
- phone_number: phoneData.phone_number,
54
- flow_id: flow._id,
55
- });
56
- // Specifically check if the flow was joined correctly
57
- expect(result?.flow.flowName).toBe(flow.flowName);
58
- expect(
59
- result?.flow.conversationSettings?.silence?.firstWarningSilenceSeconds,
60
- ).toBe(5);
61
- });
62
- });
63
-
64
- describe("getClientPhoneNumber", () => {
65
- it("return phone number by client id", async () => {
66
- const flow = createFlow();
67
- const phoneData = createPhoneNumber({
68
- flow_id: flow._id,
69
- is_primary: true,
70
- });
71
-
72
- await getFlowsCollection().insertOne(flow);
73
- await getPhoneNumbersCollection().insertOne(phoneData);
74
-
75
- const result = await getClientPhoneNumber(phoneData.client_id);
76
- expect(result).toBe(phoneData.phone_number);
77
- });
78
- });
79
-
80
- describe("getClientPhoneData", () => {
81
- it("returns primary phone data by default", async () => {
82
- const clientId = "multiPhoneClient";
83
- const phone1 = createPhoneNumber({
84
- client_id: clientId,
85
- is_primary: false,
86
- phone_number: "1",
87
- createdAt: new Date("2023-01-01"),
88
- });
89
- const phone2 = createPhoneNumber({
90
- client_id: clientId,
91
- is_primary: true,
92
- phone_number: "2",
93
- createdAt: new Date("2023-01-02"),
94
- });
95
-
96
- await getPhoneNumbersCollection().insertOne(phone1);
97
- await getPhoneNumbersCollection().insertOne(phone2);
98
-
99
- const result = await getClientPhoneData(clientId);
100
- expect(result?.phone_number).toBe("2");
101
- });
102
-
103
- it("returns most recent non-primary phone data if isPrimary is false", async () => {
104
- const clientId = "multiPhoneClient2";
105
- const phone1 = createPhoneNumber({
106
- client_id: clientId,
107
- is_primary: false,
108
- phone_number: "1",
109
- createdAt: new Date("2023-01-01"),
110
- });
111
- const phone2 = createPhoneNumber({
112
- client_id: clientId,
113
- is_primary: false,
114
- phone_number: "2",
115
- createdAt: new Date("2023-01-02"),
116
- });
117
- const phone3 = createPhoneNumber({
118
- client_id: clientId,
119
- is_primary: true,
120
- phone_number: "3",
121
- createdAt: new Date("2023-01-03"),
122
- });
123
-
124
- await getPhoneNumbersCollection().insertOne(phone1);
125
- await getPhoneNumbersCollection().insertOne(phone2);
126
- await getPhoneNumbersCollection().insertOne(phone3);
127
-
128
- const result = await getClientPhoneData(clientId, false);
129
- expect(result?.phone_number).toBe("2");
130
- });
131
- });
132
-
133
- describe("getPhoneNumbersForFlows", () => {
134
- it("returns all phone numbers for the client with flow ObjectIds, newest first", async () => {
135
- const clientId = "flowsClient";
136
- const flow1 = createFlow({ clientId });
137
- const flow2 = createFlow({ clientId });
138
- await getFlowsCollection().insertMany([flow1, flow2]);
139
-
140
- const older = createPhoneNumber({
141
- client_id: clientId,
142
- flow_id: flow1._id,
143
- phone_number: "+100",
144
- is_primary: true,
145
- createdAt: new Date("2023-01-01"),
146
- });
147
- const newer = createPhoneNumber({
148
- client_id: clientId,
149
- flow_id: flow2._id,
150
- phone_number: "+200",
151
- is_primary: false,
152
- createdAt: new Date("2023-06-01"),
153
- });
154
-
155
- await getPhoneNumbersCollection().insertMany([older, newer]);
156
-
157
- const result = await getPhoneNumbersForFlows(clientId);
158
-
159
- expect(result).toHaveLength(2);
160
- expect(result[0]).toEqual({
161
- flowId: String(flow2._id),
162
- phoneNumber: "+200",
163
- isPrimary: false,
164
- });
165
- expect(result[1]).toEqual({
166
- flowId: String(flow1._id),
167
- phoneNumber: "+100",
168
- isPrimary: true,
169
- });
170
- });
171
-
172
- it("returns an empty array when the client has no phone numbers", async () => {
173
- const result = await getPhoneNumbersForFlows("noSuchClient");
174
- expect(result).toEqual([]);
175
- });
176
-
177
- it("does not include phone numbers for other clients", async () => {
178
- const clientA = "clientA";
179
- const clientB = "clientB";
180
- const flow = createFlow();
181
- await getFlowsCollection().insertOne(flow);
182
- const phone = createPhoneNumber({
183
- client_id: clientA,
184
- flow_id: flow._id,
185
- });
186
- await getPhoneNumbersCollection().insertOne(phone);
187
-
188
- const result = await getPhoneNumbersForFlows(clientB);
189
- expect(result).toEqual([]);
190
- });
191
- });
192
-
193
- describe("createPhoneNumberEntity", () => {
194
- it("creates first phone number as primary", async () => {
195
- const clientId = "newClient";
196
- const flowId = new ObjectId().toHexString();
197
- const phoneNumber = "+123456789";
198
-
199
- const result = await createPhoneNumberEntity(
200
- phoneNumber,
201
- flowId,
202
- clientId,
203
- );
204
- expect(result.phone_number).toBe(phoneNumber);
205
- expect(result.is_primary).toBe(true);
206
- });
207
-
208
- it("creates subsequent phone number as non-primary", async () => {
209
- const clientId = "existingClient";
210
- const flowId = new ObjectId().toHexString();
211
-
212
- // Create first one
213
- await createPhoneNumberEntity("+111", flowId, clientId);
214
-
215
- // Create second one
216
- const result = await createPhoneNumberEntity("+222", flowId, clientId);
217
- expect(result.phone_number).toBe("+222");
218
- expect(result.is_primary).toBe(false);
219
- });
220
- });
221
-
222
- describe("createPurchasedPhoneNumber", () => {
223
- it("persists a phone_numbers document with provider payload and flow", async () => {
224
- const clientId = "purchasedClientDb";
225
- const flowId = new ObjectId().toHexString();
226
- const phoneNumber = "+15551234567";
227
- const providerPayload = {
228
- provider: "twilio" as const,
229
- provider_sid: "PNxxxxxxxx",
230
- };
231
-
232
- await createPurchasedPhoneNumber(
233
- phoneNumber,
234
- providerPayload,
235
- clientId,
236
- flowId,
237
- );
238
-
239
- // Re-read from Mongo to confirm insert succeeded (not only the function return value).
240
- const stored = await getPhoneNumbersCollection().findOne({
241
- phone_number: phoneNumber,
242
- });
243
- expect(stored).not.toBeNull();
244
- expect(stored).toMatchObject({
245
- phone_number: phoneNumber,
246
- client_id: clientId,
247
- provider: "twilio",
248
- provider_sid: "PNxxxxxxxx",
249
- flow_id: new ObjectId(flowId),
250
- });
251
- });
252
- });
253
-
254
- describe("getPrimaryPhoneFlowId", () => {
255
- it("returns flow_id from primary phone as string", async () => {
256
- const clientId = "primaryFlowClient";
257
- const flowId = new ObjectId();
258
- await getPhoneNumbersCollection().insertOne(
259
- createPhoneNumber({
260
- client_id: clientId,
261
- is_primary: true,
262
- flow_id: flowId,
263
- }),
264
- );
265
-
266
- expect(await getPrimaryPhoneFlowId(clientId)).toBe(flowId.toHexString());
267
- });
268
-
269
- it("returns null when client has no primary phone", async () => {
270
- expect(await getPrimaryPhoneFlowId("no-phone-client")).toBeNull();
271
- });
272
-
273
- it("returns null when primary phone has no flow_id", async () => {
274
- const clientId = "phoneNoFlow";
275
- const phone = createPhoneNumber({ client_id: clientId, is_primary: true });
276
- delete phone.flow_id;
277
- await getPhoneNumbersCollection().insertOne(phone);
278
-
279
- expect(await getPrimaryPhoneFlowId(clientId)).toBeNull();
280
- });
281
- });
282
- });
1
+ import {
2
+ getPhoneDataByPhoneNumber,
3
+ getPhoneNumbersCollection,
4
+ getPhoneNumbersForFlows,
5
+ getClientPhoneNumber,
6
+ getClientPhoneData,
7
+ createPhoneNumberEntity,
8
+ createPurchasedPhoneNumber,
9
+ getPrimaryPhoneFlowId,
10
+ getClientPhones,
11
+ getAllClientPhoneMappings,
12
+ getDistinctClientPhonePairs,
13
+ findClientsByPhoneNumbers,
14
+ deletePhoneNumberRecords,
15
+ } from "../phone_numbers.getter";
16
+ import { getFlowsCollection } from "../../flows/flows.getter";
17
+ import { createFlow, createPhoneNumber } from "../../../test-utils/factories";
18
+ import { ObjectId } from "mongodb";
19
+
20
+ describe("db.phoneNumbers", () => {
21
+ describe("getPhoneDataByPhoneNumber", () => {
22
+ it("should return phone number data with flow", async () => {
23
+ const flow = createFlow({
24
+ clientId: "test-client-id",
25
+ conversationSettings: {
26
+ interruptions: {
27
+ enableInterruptionDetection: true,
28
+ interruptionWindowSeconds: 60,
29
+ interruptionThresholdSeconds: 10,
30
+ interruptionInstruction:
31
+ "I am hearing background noise that makes it hard for me to focus on what you are saying. Please try to move to a quieter place or reduce the noise so I can better assist you.",
32
+ },
33
+ silence: {
34
+ enableSilenceDetection: true,
35
+ firstWarningSilenceSeconds: 5,
36
+ firstWarningInstruction:
37
+ "I have not heard you for a few seconds. If you are still on the line, please say something so we can continue.",
38
+ secondWarningSilenceSeconds: 10,
39
+ secondWarningInstruction:
40
+ "I still have not heard anything from you. If you do not respond in the next few seconds, I will have to end this call.",
41
+ disconnectSilenceSeconds: 30,
42
+ },
43
+ },
44
+ });
45
+ const phoneData = createPhoneNumber({
46
+ flow_id: flow._id,
47
+ client_id: "test-client-id",
48
+ });
49
+
50
+ await getFlowsCollection().insertOne(flow);
51
+ await getPhoneNumbersCollection().insertOne(phoneData);
52
+
53
+ const result = await getPhoneDataByPhoneNumber(phoneData.phone_number);
54
+
55
+ expect(result).toBeDefined();
56
+ expect(result).toMatchObject({
57
+ client_id: phoneData.client_id,
58
+ phone_number: phoneData.phone_number,
59
+ flow_id: flow._id,
60
+ });
61
+ // Specifically check if the flow was joined correctly
62
+ expect(result?.flow.flowName).toBe(flow.flowName);
63
+ expect(
64
+ result?.flow.conversationSettings?.silence?.firstWarningSilenceSeconds,
65
+ ).toBe(5);
66
+ });
67
+ });
68
+
69
+ describe("getClientPhoneNumber", () => {
70
+ it("should return phone number by client id", async () => {
71
+ const flow = createFlow();
72
+ const phoneData = createPhoneNumber({
73
+ flow_id: flow._id,
74
+ is_primary: true,
75
+ });
76
+
77
+ await getFlowsCollection().insertOne(flow);
78
+ await getPhoneNumbersCollection().insertOne(phoneData);
79
+
80
+ const result = await getClientPhoneNumber(phoneData.client_id);
81
+ expect(result).toBe(phoneData.phone_number);
82
+ });
83
+ });
84
+
85
+ describe("getClientPhoneData", () => {
86
+ it("should return primary phone data by default", async () => {
87
+ const clientId = "multiPhoneClient";
88
+ const phone1 = createPhoneNumber({
89
+ client_id: clientId,
90
+ is_primary: false,
91
+ phone_number: "1",
92
+ createdAt: new Date("2023-01-01"),
93
+ });
94
+ const phone2 = createPhoneNumber({
95
+ client_id: clientId,
96
+ is_primary: true,
97
+ phone_number: "2",
98
+ createdAt: new Date("2023-01-02"),
99
+ });
100
+
101
+ await getPhoneNumbersCollection().insertOne(phone1);
102
+ await getPhoneNumbersCollection().insertOne(phone2);
103
+
104
+ const result = await getClientPhoneData(clientId);
105
+ expect(result?.phone_number).toBe("2");
106
+ });
107
+
108
+ it("should return most recent non-primary phone data if isPrimary is false", async () => {
109
+ const clientId = "multiPhoneClient2";
110
+ const phone1 = createPhoneNumber({
111
+ client_id: clientId,
112
+ is_primary: false,
113
+ phone_number: "1",
114
+ createdAt: new Date("2023-01-01"),
115
+ });
116
+ const phone2 = createPhoneNumber({
117
+ client_id: clientId,
118
+ is_primary: false,
119
+ phone_number: "2",
120
+ createdAt: new Date("2023-01-02"),
121
+ });
122
+ const phone3 = createPhoneNumber({
123
+ client_id: clientId,
124
+ is_primary: true,
125
+ phone_number: "3",
126
+ createdAt: new Date("2023-01-03"),
127
+ });
128
+
129
+ await getPhoneNumbersCollection().insertOne(phone1);
130
+ await getPhoneNumbersCollection().insertOne(phone2);
131
+ await getPhoneNumbersCollection().insertOne(phone3);
132
+
133
+ const result = await getClientPhoneData(clientId, false);
134
+ expect(result?.phone_number).toBe("2");
135
+ });
136
+ });
137
+
138
+ describe("getPhoneNumbersForFlows", () => {
139
+ it("should return all phone numbers for the client with flow ObjectIds, newest first", async () => {
140
+ const clientId = "flowsClient";
141
+ const flow1 = createFlow({ clientId });
142
+ const flow2 = createFlow({ clientId });
143
+ await getFlowsCollection().insertMany([flow1, flow2]);
144
+
145
+ const older = createPhoneNumber({
146
+ client_id: clientId,
147
+ flow_id: flow1._id,
148
+ phone_number: "+100",
149
+ is_primary: true,
150
+ createdAt: new Date("2023-01-01"),
151
+ });
152
+ const newer = createPhoneNumber({
153
+ client_id: clientId,
154
+ flow_id: flow2._id,
155
+ phone_number: "+200",
156
+ is_primary: false,
157
+ createdAt: new Date("2023-06-01"),
158
+ });
159
+
160
+ await getPhoneNumbersCollection().insertMany([older, newer]);
161
+
162
+ const result = await getPhoneNumbersForFlows(clientId);
163
+
164
+ expect(result).toHaveLength(2);
165
+ expect(result[0]).toEqual({
166
+ flowId: String(flow2._id),
167
+ phoneNumber: "+200",
168
+ isPrimary: false,
169
+ });
170
+ expect(result[1]).toEqual({
171
+ flowId: String(flow1._id),
172
+ phoneNumber: "+100",
173
+ isPrimary: true,
174
+ });
175
+ });
176
+
177
+ it("should return an empty array when the client has no phone numbers", async () => {
178
+ const result = await getPhoneNumbersForFlows("noSuchClient");
179
+ expect(result).toEqual([]);
180
+ });
181
+
182
+ it("should not include phone numbers for other clients", async () => {
183
+ const clientA = "clientA";
184
+ const clientB = "clientB";
185
+ const flow = createFlow();
186
+ await getFlowsCollection().insertOne(flow);
187
+ const phone = createPhoneNumber({
188
+ client_id: clientA,
189
+ flow_id: flow._id,
190
+ });
191
+ await getPhoneNumbersCollection().insertOne(phone);
192
+
193
+ const result = await getPhoneNumbersForFlows(clientB);
194
+ expect(result).toEqual([]);
195
+ });
196
+ });
197
+
198
+ describe("createPhoneNumberEntity", () => {
199
+ it("should create first phone number as primary", async () => {
200
+ const clientId = "newClient";
201
+ const flowId = new ObjectId().toHexString();
202
+ const phoneNumber = "+123456789";
203
+
204
+ const result = await createPhoneNumberEntity(
205
+ phoneNumber,
206
+ flowId,
207
+ clientId,
208
+ );
209
+ expect(result.phone_number).toBe(phoneNumber);
210
+ expect(result.is_primary).toBe(true);
211
+ });
212
+
213
+ it("should create subsequent phone number as non-primary", async () => {
214
+ const clientId = "existingClient";
215
+ const flowId = new ObjectId().toHexString();
216
+
217
+ // Create first one
218
+ await createPhoneNumberEntity("+111", flowId, clientId);
219
+
220
+ // Create second one
221
+ const result = await createPhoneNumberEntity("+222", flowId, clientId);
222
+ expect(result.phone_number).toBe("+222");
223
+ expect(result.is_primary).toBe(false);
224
+ });
225
+ });
226
+
227
+ describe("createPurchasedPhoneNumber", () => {
228
+ it("should persist a phone_numbers document with provider payload and flow", async () => {
229
+ const clientId = "purchasedClientDb";
230
+ const flowId = new ObjectId().toHexString();
231
+ const phoneNumber = "+15551234567";
232
+ const providerPayload = {
233
+ provider: "twilio" as const,
234
+ provider_sid: "PNxxxxxxxx",
235
+ };
236
+
237
+ await createPurchasedPhoneNumber(
238
+ phoneNumber,
239
+ providerPayload,
240
+ clientId,
241
+ flowId,
242
+ );
243
+
244
+ // Re-read from Mongo to confirm insert succeeded (not only the function return value).
245
+ const stored = await getPhoneNumbersCollection().findOne({
246
+ phone_number: phoneNumber,
247
+ });
248
+ expect(stored).not.toBeNull();
249
+ expect(stored).toMatchObject({
250
+ phone_number: phoneNumber,
251
+ client_id: clientId,
252
+ provider: "twilio",
253
+ provider_sid: "PNxxxxxxxx",
254
+ flow_id: new ObjectId(flowId),
255
+ });
256
+ });
257
+ });
258
+
259
+ describe("getPrimaryPhoneFlowId", () => {
260
+ it("should return flow_id from primary phone as string", async () => {
261
+ const clientId = "primaryFlowClient";
262
+ const flowId = new ObjectId();
263
+ await getPhoneNumbersCollection().insertOne(
264
+ createPhoneNumber({
265
+ client_id: clientId,
266
+ is_primary: true,
267
+ flow_id: flowId,
268
+ }),
269
+ );
270
+
271
+ expect(await getPrimaryPhoneFlowId(clientId)).toBe(flowId.toHexString());
272
+ });
273
+
274
+ it("should return null when client has no primary phone", async () => {
275
+ expect(await getPrimaryPhoneFlowId("no-phone-client")).toBeNull();
276
+ });
277
+
278
+ it("should return null when primary phone has no flow_id", async () => {
279
+ const clientId = "phoneNoFlow";
280
+ const phone = createPhoneNumber({ client_id: clientId, is_primary: true });
281
+ delete phone.flow_id;
282
+ await getPhoneNumbersCollection().insertOne(phone);
283
+
284
+ expect(await getPrimaryPhoneFlowId(clientId)).toBeNull();
285
+ });
286
+ });
287
+
288
+ describe("getClientPhones", () => {
289
+ it("should return all phone numbers for the given client, projected to phoneNumber only", async () => {
290
+ const clientId = "phonesClientA";
291
+ await getPhoneNumbersCollection().insertMany([
292
+ createPhoneNumber({ client_id: clientId, phone_number: "+100" }),
293
+ createPhoneNumber({ client_id: clientId, phone_number: "+200" }),
294
+ ]);
295
+
296
+ const result = await getClientPhones(clientId);
297
+
298
+ expect(result).toHaveLength(2);
299
+ expect(result).toEqual(
300
+ expect.arrayContaining([
301
+ { phoneNumber: "+100" },
302
+ { phoneNumber: "+200" },
303
+ ]),
304
+ );
305
+ });
306
+
307
+ it("should not include phone numbers belonging to other clients", async () => {
308
+ await getPhoneNumbersCollection().insertOne(
309
+ createPhoneNumber({ client_id: "someOtherClient" }),
310
+ );
311
+
312
+ const result = await getClientPhones("phonesClientB");
313
+ expect(result).toEqual([]);
314
+ });
315
+
316
+ it("should remove exact duplicate phone records for the same client", async () => {
317
+ const clientId = "phonesClientDup";
318
+ await getPhoneNumbersCollection().insertMany([
319
+ createPhoneNumber({ client_id: clientId, phone_number: "+300" }),
320
+ createPhoneNumber({ client_id: clientId, phone_number: "+300" }),
321
+ createPhoneNumber({ client_id: clientId, phone_number: "+400" }),
322
+ ]);
323
+
324
+ const result = await getClientPhones(clientId);
325
+
326
+ expect(result).toHaveLength(2);
327
+ expect(result).toEqual(
328
+ expect.arrayContaining([
329
+ { phoneNumber: "+300" },
330
+ { phoneNumber: "+400" },
331
+ ]),
332
+ );
333
+ });
334
+ });
335
+
336
+ describe("getAllClientPhoneMappings", () => {
337
+ it("should return a Map of clientId to phone number across all clients", async () => {
338
+ await getPhoneNumbersCollection().insertMany([
339
+ createPhoneNumber({ client_id: "mapClientA", phone_number: "+111" }),
340
+ createPhoneNumber({ client_id: "mapClientB", phone_number: "+222" }),
341
+ ]);
342
+
343
+ const result = await getAllClientPhoneMappings();
344
+
345
+ expect(result).toBeInstanceOf(Map);
346
+ expect(result.get("mapClientA")).toBe("+111");
347
+ expect(result.get("mapClientB")).toBe("+222");
348
+ });
349
+
350
+ it("should return an empty Map when there are no phone numbers", async () => {
351
+ const result = await getAllClientPhoneMappings();
352
+ expect(result.size).toBe(0);
353
+ });
354
+
355
+ it("should return the most recently updated phone number when a client has several", async () => {
356
+ const clientId = "mapClientRecent";
357
+ await getPhoneNumbersCollection().insertMany([
358
+ createPhoneNumber({
359
+ client_id: clientId,
360
+ phone_number: "+old",
361
+ updatedAt: new Date("2023-01-01"),
362
+ }),
363
+ createPhoneNumber({
364
+ client_id: clientId,
365
+ phone_number: "+new",
366
+ updatedAt: new Date("2023-06-01"),
367
+ }),
368
+ ]);
369
+
370
+ const result = await getAllClientPhoneMappings();
371
+
372
+ expect(result.get(clientId)).toBe("+new");
373
+ });
374
+
375
+ it("should prefer the is_primary number over a more recently updated non-primary one", async () => {
376
+ const clientId = "mapClientPrimary";
377
+ await getPhoneNumbersCollection().insertMany([
378
+ createPhoneNumber({
379
+ client_id: clientId,
380
+ phone_number: "+primary",
381
+ is_primary: true,
382
+ updatedAt: new Date("2023-01-01"),
383
+ }),
384
+ createPhoneNumber({
385
+ client_id: clientId,
386
+ phone_number: "+secondary",
387
+ is_primary: false,
388
+ updatedAt: new Date("2023-06-01"),
389
+ }),
390
+ ]);
391
+
392
+ const result = await getAllClientPhoneMappings();
393
+
394
+ expect(result.get(clientId)).toBe("+primary");
395
+ });
396
+ });
397
+
398
+ describe("getDistinctClientPhonePairs", () => {
399
+ it("should return unique client+phone pairs, collapsing exact duplicates", async () => {
400
+ await getPhoneNumbersCollection().insertMany([
401
+ createPhoneNumber({ client_id: "pairClientA", phone_number: "+500" }),
402
+ createPhoneNumber({ client_id: "pairClientA", phone_number: "+500" }),
403
+ createPhoneNumber({ client_id: "pairClientB", phone_number: "+600" }),
404
+ ]);
405
+
406
+ const result = await getDistinctClientPhonePairs();
407
+
408
+ expect(result).toEqual(
409
+ expect.arrayContaining([
410
+ { clientId: "pairClientA", phoneNumber: "+500" },
411
+ { clientId: "pairClientB", phoneNumber: "+600" },
412
+ ]),
413
+ );
414
+ expect(
415
+ result.filter(
416
+ (p) => p.clientId === "pairClientA" && p.phoneNumber === "+500",
417
+ ),
418
+ ).toHaveLength(1);
419
+ });
420
+
421
+ it("should keep separate pairs when the same number is shared by different clients", async () => {
422
+ await getPhoneNumbersCollection().insertMany([
423
+ createPhoneNumber({ client_id: "sharedClientA", phone_number: "+700" }),
424
+ createPhoneNumber({ client_id: "sharedClientB", phone_number: "+700" }),
425
+ ]);
426
+
427
+ const result = await getDistinctClientPhonePairs();
428
+
429
+ expect(result).toEqual(
430
+ expect.arrayContaining([
431
+ { clientId: "sharedClientA", phoneNumber: "+700" },
432
+ { clientId: "sharedClientB", phoneNumber: "+700" },
433
+ ]),
434
+ );
435
+ });
436
+ });
437
+
438
+ describe("findClientsByPhoneNumbers", () => {
439
+ it("should return other clients sharing each phone number, excluding the given client", async () => {
440
+ await getPhoneNumbersCollection().insertMany([
441
+ createPhoneNumber({ client_id: "excludeMe", phone_number: "+800" }),
442
+ createPhoneNumber({ client_id: "otherClient1", phone_number: "+800" }),
443
+ createPhoneNumber({ client_id: "otherClient2", phone_number: "+800" }),
444
+ ]);
445
+
446
+ const result = await findClientsByPhoneNumbers(["+800"], "excludeMe");
447
+
448
+ expect(result.get("+800")).toEqual(
449
+ expect.arrayContaining(["otherClient1", "otherClient2"]),
450
+ );
451
+ expect(result.get("+800")).not.toContain("excludeMe");
452
+ });
453
+
454
+ it("should return an empty array for a phone number with no other clients", async () => {
455
+ await getPhoneNumbersCollection().insertOne(
456
+ createPhoneNumber({ client_id: "soleOwner", phone_number: "+900" }),
457
+ );
458
+
459
+ const result = await findClientsByPhoneNumbers(["+900"], "soleOwner");
460
+
461
+ expect(result.get("+900")).toEqual([]);
462
+ });
463
+
464
+ it("should return an empty map immediately when given no phone numbers", async () => {
465
+ const result = await findClientsByPhoneNumbers([]);
466
+ expect(result.size).toBe(0);
467
+ });
468
+ });
469
+
470
+ describe("deletePhoneNumberRecords", () => {
471
+ it("should delete every record for the given phone number across all clients", async () => {
472
+ await getPhoneNumbersCollection().insertMany([
473
+ createPhoneNumber({ client_id: "delClientA", phone_number: "+1000" }),
474
+ createPhoneNumber({ client_id: "delClientB", phone_number: "+1000" }),
475
+ createPhoneNumber({ client_id: "delClientC", phone_number: "+2000" }),
476
+ ]);
477
+
478
+ const deletedCount = await deletePhoneNumberRecords("+1000");
479
+
480
+ expect(deletedCount).toBe(2);
481
+ const remaining = await getPhoneNumbersCollection()
482
+ .find({ phone_number: "+1000" })
483
+ .toArray();
484
+ expect(remaining).toHaveLength(0);
485
+ const untouched = await getPhoneNumbersCollection().findOne({
486
+ phone_number: "+2000",
487
+ });
488
+ expect(untouched?.client_id).toBe("delClientC");
489
+ });
490
+
491
+ it("should return 0 when no records match the phone number", async () => {
492
+ const deletedCount = await deletePhoneNumberRecords("+9999999");
493
+ expect(deletedCount).toBe(0);
494
+ });
495
+ });
496
+ });