@talkpilot/core-db 1.0.1

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 (410) hide show
  1. package/.cursor/rules/development.mdc +65 -0
  2. package/DEVELOPMENT.md +98 -0
  3. package/README.md +140 -0
  4. package/dist/index.d.ts +7 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +29 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/municipal/cities/cities.getters.d.ts +9 -0
  9. package/dist/municipal/cities/cities.getters.d.ts.map +1 -0
  10. package/dist/municipal/cities/cities.getters.js +38 -0
  11. package/dist/municipal/cities/cities.getters.js.map +1 -0
  12. package/dist/municipal/cities/cities.types.d.ts +10 -0
  13. package/dist/municipal/cities/cities.types.d.ts.map +1 -0
  14. package/dist/municipal/cities/cities.types.js +3 -0
  15. package/dist/municipal/cities/cities.types.js.map +1 -0
  16. package/dist/municipal/cities/index.d.ts +3 -0
  17. package/dist/municipal/cities/index.d.ts.map +1 -0
  18. package/dist/municipal/cities/index.js +18 -0
  19. package/dist/municipal/cities/index.js.map +1 -0
  20. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts +62 -0
  21. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts.map +1 -0
  22. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js +180 -0
  23. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js.map +1 -0
  24. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts +69 -0
  25. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts.map +1 -0
  26. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.js +3 -0
  27. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.js.map +1 -0
  28. package/dist/municipal/departmentsSubjects/index.d.ts +3 -0
  29. package/dist/municipal/departmentsSubjects/index.d.ts.map +1 -0
  30. package/dist/municipal/departmentsSubjects/index.js +18 -0
  31. package/dist/municipal/departmentsSubjects/index.js.map +1 -0
  32. package/dist/municipal/index.d.ts +10 -0
  33. package/dist/municipal/index.d.ts.map +1 -0
  34. package/dist/municipal/index.js +37 -0
  35. package/dist/municipal/index.js.map +1 -0
  36. package/dist/municipal/mongodb-client.d.ts +13 -0
  37. package/dist/municipal/mongodb-client.d.ts.map +1 -0
  38. package/dist/municipal/mongodb-client.js +56 -0
  39. package/dist/municipal/mongodb-client.js.map +1 -0
  40. package/dist/municipal/streets/index.d.ts +3 -0
  41. package/dist/municipal/streets/index.d.ts.map +1 -0
  42. package/dist/municipal/streets/index.js +18 -0
  43. package/dist/municipal/streets/index.js.map +1 -0
  44. package/dist/municipal/streets/streets.getters.d.ts +21 -0
  45. package/dist/municipal/streets/streets.getters.d.ts.map +1 -0
  46. package/dist/municipal/streets/streets.getters.js +85 -0
  47. package/dist/municipal/streets/streets.getters.js.map +1 -0
  48. package/dist/municipal/streets/streets.types.d.ts +16 -0
  49. package/dist/municipal/streets/streets.types.d.ts.map +1 -0
  50. package/dist/municipal/streets/streets.types.js +3 -0
  51. package/dist/municipal/streets/streets.types.js.map +1 -0
  52. package/dist/municipal/tickets/index.d.ts +3 -0
  53. package/dist/municipal/tickets/index.d.ts.map +1 -0
  54. package/dist/municipal/tickets/index.js +18 -0
  55. package/dist/municipal/tickets/index.js.map +1 -0
  56. package/dist/municipal/tickets/tickets.getters.d.ts +44 -0
  57. package/dist/municipal/tickets/tickets.getters.d.ts.map +1 -0
  58. package/dist/municipal/tickets/tickets.getters.js +199 -0
  59. package/dist/municipal/tickets/tickets.getters.js.map +1 -0
  60. package/dist/municipal/tickets/tickets.types.d.ts +35 -0
  61. package/dist/municipal/tickets/tickets.types.d.ts.map +1 -0
  62. package/dist/municipal/tickets/tickets.types.js +3 -0
  63. package/dist/municipal/tickets/tickets.types.js.map +1 -0
  64. package/dist/talkpilot/agents/agents.getters.d.ts +9 -0
  65. package/dist/talkpilot/agents/agents.getters.d.ts.map +1 -0
  66. package/dist/talkpilot/agents/agents.getters.js +34 -0
  67. package/dist/talkpilot/agents/agents.getters.js.map +1 -0
  68. package/dist/talkpilot/agents/agents.types.d.ts +13 -0
  69. package/dist/talkpilot/agents/agents.types.d.ts.map +1 -0
  70. package/dist/talkpilot/agents/agents.types.js +3 -0
  71. package/dist/talkpilot/agents/agents.types.js.map +1 -0
  72. package/dist/talkpilot/agents/index.d.ts +3 -0
  73. package/dist/talkpilot/agents/index.d.ts.map +1 -0
  74. package/dist/talkpilot/agents/index.js +19 -0
  75. package/dist/talkpilot/agents/index.js.map +1 -0
  76. package/dist/talkpilot/calls/calls.getters.d.ts +24 -0
  77. package/dist/talkpilot/calls/calls.getters.d.ts.map +1 -0
  78. package/dist/talkpilot/calls/calls.getters.js +151 -0
  79. package/dist/talkpilot/calls/calls.getters.js.map +1 -0
  80. package/dist/talkpilot/calls/calls.types.d.ts +32 -0
  81. package/dist/talkpilot/calls/calls.types.d.ts.map +1 -0
  82. package/dist/talkpilot/calls/calls.types.js +3 -0
  83. package/dist/talkpilot/calls/calls.types.js.map +1 -0
  84. package/dist/talkpilot/calls/index.d.ts +3 -0
  85. package/dist/talkpilot/calls/index.d.ts.map +1 -0
  86. package/dist/talkpilot/calls/index.js +19 -0
  87. package/dist/talkpilot/calls/index.js.map +1 -0
  88. package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.d.ts +9 -0
  89. package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.d.ts.map +1 -0
  90. package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.js +76 -0
  91. package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.js.map +1 -0
  92. package/dist/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.d.ts +23 -0
  93. package/dist/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.d.ts.map +1 -0
  94. package/dist/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.js +3 -0
  95. package/dist/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.js.map +1 -0
  96. package/dist/talkpilot/clients/clients.getters.d.ts +5 -0
  97. package/dist/talkpilot/clients/clients.getters.d.ts.map +1 -0
  98. package/dist/talkpilot/clients/clients.getters.js +14 -0
  99. package/dist/talkpilot/clients/clients.getters.js.map +1 -0
  100. package/dist/talkpilot/clients/clients.types.d.ts +12 -0
  101. package/dist/talkpilot/clients/clients.types.d.ts.map +1 -0
  102. package/dist/talkpilot/clients/clients.types.js +3 -0
  103. package/dist/talkpilot/clients/clients.types.js.map +1 -0
  104. package/dist/talkpilot/clients/index.d.ts +3 -0
  105. package/dist/talkpilot/clients/index.d.ts.map +1 -0
  106. package/dist/talkpilot/clients/index.js +18 -0
  107. package/dist/talkpilot/clients/index.js.map +1 -0
  108. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +7 -0
  109. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -0
  110. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +21 -0
  111. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -0
  112. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +53 -0
  113. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -0
  114. package/dist/talkpilot/clientsConfig/clientsConfig.types.js +3 -0
  115. package/dist/talkpilot/clientsConfig/clientsConfig.types.js.map +1 -0
  116. package/dist/talkpilot/clientsConfig/index.d.ts +3 -0
  117. package/dist/talkpilot/clientsConfig/index.d.ts.map +1 -0
  118. package/dist/talkpilot/clientsConfig/index.js +19 -0
  119. package/dist/talkpilot/clientsConfig/index.js.map +1 -0
  120. package/dist/talkpilot/flows/flows.getter.d.ts +5 -0
  121. package/dist/talkpilot/flows/flows.getter.d.ts.map +1 -0
  122. package/dist/talkpilot/flows/flows.getter.js +13 -0
  123. package/dist/talkpilot/flows/flows.getter.js.map +1 -0
  124. package/dist/talkpilot/flows/flows.schema.d.ts +154 -0
  125. package/dist/talkpilot/flows/flows.schema.d.ts.map +1 -0
  126. package/dist/talkpilot/flows/flows.schema.js +94 -0
  127. package/dist/talkpilot/flows/flows.schema.js.map +1 -0
  128. package/dist/talkpilot/flows/flows.types.d.ts +116 -0
  129. package/dist/talkpilot/flows/flows.types.d.ts.map +1 -0
  130. package/dist/talkpilot/flows/flows.types.js +3 -0
  131. package/dist/talkpilot/flows/flows.types.js.map +1 -0
  132. package/dist/talkpilot/flows/index.d.ts +3 -0
  133. package/dist/talkpilot/flows/index.d.ts.map +1 -0
  134. package/dist/talkpilot/flows/index.js +6 -0
  135. package/dist/talkpilot/flows/index.js.map +1 -0
  136. package/dist/talkpilot/groups/groups.getters.d.ts +8 -0
  137. package/dist/talkpilot/groups/groups.getters.d.ts.map +1 -0
  138. package/dist/talkpilot/groups/groups.getters.js +26 -0
  139. package/dist/talkpilot/groups/groups.getters.js.map +1 -0
  140. package/dist/talkpilot/groups/groups.types.d.ts +26 -0
  141. package/dist/talkpilot/groups/groups.types.d.ts.map +1 -0
  142. package/dist/talkpilot/groups/groups.types.js +3 -0
  143. package/dist/talkpilot/groups/groups.types.js.map +1 -0
  144. package/dist/talkpilot/groups/index.d.ts +3 -0
  145. package/dist/talkpilot/groups/index.d.ts.map +1 -0
  146. package/dist/talkpilot/groups/index.js +19 -0
  147. package/dist/talkpilot/groups/index.js.map +1 -0
  148. package/dist/talkpilot/groups/phone.utils.d.ts +4 -0
  149. package/dist/talkpilot/groups/phone.utils.d.ts.map +1 -0
  150. package/dist/talkpilot/groups/phone.utils.js +42 -0
  151. package/dist/talkpilot/groups/phone.utils.js.map +1 -0
  152. package/dist/talkpilot/index.d.ts +19 -0
  153. package/dist/talkpilot/index.d.ts.map +1 -0
  154. package/dist/talkpilot/index.js +46 -0
  155. package/dist/talkpilot/index.js.map +1 -0
  156. package/dist/talkpilot/leads/index.d.ts +3 -0
  157. package/dist/talkpilot/leads/index.d.ts.map +1 -0
  158. package/dist/talkpilot/leads/index.js +6 -0
  159. package/dist/talkpilot/leads/index.js.map +1 -0
  160. package/dist/talkpilot/leads/leads.getter.d.ts +4 -0
  161. package/dist/talkpilot/leads/leads.getter.d.ts.map +1 -0
  162. package/dist/talkpilot/leads/leads.getter.js +7 -0
  163. package/dist/talkpilot/leads/leads.getter.js.map +1 -0
  164. package/dist/talkpilot/leads/leads.schema.d.ts +50 -0
  165. package/dist/talkpilot/leads/leads.schema.d.ts.map +1 -0
  166. package/dist/talkpilot/leads/leads.schema.js +37 -0
  167. package/dist/talkpilot/leads/leads.schema.js.map +1 -0
  168. package/dist/talkpilot/leads/leads.types.d.ts +18 -0
  169. package/dist/talkpilot/leads/leads.types.d.ts.map +1 -0
  170. package/dist/talkpilot/leads/leads.types.js +3 -0
  171. package/dist/talkpilot/leads/leads.types.js.map +1 -0
  172. package/dist/talkpilot/mongodb-client.d.ts +13 -0
  173. package/dist/talkpilot/mongodb-client.d.ts.map +1 -0
  174. package/dist/talkpilot/mongodb-client.js +71 -0
  175. package/dist/talkpilot/mongodb-client.js.map +1 -0
  176. package/dist/talkpilot/phone_numbers/index.d.ts +3 -0
  177. package/dist/talkpilot/phone_numbers/index.d.ts.map +1 -0
  178. package/dist/talkpilot/phone_numbers/index.js +18 -0
  179. package/dist/talkpilot/phone_numbers/index.js.map +1 -0
  180. package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts +8 -0
  181. package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts.map +1 -0
  182. package/dist/talkpilot/phone_numbers/phone_numbers.getter.js +44 -0
  183. package/dist/talkpilot/phone_numbers/phone_numbers.getter.js.map +1 -0
  184. package/dist/talkpilot/phone_numbers/phone_numbers.schema.d.ts +29 -0
  185. package/dist/talkpilot/phone_numbers/phone_numbers.schema.d.ts.map +1 -0
  186. package/dist/talkpilot/phone_numbers/phone_numbers.schema.js +18 -0
  187. package/dist/talkpilot/phone_numbers/phone_numbers.schema.js.map +1 -0
  188. package/dist/talkpilot/phone_numbers/phone_numbers.types.d.ts +16 -0
  189. package/dist/talkpilot/phone_numbers/phone_numbers.types.d.ts.map +1 -0
  190. package/dist/talkpilot/phone_numbers/phone_numbers.types.js +3 -0
  191. package/dist/talkpilot/phone_numbers/phone_numbers.types.js.map +1 -0
  192. package/dist/talkpilot/plans/index.d.ts +3 -0
  193. package/dist/talkpilot/plans/index.d.ts.map +1 -0
  194. package/dist/talkpilot/plans/index.js +19 -0
  195. package/dist/talkpilot/plans/index.js.map +1 -0
  196. package/dist/talkpilot/plans/plans.getters.d.ts +10 -0
  197. package/dist/talkpilot/plans/plans.getters.d.ts.map +1 -0
  198. package/dist/talkpilot/plans/plans.getters.js +103 -0
  199. package/dist/talkpilot/plans/plans.getters.js.map +1 -0
  200. package/dist/talkpilot/plans/plans.types.d.ts +53 -0
  201. package/dist/talkpilot/plans/plans.types.d.ts.map +1 -0
  202. package/dist/talkpilot/plans/plans.types.js +29 -0
  203. package/dist/talkpilot/plans/plans.types.js.map +1 -0
  204. package/dist/talkpilot/results/index.d.ts +3 -0
  205. package/dist/talkpilot/results/index.d.ts.map +1 -0
  206. package/dist/talkpilot/results/index.js +6 -0
  207. package/dist/talkpilot/results/index.js.map +1 -0
  208. package/dist/talkpilot/results/results.getter.d.ts +7 -0
  209. package/dist/talkpilot/results/results.getter.d.ts.map +1 -0
  210. package/dist/talkpilot/results/results.getter.js +26 -0
  211. package/dist/talkpilot/results/results.getter.js.map +1 -0
  212. package/dist/talkpilot/results/results.schema.d.ts +35 -0
  213. package/dist/talkpilot/results/results.schema.d.ts.map +1 -0
  214. package/dist/talkpilot/results/results.schema.js +29 -0
  215. package/dist/talkpilot/results/results.schema.js.map +1 -0
  216. package/dist/talkpilot/results/results.types.d.ts +34 -0
  217. package/dist/talkpilot/results/results.types.d.ts.map +1 -0
  218. package/dist/talkpilot/results/results.types.js +3 -0
  219. package/dist/talkpilot/results/results.types.js.map +1 -0
  220. package/dist/talkpilot/sessions/index.d.ts +3 -0
  221. package/dist/talkpilot/sessions/index.d.ts.map +1 -0
  222. package/dist/talkpilot/sessions/index.js +6 -0
  223. package/dist/talkpilot/sessions/index.js.map +1 -0
  224. package/dist/talkpilot/sessions/sessions.getter.d.ts +7 -0
  225. package/dist/talkpilot/sessions/sessions.getter.d.ts.map +1 -0
  226. package/dist/talkpilot/sessions/sessions.getter.js +26 -0
  227. package/dist/talkpilot/sessions/sessions.getter.js.map +1 -0
  228. package/dist/talkpilot/sessions/sessions.schema.d.ts +53 -0
  229. package/dist/talkpilot/sessions/sessions.schema.d.ts.map +1 -0
  230. package/dist/talkpilot/sessions/sessions.schema.js +38 -0
  231. package/dist/talkpilot/sessions/sessions.schema.js.map +1 -0
  232. package/dist/talkpilot/sessions/sessions.types.d.ts +22 -0
  233. package/dist/talkpilot/sessions/sessions.types.d.ts.map +1 -0
  234. package/dist/talkpilot/sessions/sessions.types.js +3 -0
  235. package/dist/talkpilot/sessions/sessions.types.js.map +1 -0
  236. package/dist/talkpilot/subscriptions/index.d.ts +4 -0
  237. package/dist/talkpilot/subscriptions/index.d.ts.map +1 -0
  238. package/dist/talkpilot/subscriptions/index.js +20 -0
  239. package/dist/talkpilot/subscriptions/index.js.map +1 -0
  240. package/dist/talkpilot/subscriptions/subscriptions.getters.d.ts +13 -0
  241. package/dist/talkpilot/subscriptions/subscriptions.getters.d.ts.map +1 -0
  242. package/dist/talkpilot/subscriptions/subscriptions.getters.js +105 -0
  243. package/dist/talkpilot/subscriptions/subscriptions.getters.js.map +1 -0
  244. package/dist/talkpilot/subscriptions/subscriptions.getters.utils.d.ts +8 -0
  245. package/dist/talkpilot/subscriptions/subscriptions.getters.utils.d.ts.map +1 -0
  246. package/dist/talkpilot/subscriptions/subscriptions.getters.utils.js +27 -0
  247. package/dist/talkpilot/subscriptions/subscriptions.getters.utils.js.map +1 -0
  248. package/dist/talkpilot/subscriptions/subscriptions.types.d.ts +50 -0
  249. package/dist/talkpilot/subscriptions/subscriptions.types.d.ts.map +1 -0
  250. package/dist/talkpilot/subscriptions/subscriptions.types.js +14 -0
  251. package/dist/talkpilot/subscriptions/subscriptions.types.js.map +1 -0
  252. package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts +4 -0
  253. package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts.map +1 -0
  254. package/dist/talkpilot/subscriptions/subscriptions.utils.js +20 -0
  255. package/dist/talkpilot/subscriptions/subscriptions.utils.js.map +1 -0
  256. package/dist/test-utils/db-utils.d.ts +4 -0
  257. package/dist/test-utils/db-utils.d.ts.map +1 -0
  258. package/dist/test-utils/db-utils.js +28 -0
  259. package/dist/test-utils/db-utils.js.map +1 -0
  260. package/dist/test-utils/factories/index.d.ts +13 -0
  261. package/dist/test-utils/factories/index.d.ts.map +1 -0
  262. package/dist/test-utils/factories/index.js +29 -0
  263. package/dist/test-utils/factories/index.js.map +1 -0
  264. package/dist/test-utils/factories/municipal/cities.d.ts +5 -0
  265. package/dist/test-utils/factories/municipal/cities.d.ts.map +1 -0
  266. package/dist/test-utils/factories/municipal/cities.js +18 -0
  267. package/dist/test-utils/factories/municipal/cities.js.map +1 -0
  268. package/dist/test-utils/factories/municipal/departmentsSubjects.d.ts +5 -0
  269. package/dist/test-utils/factories/municipal/departmentsSubjects.d.ts.map +1 -0
  270. package/dist/test-utils/factories/municipal/departmentsSubjects.js +27 -0
  271. package/dist/test-utils/factories/municipal/departmentsSubjects.js.map +1 -0
  272. package/dist/test-utils/factories/municipal/streets.d.ts +5 -0
  273. package/dist/test-utils/factories/municipal/streets.d.ts.map +1 -0
  274. package/dist/test-utils/factories/municipal/streets.js +19 -0
  275. package/dist/test-utils/factories/municipal/streets.js.map +1 -0
  276. package/dist/test-utils/factories/municipal/tickets.d.ts +5 -0
  277. package/dist/test-utils/factories/municipal/tickets.d.ts.map +1 -0
  278. package/dist/test-utils/factories/municipal/tickets.js +27 -0
  279. package/dist/test-utils/factories/municipal/tickets.js.map +1 -0
  280. package/dist/test-utils/factories/talkpilot/agents.d.ts +5 -0
  281. package/dist/test-utils/factories/talkpilot/agents.d.ts.map +1 -0
  282. package/dist/test-utils/factories/talkpilot/agents.js +21 -0
  283. package/dist/test-utils/factories/talkpilot/agents.js.map +1 -0
  284. package/dist/test-utils/factories/talkpilot/calls.d.ts +6 -0
  285. package/dist/test-utils/factories/talkpilot/calls.d.ts.map +1 -0
  286. package/dist/test-utils/factories/talkpilot/calls.js +39 -0
  287. package/dist/test-utils/factories/talkpilot/calls.js.map +1 -0
  288. package/dist/test-utils/factories/talkpilot/clientAudioBuffers.d.ts +5 -0
  289. package/dist/test-utils/factories/talkpilot/clientAudioBuffers.d.ts.map +1 -0
  290. package/dist/test-utils/factories/talkpilot/clientAudioBuffers.js +18 -0
  291. package/dist/test-utils/factories/talkpilot/clientAudioBuffers.js.map +1 -0
  292. package/dist/test-utils/factories/talkpilot/clientsConfig.d.ts +5 -0
  293. package/dist/test-utils/factories/talkpilot/clientsConfig.d.ts.map +1 -0
  294. package/dist/test-utils/factories/talkpilot/clientsConfig.js +18 -0
  295. package/dist/test-utils/factories/talkpilot/clientsConfig.js.map +1 -0
  296. package/dist/test-utils/factories/talkpilot/flows.d.ts +14 -0
  297. package/dist/test-utils/factories/talkpilot/flows.d.ts.map +1 -0
  298. package/dist/test-utils/factories/talkpilot/flows.js +33 -0
  299. package/dist/test-utils/factories/talkpilot/flows.js.map +1 -0
  300. package/dist/test-utils/factories/talkpilot/groups.d.ts +6 -0
  301. package/dist/test-utils/factories/talkpilot/groups.d.ts.map +1 -0
  302. package/dist/test-utils/factories/talkpilot/groups.js +34 -0
  303. package/dist/test-utils/factories/talkpilot/groups.js.map +1 -0
  304. package/dist/test-utils/factories/talkpilot/phone_numbers.d.ts +16 -0
  305. package/dist/test-utils/factories/talkpilot/phone_numbers.d.ts.map +1 -0
  306. package/dist/test-utils/factories/talkpilot/phone_numbers.js +20 -0
  307. package/dist/test-utils/factories/talkpilot/phone_numbers.js.map +1 -0
  308. package/dist/test-utils/factories/talkpilot/sessions.d.ts +5 -0
  309. package/dist/test-utils/factories/talkpilot/sessions.d.ts.map +1 -0
  310. package/dist/test-utils/factories/talkpilot/sessions.js +37 -0
  311. package/dist/test-utils/factories/talkpilot/sessions.js.map +1 -0
  312. package/dist/utils/validation.d.ts +7 -0
  313. package/dist/utils/validation.d.ts.map +1 -0
  314. package/dist/utils/validation.js +23 -0
  315. package/dist/utils/validation.js.map +1 -0
  316. package/jest.config.js +22 -0
  317. package/package.json +43 -0
  318. package/src/__tests__/setup.ts +20 -0
  319. package/src/index.ts +13 -0
  320. package/src/municipal/__tests__/validation.spec.ts +50 -0
  321. package/src/municipal/cities/cities.getters.ts +44 -0
  322. package/src/municipal/cities/cities.types.ts +11 -0
  323. package/src/municipal/cities/index.ts +2 -0
  324. package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +273 -0
  325. package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +76 -0
  326. package/src/municipal/departmentsSubjects/index.ts +11 -0
  327. package/src/municipal/index.ts +18 -0
  328. package/src/municipal/mongodb-client.ts +58 -0
  329. package/src/municipal/streets/index.ts +2 -0
  330. package/src/municipal/streets/streets.getters.ts +105 -0
  331. package/src/municipal/streets/streets.types.ts +18 -0
  332. package/src/municipal/tickets/__tests__/tickets.getters.spec.ts +30 -0
  333. package/src/municipal/tickets/index.ts +2 -0
  334. package/src/municipal/tickets/tickets.getters.ts +234 -0
  335. package/src/municipal/tickets/tickets.types.ts +39 -0
  336. package/src/talkpilot/__tests__/db.spec.ts +38 -0
  337. package/src/talkpilot/__tests__/mongodb-client.spec.ts +19 -0
  338. package/src/talkpilot/__tests__/validation.spec.ts +62 -0
  339. package/src/talkpilot/agents/__tests__/agents.getters.spec.ts +29 -0
  340. package/src/talkpilot/agents/agents.getters.ts +34 -0
  341. package/src/talkpilot/agents/agents.types.ts +14 -0
  342. package/src/talkpilot/agents/index.ts +2 -0
  343. package/src/talkpilot/calls/__tests__/calls.spec.ts +56 -0
  344. package/src/talkpilot/calls/calls.getters.ts +134 -0
  345. package/src/talkpilot/calls/calls.types.ts +45 -0
  346. package/src/talkpilot/calls/index.ts +2 -0
  347. package/src/talkpilot/clientAudioBuffers/__tests__/clientAudioBuffer.getters.spec.ts +78 -0
  348. package/src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts +101 -0
  349. package/src/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.ts +25 -0
  350. package/src/talkpilot/clients/clients.getters.ts +11 -0
  351. package/src/talkpilot/clients/clients.types.ts +12 -0
  352. package/src/talkpilot/clients/index.ts +2 -0
  353. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +28 -0
  354. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +20 -0
  355. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +55 -0
  356. package/src/talkpilot/clientsConfig/index.ts +2 -0
  357. package/src/talkpilot/flows/flows.getter.ts +11 -0
  358. package/src/talkpilot/flows/flows.schema.ts +90 -0
  359. package/src/talkpilot/flows/flows.types.ts +151 -0
  360. package/src/talkpilot/flows/index.ts +2 -0
  361. package/src/talkpilot/groups/__tests__/groups.spec.ts +83 -0
  362. package/src/talkpilot/groups/__tests__/phone.utils.spec.ts +32 -0
  363. package/src/talkpilot/groups/groups.getters.ts +27 -0
  364. package/src/talkpilot/groups/groups.types.ts +29 -0
  365. package/src/talkpilot/groups/index.ts +2 -0
  366. package/src/talkpilot/groups/phone.utils.ts +40 -0
  367. package/src/talkpilot/index.ts +27 -0
  368. package/src/talkpilot/leads/index.ts +2 -0
  369. package/src/talkpilot/leads/leads.getter.ts +5 -0
  370. package/src/talkpilot/leads/leads.schema.ts +33 -0
  371. package/src/talkpilot/leads/leads.types.ts +20 -0
  372. package/src/talkpilot/mongodb-client.ts +78 -0
  373. package/src/talkpilot/phone_numbers/__tests__/phone_numbers.spec.ts +43 -0
  374. package/src/talkpilot/phone_numbers/index.ts +2 -0
  375. package/src/talkpilot/phone_numbers/phone_numbers.getter.ts +47 -0
  376. package/src/talkpilot/phone_numbers/phone_numbers.schema.ts +14 -0
  377. package/src/talkpilot/phone_numbers/phone_numbers.types.ts +15 -0
  378. package/src/talkpilot/plans/index.ts +2 -0
  379. package/src/talkpilot/plans/plans.getters.ts +121 -0
  380. package/src/talkpilot/plans/plans.types.ts +84 -0
  381. package/src/talkpilot/results/index.ts +2 -0
  382. package/src/talkpilot/results/results.getter.ts +26 -0
  383. package/src/talkpilot/results/results.schema.ts +25 -0
  384. package/src/talkpilot/results/results.types.ts +32 -0
  385. package/src/talkpilot/sessions/__tests__/sessions.spec.ts +43 -0
  386. package/src/talkpilot/sessions/index.ts +2 -0
  387. package/src/talkpilot/sessions/sessions.getter.ts +28 -0
  388. package/src/talkpilot/sessions/sessions.schema.ts +34 -0
  389. package/src/talkpilot/sessions/sessions.types.ts +28 -0
  390. package/src/talkpilot/subscriptions/__tests__/subscriptions.getters.utils.spec.ts +42 -0
  391. package/src/talkpilot/subscriptions/index.ts +3 -0
  392. package/src/talkpilot/subscriptions/subscriptions.getters.ts +147 -0
  393. package/src/talkpilot/subscriptions/subscriptions.getters.utils.ts +26 -0
  394. package/src/talkpilot/subscriptions/subscriptions.types.ts +65 -0
  395. package/src/test-utils/db-utils.ts +24 -0
  396. package/src/test-utils/factories/index.ts +12 -0
  397. package/src/test-utils/factories/municipal/cities.ts +16 -0
  398. package/src/test-utils/factories/municipal/departmentsSubjects.ts +29 -0
  399. package/src/test-utils/factories/municipal/streets.ts +17 -0
  400. package/src/test-utils/factories/municipal/tickets.ts +27 -0
  401. package/src/test-utils/factories/talkpilot/agents.ts +19 -0
  402. package/src/test-utils/factories/talkpilot/calls.ts +37 -0
  403. package/src/test-utils/factories/talkpilot/clientAudioBuffers.ts +18 -0
  404. package/src/test-utils/factories/talkpilot/clientsConfig.ts +19 -0
  405. package/src/test-utils/factories/talkpilot/flows.ts +31 -0
  406. package/src/test-utils/factories/talkpilot/groups.ts +33 -0
  407. package/src/test-utils/factories/talkpilot/phone_numbers.ts +20 -0
  408. package/src/test-utils/factories/talkpilot/sessions.ts +35 -0
  409. package/src/utils/validation.ts +23 -0
  410. package/tsconfig.json +27 -0
@@ -0,0 +1,84 @@
1
+ import { Sort, ObjectId, WithId } from 'mongodb';
2
+
3
+ export const CURRENCY_CODES = ['ILS', 'USD', 'EUR'] as const;
4
+ export type CurrencyCode = (typeof CURRENCY_CODES)[number];
5
+
6
+ /** Required fields for creating a new plan */
7
+ export const PLAN_REQUIRED_FIELDS = [
8
+ 'productKey',
9
+ 'name',
10
+ 'monthlyCallQuota',
11
+ 'currency',
12
+ 'priceMonthlyMinor',
13
+ 'isActive',
14
+ ];
15
+
16
+ /** All allowed fields that can be set/updated on a plan (excluding system fields) */
17
+ export const PLAN_ALLOWED_FIELDS = [
18
+ 'productKey',
19
+ 'name',
20
+ 'monthlyCallQuota',
21
+ 'currency',
22
+ 'priceMonthlyMinor',
23
+ 'region',
24
+ 'countryCode',
25
+ 'isActive',
26
+ 'originalPlanId',
27
+ 'tax',
28
+ 'metadata',
29
+ 'updatedAt',
30
+ ];
31
+
32
+ /** Logical product family, like "basic-500". Multiple regional variants can share it. */
33
+ export type ProductKey = 'basic-500' | 'pro-1100' | 'scale-6000' | (string & {});
34
+
35
+ /** Optional targeting to keep variants explicit */
36
+ export type RegionCode = 'IL' | 'EU' | 'US' | (string & {});
37
+ export type CountryCode = 'IL' | 'DE' | 'FR' | 'ES' | 'GB' | (string & {});
38
+
39
+ export type Tax = {
40
+ vatIncluded: boolean;
41
+ vatRatePct: number;
42
+ countryCode?: CountryCode;
43
+ };
44
+
45
+ export type Plan = {
46
+ productKey: ProductKey;
47
+ name: string;
48
+ monthlyCallQuota: number;
49
+ currency: CurrencyCode;
50
+ priceMonthlyMinor: number;
51
+ region?: RegionCode;
52
+ countryCode?: CountryCode;
53
+ isActive: boolean;
54
+ isFree?: boolean;
55
+ createdAt: Date;
56
+ updatedAt: Date;
57
+ originalPlanId?: string;
58
+ tax?: Tax;
59
+ metadata?: Record<string, unknown>;
60
+ };
61
+
62
+ export type SimplePlanFilter = {
63
+ _id?: ObjectId | string;
64
+ productKey?: ProductKey;
65
+ isActive?: boolean;
66
+ currency?: CurrencyCode;
67
+ region?: RegionCode;
68
+ countryCode?: CountryCode;
69
+ originalPlanId?: string;
70
+ };
71
+
72
+ export type PlanFilter = SimplePlanFilter & {
73
+ or?: SimplePlanFilter[];
74
+ and?: SimplePlanFilter[];
75
+ };
76
+
77
+ export type PlanQueryOptions = {
78
+ sort?: Sort;
79
+ limit?: number;
80
+ };
81
+
82
+ // Convenient aliases
83
+ export type PlanDoc = WithId<Plan>;
84
+ export type Plans = PlanDoc[];
@@ -0,0 +1,2 @@
1
+ export { getResultsCollection } from './results.getter';
2
+ export type * from './results.types';
@@ -0,0 +1,26 @@
1
+ import type { Collection } from 'mongodb';
2
+ import { getDb, ObjectId, Session } from '../index';
3
+ import type { Result } from './results.types';
4
+
5
+ export const getResultsCollection = (): Collection<Result> => getDb().collection<Result>('results');
6
+
7
+ export const createRunId = (): string => {
8
+ return `run_${new ObjectId().toString()}`;
9
+ };
10
+
11
+ export const initializeResultDocument = async (
12
+ clientId: string,
13
+ session?: Session
14
+ ): Promise<Result> => {
15
+ const { insertedId } = await getResultsCollection().insertOne({
16
+ runId: createRunId(),
17
+ date: new Date(),
18
+ calls: [],
19
+ sessionId: session ? session._id!.toString() : 'incomingCall',
20
+ sessionName: session ? session.session_name : 'incomingCall',
21
+ clientId,
22
+ });
23
+ const result = await getResultsCollection().findOne({ _id: insertedId });
24
+ if (!result) throw new Error('Failed to initialized result');
25
+ return result;
26
+ };
@@ -0,0 +1,25 @@
1
+ export const resultMongoSchema = {
2
+ bsonType: 'object',
3
+ required: [
4
+ '_id',
5
+ 'sessionId',
6
+ 'status',
7
+ 'callLength',
8
+ 'date',
9
+ 'userAnswer',
10
+ 'systemSessionId',
11
+ 'clientId',
12
+ ],
13
+ properties: {
14
+ _id: { bsonType: 'objectId' },
15
+ sessionId: { bsonType: 'string' },
16
+ status: { bsonType: 'string' },
17
+ callLength: { bsonType: 'string' },
18
+ date: { bsonType: 'date' },
19
+ userAnswer: { bsonType: 'string' },
20
+ recordingUrl: { bsonType: ['string', 'null'] },
21
+ systemSessionId: { bsonType: 'string' },
22
+ clientId: { bsonType: 'string' },
23
+ },
24
+ additionalProperties: false,
25
+ } as const;
@@ -0,0 +1,32 @@
1
+ import { ObjectId } from 'mongodb';
2
+
3
+ export interface Result {
4
+ _id?: ObjectId;
5
+ calls: CallResult[];
6
+ clientId: string;
7
+ date: Date;
8
+ runId: string;
9
+ sessionId: ObjectId | string;
10
+ sessionName: string;
11
+ }
12
+
13
+ export interface CallResult {
14
+ callSid: string;
15
+ status: string;
16
+ date: Date;
17
+ statuses: string[];
18
+ userAnswer: string | null;
19
+ color: string | null;
20
+ name: string | null;
21
+ callLength: string;
22
+ answeredBy: string | null;
23
+ to: string;
24
+ from: string;
25
+ transcription: TranscriptionSegment[];
26
+ flowId: ObjectId | string;
27
+ recordingUrl: string | null;
28
+ }
29
+
30
+ export type TranscriptionSegment =
31
+ | { assistant: string; user?: never }
32
+ | { user: string; assistant?: never };
@@ -0,0 +1,43 @@
1
+ import { findSessionOfIncomingCall, getSessionsCollection } from '../sessions.getter';
2
+ import { getFlowsCollection } from '../../flows/flows.getter';
3
+ import { getPhoneNumbersCollection } from '../../phone_numbers/phone_numbers.getter';
4
+ import { createFlow, createPhoneNumber, createSession } from '../../../test-utils/factories';
5
+ import { faker } from '@faker-js/faker';
6
+
7
+ describe('sessions', () => {
8
+ describe('findSessionOfIncomingCall()', () => {
9
+ it('returns session by incoming phone number', async () => {
10
+ const to = faker.phone.number();
11
+ const from = faker.phone.number();
12
+ const flow = createFlow();
13
+ const phoneNumberData = createPhoneNumber({ phone_number: to, flow_id: flow._id });
14
+ const json = {
15
+ [faker.lorem.word()]: faker.lorem.sentence(),
16
+ [faker.lorem.word()]: faker.number.int(),
17
+ [faker.lorem.word()]: faker.datatype.boolean(),
18
+ [faker.lorem.word()]: faker.internet.email(),
19
+ };
20
+
21
+ const session = createSession({
22
+ flow_id: flow._id,
23
+ phone_numbers: [{ phoneNumber: from, gender: 'male', name: 'yakov' }],
24
+ json,
25
+ });
26
+
27
+ await getFlowsCollection().insertOne(flow);
28
+ await getPhoneNumbersCollection().insertOne(phoneNumberData);
29
+ await getSessionsCollection().insertOne(session);
30
+
31
+ const result = await findSessionOfIncomingCall(from, to);
32
+ expect(result).toBeDefined();
33
+ expect(result?.phone_numbers[0].phoneNumber).toEqual(from);
34
+ expect(result?.flow_id).toEqual(flow._id);
35
+ expect(result?.json).toEqual(json);
36
+ });
37
+
38
+ it('returns null if receiver phone number is not found', async () => {
39
+ const result = await findSessionOfIncomingCall('any-from', 'non-existent-to');
40
+ expect(result).toBeNull();
41
+ });
42
+ });
43
+ });
@@ -0,0 +1,2 @@
1
+ export { getSessionsCollection } from './sessions.getter';
2
+ export type { Session } from './sessions.types';
@@ -0,0 +1,28 @@
1
+ import { ObjectId } from 'mongodb';
2
+ import type { Collection, WithId } from 'mongodb';
3
+ import { getDb, getPhoneNumbersCollection } from '../index';
4
+ import type { Session } from './sessions.types';
5
+
6
+ export const getSessionsCollection = (): Collection<Session> =>
7
+ getDb().collection<Session>('sessions');
8
+
9
+ export const findSessionById = (
10
+ sessionId: ObjectId,
11
+ clientId: string
12
+ ): Promise<WithId<Session> | null> => {
13
+ return getSessionsCollection().findOne({ _id: sessionId, clientId: clientId });
14
+ };
15
+
16
+ export const findSessionOfIncomingCall = async (from: string, to: string) => {
17
+ const receiverPhoneData = await getPhoneNumbersCollection().findOne({ phone_number: to });
18
+ if (!receiverPhoneData) {
19
+ return null;
20
+ }
21
+ const session = await getSessionsCollection().findOne({
22
+ flow_id: new ObjectId(receiverPhoneData.flow_id),
23
+ phone_numbers: {
24
+ $elemMatch: { phoneNumber: from },
25
+ },
26
+ });
27
+ return session;
28
+ };
@@ -0,0 +1,34 @@
1
+ export const sessionMongoSchema = {
2
+ bsonType: 'object',
3
+ required: ['session_name'],
4
+ properties: {
5
+ _id: { bsonType: 'objectId' },
6
+ session_name: { bsonType: 'string' },
7
+ company_name: { bsonType: ['string', 'null'] },
8
+ flow_id: { bsonType: ['objectId', 'string', 'null'] },
9
+ metaKeys: {
10
+ bsonType: ['array', 'null'],
11
+ items: {
12
+ bsonType: 'object',
13
+ required: ['key'],
14
+ properties: {
15
+ key: { bsonType: 'string' },
16
+ value: { bsonType: 'string' },
17
+ },
18
+ },
19
+ },
20
+ phone_numbers: {
21
+ bsonType: 'array',
22
+ items: {
23
+ bsonType: 'object',
24
+ required: ['name', 'phoneNumber'],
25
+ properties: {
26
+ name: { bsonType: 'string' },
27
+ phoneNumber: { bsonType: 'string' },
28
+ },
29
+ },
30
+ },
31
+ voice: { bsonType: ['string', 'null'] },
32
+ },
33
+ additionalProperties: false,
34
+ } as const;
@@ -0,0 +1,28 @@
1
+ import { ObjectId } from 'mongodb';
2
+
3
+ export type Session = {
4
+ _id?: ObjectId;
5
+
6
+ session_name: string;
7
+ company_name?: string | null;
8
+ flow_id?: ObjectId | string | null;
9
+ clientId: string;
10
+
11
+ metaKeys?: Array<{
12
+ key: string;
13
+ value?: string;
14
+ }> | null;
15
+
16
+ phone_numbers: Array<{
17
+ name: string;
18
+ phoneNumber: string;
19
+ gender: 'male' | 'female';
20
+ }>;
21
+
22
+ voice?: string | null;
23
+ json: Record<string, any> | null;
24
+
25
+ // Timestamps
26
+ createdAt?: Date;
27
+ updatedAt?: Date;
28
+ };
@@ -0,0 +1,42 @@
1
+ import { ObjectId } from 'mongodb';
2
+ import { buildSimpleQuery, buildDateRangeFilter } from '../subscriptions.getters.utils';
3
+ import { SimpleSubscriptionFilter } from '../subscriptions.types';
4
+
5
+ describe('subscriptions.getters.utils', () => {
6
+ describe('buildSimpleQuery', () => {
7
+ it('returns empty query when filter is empty', () => {
8
+ const filter: SimpleSubscriptionFilter = {};
9
+ const result = buildSimpleQuery(filter);
10
+ expect(result).toEqual({});
11
+ });
12
+
13
+ it('converts _id string to ObjectId', () => {
14
+ const id = '507f1f77bcf86cd799439011';
15
+ const filter: SimpleSubscriptionFilter = { _id: id };
16
+ const result = buildSimpleQuery(filter);
17
+ expect(result._id).toBeInstanceOf(ObjectId);
18
+ expect((result._id as ObjectId).toHexString()).toBe(id);
19
+ });
20
+
21
+ it('includes valid simple fields', () => {
22
+ const filter: SimpleSubscriptionFilter = {
23
+ clientId: 'abc',
24
+ isActive: true,
25
+ };
26
+ const result = buildSimpleQuery(filter);
27
+ expect(result).toEqual({ clientId: 'abc', isActive: true });
28
+ });
29
+ });
30
+
31
+ describe('buildDateRangeFilter', () => {
32
+ it('returns a mongo date range filter', () => {
33
+ const from = new Date('2023-01-01');
34
+ const to = new Date('2023-01-02');
35
+ const result = buildDateRangeFilter(from, to);
36
+ expect(result).toEqual({
37
+ $gte: from,
38
+ $lt: to,
39
+ });
40
+ });
41
+ });
42
+ });
@@ -0,0 +1,3 @@
1
+ export * from './subscriptions.types';
2
+ export * from './subscriptions.getters';
3
+ export * from './subscriptions.getters.utils';
@@ -0,0 +1,147 @@
1
+ import { getDb } from '../index';
2
+ import {
3
+ Subscription,
4
+ SubscriptionDoc,
5
+ SimpleSubscriptionFilter,
6
+ SubscriptionFilter,
7
+ SubscriptionQueryOptions,
8
+ } from './subscriptions.types';
9
+ import { ObjectId, Filter } from 'mongodb';
10
+ import { buildSimpleQuery } from './subscriptions.getters.utils';
11
+
12
+ export const getSubscriptionsCollection = () => {
13
+ return getDb().collection<Subscription>('subscriptions');
14
+ };
15
+
16
+ /**
17
+ * A simple filter for Subscription: each field may be a value or a MongoDB operator expression
18
+ */
19
+
20
+ export const findSubscriptions = async (
21
+ filter?: SubscriptionFilter,
22
+ options?: SubscriptionQueryOptions
23
+ ): Promise<SubscriptionDoc[]> => {
24
+ let query: Filter<Subscription> = {};
25
+
26
+ if (filter) {
27
+ const simpleFilter: SimpleSubscriptionFilter = {
28
+ _id: filter._id,
29
+ clientId: filter.clientId,
30
+ planId: filter.planId,
31
+ startDate: filter.startDate,
32
+ endDate: filter.endDate,
33
+ isActive: filter.isActive,
34
+ createdAt: filter.createdAt,
35
+ nextRenewalDate: filter.nextRenewalDate,
36
+ };
37
+ const baseQuery = buildSimpleQuery(simpleFilter);
38
+ if (Object.keys(baseQuery).length > 0) {
39
+ query = baseQuery;
40
+ }
41
+ if (filter.or && filter.or.length > 0) {
42
+ query.$or = filter.or.map(buildSimpleQuery);
43
+ }
44
+ if (filter.and && filter.and.length > 0) {
45
+ query.$and = filter.and.map(buildSimpleQuery);
46
+ }
47
+ }
48
+
49
+ let queryBuilder = getSubscriptionsCollection().find(query);
50
+ if (options?.sort) {
51
+ queryBuilder = queryBuilder.sort(options.sort);
52
+ }
53
+ if (options?.limit) {
54
+ queryBuilder = queryBuilder.limit(options.limit);
55
+ }
56
+
57
+ return await queryBuilder.toArray();
58
+ };
59
+
60
+ export const createSubscriptionDoc = async (
61
+ subscriptionData: Omit<Subscription, 'createdAt' | 'updatedAt'>
62
+ ): Promise<SubscriptionDoc> => {
63
+ const now = new Date();
64
+ const result = await getSubscriptionsCollection().insertOne({
65
+ ...subscriptionData,
66
+ createdAt: now,
67
+ updatedAt: now,
68
+ });
69
+
70
+ const subscriptions = await findSubscriptions({ _id: result.insertedId });
71
+ if (!subscriptions[0]) throw new Error('Failed to retrieve created subscription');
72
+ return subscriptions[0];
73
+ };
74
+
75
+ export const updateSubscriptionDoc = async (
76
+ subscriptionId: ObjectId,
77
+ updates: Partial<Subscription>
78
+ ): Promise<SubscriptionDoc | null> => {
79
+ return await getSubscriptionsCollection().findOneAndUpdate(
80
+ { _id: subscriptionId },
81
+ {
82
+ $set: {
83
+ ...updates,
84
+ updatedAt: new Date(),
85
+ },
86
+ },
87
+ {
88
+ returnDocument: 'after',
89
+ }
90
+ );
91
+ };
92
+
93
+ export const incrementSubscriptionUsage = async (
94
+ subscriptionId: ObjectId,
95
+ amount: number = 1
96
+ ): Promise<SubscriptionDoc | null> => {
97
+ const fieldToIncrement = 'cycles.$[activeCycle].usage.usedCalls';
98
+ return await getSubscriptionsCollection().findOneAndUpdate(
99
+ { _id: subscriptionId, 'cycles.isActive': true },
100
+ {
101
+ $inc: { [fieldToIncrement]: amount },
102
+ $set: { updatedAt: new Date() },
103
+ },
104
+ {
105
+ arrayFilters: [{ 'activeCycle.isActive': true }],
106
+ returnDocument: 'after',
107
+ }
108
+ );
109
+ };
110
+
111
+ export const deactivateCurrentSubscriptionCycle = async (
112
+ subscriptionId: ObjectId,
113
+ nextRenewalDate: Date
114
+ ): Promise<void> => {
115
+ await getSubscriptionsCollection().updateOne(
116
+ { _id: subscriptionId, 'cycles.isActive': true },
117
+ {
118
+ $set: {
119
+ 'cycles.$[activeCycle].isActive': false,
120
+ nextRenewalDate: nextRenewalDate,
121
+ updatedAt: new Date(),
122
+ },
123
+ },
124
+ {
125
+ arrayFilters: [{ 'activeCycle.isActive': true }],
126
+ }
127
+ );
128
+ };
129
+
130
+ export const pushNewSubscriptionCycle = async (
131
+ subscriptionId: ObjectId,
132
+ newCycle: Subscription['cycles'][number]
133
+ ): Promise<SubscriptionDoc | null> => {
134
+ const result = await getSubscriptionsCollection().findOneAndUpdate(
135
+ { _id: subscriptionId },
136
+ {
137
+ $push: {
138
+ cycles: newCycle,
139
+ },
140
+ },
141
+ {
142
+ returnDocument: 'after',
143
+ }
144
+ );
145
+
146
+ return result;
147
+ };
@@ -0,0 +1,26 @@
1
+ import { ObjectId, Filter } from 'mongodb';
2
+ import { Subscription, SimpleSubscriptionFilter, simpleFields, SimpleField } from '../index';
3
+
4
+ export const buildSimpleQuery = (filter: SimpleSubscriptionFilter): Filter<Subscription> => {
5
+ const query: Filter<Subscription> = {};
6
+
7
+ if (filter._id) {
8
+ query._id = new ObjectId(filter._id);
9
+ }
10
+
11
+ simpleFields.forEach((field: SimpleField) => {
12
+ const val = filter[field];
13
+ if (val !== undefined) {
14
+ query[field] = val as any;
15
+ }
16
+ });
17
+
18
+ return query;
19
+ };
20
+
21
+ export const buildDateRangeFilter = (from: Date, to: Date) => {
22
+ return {
23
+ $gte: from,
24
+ $lt: to,
25
+ };
26
+ };
@@ -0,0 +1,65 @@
1
+ import { Filter, ObjectId, WithId, Sort } from 'mongodb';
2
+ import { PlanDoc } from '../plans/plans.types';
3
+
4
+ export type SubscriptionCycle = {
5
+ usage: {
6
+ usedCalls: number;
7
+ };
8
+ startDate: Date;
9
+ endDate: Date;
10
+ cycleNumber: number;
11
+ isActive: boolean;
12
+ };
13
+
14
+ export type Subscription = {
15
+ clientId: string;
16
+ planId: string;
17
+ planSnapshot: PlanDoc;
18
+ startDate: Date;
19
+ endDate?: Date; // optional, if we want to predefine the end date
20
+ isActive: boolean;
21
+ createdAt: Date;
22
+ updatedAt: Date;
23
+ nextRenewalDate: Date;
24
+ cycles: SubscriptionCycle[];
25
+ };
26
+
27
+ export type SubscriptionDoc = WithId<Subscription>;
28
+ export type Subscriptions = SubscriptionDoc[];
29
+
30
+ // Define which Subscription fields we allow in a simple filter
31
+ export const simpleFields = [
32
+ 'clientId',
33
+ 'planId',
34
+ 'startDate',
35
+ 'endDate',
36
+ 'isActive',
37
+ 'createdAt',
38
+ 'nextRenewalDate',
39
+ ] as const;
40
+ export type SimpleField = (typeof simpleFields)[number];
41
+
42
+ /**
43
+ * A simple filter for Subscription: each field may be a literal or a MongoDB operator
44
+ */
45
+ export type SimpleSubscriptionFilter = {
46
+ _id?: ObjectId | string;
47
+ } & {
48
+ [K in keyof Subscription]?: Filter<Subscription>[K];
49
+ };
50
+
51
+ /**
52
+ * Composite filter allowing nested OR/AND arrays
53
+ */
54
+ export type SubscriptionFilter = SimpleSubscriptionFilter & {
55
+ or?: SimpleSubscriptionFilter[];
56
+ and?: SimpleSubscriptionFilter[];
57
+ };
58
+
59
+ /**
60
+ * Query options for sorting and limiting results
61
+ */
62
+ export type SubscriptionQueryOptions = {
63
+ sort?: Sort;
64
+ limit?: number;
65
+ };
@@ -0,0 +1,24 @@
1
+ import { MongoMemoryServer } from 'mongodb-memory-server';
2
+ import { MongoClient, Db } from 'mongodb';
3
+ import { setDb, setMunicipalDataDb } from '../index';
4
+
5
+ let mongoServer: MongoMemoryServer;
6
+ let client: MongoClient;
7
+
8
+ export async function initTestDb(): Promise<Db> {
9
+ mongoServer = await MongoMemoryServer.create({
10
+ instance: { ip: '127.0.0.1' },
11
+ });
12
+ client = new MongoClient(mongoServer.getUri());
13
+ await client.connect();
14
+ const talkpilotDb = client.db();
15
+ const municipalDb = client.db('municipal-data');
16
+ setDb(talkpilotDb);
17
+ setMunicipalDataDb(municipalDb);
18
+ return talkpilotDb;
19
+ }
20
+
21
+ export async function closeTestDb(): Promise<void> {
22
+ if (client) await client.close();
23
+ if (mongoServer) await mongoServer.stop();
24
+ }
@@ -0,0 +1,12 @@
1
+ export * from './talkpilot/agents';
2
+ export * from './talkpilot/calls';
3
+ export * from './talkpilot/clientAudioBuffers';
4
+ export * from './talkpilot/clientsConfig';
5
+ export * from './talkpilot/flows';
6
+ export * from './talkpilot/groups';
7
+ export * from './talkpilot/phone_numbers';
8
+ export * from './talkpilot/sessions';
9
+ export * from './municipal/cities';
10
+ export * from './municipal/departmentsSubjects';
11
+ export * from './municipal/streets';
12
+ export * from './municipal/tickets';
@@ -0,0 +1,16 @@
1
+ import { Factory } from 'fishery';
2
+ import { ObjectId } from 'mongodb';
3
+ import { faker } from '@faker-js/faker';
4
+ import type { City } from '../../../municipal/cities/cities.types';
5
+
6
+ export const cityFactory = Factory.define<City>(() => ({
7
+ _id: new ObjectId(),
8
+ name: faker.location.city().toLowerCase().replace(/\s/g, '_'),
9
+ displayName: faker.location.city(),
10
+ createdAt: new Date(),
11
+ updatedAt: new Date(),
12
+ }));
13
+
14
+ export function createCity(overrides?: Partial<City>): City {
15
+ return cityFactory.build(overrides);
16
+ }
@@ -0,0 +1,29 @@
1
+ import { Factory } from 'fishery';
2
+ import { ObjectId } from 'mongodb';
3
+ import { faker } from '@faker-js/faker';
4
+ import type {
5
+ DepartmentSubject,
6
+ CityName,
7
+ } from '../../../municipal/departmentsSubjects/departmentsSubjects.types';
8
+
9
+ const VALID_CITIES: CityName[] = ['ashdod', 'maltar', 'billit', 'hashkelon', 'tests'];
10
+
11
+ export const departmentSubjectFactory = Factory.define<DepartmentSubject>(() => ({
12
+ _id: new ObjectId(),
13
+ createdAt: new Date(),
14
+ updatedAt: new Date(),
15
+ subjectName: faker.lorem.words(2),
16
+ subject_id: faker.string.numeric(4),
17
+ sub_subject_name: faker.lorem.word(),
18
+ sub_subject_id: faker.string.numeric(4),
19
+ descriptions: [faker.lorem.sentence()],
20
+ cityName: faker.helpers.arrayElement(VALID_CITIES),
21
+ guidelines: null,
22
+ instructions: [],
23
+ communications: [],
24
+ sla: faker.number.int({ min: 24, max: 720 }),
25
+ }));
26
+
27
+ export function createDepartmentSubject(overrides?: Partial<DepartmentSubject>): DepartmentSubject {
28
+ return departmentSubjectFactory.build(overrides);
29
+ }