@talkpilot/core-db 1.1.15 → 1.1.16

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 (295) hide show
  1. package/.cursor/rules/development.mdc +65 -65
  2. package/DEVELOPMENT.md +141 -98
  3. package/README.md +297 -160
  4. package/README_OLD.md +160 -0
  5. package/dist/connection.d.ts.map +1 -1
  6. package/dist/connection.js +10 -0
  7. package/dist/connection.js.map +1 -1
  8. package/dist/index.d.ts +4 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +8 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/municipal/index.d.ts +1 -0
  13. package/dist/municipal/index.d.ts.map +1 -1
  14. package/dist/municipal/index.js +1 -0
  15. package/dist/municipal/index.js.map +1 -1
  16. package/dist/municipal/muniIssues/index.d.ts +7 -0
  17. package/dist/municipal/muniIssues/index.d.ts.map +1 -0
  18. package/dist/municipal/muniIssues/index.js +29 -0
  19. package/dist/municipal/muniIssues/index.js.map +1 -0
  20. package/dist/municipal/muniIssues/muniIssues.constants.d.ts +14 -0
  21. package/dist/municipal/muniIssues/muniIssues.constants.d.ts.map +1 -0
  22. package/dist/municipal/muniIssues/muniIssues.constants.js +17 -0
  23. package/dist/municipal/muniIssues/muniIssues.constants.js.map +1 -0
  24. package/dist/municipal/muniIssues/muniIssues.getters.d.ts +7 -0
  25. package/dist/municipal/muniIssues/muniIssues.getters.d.ts.map +1 -0
  26. package/dist/municipal/muniIssues/muniIssues.getters.js +24 -0
  27. package/dist/municipal/muniIssues/muniIssues.getters.js.map +1 -0
  28. package/dist/municipal/muniIssues/muniIssues.schema.d.ts +4 -0
  29. package/dist/municipal/muniIssues/muniIssues.schema.d.ts.map +1 -0
  30. package/dist/municipal/muniIssues/muniIssues.schema.js +63 -0
  31. package/dist/municipal/muniIssues/muniIssues.schema.js.map +1 -0
  32. package/dist/municipal/muniIssues/muniIssues.setters.d.ts +4 -0
  33. package/dist/municipal/muniIssues/muniIssues.setters.d.ts.map +1 -0
  34. package/dist/municipal/muniIssues/muniIssues.setters.js +22 -0
  35. package/dist/municipal/muniIssues/muniIssues.setters.js.map +1 -0
  36. package/dist/municipal/muniIssues/muniIssues.setup.d.ts +3 -0
  37. package/dist/municipal/muniIssues/muniIssues.setup.d.ts.map +1 -0
  38. package/dist/municipal/muniIssues/muniIssues.setup.js +61 -0
  39. package/dist/municipal/muniIssues/muniIssues.setup.js.map +1 -0
  40. package/dist/municipal/muniIssues/muniIssues.types.d.ts +30 -0
  41. package/dist/municipal/muniIssues/muniIssues.types.d.ts.map +1 -0
  42. package/dist/municipal/muniIssues/muniIssues.types.js +3 -0
  43. package/dist/municipal/muniIssues/muniIssues.types.js.map +1 -0
  44. package/dist/municipal/tickets/index.d.ts +2 -1
  45. package/dist/municipal/tickets/index.d.ts.map +1 -1
  46. package/dist/municipal/tickets/index.js +1 -0
  47. package/dist/municipal/tickets/index.js.map +1 -1
  48. package/dist/municipal/tickets/tickets.constants.d.ts +7 -0
  49. package/dist/municipal/tickets/tickets.constants.d.ts.map +1 -0
  50. package/dist/municipal/tickets/tickets.constants.js +10 -0
  51. package/dist/municipal/tickets/tickets.constants.js.map +1 -0
  52. package/dist/municipal/tickets/tickets.getters.d.ts +0 -11
  53. package/dist/municipal/tickets/tickets.getters.d.ts.map +1 -1
  54. package/dist/municipal/tickets/tickets.getters.js +0 -128
  55. package/dist/municipal/tickets/tickets.getters.js.map +1 -1
  56. package/dist/municipal/tickets/tickets.statistics.aggregation.d.ts +69 -0
  57. package/dist/municipal/tickets/tickets.statistics.aggregation.d.ts.map +1 -0
  58. package/dist/municipal/tickets/tickets.statistics.aggregation.js +99 -0
  59. package/dist/municipal/tickets/tickets.statistics.aggregation.js.map +1 -0
  60. package/dist/municipal/tickets/tickets.statistics.getters.d.ts +12 -0
  61. package/dist/municipal/tickets/tickets.statistics.getters.d.ts.map +1 -0
  62. package/dist/municipal/tickets/tickets.statistics.getters.js +80 -0
  63. package/dist/municipal/tickets/tickets.statistics.getters.js.map +1 -0
  64. package/dist/municipal/tickets/tickets.types.d.ts +15 -5
  65. package/dist/municipal/tickets/tickets.types.d.ts.map +1 -1
  66. package/dist/talkpilot/calls/calls.constants.d.ts +17 -0
  67. package/dist/talkpilot/calls/calls.constants.d.ts.map +1 -0
  68. package/dist/talkpilot/calls/calls.constants.js +20 -0
  69. package/dist/talkpilot/calls/calls.constants.js.map +1 -0
  70. package/dist/talkpilot/calls/calls.dashboard.d.ts +3 -0
  71. package/dist/talkpilot/calls/calls.dashboard.d.ts.map +1 -0
  72. package/dist/talkpilot/calls/calls.dashboard.js +191 -0
  73. package/dist/talkpilot/calls/calls.dashboard.js.map +1 -0
  74. package/dist/talkpilot/calls/calls.getters.d.ts +3 -2
  75. package/dist/talkpilot/calls/calls.getters.d.ts.map +1 -1
  76. package/dist/talkpilot/calls/calls.getters.js +1 -2
  77. package/dist/talkpilot/calls/calls.getters.js.map +1 -1
  78. package/dist/talkpilot/calls/calls.statistics.getters.d.ts +28 -0
  79. package/dist/talkpilot/calls/calls.statistics.getters.d.ts.map +1 -0
  80. package/dist/talkpilot/calls/calls.statistics.getters.js +453 -0
  81. package/dist/talkpilot/calls/calls.statistics.getters.js.map +1 -0
  82. package/dist/talkpilot/calls/calls.statistics.types.d.ts +43 -0
  83. package/dist/talkpilot/calls/calls.statistics.types.d.ts.map +1 -0
  84. package/dist/talkpilot/calls/calls.statistics.types.js +3 -0
  85. package/dist/talkpilot/calls/calls.statistics.types.js.map +1 -0
  86. package/dist/talkpilot/calls/calls.types.d.ts +8 -7
  87. package/dist/talkpilot/calls/calls.types.d.ts.map +1 -1
  88. package/dist/talkpilot/calls/calls.types.js +0 -3
  89. package/dist/talkpilot/calls/calls.types.js.map +1 -1
  90. package/dist/talkpilot/calls/dashboard/calls.dashboard.d.ts +4 -0
  91. package/dist/talkpilot/calls/dashboard/calls.dashboard.d.ts.map +1 -0
  92. package/dist/talkpilot/calls/dashboard/calls.dashboard.js +223 -0
  93. package/dist/talkpilot/calls/dashboard/calls.dashboard.js.map +1 -0
  94. package/dist/talkpilot/calls/dashboard/calls.dashboard.types.d.ts +45 -0
  95. package/dist/talkpilot/calls/dashboard/calls.dashboard.types.d.ts.map +1 -0
  96. package/dist/talkpilot/calls/dashboard/calls.dashboard.types.js +3 -0
  97. package/dist/talkpilot/calls/dashboard/calls.dashboard.types.js.map +1 -0
  98. package/dist/talkpilot/calls/index.d.ts +4 -0
  99. package/dist/talkpilot/calls/index.d.ts.map +1 -1
  100. package/dist/talkpilot/calls/index.js +4 -0
  101. package/dist/talkpilot/calls/index.js.map +1 -1
  102. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +2 -1
  103. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
  104. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +14 -0
  105. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
  106. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +20 -7
  107. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
  108. package/dist/talkpilot/clientsConfig/clientsConfig.types.js +6 -0
  109. package/dist/talkpilot/clientsConfig/clientsConfig.types.js.map +1 -1
  110. package/dist/talkpilot/contextNotes/contextNotes.getters.d.ts +19 -0
  111. package/dist/talkpilot/contextNotes/contextNotes.getters.d.ts.map +1 -0
  112. package/dist/talkpilot/contextNotes/contextNotes.getters.js +55 -0
  113. package/dist/talkpilot/contextNotes/contextNotes.getters.js.map +1 -0
  114. package/dist/talkpilot/contextNotes/contextNotes.types.d.ts +19 -0
  115. package/dist/talkpilot/contextNotes/contextNotes.types.d.ts.map +1 -0
  116. package/dist/talkpilot/contextNotes/contextNotes.types.js +3 -0
  117. package/dist/talkpilot/contextNotes/contextNotes.types.js.map +1 -0
  118. package/dist/talkpilot/contextNotes/index.d.ts +3 -0
  119. package/dist/talkpilot/contextNotes/index.d.ts.map +1 -0
  120. package/dist/talkpilot/contextNotes/index.js +19 -0
  121. package/dist/talkpilot/contextNotes/index.js.map +1 -0
  122. package/dist/talkpilot/flows/flows.schema.d.ts +3 -0
  123. package/dist/talkpilot/flows/flows.schema.d.ts.map +1 -1
  124. package/dist/talkpilot/flows/flows.schema.js +2 -1
  125. package/dist/talkpilot/flows/flows.schema.js.map +1 -1
  126. package/dist/talkpilot/flows/flows.types.d.ts +2 -0
  127. package/dist/talkpilot/flows/flows.types.d.ts.map +1 -1
  128. package/dist/talkpilot/index.d.ts +3 -0
  129. package/dist/talkpilot/index.d.ts.map +1 -1
  130. package/dist/talkpilot/index.js +3 -0
  131. package/dist/talkpilot/index.js.map +1 -1
  132. package/dist/talkpilot/phone_numbers/index.d.ts +2 -2
  133. package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts +1 -1
  134. package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts.map +1 -1
  135. package/dist/talkpilot/phone_numbers/phone_numbers.getter.js +5 -3
  136. package/dist/talkpilot/phone_numbers/phone_numbers.getter.js.map +1 -1
  137. package/dist/talkpilot/phone_numbers/phone_numbers.schema.js +12 -12
  138. package/dist/talkpilot/phone_numbers/phone_numbers.types.d.ts +4 -4
  139. package/dist/talkpilot/products/index.d.ts +3 -0
  140. package/dist/talkpilot/products/index.d.ts.map +1 -0
  141. package/dist/talkpilot/products/index.js +19 -0
  142. package/dist/talkpilot/products/index.js.map +1 -0
  143. package/dist/talkpilot/products/products.getters.d.ts +6 -0
  144. package/dist/talkpilot/products/products.getters.d.ts.map +1 -0
  145. package/dist/talkpilot/products/products.getters.js +14 -0
  146. package/dist/talkpilot/products/products.getters.js.map +1 -0
  147. package/dist/talkpilot/products/products.types.d.ts +9 -0
  148. package/dist/talkpilot/products/products.types.d.ts.map +1 -0
  149. package/dist/talkpilot/products/products.types.js +3 -0
  150. package/dist/talkpilot/products/products.types.js.map +1 -0
  151. package/dist/talkpilot/results/index.d.ts +1 -1
  152. package/dist/talkpilot/results/index.d.ts.map +1 -1
  153. package/dist/talkpilot/results/index.js +3 -1
  154. package/dist/talkpilot/results/index.js.map +1 -1
  155. package/dist/talkpilot/results/results.getter.d.ts +3 -1
  156. package/dist/talkpilot/results/results.getter.d.ts.map +1 -1
  157. package/dist/talkpilot/results/results.getter.js +11 -2
  158. package/dist/talkpilot/results/results.getter.js.map +1 -1
  159. package/dist/talkpilot/retry_analyze/index.d.ts +3 -0
  160. package/dist/talkpilot/retry_analyze/index.d.ts.map +1 -0
  161. package/dist/talkpilot/retry_analyze/index.js +19 -0
  162. package/dist/talkpilot/retry_analyze/index.js.map +1 -0
  163. package/dist/talkpilot/retry_analyze/retryAnalyze.getters.d.ts +10 -0
  164. package/dist/talkpilot/retry_analyze/retryAnalyze.getters.d.ts.map +1 -0
  165. package/dist/talkpilot/retry_analyze/retryAnalyze.getters.js +60 -0
  166. package/dist/talkpilot/retry_analyze/retryAnalyze.getters.js.map +1 -0
  167. package/dist/talkpilot/retry_analyze/retryAnalyze.types.d.ts +12 -0
  168. package/dist/talkpilot/retry_analyze/retryAnalyze.types.d.ts.map +1 -0
  169. package/dist/talkpilot/retry_analyze/retryAnalyze.types.js +3 -0
  170. package/dist/talkpilot/retry_analyze/retryAnalyze.types.js.map +1 -0
  171. package/dist/test-utils/db-utils.d.ts.map +1 -1
  172. package/dist/test-utils/db-utils.js +2 -0
  173. package/dist/test-utils/db-utils.js.map +1 -1
  174. package/dist/test-utils/factories/index.d.ts +3 -0
  175. package/dist/test-utils/factories/index.d.ts.map +1 -1
  176. package/dist/test-utils/factories/index.js +3 -0
  177. package/dist/test-utils/factories/index.js.map +1 -1
  178. package/dist/test-utils/factories/municipal/muniIssues.d.ts +10 -0
  179. package/dist/test-utils/factories/municipal/muniIssues.d.ts.map +1 -0
  180. package/dist/test-utils/factories/municipal/muniIssues.js +30 -0
  181. package/dist/test-utils/factories/municipal/muniIssues.js.map +1 -0
  182. package/dist/test-utils/factories/talkpilot/contextNotes.d.ts +16 -0
  183. package/dist/test-utils/factories/talkpilot/contextNotes.d.ts.map +1 -0
  184. package/dist/test-utils/factories/talkpilot/contextNotes.js +33 -0
  185. package/dist/test-utils/factories/talkpilot/contextNotes.js.map +1 -0
  186. package/dist/test-utils/factories/websitalk/scans.d.ts +5 -0
  187. package/dist/test-utils/factories/websitalk/scans.d.ts.map +1 -0
  188. package/dist/test-utils/factories/websitalk/scans.js +25 -0
  189. package/dist/test-utils/factories/websitalk/scans.js.map +1 -0
  190. package/dist/utils/date.utils.d.ts +49 -0
  191. package/dist/utils/date.utils.d.ts.map +1 -0
  192. package/dist/utils/date.utils.js +108 -0
  193. package/dist/utils/date.utils.js.map +1 -0
  194. package/dist/utils/shared.types.d.ts +5 -0
  195. package/dist/utils/shared.types.d.ts.map +1 -0
  196. package/dist/utils/shared.types.js +3 -0
  197. package/dist/utils/shared.types.js.map +1 -0
  198. package/dist/websitalk/index.d.ts +7 -0
  199. package/dist/websitalk/index.d.ts.map +1 -0
  200. package/dist/websitalk/index.js +34 -0
  201. package/dist/websitalk/index.js.map +1 -0
  202. package/dist/websitalk/mongodb-client.d.ts +13 -0
  203. package/dist/websitalk/mongodb-client.d.ts.map +1 -0
  204. package/dist/websitalk/mongodb-client.js +56 -0
  205. package/dist/websitalk/mongodb-client.js.map +1 -0
  206. package/dist/websitalk/scans/index.d.ts +3 -0
  207. package/dist/websitalk/scans/index.d.ts.map +1 -0
  208. package/dist/websitalk/scans/index.js +19 -0
  209. package/dist/websitalk/scans/index.js.map +1 -0
  210. package/dist/websitalk/scans/scans.getters.d.ts +12 -0
  211. package/dist/websitalk/scans/scans.getters.d.ts.map +1 -0
  212. package/dist/websitalk/scans/scans.getters.js +74 -0
  213. package/dist/websitalk/scans/scans.getters.js.map +1 -0
  214. package/dist/websitalk/scans/scans.types.d.ts +45 -0
  215. package/dist/websitalk/scans/scans.types.d.ts.map +1 -0
  216. package/dist/websitalk/scans/scans.types.js +3 -0
  217. package/dist/websitalk/scans/scans.types.js.map +1 -0
  218. package/jest.config.js +20 -19
  219. package/package.json +46 -45
  220. package/src/connection.ts +12 -0
  221. package/src/index.ts +10 -0
  222. package/src/municipal/index.ts +1 -0
  223. package/src/municipal/muniIssues/__tests__/muniIssues.setters.spec.ts +94 -0
  224. package/src/municipal/muniIssues/__tests__/muniIssues.setup.spec.ts +92 -0
  225. package/src/municipal/muniIssues/index.ts +17 -0
  226. package/src/municipal/muniIssues/muniIssues.constants.ts +18 -0
  227. package/src/municipal/muniIssues/muniIssues.getters.ts +24 -0
  228. package/src/municipal/muniIssues/muniIssues.schema.ts +66 -0
  229. package/src/municipal/muniIssues/muniIssues.setters.ts +24 -0
  230. package/src/municipal/muniIssues/muniIssues.setup.ts +65 -0
  231. package/src/municipal/muniIssues/muniIssues.types.ts +37 -0
  232. package/src/municipal/tickets/__tests__/tickets.getters.spec.ts +1 -37
  233. package/src/municipal/tickets/__tests__/tickets.statistics.spec.ts +99 -0
  234. package/src/municipal/tickets/index.ts +9 -2
  235. package/src/municipal/tickets/tickets.constants.ts +8 -0
  236. package/src/municipal/tickets/tickets.getters.ts +0 -140
  237. package/src/municipal/tickets/tickets.statistics.aggregation.ts +110 -0
  238. package/src/municipal/tickets/tickets.statistics.getters.ts +132 -0
  239. package/src/municipal/tickets/tickets.types.ts +54 -43
  240. package/src/talkpilot/calls/__tests__/calls.dashboard.spec.ts +149 -0
  241. package/src/talkpilot/calls/__tests__/calls.spec.ts +48 -30
  242. package/src/talkpilot/calls/__tests__/calls.statistics.spec.ts +481 -0
  243. package/src/talkpilot/calls/calls.constants.ts +20 -0
  244. package/src/talkpilot/calls/calls.getters.ts +6 -6
  245. package/src/talkpilot/calls/calls.statistics.getters.ts +668 -0
  246. package/src/talkpilot/calls/calls.statistics.types.ts +48 -0
  247. package/src/talkpilot/calls/calls.types.ts +18 -14
  248. package/src/talkpilot/calls/dashboard/calls.dashboard.ts +292 -0
  249. package/src/talkpilot/calls/dashboard/calls.dashboard.types.ts +57 -0
  250. package/src/talkpilot/calls/index.ts +4 -0
  251. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.getters.spec.ts +53 -0
  252. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +91 -7
  253. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +34 -1
  254. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +127 -92
  255. package/src/talkpilot/contextNotes/__tests__/contextNotes.getters.spec.ts +217 -0
  256. package/src/talkpilot/contextNotes/contextNotes.getters.ts +85 -0
  257. package/src/talkpilot/contextNotes/contextNotes.types.ts +20 -0
  258. package/src/talkpilot/contextNotes/index.ts +2 -0
  259. package/src/talkpilot/flows/__tests__/flows.schema.spec.ts +31 -2
  260. package/src/talkpilot/flows/flows.schema.ts +2 -1
  261. package/src/talkpilot/flows/flows.types.ts +3 -0
  262. package/src/talkpilot/index.ts +3 -0
  263. package/src/talkpilot/phone_numbers/__tests__/phone_numbers.spec.ts +40 -35
  264. package/src/talkpilot/phone_numbers/index.ts +2 -2
  265. package/src/talkpilot/phone_numbers/phone_numbers.getter.ts +10 -6
  266. package/src/talkpilot/phone_numbers/phone_numbers.schema.ts +12 -12
  267. package/src/talkpilot/phone_numbers/phone_numbers.types.ts +4 -4
  268. package/src/talkpilot/products/__tests__/products.getters.spec.ts +44 -0
  269. package/src/talkpilot/products/index.ts +2 -0
  270. package/src/talkpilot/products/products.getters.ts +12 -0
  271. package/src/talkpilot/products/products.types.ts +9 -0
  272. package/src/talkpilot/results/index.ts +2 -0
  273. package/src/talkpilot/results/results.getter.ts +14 -2
  274. package/src/talkpilot/retry_analyze/__tests__/retryAnalyze.getters.spec.ts +156 -0
  275. package/src/talkpilot/retry_analyze/index.ts +2 -0
  276. package/src/talkpilot/retry_analyze/retryAnalyze.getters.ts +84 -0
  277. package/src/talkpilot/retry_analyze/retryAnalyze.types.ts +13 -0
  278. package/src/test-utils/db-utils.ts +3 -1
  279. package/src/test-utils/factories/index.ts +3 -0
  280. package/src/test-utils/factories/municipal/muniIssues.ts +32 -0
  281. package/src/test-utils/factories/talkpilot/contextNotes.ts +40 -0
  282. package/src/test-utils/factories/websitalk/scans.ts +23 -0
  283. package/src/utils/date.utils.ts +126 -0
  284. package/src/utils/shared.types.ts +4 -0
  285. package/src/websitalk/index.ts +15 -0
  286. package/src/websitalk/mongodb-client.ts +61 -0
  287. package/src/websitalk/scans/__tests__/scans.spec.ts +218 -0
  288. package/src/websitalk/scans/index.ts +2 -0
  289. package/src/websitalk/scans/scans.getters.ts +113 -0
  290. package/src/websitalk/scans/scans.types.ts +53 -0
  291. package/tsconfig.json +23 -23
  292. package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts +0 -4
  293. package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts.map +0 -1
  294. package/dist/talkpilot/subscriptions/subscriptions.utils.js +0 -20
  295. package/dist/talkpilot/subscriptions/subscriptions.utils.js.map +0 -1
@@ -0,0 +1,32 @@
1
+ import { Factory } from "fishery";
2
+ import { faker } from "@faker-js/faker";
3
+ import type { CreateMuniIssueInput } from "../../../municipal";
4
+
5
+ /**
6
+ * Test factory for `createMuniIssue` input.
7
+ * `jira.key` uses `sequence` so each build gets a unique key (the real key
8
+ * comes from Jira in production).
9
+ */
10
+ export const muniIssueInputFactory = Factory.define<CreateMuniIssueInput>(
11
+ ({ sequence }) => ({
12
+ callSid: `CA${faker.string.alphanumeric(10)}`,
13
+ clientId: faker.string.uuid(),
14
+ cityName: faker.location.city(),
15
+ issueContent: {
16
+ issuerName: faker.person.fullName(),
17
+ issuerRole: faker.person.jobTitle(),
18
+ issueDescription: faker.lorem.sentence(),
19
+ desiredBehavior: faker.lorem.sentence(),
20
+ },
21
+ jira: {
22
+ key: `CP-${sequence}`,
23
+ syncStatus: "created",
24
+ },
25
+ }),
26
+ );
27
+
28
+ export function createMuniIssueInput(
29
+ overrides?: Partial<CreateMuniIssueInput>,
30
+ ): CreateMuniIssueInput {
31
+ return muniIssueInputFactory.build(overrides);
32
+ }
@@ -0,0 +1,40 @@
1
+ import { Factory } from "fishery";
2
+ import { ObjectId } from "mongodb";
3
+ import { faker } from "@faker-js/faker";
4
+ import type { ContextNote, ContextNoteEntry } from "../../../talkpilot";
5
+
6
+ export const contextNoteEntryFactory = Factory.define<ContextNoteEntry>(() => ({
7
+ _id: new ObjectId(),
8
+ title: faker.lorem.words(3),
9
+ content: faker.lorem.sentences(2),
10
+ activeFrom: new Date(),
11
+ expiresAt: null,
12
+ createdAt: new Date(),
13
+ updatedAt: new Date(),
14
+ }));
15
+
16
+ export const contextNoteFactory = Factory.define<
17
+ ContextNote & { _id: ObjectId }
18
+ >(() => ({
19
+ _id: new ObjectId(),
20
+ clientId: faker.string.uuid(),
21
+ product: "Municipal",
22
+ notes: [],
23
+ }));
24
+
25
+ export function createContextNoteEntry(
26
+ overrides?: Partial<ContextNoteEntry>,
27
+ ): ContextNoteEntry {
28
+ return {
29
+ ...contextNoteEntryFactory.build(),
30
+ ...overrides,
31
+ } as ContextNoteEntry;
32
+ }
33
+
34
+ export function createContextNote(
35
+ overrides?: Partial<ContextNote>,
36
+ ): ContextNote & { _id: ObjectId } {
37
+ return { ...contextNoteFactory.build(), ...overrides } as ContextNote & {
38
+ _id: ObjectId;
39
+ };
40
+ }
@@ -0,0 +1,23 @@
1
+ import { Factory } from "fishery";
2
+ import { faker } from "@faker-js/faker";
3
+ import type { Scan } from "../../../websitalk";
4
+
5
+ export const scanFactory = Factory.define<Scan>(() => ({
6
+ clientId: faker.string.uuid(),
7
+ baseUrl: faker.internet.url(),
8
+ status: "CHECKING",
9
+ activeTasksCount: 0,
10
+ pagesScanned: 0,
11
+ pagesSkipped: 0,
12
+ skipped: [],
13
+ siteChrome: {
14
+ url: faker.internet.url(),
15
+ sourceType: "site_chrome",
16
+ },
17
+ createdAt: new Date(),
18
+ updatedAt: new Date(),
19
+ }));
20
+
21
+ export function createScanFixture(overrides?: Partial<Scan>): Scan {
22
+ return scanFactory.build(overrides);
23
+ }
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Shared timezone-aware date helpers for statistics aggregations.
3
+ */
4
+
5
+ /** Milliseconds in a single day. */
6
+ export const DAY_MS = 24 * 60 * 60 * 1000;
7
+
8
+ /** Maps a short weekday name (as produced by Intl) to its index (Sun = 0). */
9
+ export const WEEKDAY: Record<string, number> = {
10
+ Sun: 0,
11
+ Mon: 1,
12
+ Tue: 2,
13
+ Wed: 3,
14
+ Thu: 4,
15
+ Fri: 5,
16
+ Sat: 6,
17
+ };
18
+
19
+ /** Parses a `YYYY-MM-DD` string into `[year, month, day]` numbers. */
20
+ export const parseYmd = (dateStr: string): [number, number, number] => {
21
+ const [y, m, d] = dateStr.split("-").map(Number);
22
+ return [y, m, d];
23
+ };
24
+
25
+ /** Builds a zero-padded `YYYY-MM-DD` string from numeric parts. */
26
+ export const ymdToStr = (y: number, m: number, d: number): string =>
27
+ `${y}-${String(m).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
28
+
29
+ /** Parses a `YYYY-MM-DD` string as midnight UTC. */
30
+ export const toUtcDate = (dateStr: string): Date =>
31
+ new Date(`${dateStr}T00:00:00.000Z`);
32
+
33
+ /** Formats a date as a `YYYY-MM-DD` string in UTC. */
34
+ export const formatDate = (date: Date): string =>
35
+ date.toISOString().slice(0, 10);
36
+
37
+ /** Adds `days` to a `YYYY-MM-DD` string, returning the resulting `YYYY-MM-DD`. */
38
+ export const addDaysToDateStr = (dateStr: string, days: number): string =>
39
+ formatDate(new Date(toUtcDate(dateStr).getTime() + days * DAY_MS));
40
+
41
+ /** Next civil calendar day (proleptic Gregorian). Avoids DST 24h steps that can stall iteration. */
42
+ export const incrementYmd = (
43
+ y: number,
44
+ m: number,
45
+ d: number,
46
+ ): [number, number, number] => {
47
+ const next = new Date(Date.UTC(y, m - 1, d + 1));
48
+ return [next.getUTCFullYear(), next.getUTCMonth() + 1, next.getUTCDate()];
49
+ };
50
+
51
+ /**
52
+ * Formats a date as the calendar day in the given timezone.
53
+ * `en-CA` yields ISO-8601 `YYYY-MM-DD`, which is lexicographically sortable
54
+ * (string compare == chronological compare) and matches MongoDB's `%Y-%m-%d`.
55
+ */
56
+ export const formatYmdInTz = (date: Date, timezone: string): string =>
57
+ new Intl.DateTimeFormat("en-CA", { timeZone: timezone }).format(date);
58
+
59
+ /**
60
+ * Returns the calendar day in the given timezone as numeric parts.
61
+ * Reads parts by `type` (not string order), so `en-US` is fine here.
62
+ */
63
+ export const getYmdInTz = (date: Date, timezone: string) => {
64
+ const parts = Object.fromEntries(
65
+ new Intl.DateTimeFormat("en-US", {
66
+ timeZone: timezone,
67
+ year: "numeric",
68
+ month: "numeric",
69
+ day: "numeric",
70
+ })
71
+ .formatToParts(date)
72
+ .map((p) => [p.type, Number(p.value)]),
73
+ );
74
+ return { y: parts.year, m: parts.month, d: parts.day };
75
+ };
76
+
77
+ /** Weekday index (Sun = 0) of a date in the given timezone. */
78
+ export const getWeekdayInTz = (date: Date, timezone: string): number =>
79
+ WEEKDAY[
80
+ new Intl.DateTimeFormat("en-US", {
81
+ timeZone: timezone,
82
+ weekday: "short",
83
+ }).format(date)
84
+ ] ?? 0;
85
+
86
+ /**
87
+ * Returns the given `YYYY-MM-DD` day at noon UTC — a stable anchor inside the
88
+ * calendar day, far from midnight so timezone/DST shifts can't cross a date boundary.
89
+ */
90
+ export const dateAtNoonUtc = (dateStr: string): Date => {
91
+ const [y, m, d] = parseYmd(dateStr);
92
+ return new Date(Date.UTC(y, m - 1, d, 12, 0, 0));
93
+ };
94
+
95
+ /**
96
+ * UTC instant at the start of the given `YYYY-MM-DD` calendar day in `timezone`.
97
+ * Binary-searches the boundary so it stays correct across DST transitions.
98
+ */
99
+ export const startOfCalendarDayInTz = (
100
+ dateStr: string,
101
+ timezone: string,
102
+ ): Date => {
103
+ const anchor = dateAtNoonUtc(dateStr).getTime();
104
+ let low = anchor - 2 * DAY_MS;
105
+ let high = anchor + DAY_MS;
106
+
107
+ while (high - low > 1 /* ms */) {
108
+ const mid = Math.floor((low + high) / 2);
109
+ if (formatYmdInTz(new Date(mid), timezone) >= dateStr) high = mid;
110
+ else low = mid;
111
+ }
112
+
113
+ return new Date(high);
114
+ };
115
+
116
+ /** UTC instant at the last millisecond of the given calendar day in `timezone`. */
117
+ export const endOfCalendarDayInTz = (
118
+ dateStr: string,
119
+ timezone: string,
120
+ ): Date => {
121
+ const [y, m, d] = parseYmd(dateStr);
122
+ const nextDay = new Date(Date.UTC(y, m - 1, d + 1))
123
+ .toISOString()
124
+ .slice(0, 10);
125
+ return new Date(startOfCalendarDayInTz(nextDay, timezone).getTime() - 1);
126
+ };
@@ -0,0 +1,4 @@
1
+ export interface CallLengthThresholds {
2
+ shortThreshold: number;
3
+ mediumThreshold: number;
4
+ }
@@ -0,0 +1,15 @@
1
+ import { Db, ObjectId as MongoObjectId } from "mongodb";
2
+
3
+ export * from "./scans";
4
+ export { websitalkMongodbClient } from "./mongodb-client";
5
+
6
+ let db: Db;
7
+ export const setDb = (d: Db) => {
8
+ db = d;
9
+ };
10
+
11
+ export const getDb = (): Db => {
12
+ if (!db) throw new Error("Website Talk DB not initialised");
13
+ return db;
14
+ };
15
+ export const ObjectId = MongoObjectId;
@@ -0,0 +1,61 @@
1
+ import { MongoClient, Db } from "mongodb";
2
+ import { setDb } from "./index";
3
+ import { validateConfig, validateMongoUri } from "../utils/validation";
4
+
5
+ class WebsitalkMongoDBClient {
6
+ private client: MongoClient | null = null;
7
+ private db: Db | null = null;
8
+ private readonly defaultDbName = "website-talk";
9
+
10
+ async connect(uri?: string, dbName?: string): Promise<void> {
11
+ if (this.client) {
12
+ return;
13
+ }
14
+
15
+ const mongodbUri = uri || process.env.MONGO_URI || process.env.MONGODB_URI;
16
+ validateConfig("MONGO_URI", mongodbUri);
17
+ validateMongoUri(mongodbUri!);
18
+
19
+ try {
20
+ this.client = new MongoClient(mongodbUri!);
21
+ await this.client.connect();
22
+ const targetDbName =
23
+ dbName || process.env.WEBSITALK_DB_NAME || this.defaultDbName;
24
+ this.db = this.client.db(targetDbName);
25
+ setDb(this.db);
26
+ console.info(`[core-db] Website Talk MongoDB connected: ${targetDbName}`);
27
+ } catch (error) {
28
+ console.error("[core-db] Website Talk connection failed", error);
29
+ throw error;
30
+ }
31
+ }
32
+
33
+ async disconnect(): Promise<void> {
34
+ if (this.client) {
35
+ try {
36
+ await this.client.close();
37
+ this.client = null;
38
+ this.db = null;
39
+ console.info("Website Talk MongoDB disconnected successfully");
40
+ } catch (error) {
41
+ console.error("[core-db] Website Talk disconnection failed", error);
42
+ throw error;
43
+ }
44
+ }
45
+ }
46
+
47
+ getDb(): Db {
48
+ if (!this.db) {
49
+ throw new Error(
50
+ "Website Talk database not initialized. Call connect() first.",
51
+ );
52
+ }
53
+ return this.db;
54
+ }
55
+
56
+ isConnected(): boolean {
57
+ return this.client !== null && this.client !== undefined;
58
+ }
59
+ }
60
+
61
+ export const websitalkMongodbClient = new WebsitalkMongoDBClient();
@@ -0,0 +1,218 @@
1
+ import {
2
+ getScansCollection,
3
+ getScansByClientAndDateRange,
4
+ getScanFieldsByClientAndDateRange,
5
+ findScansByQuery,
6
+ countScans,
7
+ createScanDoc,
8
+ updateScanDoc,
9
+ getActiveScanStatus,
10
+ } from "../scans.getters";
11
+ import { createScanFixture } from "../../../test-utils/factories";
12
+ import { ObjectId } from "mongodb";
13
+
14
+ describe("db.websitalk.scans", () => {
15
+ describe("getScansByClientAndDateRange()", () => {
16
+ it("should return full scan documents within the date range", async () => {
17
+ const clientId = "dateRangeClient";
18
+ const startDate = new Date("2023-05-01");
19
+ const endDate = new Date("2023-05-31");
20
+
21
+ const scanInside = createScanFixture({ clientId, status: "COMPLETED" });
22
+ const scanBefore = createScanFixture({ clientId });
23
+ const scanAfter = createScanFixture({ clientId });
24
+
25
+ await getScansCollection().insertMany([
26
+ { ...scanInside, createdAt: new Date("2023-05-15") },
27
+ { ...scanBefore, createdAt: new Date("2023-04-30") },
28
+ { ...scanAfter, createdAt: new Date("2023-06-01") },
29
+ ]);
30
+
31
+ const result = await getScansByClientAndDateRange(
32
+ clientId,
33
+ startDate,
34
+ endDate,
35
+ );
36
+
37
+ expect(result.length).toBe(1);
38
+ expect(result[0]).toMatchObject({
39
+ clientId,
40
+ baseUrl: scanInside.baseUrl,
41
+ status: "COMPLETED",
42
+ });
43
+ expect(result[0]).toHaveProperty("siteChrome");
44
+ expect(result[0]).toHaveProperty("skipped");
45
+ });
46
+
47
+ it("should respect the date range boundaries (inclusive)", async () => {
48
+ const clientId = "boundaryClient";
49
+ const onStart = createScanFixture({ clientId });
50
+ const onEnd = createScanFixture({ clientId });
51
+
52
+ await getScansCollection().insertMany([
53
+ { ...onStart, createdAt: new Date("2023-05-01") },
54
+ { ...onEnd, createdAt: new Date("2023-05-31") },
55
+ ]);
56
+
57
+ const result = await getScansByClientAndDateRange(
58
+ clientId,
59
+ new Date("2023-05-01"),
60
+ new Date("2023-05-31"),
61
+ );
62
+
63
+ expect(result.length).toBe(2);
64
+ });
65
+ });
66
+
67
+ describe("getScanFieldsByClientAndDateRange()", () => {
68
+ it("should return only the requested fields (plus _id)", async () => {
69
+ const clientId = "projectionClient";
70
+ const scan = createScanFixture({
71
+ clientId,
72
+ status: "COMPLETED",
73
+ siteChrome: { url: "https://example.com", sourceType: "site_chrome" },
74
+ skipped: [{ path: "/broken", reason: "HTTP 500" }],
75
+ });
76
+
77
+ await getScansCollection().insertOne({
78
+ ...scan,
79
+ createdAt: new Date("2023-05-15"),
80
+ });
81
+
82
+ const [result] = await getScanFieldsByClientAndDateRange(
83
+ clientId,
84
+ new Date("2023-05-01"),
85
+ new Date("2023-05-31"),
86
+ ["clientId", "baseUrl", "status"],
87
+ );
88
+
89
+ expect(result).toMatchObject({
90
+ clientId,
91
+ baseUrl: scan.baseUrl,
92
+ status: "COMPLETED",
93
+ });
94
+ expect(result).toHaveProperty("_id");
95
+ expect(result).not.toHaveProperty("siteChrome");
96
+ expect(result).not.toHaveProperty("skipped");
97
+ expect(result).not.toHaveProperty("createdAt");
98
+ });
99
+ });
100
+
101
+ describe("findScansByQuery() / countScans()", () => {
102
+ it("should find scans matching a raw filter and count them", async () => {
103
+ const clientId = "queryClient";
104
+ const completed = createScanFixture({ clientId, status: "COMPLETED" });
105
+ const scanning = createScanFixture({ clientId, status: "SCANNING" });
106
+
107
+ await getScansCollection().insertMany([completed, scanning]);
108
+
109
+ const result = await findScansByQuery({ clientId, status: "COMPLETED" });
110
+ const count = await countScans({ clientId });
111
+
112
+ expect(result.length).toBe(1);
113
+ expect(result[0].baseUrl).toBe(completed.baseUrl);
114
+ expect(count).toBe(2);
115
+ });
116
+ });
117
+
118
+ describe("createScanDoc()", () => {
119
+ it("should insert a scan with default values for the rest of the fields", async () => {
120
+ const clientId = "createClient";
121
+ const baseUrl = "https://example.com";
122
+
123
+ const { insertedId } = await createScanDoc({ clientId, baseUrl });
124
+
125
+ const result = await getScansCollection().findOne({ _id: insertedId });
126
+
127
+ expect(result).toMatchObject({
128
+ clientId,
129
+ baseUrl,
130
+ status: "CHECKING",
131
+ activeTasksCount: 0,
132
+ pagesScanned: 0,
133
+ pagesSkipped: 0,
134
+ skipped: [],
135
+ siteChrome: {},
136
+ });
137
+ expect(result?.createdAt).toBeInstanceOf(Date);
138
+ expect(result?.updatedAt).toBeInstanceOf(Date);
139
+ });
140
+ });
141
+
142
+ describe("updateScanDoc()", () => {
143
+ it("should update the given fields and bump updatedAt", async () => {
144
+ const scan = createScanFixture({ status: "SCANNING", pagesScanned: 0 });
145
+ const { insertedId } = await getScansCollection().insertOne(scan);
146
+
147
+ const result = await updateScanDoc(insertedId, {
148
+ status: "COMPLETED",
149
+ pagesScanned: 10,
150
+ });
151
+
152
+ expect(result).toMatchObject({
153
+ _id: insertedId,
154
+ status: "COMPLETED",
155
+ pagesScanned: 10,
156
+ });
157
+ expect(result?.updatedAt.getTime()).toBeGreaterThan(
158
+ scan.updatedAt.getTime(),
159
+ );
160
+ });
161
+
162
+ it("should return null when the scan does not exist", async () => {
163
+ const result = await updateScanDoc(new ObjectId(), { status: "FAILED" });
164
+
165
+ expect(result).toBeNull();
166
+ });
167
+ });
168
+
169
+ describe("getActiveScanStatus()", () => {
170
+ it("should return isActive: true with the status when the latest scan is still in progress", async () => {
171
+ const clientId = "activeClient";
172
+
173
+ await getScansCollection().insertMany([
174
+ createScanFixture({
175
+ clientId,
176
+ status: "COMPLETED",
177
+ createdAt: new Date("2023-05-01"),
178
+ }),
179
+ createScanFixture({
180
+ clientId,
181
+ status: "SCANNING",
182
+ createdAt: new Date("2023-05-15"),
183
+ }),
184
+ ]);
185
+
186
+ const result = await getActiveScanStatus(clientId);
187
+
188
+ expect(result).toEqual({ isActive: true, status: "SCANNING" });
189
+ });
190
+
191
+ it("should return isActive: false when the latest scan finished (COMPLETED / FAILED)", async () => {
192
+ const clientId = "finishedClient";
193
+
194
+ await getScansCollection().insertMany([
195
+ createScanFixture({
196
+ clientId,
197
+ status: "SCANNING",
198
+ createdAt: new Date("2023-05-01"),
199
+ }),
200
+ createScanFixture({
201
+ clientId,
202
+ status: "FAILED",
203
+ createdAt: new Date("2023-05-15"),
204
+ }),
205
+ ]);
206
+
207
+ const result = await getActiveScanStatus(clientId);
208
+
209
+ expect(result).toEqual({ isActive: false });
210
+ });
211
+
212
+ it("should return isActive: false when the client has no scans", async () => {
213
+ const result = await getActiveScanStatus("noScansClient");
214
+
215
+ expect(result).toEqual({ isActive: false });
216
+ });
217
+ });
218
+ });
@@ -0,0 +1,2 @@
1
+ export * from "./scans.getters";
2
+ export * from "./scans.types";
@@ -0,0 +1,113 @@
1
+ import { Collection, Filter, ObjectId } from "mongodb";
2
+ import { getDb } from "../index";
3
+ import {
4
+ applyQueryOptions,
5
+ QueryOptions,
6
+ } from "../../talkpilot/utils/query.utils";
7
+ import type {
8
+ ActiveScanStatus,
9
+ Scan,
10
+ ScanDoc,
11
+ ScanStatus,
12
+ } from "./scans.types";
13
+
14
+ export const getScansCollection = (): Collection<Scan> => {
15
+ return getDb().collection<Scan>("scans");
16
+ };
17
+
18
+ export const getScansByClientAndDateRange = (
19
+ clientId: string,
20
+ startDate: Date,
21
+ endDate: Date,
22
+ ): Promise<ScanDoc[]> => {
23
+ return getScansCollection()
24
+ .find({
25
+ clientId,
26
+ createdAt: {
27
+ $gte: startDate,
28
+ $lte: endDate,
29
+ },
30
+ })
31
+ .toArray();
32
+ };
33
+
34
+ export const getScanFieldsByClientAndDateRange = <K extends keyof Scan>(
35
+ clientId: string,
36
+ startDate: Date,
37
+ endDate: Date,
38
+ fields: K[],
39
+ ): Promise<Pick<ScanDoc, "_id" | K>[]> => {
40
+ const projection = fields.reduce<Record<string, 1>>((acc, field) => {
41
+ acc[field as string] = 1;
42
+ return acc;
43
+ }, {});
44
+
45
+ return getScansCollection()
46
+ .find(
47
+ {
48
+ clientId,
49
+ createdAt: {
50
+ $gte: startDate,
51
+ $lte: endDate,
52
+ },
53
+ },
54
+ { projection },
55
+ )
56
+ .toArray() as Promise<Pick<ScanDoc, "_id" | K>[]>;
57
+ };
58
+
59
+ export const createScanDoc = (
60
+ scan: Pick<Scan, "clientId" | "baseUrl" | "notifyPhoneNumber">,
61
+ ) => {
62
+ return getScansCollection().insertOne({
63
+ ...scan,
64
+ status: "CHECKING",
65
+ activeTasksCount: 0,
66
+ pagesScanned: 0,
67
+ pagesSkipped: 0,
68
+ skipped: [],
69
+ siteChrome: {} as Scan["siteChrome"],
70
+ createdAt: new Date(),
71
+ updatedAt: new Date(),
72
+ });
73
+ };
74
+
75
+ const FINISHED_SCAN_STATUSES: ScanStatus[] = ["COMPLETED", "FAILED"];
76
+
77
+ export const getActiveScanStatus = async (
78
+ clientId: string,
79
+ ): Promise<ActiveScanStatus> => {
80
+ const latestScan = await getScansCollection().findOne(
81
+ { clientId },
82
+ { sort: { createdAt: -1 } },
83
+ );
84
+
85
+ if (!latestScan || FINISHED_SCAN_STATUSES.includes(latestScan.status)) {
86
+ return { isActive: false };
87
+ }
88
+
89
+ return { isActive: true, status: latestScan.status };
90
+ };
91
+
92
+ export const updateScanDoc = async (
93
+ scanId: ObjectId,
94
+ updates: Partial<Omit<Scan, "clientId" | "createdAt">>,
95
+ ): Promise<ScanDoc | null> => {
96
+ return await getScansCollection().findOneAndUpdate(
97
+ { _id: scanId },
98
+ { $set: { ...updates, updatedAt: new Date() } },
99
+ { returnDocument: "after" },
100
+ );
101
+ };
102
+
103
+ export const findScansByQuery = async (
104
+ query: Filter<Scan>,
105
+ options?: QueryOptions,
106
+ ): Promise<ScanDoc[]> => {
107
+ const cursor = getScansCollection().find(query);
108
+ return await applyQueryOptions(cursor, options).toArray();
109
+ };
110
+
111
+ export const countScans = async (query: Filter<Scan>): Promise<number> => {
112
+ return getScansCollection().countDocuments(query);
113
+ };
@@ -0,0 +1,53 @@
1
+ import { WithId } from "mongodb";
2
+
3
+ export type ScanStatus =
4
+ | "CHECKING"
5
+ | "PREPARING"
6
+ | "SCANNING"
7
+ | "PROCESSING"
8
+ | "COMPLETED"
9
+ | "FAILED";
10
+
11
+ export type ActiveScanStatus =
12
+ | { isActive: true; status: ScanStatus }
13
+ | { isActive: false };
14
+
15
+ export type SiteChromeEntry = {
16
+ url: string;
17
+ urlCanonical?: string | null;
18
+ title?: string | null;
19
+ h1?: string | null;
20
+ language?: string | null;
21
+ sourceType?: string | null;
22
+ status?: string | null;
23
+ context?: string | null;
24
+ contentHash?: string | null;
25
+ errorMessage?: string | null;
26
+ breadcrumbs?: unknown;
27
+ description?: string | null;
28
+ sourceUrl?: string | null;
29
+ fetchedAt?: Date | null;
30
+ createdAt?: Date;
31
+ updatedAt?: Date;
32
+ };
33
+
34
+ export type SkippedEntry = {
35
+ path: string;
36
+ reason: string;
37
+ };
38
+
39
+ export type Scan = {
40
+ clientId: string;
41
+ baseUrl: string;
42
+ notifyPhoneNumber?: string | null;
43
+ status: ScanStatus;
44
+ activeTasksCount: number;
45
+ pagesScanned: number;
46
+ pagesSkipped: number;
47
+ skipped: SkippedEntry[];
48
+ siteChrome: SiteChromeEntry;
49
+ createdAt: Date;
50
+ updatedAt: Date;
51
+ };
52
+
53
+ export type ScanDoc = WithId<Scan>;