@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,83 @@
1
+ import { ObjectId } from 'mongodb';
2
+ import { faker } from '@faker-js/faker';
3
+ import {
4
+ getGroupsCollection,
5
+ findGroups,
6
+ createGroup,
7
+ updateGroup,
8
+ removeGroup,
9
+ } from '../groups.getters';
10
+ import { createGroup as createGroupDoc } from '../../../test-utils/factories';
11
+
12
+ describe('db.groups', () => {
13
+ describe('getGroupsCollection', () => {
14
+ it('returns the "groups" collection', () => {
15
+ expect(getGroupsCollection().collectionName).toBe('groups');
16
+ });
17
+ });
18
+
19
+ describe('findGroups', () => {
20
+ it('returns groups matching the provided filter', async () => {
21
+ const clientId = faker.string.uuid();
22
+ const g1 = createGroupDoc({ clientId, name: 'Group A' });
23
+ const g2 = createGroupDoc({ clientId, name: 'Group B' });
24
+ const gOther = createGroupDoc({ clientId: faker.string.uuid(), name: 'Group C' });
25
+
26
+ await getGroupsCollection().insertMany([g1, g2, gOther]);
27
+
28
+ const result = await findGroups({ clientId });
29
+ const names = result.map((g) => g.name).sort();
30
+
31
+ expect(result).toHaveLength(2);
32
+ expect(names).toEqual(['Group A', 'Group B']);
33
+ });
34
+ });
35
+
36
+ describe('createGroup', () => {
37
+ it('inserts a group and returns insertedId', async () => {
38
+ const toInsert = createGroupDoc({ name: 'New Group' });
39
+
40
+ const insertedId = await createGroup(toInsert);
41
+ expect(insertedId).toBeInstanceOf(ObjectId);
42
+
43
+ const fromDb = await getGroupsCollection().findOne({ _id: insertedId });
44
+ expect(fromDb?.name).toBe('New Group');
45
+ expect(fromDb?.clientId).toBe(toInsert.clientId);
46
+ });
47
+ });
48
+
49
+ describe('updateGroup', () => {
50
+ it('updates the group and sets updatedAt, returning the updated document', async () => {
51
+ const original = createGroupDoc({ name: 'Before', description: 'Old desc' });
52
+ const { insertedId } = await getGroupsCollection().insertOne(original);
53
+ const before = await getGroupsCollection().findOne({ _id: insertedId });
54
+
55
+ // Add a small delay to ensure updatedAt will be different
56
+ await new Promise((resolve) => setTimeout(resolve, 10));
57
+
58
+ const res = await updateGroup(
59
+ { _id: insertedId },
60
+ { name: 'After', description: 'New desc' }
61
+ );
62
+
63
+ expect(res?._id).toEqual(insertedId);
64
+ expect(res?.name).toBe('After');
65
+ expect(res?.description).toBe('New desc');
66
+ expect(new Date(res!.updatedAt).getTime()).toBeGreaterThan(
67
+ new Date(before!.updatedAt).getTime()
68
+ );
69
+ });
70
+ });
71
+
72
+ describe('removeGroup', () => {
73
+ it('removes a group by string id', async () => {
74
+ const { insertedId } = await getGroupsCollection().insertOne(createGroupDoc());
75
+
76
+ const result = await removeGroup(insertedId.toHexString());
77
+ expect(result.deletedCount).toBe(1);
78
+
79
+ const check = await getGroupsCollection().findOne({ _id: insertedId });
80
+ expect(check).toBeNull();
81
+ });
82
+ });
83
+ });
@@ -0,0 +1,32 @@
1
+ import { enrichPhoneNumber, isValidE164 } from '../phone.utils';
2
+
3
+ describe('Phone Utilities', () => {
4
+ describe('enrichPhoneNumber', () => {
5
+ it('should enrich Israeli number', () => {
6
+ const result = enrichPhoneNumber('+972507725874');
7
+ expect(result?.e164).toBe('+972507725874');
8
+ expect(result?.region).toBeDefined();
9
+ expect(result?.countryCallingCode).toBe('972');
10
+ });
11
+
12
+ it('should enrich US number', () => {
13
+ const result = enrichPhoneNumber('+12025551234');
14
+ expect(result?.region).toBe('US');
15
+ expect(result?.countryCallingCode).toBe('1');
16
+ });
17
+
18
+ it('should return null for invalid', () => {
19
+ expect(enrichPhoneNumber('invalid')).toBeNull();
20
+ expect(enrichPhoneNumber('0507725874')).toBeNull();
21
+ });
22
+ });
23
+
24
+ describe('isValidE164', () => {
25
+ it('should validate E.164 format', () => {
26
+ expect(isValidE164('+972508832983')).toBe(true);
27
+ expect(isValidE164('+12025551234')).toBe(true);
28
+ expect(isValidE164('0501234567')).toBe(false);
29
+ expect(isValidE164('invalid')).toBe(false);
30
+ });
31
+ });
32
+ });
@@ -0,0 +1,27 @@
1
+ import { getDb, ObjectId, Group } from '../index';
2
+ import { Collection, Filter } from 'mongodb';
3
+
4
+ export const getGroupsCollection = (): Collection<Group> => {
5
+ return getDb().collection<Group>('groups');
6
+ };
7
+
8
+ export const findGroups = async (filter: Filter<Group>): Promise<Group[]> => {
9
+ return await getGroupsCollection().find(filter).toArray();
10
+ };
11
+
12
+ export const createGroup = async (group: Group) => {
13
+ const { insertedId } = await getGroupsCollection().insertOne(group);
14
+ return insertedId;
15
+ };
16
+
17
+ export const updateGroup = async (filter: Filter<Group>, data: Partial<Group>) => {
18
+ return await getGroupsCollection().findOneAndUpdate(
19
+ filter,
20
+ { $set: { ...data, updatedAt: new Date() } },
21
+ { returnDocument: 'after' }
22
+ );
23
+ };
24
+
25
+ export const removeGroup = async (groupId: string) => {
26
+ return await getGroupsCollection().deleteOne({ _id: new ObjectId(groupId) });
27
+ };
@@ -0,0 +1,29 @@
1
+ import { WithId } from 'mongodb';
2
+ export type E164 = `+${string}`;
3
+ export type CountryISO = Uppercase<string>;
4
+
5
+ export type Phone = {
6
+ e164: E164;
7
+ region?: CountryISO;
8
+ countryCallingCode?: string;
9
+ national?: string;
10
+ ext?: string;
11
+ formatted?: string;
12
+ };
13
+
14
+ export type GroupContact = {
15
+ name: string;
16
+ phone: Phone;
17
+ gender: string;
18
+ };
19
+
20
+ export type Group = {
21
+ clientId: string;
22
+ createdAt: Date;
23
+ updatedAt: Date;
24
+ name: string;
25
+ description: string;
26
+ contacts: GroupContact[];
27
+ };
28
+
29
+ export type GroupDoc = WithId<Group>;
@@ -0,0 +1,2 @@
1
+ export * from './groups.getters';
2
+ export * from './groups.types';
@@ -0,0 +1,40 @@
1
+ import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber';
2
+ import type { Phone, E164, CountryISO } from './groups.types';
3
+
4
+ const phoneUtil = PhoneNumberUtil.getInstance();
5
+
6
+ export const enrichPhoneNumber = (e164: string): Phone | null => {
7
+ try {
8
+ const phoneNumber = phoneUtil.parse(e164, 'ZZ');
9
+ if (!phoneUtil.isValidNumber(phoneNumber)) {
10
+ return null;
11
+ }
12
+
13
+ const normalizedE164 = phoneUtil.format(phoneNumber, PhoneNumberFormat.E164) as E164;
14
+ const region = phoneUtil.getRegionCodeForNumber(phoneNumber) as CountryISO;
15
+ const countryCallingCode = phoneNumber.getCountryCode()?.toString();
16
+ const national = phoneNumber.getNationalNumber()?.toString();
17
+ const ext = phoneNumber.getExtension() || undefined;
18
+ const formatted = phoneUtil.format(phoneNumber, PhoneNumberFormat.INTERNATIONAL);
19
+
20
+ return {
21
+ e164: normalizedE164,
22
+ region,
23
+ countryCallingCode,
24
+ national,
25
+ ext,
26
+ formatted,
27
+ };
28
+ } catch (error) {
29
+ return null;
30
+ }
31
+ };
32
+
33
+ export const isValidE164 = (e164: string): boolean => {
34
+ try {
35
+ const phoneNumber = phoneUtil.parse(e164, 'ZZ');
36
+ return phoneUtil.isValidNumber(phoneNumber);
37
+ } catch (error) {
38
+ return false;
39
+ }
40
+ };
@@ -0,0 +1,27 @@
1
+ import { Db, ObjectId as MongoObjectId } from 'mongodb';
2
+
3
+ export * from './flows';
4
+ export * from './results';
5
+ export * from './sessions';
6
+ export * from './phone_numbers';
7
+ export * from './leads';
8
+ export * from './clients';
9
+ export * from './agents';
10
+ export * from './plans';
11
+ export * from './subscriptions';
12
+ export * from './groups';
13
+ export * from './calls';
14
+ export * from './clientAudioBuffers/clientAudioBuffer.getters';
15
+ export * from './clientsConfig';
16
+ export { mongodbClient } from './mongodb-client';
17
+
18
+ let db: Db;
19
+ export const setDb = (d: Db) => {
20
+ db = d;
21
+ };
22
+ export const getDb = (): Db => {
23
+ if (!db) throw new Error('DB not initialised');
24
+ return db;
25
+ };
26
+
27
+ export const ObjectId = MongoObjectId;
@@ -0,0 +1,2 @@
1
+ export { getLeadsCollection } from './leads.getter';
2
+ export type { Lead, LeadItem, LeadProperty } from './leads.types';
@@ -0,0 +1,5 @@
1
+ import type { Collection } from 'mongodb';
2
+ import { getDb } from '../index';
3
+ import type { Lead } from './leads.types';
4
+
5
+ export const getLeadsCollection = (): Collection<Lead> => getDb().collection<Lead>('leads');
@@ -0,0 +1,33 @@
1
+ export const leadsMongoSchema = {
2
+ bsonType: 'object',
3
+ required: ['flowId', 'clientId', 'createdAt', 'leads'],
4
+ properties: {
5
+ _id: { bsonType: 'objectId' },
6
+ flowId: { bsonType: 'string' },
7
+ clientId: { bsonType: 'string' },
8
+ createdAt: { bsonType: 'date' },
9
+ leads: {
10
+ bsonType: 'array',
11
+ items: {
12
+ bsonType: 'object',
13
+ required: ['callSid', 'timestamp'],
14
+ properties: {
15
+ callSid: { bsonType: 'string' },
16
+ timestamp: { bsonType: 'date' },
17
+ properties: {
18
+ bsonType: 'array',
19
+ items: {
20
+ bsonType: 'object',
21
+ required: ['name', 'value'],
22
+ properties: {
23
+ name: { bsonType: 'string' },
24
+ value: { bsonType: 'string' },
25
+ },
26
+ },
27
+ },
28
+ },
29
+ },
30
+ },
31
+ },
32
+ additionalProperties: false,
33
+ } as const;
@@ -0,0 +1,20 @@
1
+ import { ObjectId } from 'mongodb';
2
+
3
+ export interface LeadProperty {
4
+ name: string;
5
+ value: string;
6
+ }
7
+
8
+ export interface LeadItem {
9
+ callSid: string;
10
+ timestamp: Date;
11
+ properties?: LeadProperty[];
12
+ }
13
+
14
+ export interface Lead {
15
+ _id?: ObjectId;
16
+ flowId: ObjectId;
17
+ clientId: string;
18
+ createdAt: Date;
19
+ leads: LeadItem[];
20
+ }
@@ -0,0 +1,78 @@
1
+ import { MongoClient, Db } from 'mongodb';
2
+ import { setDb } from './index';
3
+ import { validateConfig, validateMongoUri } from '../utils/validation';
4
+
5
+ class MongoDBClient {
6
+ private client: MongoClient | null = null;
7
+ private db: Db | null = null;
8
+
9
+ private extractDatabaseName(uri: string): string | null {
10
+ try {
11
+ const url = new URL(uri);
12
+ const dbName = url.pathname.slice(1);
13
+ return dbName || null;
14
+ } catch {
15
+ return null;
16
+ }
17
+ }
18
+
19
+ async connect(uri?: string, databaseName?: string): Promise<void> {
20
+ if (this.client) {
21
+ return;
22
+ }
23
+
24
+ const mongodbUri = uri || process.env.MONGO_URI || process.env.MONGODB_URI;
25
+ validateConfig('MONGO_URI', mongodbUri);
26
+ validateMongoUri(mongodbUri!);
27
+
28
+ try {
29
+ this.client = new MongoClient(mongodbUri!);
30
+ await this.client.connect();
31
+
32
+ const dbNameFromUri = this.extractDatabaseName(mongodbUri!);
33
+ const dbNameFromEnv = process.env.MONGODB_DB_NAME;
34
+ const dbName = databaseName || dbNameFromUri || dbNameFromEnv;
35
+
36
+ if (!dbName) {
37
+ throw new Error(
38
+ '[core-db] Database name not specified. Please pass databaseName to connect(), ' +
39
+ 'add it to the URI, or set MONGODB_DB_NAME.'
40
+ );
41
+ }
42
+
43
+ this.db = this.client.db(dbName);
44
+ setDb(this.db);
45
+ console.info(`[core-db] TalkPilot MongoDB connected: ${dbName}`);
46
+ } catch (error) {
47
+ console.error('[core-db] TalkPilot connection failed', error as Error);
48
+ throw error;
49
+ }
50
+ }
51
+
52
+ async disconnect(): Promise<void> {
53
+ if (this.client) {
54
+ try {
55
+ await this.client.close();
56
+ this.client = null;
57
+ this.db = null;
58
+ console.info('MongoDB disconnected successfully');
59
+ } catch (error) {
60
+ console.error('Error disconnecting from MongoDB', error as Error);
61
+ throw error;
62
+ }
63
+ }
64
+ }
65
+
66
+ getDb(): Db {
67
+ if (!this.db) {
68
+ throw new Error('Database not initialized. Call connect() first.');
69
+ }
70
+ return this.db;
71
+ }
72
+
73
+ isConnected(): boolean {
74
+ return this.client !== null && this.client !== undefined;
75
+ }
76
+ }
77
+
78
+ export const mongodbClient = new MongoDBClient();
@@ -0,0 +1,43 @@
1
+ import {
2
+ getPhoneDataByPhoneNumber,
3
+ getPhoneNumbersCollection,
4
+ getClientPhoneNumber,
5
+ } from '../phone_numbers.getter';
6
+ import { getFlowsCollection } from '../../flows/flows.getter';
7
+ import { createFlow, createPhoneNumber } from '../../../test-utils/factories';
8
+
9
+ describe('db.phoneNumbers', () => {
10
+ describe('getPhoneDataByPhoneNumber', () => {
11
+ it('return phone number data with flow', async () => {
12
+ const flow = createFlow({ clientId: 'test-client-id' });
13
+ const phoneData = createPhoneNumber({ flow_id: flow._id, client_id: 'test-client-id' });
14
+
15
+ await getFlowsCollection().insertOne(flow);
16
+ await getPhoneNumbersCollection().insertOne(phoneData);
17
+
18
+ const result = await getPhoneDataByPhoneNumber(phoneData.phone_number);
19
+
20
+ expect(result).toBeDefined();
21
+ expect(result).toMatchObject({
22
+ client_id: phoneData.client_id,
23
+ phone_number: phoneData.phone_number,
24
+ flow_id: flow._id,
25
+ });
26
+ // Specifically check if the flow was joined correctly
27
+ expect(result?.flow.flowName).toBe(flow.flowName);
28
+ });
29
+ });
30
+
31
+ describe('getClientPhoneNumber', () => {
32
+ it('return phone number by client id', async () => {
33
+ const flow = createFlow();
34
+ const phoneData = createPhoneNumber({ flow_id: flow._id });
35
+
36
+ await getFlowsCollection().insertOne(flow);
37
+ await getPhoneNumbersCollection().insertOne(phoneData);
38
+
39
+ const result = await getClientPhoneNumber(phoneData.client_id);
40
+ expect(result).toBe(phoneData.phone_number);
41
+ });
42
+ });
43
+ });
@@ -0,0 +1,2 @@
1
+ export * from './phone_numbers.getter';
2
+ export type { PhoneNumber } from './phone_numbers.types';
@@ -0,0 +1,47 @@
1
+ import type { Collection } from 'mongodb';
2
+ import { getDb, ObjectId } from '../index';
3
+ import { findFlowById } from '../flows/flows.getter';
4
+ import type { PhoneNumber, PhoneNumberWithFlow } from './phone_numbers.types';
5
+
6
+ export const getPhoneNumbersCollection = (): Collection<PhoneNumber> =>
7
+ getDb().collection<PhoneNumber>('phone_numbers');
8
+
9
+ export const getPhoneDataByPhoneNumber = async (
10
+ phoneNumber: string
11
+ ): Promise<PhoneNumberWithFlow | null> => {
12
+ const phoneData = await getPhoneNumbersCollection().findOne({ phone_number: phoneNumber });
13
+ if (!phoneData) return null;
14
+ const flow = await findFlowById(phoneData.flow_id, phoneData.client_id);
15
+ if (!flow) return null;
16
+ return { ...phoneData, flow };
17
+ };
18
+
19
+ export const getClientPhoneNumber = async (clientId: string): Promise<string | null> => {
20
+ return (await getPhoneNumbersCollection().findOne({ client_id: clientId }))?.phone_number ?? null;
21
+ };
22
+
23
+ export const getClientPhoneData = async (clientId: string): Promise<PhoneNumber | null> => {
24
+ const phoneNumberData = await getPhoneNumbersCollection().findOne({
25
+ client_id: clientId,
26
+ });
27
+ return phoneNumberData;
28
+ };
29
+
30
+ export const createPhoneNumberEntity = async (
31
+ phoneNumber: string,
32
+ flowId: string,
33
+ clientId: string,
34
+ isPrimary = true,
35
+ ): Promise<PhoneNumber> => {
36
+ await getPhoneNumbersCollection().insertOne({
37
+ phone_number: phoneNumber,
38
+ flow_id: new ObjectId(flowId),
39
+ client_id: clientId,
40
+ is_primary: isPrimary,
41
+ createdAt: new Date(),
42
+ updatedAt: new Date(),
43
+ });
44
+ const phoneNumberData = await getClientPhoneData(clientId);
45
+ if (!phoneNumberData) throw new Error('Failed to create phoneNumber');
46
+ return phoneNumberData;
47
+ };
@@ -0,0 +1,14 @@
1
+ export const phoneNumbersMongoSchema = {
2
+ bsonType: 'object',
3
+ required: ['client_id', 'flow_id', 'phone_number'],
4
+ properties: {
5
+ _id: { bsonType: 'objectId' },
6
+ client_id: { bsonType: 'string' },
7
+ flow_id: { bsonType: 'string' },
8
+ phone_number: { bsonType: 'string' },
9
+ is_primary: { bsonType: 'bool' },
10
+ createdAt: { bsonType: 'date' },
11
+ updatedAt: { bsonType: 'date' },
12
+ },
13
+ additionalProperties: false,
14
+ } as const;
@@ -0,0 +1,15 @@
1
+ import { ObjectId, WithId } from "mongodb";
2
+ import { Flow } from "../flows/flows.types";
3
+
4
+ export type PhoneNumber = {
5
+ _id?: ObjectId;
6
+ client_id: string;
7
+ flow_id: ObjectId;
8
+ phone_number: string;
9
+ subscriptionId?: string;
10
+ is_primary: boolean;
11
+ createdAt: Date;
12
+ updatedAt: Date;
13
+ };
14
+
15
+ export type PhoneNumberWithFlow = WithId<PhoneNumber> & { flow: Flow };
@@ -0,0 +1,2 @@
1
+ export * from './plans.types';
2
+ export * from './plans.getters';
@@ -0,0 +1,121 @@
1
+ import { Plan, getDb, PlanDoc, PlanFilter, SimplePlanFilter, PlanQueryOptions } from '../index';
2
+ import { ObjectId, Filter } from 'mongodb';
3
+
4
+ export const getPlansCollection = () => {
5
+ return getDb().collection<Plan>('plans');
6
+ };
7
+
8
+ /**
9
+ * Converts a simple filter to MongoDB query format
10
+ */
11
+ const buildSimpleQuery = (filter: SimplePlanFilter): Filter<Plan> => {
12
+ const query: Filter<Plan> = {};
13
+
14
+ if (filter._id) {
15
+ query._id = new ObjectId(filter._id);
16
+ }
17
+ if (filter.productKey !== undefined) {
18
+ query.productKey = filter.productKey;
19
+ }
20
+ if (filter.isActive !== undefined) {
21
+ query.isActive = filter.isActive;
22
+ }
23
+ if (filter.currency !== undefined) {
24
+ query.currency = filter.currency;
25
+ }
26
+ if (filter.region !== undefined) {
27
+ query.region = filter.region;
28
+ }
29
+ if (filter.countryCode !== undefined) {
30
+ query.countryCode = filter.countryCode;
31
+ }
32
+ if (filter.originalPlanId !== undefined) {
33
+ query.originalPlanId = filter.originalPlanId;
34
+ }
35
+
36
+ return query;
37
+ };
38
+
39
+ /**
40
+ * Generic function to find plans with flexible filtering
41
+ */
42
+ export const findPlans = async (
43
+ filter?: PlanFilter,
44
+ options?: PlanQueryOptions
45
+ ): Promise<PlanDoc[]> => {
46
+ let query: Filter<Plan> = {};
47
+
48
+ if (filter) {
49
+ // Build the base query from simple filter properties
50
+ const simpleFilter: SimplePlanFilter = {
51
+ _id: filter._id,
52
+ productKey: filter.productKey,
53
+ isActive: filter.isActive,
54
+ currency: filter.currency,
55
+ region: filter.region,
56
+ countryCode: filter.countryCode,
57
+ originalPlanId: filter.originalPlanId,
58
+ };
59
+
60
+ // Apply simple filters if any are present
61
+ const baseQuery = buildSimpleQuery(simpleFilter);
62
+ if (Object.keys(baseQuery).length > 0) {
63
+ query = baseQuery;
64
+ }
65
+
66
+ // Add OR conditions if present
67
+ if (filter.or && filter.or.length > 0) {
68
+ query.$or = filter.or.map(buildSimpleQuery);
69
+ }
70
+
71
+ // Add AND conditions if present
72
+ if (filter.and && filter.and.length > 0) {
73
+ query.$and = filter.and.map(buildSimpleQuery);
74
+ }
75
+ }
76
+
77
+ let queryBuilder = getPlansCollection().find(query);
78
+
79
+ if (options?.sort) {
80
+ queryBuilder = queryBuilder.sort(options.sort);
81
+ }
82
+ if (options?.limit) {
83
+ queryBuilder = queryBuilder.limit(options.limit);
84
+ }
85
+
86
+ return await queryBuilder.toArray();
87
+ };
88
+
89
+ export const createPlanDoc = async (
90
+ planData: Omit<Plan, 'createdAt' | 'updatedAt'>
91
+ ): Promise<PlanDoc> => {
92
+ const now = new Date();
93
+ const result = await getPlansCollection().insertOne({
94
+ ...planData,
95
+ createdAt: now,
96
+ updatedAt: now,
97
+ });
98
+
99
+ const plans = await findPlans({ _id: result.insertedId });
100
+ if (!plans[0]) throw new Error('Failed to retrieve created plan');
101
+ return plans[0];
102
+ };
103
+
104
+ export const updatePlanDoc = async (
105
+ planId: ObjectId,
106
+ updates: Partial<Pick<Plan, 'isActive'>>
107
+ ): Promise<PlanDoc | null> => {
108
+ const result = await getPlansCollection().findOneAndUpdate(
109
+ { _id: planId },
110
+ {
111
+ $set: {
112
+ ...updates,
113
+ updatedAt: new Date(),
114
+ },
115
+ },
116
+ {
117
+ returnDocument: 'after',
118
+ }
119
+ );
120
+ return result;
121
+ };