@aglyn/plugins-crm 1.0.0-beta.221 → 1.0.0-beta.222

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 (514) hide show
  1. package/README.md +2 -1
  2. package/package.json +13 -13
  3. package/src/lib/components/activity-list.js +12 -5
  4. package/src/lib/components/activity-list.js.map +1 -1
  5. package/src/lib/components/companies-bulk-bar.d.ts +1 -4
  6. package/src/lib/components/companies-bulk-bar.js +7 -20
  7. package/src/lib/components/companies-bulk-bar.js.map +1 -1
  8. package/src/lib/components/companies-section.d.ts +3 -3
  9. package/src/lib/components/companies-section.js +149 -46
  10. package/src/lib/components/companies-section.js.map +1 -1
  11. package/src/lib/components/company-edit-drawer.js +281 -20
  12. package/src/lib/components/company-edit-drawer.js.map +1 -1
  13. package/src/lib/components/company-properties-card.js +99 -8
  14. package/src/lib/components/company-properties-card.js.map +1 -1
  15. package/src/lib/components/contact-detail-page.js +3 -1
  16. package/src/lib/components/contact-detail-page.js.map +1 -1
  17. package/src/lib/components/contact-merge-dialog.js +33 -10
  18. package/src/lib/components/contact-merge-dialog.js.map +1 -1
  19. package/src/lib/components/contact-properties-card.d.ts +0 -69
  20. package/src/lib/components/contact-properties-card.js +350 -60
  21. package/src/lib/components/contact-properties-card.js.map +1 -1
  22. package/src/lib/components/contact-reports-to-field.d.ts +46 -0
  23. package/src/lib/components/contact-reports-to-field.js +67 -0
  24. package/src/lib/components/contact-reports-to-field.js.map +1 -0
  25. package/src/lib/components/contacts-bulk-bar.d.ts +1 -8
  26. package/src/lib/components/contacts-bulk-bar.js +7 -19
  27. package/src/lib/components/contacts-bulk-bar.js.map +1 -1
  28. package/src/lib/components/contacts-section.js +46 -33
  29. package/src/lib/components/contacts-section.js.map +1 -1
  30. package/src/lib/components/crm-call-actions.d.ts +10 -0
  31. package/src/lib/components/crm-call-actions.js +26 -9
  32. package/src/lib/components/crm-call-actions.js.map +1 -1
  33. package/src/lib/components/crm-record-picker.d.ts +15 -0
  34. package/src/lib/components/crm-record-picker.js +16 -10
  35. package/src/lib/components/crm-record-picker.js.map +1 -1
  36. package/src/lib/components/crm-transfer-buttons.d.ts +54 -0
  37. package/src/lib/components/crm-transfer-buttons.js +167 -0
  38. package/src/lib/components/crm-transfer-buttons.js.map +1 -0
  39. package/src/lib/components/deal-contact-roles-card.d.ts +46 -0
  40. package/src/lib/components/deal-contact-roles-card.js +368 -0
  41. package/src/lib/components/deal-contact-roles-card.js.map +1 -0
  42. package/src/lib/components/deal-detail-page.d.ts +3 -2
  43. package/src/lib/components/deal-detail-page.js +25 -6
  44. package/src/lib/components/deal-detail-page.js.map +1 -1
  45. package/src/lib/components/deal-edit-drawer.d.ts +10 -0
  46. package/src/lib/components/deal-edit-drawer.js +219 -10
  47. package/src/lib/components/deal-edit-drawer.js.map +1 -1
  48. package/src/lib/components/deal-properties-card.d.ts +7 -0
  49. package/src/lib/components/deal-properties-card.js +34 -3
  50. package/src/lib/components/deal-properties-card.js.map +1 -1
  51. package/src/lib/components/deal-stage-card.d.ts +4 -0
  52. package/src/lib/components/deal-stage-card.js +10 -2
  53. package/src/lib/components/deal-stage-card.js.map +1 -1
  54. package/src/lib/components/deals-bulk-bar.d.ts +0 -3
  55. package/src/lib/components/deals-bulk-bar.js +7 -20
  56. package/src/lib/components/deals-bulk-bar.js.map +1 -1
  57. package/src/lib/components/deals-section.d.ts +3 -3
  58. package/src/lib/components/deals-section.js +123 -63
  59. package/src/lib/components/deals-section.js.map +1 -1
  60. package/src/lib/components/fields-section.js +31 -17
  61. package/src/lib/components/fields-section.js.map +1 -1
  62. package/src/lib/components/lead-convert-dialog.d.ts +4 -0
  63. package/src/lib/components/lead-convert-dialog.js +23 -1
  64. package/src/lib/components/lead-convert-dialog.js.map +1 -1
  65. package/src/lib/components/lead-detail-page.js +8 -2
  66. package/src/lib/components/lead-detail-page.js.map +1 -1
  67. package/src/lib/components/lead-profile-fields.d.ts +71 -0
  68. package/src/lib/components/lead-profile-fields.js +386 -0
  69. package/src/lib/components/lead-profile-fields.js.map +1 -0
  70. package/src/lib/components/lead-properties-card.d.ts +2 -1
  71. package/src/lib/components/lead-properties-card.js +85 -176
  72. package/src/lib/components/lead-properties-card.js.map +1 -1
  73. package/src/lib/components/lead-source-select.d.ts +4 -23
  74. package/src/lib/components/lead-source-select.js +9 -73
  75. package/src/lib/components/lead-source-select.js.map +1 -1
  76. package/src/lib/components/lead-status-chip.d.ts +9 -3
  77. package/src/lib/components/lead-status-chip.js +8 -4
  78. package/src/lib/components/lead-status-chip.js.map +1 -1
  79. package/src/lib/components/lead-status-options.d.ts +20 -0
  80. package/src/lib/components/lead-status-options.js +51 -0
  81. package/src/lib/components/lead-status-options.js.map +1 -0
  82. package/src/lib/components/lead-unqualify-dialog.d.ts +5 -0
  83. package/src/lib/components/lead-unqualify-dialog.js +50 -18
  84. package/src/lib/components/lead-unqualify-dialog.js.map +1 -1
  85. package/src/lib/components/leads-bulk-bar.d.ts +0 -3
  86. package/src/lib/components/leads-bulk-bar.js +29 -29
  87. package/src/lib/components/leads-bulk-bar.js.map +1 -1
  88. package/src/lib/components/leads-section.js +120 -68
  89. package/src/lib/components/leads-section.js.map +1 -1
  90. package/src/lib/components/linked-deals-card.d.ts +4 -0
  91. package/src/lib/components/linked-deals-card.js +47 -3
  92. package/src/lib/components/linked-deals-card.js.map +1 -1
  93. package/src/lib/components/log-activity-dialog.js +43 -3
  94. package/src/lib/components/log-activity-dialog.js.map +1 -1
  95. package/src/lib/components/new-contact-drawer.d.ts +8 -0
  96. package/src/lib/components/new-contact-drawer.js +108 -6
  97. package/src/lib/components/new-contact-drawer.js.map +1 -1
  98. package/src/lib/components/new-lead-drawer.d.ts +9 -2
  99. package/src/lib/components/new-lead-drawer.js +88 -121
  100. package/src/lib/components/new-lead-drawer.js.map +1 -1
  101. package/src/lib/components/picklist-select.d.ts +36 -0
  102. package/src/lib/components/picklist-select.js +114 -0
  103. package/src/lib/components/picklist-select.js.map +1 -0
  104. package/src/lib/components/picklist-values-card.d.ts +34 -0
  105. package/src/lib/components/{lead-source-values-card.js → picklist-values-card.js} +241 -58
  106. package/src/lib/components/picklist-values-card.js.map +1 -0
  107. package/src/lib/components/pipeline-stages-dialog.d.ts +7 -0
  108. package/src/lib/components/pipeline-stages-dialog.js +26 -4
  109. package/src/lib/components/pipeline-stages-dialog.js.map +1 -1
  110. package/src/lib/components/pipelines-dialog.js +15 -6
  111. package/src/lib/components/pipelines-dialog.js.map +1 -1
  112. package/src/lib/components/recent-activity-feed.js +20 -5
  113. package/src/lib/components/recent-activity-feed.js.map +1 -1
  114. package/src/lib/components/recipes-card.js +4 -2
  115. package/src/lib/components/recipes-card.js.map +1 -1
  116. package/src/lib/components/record-tasks-card.js +15 -8
  117. package/src/lib/components/record-tasks-card.js.map +1 -1
  118. package/src/lib/components/reports/forecast-card.d.ts +6 -0
  119. package/src/lib/components/reports/forecast-card.js +101 -3
  120. package/src/lib/components/reports/forecast-card.js.map +1 -1
  121. package/src/lib/components/reports/lead-funnel-card.d.ts +12 -0
  122. package/src/lib/components/reports/lead-funnel-card.js +9 -5
  123. package/src/lib/components/reports/lead-funnel-card.js.map +1 -1
  124. package/src/lib/components/reports/lead-source-card.d.ts +23 -0
  125. package/src/lib/components/reports/lead-source-card.js +318 -0
  126. package/src/lib/components/reports/lead-source-card.js.map +1 -0
  127. package/src/lib/components/reports/report-export.js +1 -1
  128. package/src/lib/components/reports/report-export.js.map +1 -1
  129. package/src/lib/components/reports-section.js +8 -0
  130. package/src/lib/components/reports-section.js.map +1 -1
  131. package/src/lib/components/sharing-rule-drawer.d.ts +5 -0
  132. package/src/lib/components/sharing-rule-drawer.js +108 -19
  133. package/src/lib/components/sharing-rule-drawer.js.map +1 -1
  134. package/src/lib/components/sharing-rules-card.js +11 -3
  135. package/src/lib/components/sharing-rules-card.js.map +1 -1
  136. package/src/lib/components/task-cells.d.ts +9 -1
  137. package/src/lib/components/task-cells.js +9 -4
  138. package/src/lib/components/task-cells.js.map +1 -1
  139. package/src/lib/components/task-edit-drawer.js +111 -43
  140. package/src/lib/components/task-edit-drawer.js.map +1 -1
  141. package/src/lib/components/tasks-bulk-bar.d.ts +0 -3
  142. package/src/lib/components/tasks-bulk-bar.js +7 -20
  143. package/src/lib/components/tasks-bulk-bar.js.map +1 -1
  144. package/src/lib/components/tasks-calendar.d.ts +3 -0
  145. package/src/lib/components/tasks-calendar.js +8 -1
  146. package/src/lib/components/tasks-calendar.js.map +1 -1
  147. package/src/lib/components/tasks-section.d.ts +3 -3
  148. package/src/lib/components/tasks-section.js +58 -43
  149. package/src/lib/components/tasks-section.js.map +1 -1
  150. package/src/lib/constants/api-routes.d.ts +2 -9
  151. package/src/lib/constants/api-routes.js +1 -7
  152. package/src/lib/constants/api-routes.js.map +1 -1
  153. package/src/lib/constants/bundle-common.d.ts +6 -0
  154. package/src/lib/constants/bundle-common.js +11 -0
  155. package/src/lib/constants/bundle-common.js.map +1 -1
  156. package/src/lib/constants/company-filters.d.ts +14 -0
  157. package/src/lib/constants/company-filters.js +51 -1
  158. package/src/lib/constants/company-filters.js.map +1 -1
  159. package/src/lib/constants/contact-filters.d.ts +2 -0
  160. package/src/lib/constants/contact-filters.js +18 -1
  161. package/src/lib/constants/contact-filters.js.map +1 -1
  162. package/src/lib/constants/deal-filters.d.ts +22 -1
  163. package/src/lib/constants/deal-filters.js +67 -4
  164. package/src/lib/constants/deal-filters.js.map +1 -1
  165. package/src/lib/declarations.console-server.d.ts +3 -0
  166. package/src/lib/declarations.console-server.js +35 -0
  167. package/src/lib/declarations.console-server.js.map +1 -1
  168. package/src/lib/declarations.d.ts +26 -0
  169. package/src/lib/declarations.js +35 -0
  170. package/src/lib/declarations.js.map +1 -0
  171. package/src/lib/declarations.server.d.ts +11 -0
  172. package/src/lib/declarations.server.js +92 -1
  173. package/src/lib/declarations.server.js.map +1 -1
  174. package/src/lib/hooks/use-company-picklists.d.ts +15 -0
  175. package/src/lib/hooks/use-company-picklists.js +55 -0
  176. package/src/lib/hooks/use-company-picklists.js.map +1 -0
  177. package/src/lib/hooks/use-crm-picklist.d.ts +25 -0
  178. package/src/lib/hooks/use-crm-picklist.js +53 -0
  179. package/src/lib/hooks/use-crm-picklist.js.map +1 -0
  180. package/src/lib/hooks/use-crm-record-names.d.ts +16 -0
  181. package/src/lib/hooks/use-crm-record-names.js +45 -19
  182. package/src/lib/hooks/use-crm-record-names.js.map +1 -1
  183. package/src/lib/hooks/use-crm-task-picklists.d.ts +11 -0
  184. package/src/lib/hooks/use-crm-task-picklists.js +45 -0
  185. package/src/lib/hooks/use-crm-task-picklists.js.map +1 -0
  186. package/src/lib/hooks/use-deals.d.ts +14 -0
  187. package/src/lib/hooks/use-deals.js +29 -0
  188. package/src/lib/hooks/use-deals.js.map +1 -1
  189. package/src/lib/hooks/use-lead-picklists.d.ts +14 -0
  190. package/src/lib/hooks/use-lead-picklists.js +46 -0
  191. package/src/lib/hooks/use-lead-picklists.js.map +1 -0
  192. package/src/lib/hooks/use-lead-source-picklist.d.ts +5 -19
  193. package/src/lib/hooks/use-lead-source-picklist.js +12 -22
  194. package/src/lib/hooks/use-lead-source-picklist.js.map +1 -1
  195. package/src/lib/hooks/use-lead-status-picklist.d.ts +9 -0
  196. package/src/lib/hooks/use-lead-status-picklist.js +40 -0
  197. package/src/lib/hooks/use-lead-status-picklist.js.map +1 -0
  198. package/src/lib/model/companies.d.ts +32 -3
  199. package/src/lib/model/companies.js +133 -16
  200. package/src/lib/model/companies.js.map +1 -1
  201. package/src/lib/model/contact-merge.d.ts +27 -3
  202. package/src/lib/model/contact-merge.js +81 -7
  203. package/src/lib/model/contact-merge.js.map +1 -1
  204. package/src/lib/model/contact-record.d.ts +17 -0
  205. package/src/lib/model/contact-record.js +15 -1
  206. package/src/lib/model/contact-record.js.map +1 -1
  207. package/src/lib/model/contact-update.d.ts +31 -2
  208. package/src/lib/model/contact-update.js +14 -0
  209. package/src/lib/model/contact-update.js.map +1 -1
  210. package/src/lib/model/crm-list-query.d.ts +5 -0
  211. package/src/lib/model/crm-list-query.js +8 -0
  212. package/src/lib/model/crm-list-query.js.map +1 -1
  213. package/src/lib/model/crm-recipes.d.ts +40 -0
  214. package/src/lib/model/crm-recipes.js +244 -0
  215. package/src/lib/model/crm-recipes.js.map +1 -0
  216. package/src/lib/model/crm-record-counts.d.ts +36 -0
  217. package/src/lib/model/crm-record-counts.js +54 -0
  218. package/src/lib/model/crm-record-counts.js.map +1 -0
  219. package/src/lib/model/crm-record-lists.d.ts +23 -0
  220. package/src/lib/model/crm-record-lists.js +161 -0
  221. package/src/lib/model/crm-record-lists.js.map +1 -0
  222. package/src/lib/model/crm-reports.d.ts +73 -3
  223. package/src/lib/model/crm-reports.js +120 -1
  224. package/src/lib/model/crm-reports.js.map +1 -1
  225. package/src/lib/model/crm-sharing.d.ts +21 -3
  226. package/src/lib/model/crm-sharing.js +27 -6
  227. package/src/lib/model/crm-sharing.js.map +1 -1
  228. package/src/lib/model/deal-board-model.d.ts +8 -2
  229. package/src/lib/model/deal-board-model.js +15 -2
  230. package/src/lib/model/deal-board-model.js.map +1 -1
  231. package/src/lib/model/deal-contact-roles.d.ts +45 -0
  232. package/src/lib/model/deal-contact-roles.js +73 -0
  233. package/src/lib/model/deal-contact-roles.js.map +1 -0
  234. package/src/lib/model/deal-form-model.d.ts +56 -0
  235. package/src/lib/model/deal-form-model.js +79 -7
  236. package/src/lib/model/deal-form-model.js.map +1 -1
  237. package/src/lib/model/download-text-file.d.ts +23 -0
  238. package/src/lib/model/download-text-file.js +34 -0
  239. package/src/lib/model/download-text-file.js.map +1 -0
  240. package/src/lib/model/lead-filters.d.ts +36 -2
  241. package/src/lib/model/lead-filters.js +123 -2
  242. package/src/lib/model/lead-filters.js.map +1 -1
  243. package/src/lib/model/lead-source-direction.d.ts +38 -0
  244. package/src/lib/model/lead-source-direction.js +52 -0
  245. package/src/lib/model/lead-source-direction.js.map +1 -0
  246. package/src/lib/model/picklist-values.d.ts +119 -0
  247. package/src/lib/model/picklist-values.js +256 -0
  248. package/src/lib/model/picklist-values.js.map +1 -0
  249. package/src/lib/model/record-facts-disclosed.d.ts +81 -0
  250. package/src/lib/model/record-facts-disclosed.js +281 -0
  251. package/src/lib/model/record-facts-disclosed.js.map +1 -0
  252. package/src/lib/model/record-facts.d.ts +215 -34
  253. package/src/lib/model/record-facts.js +307 -128
  254. package/src/lib/model/record-facts.js.map +1 -1
  255. package/src/lib/model/task-picklists.d.ts +52 -0
  256. package/src/lib/model/task-picklists.js +83 -0
  257. package/src/lib/model/task-picklists.js.map +1 -0
  258. package/src/lib/model/task-routes.d.ts +13 -0
  259. package/src/lib/model/task-routes.js +23 -4
  260. package/src/lib/model/task-routes.js.map +1 -1
  261. package/src/lib/plan-entitlements.js +6 -4
  262. package/src/lib/plan-entitlements.js.map +1 -1
  263. package/src/lib/plugin.js +17 -0
  264. package/src/lib/plugin.js.map +1 -1
  265. package/src/lib/server/api-v1/contacts.js +220 -14
  266. package/src/lib/server/api-v1/contacts.js.map +1 -1
  267. package/src/lib/server/api-v1/crm-activities.js +14 -1
  268. package/src/lib/server/api-v1/crm-activities.js.map +1 -1
  269. package/src/lib/server/api-v1/crm-companies.js +123 -14
  270. package/src/lib/server/api-v1/crm-companies.js.map +1 -1
  271. package/src/lib/server/api-v1/crm-deals.js +291 -26
  272. package/src/lib/server/api-v1/crm-deals.js.map +1 -1
  273. package/src/lib/server/api-v1/crm-leads.js +144 -38
  274. package/src/lib/server/api-v1/crm-leads.js.map +1 -1
  275. package/src/lib/server/api-v1/crm-pipelines.d.ts +6 -0
  276. package/src/lib/server/api-v1/crm-pipelines.js +8 -2
  277. package/src/lib/server/api-v1/crm-pipelines.js.map +1 -1
  278. package/src/lib/server/api-v1/crm-tasks.js +75 -33
  279. package/src/lib/server/api-v1/crm-tasks.js.map +1 -1
  280. package/src/lib/server/api-v1/openapi.d.ts +0 -11
  281. package/src/lib/server/api-v1/openapi.js +171 -16
  282. package/src/lib/server/api-v1/openapi.js.map +1 -1
  283. package/src/lib/server/assign-contact-owner.d.ts +162 -0
  284. package/src/lib/server/assign-contact-owner.js +340 -0
  285. package/src/lib/server/assign-contact-owner.js.map +1 -0
  286. package/src/lib/server/associate-company-by-domain.d.ts +70 -0
  287. package/src/lib/server/associate-company-by-domain.js +153 -0
  288. package/src/lib/server/associate-company-by-domain.js.map +1 -0
  289. package/src/lib/server/automation-steps.d.ts +139 -0
  290. package/src/lib/server/automation-steps.js +472 -0
  291. package/src/lib/server/automation-steps.js.map +1 -0
  292. package/src/lib/server/capture-contact.js +65 -21
  293. package/src/lib/server/capture-contact.js.map +1 -1
  294. package/src/lib/server/capture-host-contact.d.ts +104 -0
  295. package/src/lib/server/capture-host-contact.js +151 -0
  296. package/src/lib/server/capture-host-contact.js.map +1 -0
  297. package/src/lib/server/company-children.d.ts +25 -0
  298. package/src/lib/server/company-children.js +42 -0
  299. package/src/lib/server/company-children.js.map +1 -0
  300. package/src/lib/server/company-delete.d.ts +18 -0
  301. package/src/lib/server/company-delete.js +44 -21
  302. package/src/lib/server/company-delete.js.map +1 -1
  303. package/src/lib/server/contact-email-history.js +21 -19
  304. package/src/lib/server/contact-email-history.js.map +1 -1
  305. package/src/lib/server/contact-remove.js +27 -2
  306. package/src/lib/server/contact-remove.js.map +1 -1
  307. package/src/lib/server/contact-reports-to.d.ts +61 -0
  308. package/src/lib/server/contact-reports-to.js +114 -0
  309. package/src/lib/server/contact-reports-to.js.map +1 -0
  310. package/src/lib/server/contact-update.d.ts +42 -1
  311. package/src/lib/server/contact-update.js +204 -23
  312. package/src/lib/server/contact-update.js.map +1 -1
  313. package/src/lib/server/convert-host-lead.d.ts +46 -1
  314. package/src/lib/server/convert-host-lead.js +173 -15
  315. package/src/lib/server/convert-host-lead.js.map +1 -1
  316. package/src/lib/server/convert-open-lead.js +12 -5
  317. package/src/lib/server/convert-open-lead.js.map +1 -1
  318. package/src/lib/server/crm-record-indexes.d.ts +45 -0
  319. package/src/lib/server/crm-record-indexes.js +78 -0
  320. package/src/lib/server/crm-record-indexes.js.map +1 -0
  321. package/src/lib/server/crm-sharing.d.ts +9 -1
  322. package/src/lib/server/crm-sharing.js +23 -3
  323. package/src/lib/server/crm-sharing.js.map +1 -1
  324. package/src/lib/server/deal-contact-roles.d.ts +27 -0
  325. package/src/lib/server/deal-contact-roles.js +89 -0
  326. package/src/lib/server/deal-contact-roles.js.map +1 -0
  327. package/src/lib/server/lead-convert.d.ts +5 -0
  328. package/src/lib/server/lead-convert.js +19 -3
  329. package/src/lib/server/lead-convert.js.map +1 -1
  330. package/src/lib/server/lead-create.d.ts +17 -2
  331. package/src/lib/server/lead-create.js +46 -14
  332. package/src/lib/server/lead-create.js.map +1 -1
  333. package/src/lib/server/lead-nurturing.d.ts +6 -0
  334. package/src/lib/server/lead-nurturing.js +57 -20
  335. package/src/lib/server/lead-nurturing.js.map +1 -1
  336. package/src/lib/server/lead-picklists.d.ts +35 -0
  337. package/src/lib/server/lead-picklists.js +52 -0
  338. package/src/lib/server/lead-picklists.js.map +1 -0
  339. package/src/lib/server/lead-source-picklist.d.ts +1 -13
  340. package/src/lib/server/lead-source-picklist.js +6 -33
  341. package/src/lib/server/lead-source-picklist.js.map +1 -1
  342. package/src/lib/server/merge-contacts.js +22 -4
  343. package/src/lib/server/merge-contacts.js.map +1 -1
  344. package/src/lib/server/person-eraser.d.ts +53 -0
  345. package/src/lib/server/person-eraser.js +149 -0
  346. package/src/lib/server/person-eraser.js.map +1 -0
  347. package/src/lib/server/person-records.d.ts +69 -0
  348. package/src/lib/server/person-records.js +317 -0
  349. package/src/lib/server/person-records.js.map +1 -0
  350. package/src/lib/server/person-refund.d.ts +97 -0
  351. package/src/lib/server/person-refund.js +221 -0
  352. package/src/lib/server/person-refund.js.map +1 -0
  353. package/src/lib/server/picklist-values.d.ts +21 -0
  354. package/src/lib/server/picklist-values.js +308 -0
  355. package/src/lib/server/picklist-values.js.map +1 -0
  356. package/src/lib/server/read-picklist.d.ts +44 -0
  357. package/src/lib/server/read-picklist.js +65 -0
  358. package/src/lib/server/read-picklist.js.map +1 -0
  359. package/src/lib/server/recipe-routes.js +15 -7
  360. package/src/lib/server/recipe-routes.js.map +1 -1
  361. package/src/lib/server/record-email-state.js +7 -1
  362. package/src/lib/server/record-email-state.js.map +1 -1
  363. package/src/lib/server/record-facts.d.ts +3 -0
  364. package/src/lib/server/record-facts.js +233 -31
  365. package/src/lib/server/record-facts.js.map +1 -1
  366. package/src/lib/server/record-origin.d.ts +27 -0
  367. package/src/lib/server/record-origin.js +104 -0
  368. package/src/lib/server/record-origin.js.map +1 -0
  369. package/src/lib/server/record-timeline.js +15 -2
  370. package/src/lib/server/record-timeline.js.map +1 -1
  371. package/src/lib/server/suite-gate.js +4 -3
  372. package/src/lib/server/suite-gate.js.map +1 -1
  373. package/src/lib/server/task-routes.js +30 -16
  374. package/src/lib/server/task-routes.js.map +1 -1
  375. package/src/lib/server-deal-stage.d.ts +5 -1
  376. package/src/lib/server-deal-stage.js +10 -5
  377. package/src/lib/server-deal-stage.js.map +1 -1
  378. package/src/lib/server.d.ts +8 -3
  379. package/src/lib/server.js +47 -43
  380. package/src/lib/server.js.map +1 -1
  381. package/src/lib/testing/memory-firestore.d.ts +55 -0
  382. package/src/lib/testing/memory-firestore.js +335 -0
  383. package/src/lib/testing/memory-firestore.js.map +1 -0
  384. package/src/lib/testing/stand-in-email-send-index.d.ts +25 -0
  385. package/src/lib/testing/stand-in-email-send-index.js +49 -0
  386. package/src/lib/testing/stand-in-email-send-index.js.map +1 -0
  387. package/src/lib/transfer/activities.d.ts +30 -0
  388. package/src/lib/transfer/activities.js +132 -0
  389. package/src/lib/transfer/activities.js.map +1 -0
  390. package/src/lib/transfer/aliases.d.ts +22 -0
  391. package/src/lib/transfer/aliases.js +1342 -0
  392. package/src/lib/transfer/aliases.js.map +1 -0
  393. package/src/lib/transfer/common.d.ts +222 -0
  394. package/src/lib/transfer/common.js +832 -0
  395. package/src/lib/transfer/common.js.map +1 -0
  396. package/src/lib/transfer/companies.d.ts +43 -0
  397. package/src/lib/transfer/companies.js +549 -0
  398. package/src/lib/transfer/companies.js.map +1 -0
  399. package/src/lib/transfer/contacts.d.ts +31 -0
  400. package/src/lib/transfer/contacts.js +727 -0
  401. package/src/lib/transfer/contacts.js.map +1 -0
  402. package/src/lib/transfer/deals.d.ts +66 -0
  403. package/src/lib/transfer/deals.js +698 -0
  404. package/src/lib/transfer/deals.js.map +1 -0
  405. package/src/lib/transfer/email-templates-package.d.ts +40 -0
  406. package/src/lib/transfer/email-templates-package.js +76 -0
  407. package/src/lib/transfer/email-templates-package.js.map +1 -0
  408. package/src/lib/transfer/email-templates-package.server.d.ts +32 -0
  409. package/src/lib/transfer/email-templates-package.server.js +176 -0
  410. package/src/lib/transfer/email-templates-package.server.js.map +1 -0
  411. package/src/lib/transfer/fields.d.ts +102 -0
  412. package/src/lib/transfer/fields.js +1755 -0
  413. package/src/lib/transfer/fields.js.map +1 -0
  414. package/src/lib/transfer/leads.d.ts +30 -0
  415. package/src/lib/transfer/leads.js +582 -0
  416. package/src/lib/transfer/leads.js.map +1 -0
  417. package/src/lib/transfer/pipelines.d.ts +21 -0
  418. package/src/lib/transfer/pipelines.js +172 -0
  419. package/src/lib/transfer/pipelines.js.map +1 -0
  420. package/src/lib/transfer/register.d.ts +47 -0
  421. package/src/lib/transfer/register.js +277 -0
  422. package/src/lib/transfer/register.js.map +1 -0
  423. package/src/lib/transfer/tasks.d.ts +30 -0
  424. package/src/lib/transfer/tasks.js +418 -0
  425. package/src/lib/transfer/tasks.js.map +1 -0
  426. package/src/lib/transfer/ui.d.ts +20 -0
  427. package/src/lib/transfer/ui.js +42 -0
  428. package/src/lib/transfer/ui.js.map +1 -0
  429. package/src/lib/components/company-import-drawer.d.ts +0 -42
  430. package/src/lib/components/company-import-drawer.js +0 -99
  431. package/src/lib/components/company-import-drawer.js.map +0 -1
  432. package/src/lib/components/contact-import-drawer.d.ts +0 -46
  433. package/src/lib/components/contact-import-drawer.js +0 -149
  434. package/src/lib/components/contact-import-drawer.js.map +0 -1
  435. package/src/lib/components/crm-export-all-button.d.ts +0 -39
  436. package/src/lib/components/crm-export-all-button.js +0 -98
  437. package/src/lib/components/crm-export-all-button.js.map +0 -1
  438. package/src/lib/components/csv-import-drawer.d.ts +0 -139
  439. package/src/lib/components/csv-import-drawer.js +0 -757
  440. package/src/lib/components/csv-import-drawer.js.map +0 -1
  441. package/src/lib/components/deal-import-drawer.d.ts +0 -24
  442. package/src/lib/components/deal-import-drawer.js +0 -83
  443. package/src/lib/components/deal-import-drawer.js.map +0 -1
  444. package/src/lib/components/lead-import-drawer.d.ts +0 -27
  445. package/src/lib/components/lead-import-drawer.js +0 -118
  446. package/src/lib/components/lead-import-drawer.js.map +0 -1
  447. package/src/lib/components/lead-source-values-card.d.ts +0 -27
  448. package/src/lib/components/lead-source-values-card.js.map +0 -1
  449. package/src/lib/components/task-import-drawer.d.ts +0 -24
  450. package/src/lib/components/task-import-drawer.js +0 -82
  451. package/src/lib/components/task-import-drawer.js.map +0 -1
  452. package/src/lib/model/companies-csv.d.ts +0 -36
  453. package/src/lib/model/companies-csv.js +0 -38
  454. package/src/lib/model/companies-csv.js.map +0 -1
  455. package/src/lib/model/contacts-csv.d.ts +0 -54
  456. package/src/lib/model/contacts-csv.js +0 -67
  457. package/src/lib/model/contacts-csv.js.map +0 -1
  458. package/src/lib/model/crm-company-import.d.ts +0 -154
  459. package/src/lib/model/crm-company-import.js +0 -331
  460. package/src/lib/model/crm-company-import.js.map +0 -1
  461. package/src/lib/model/crm-csv.d.ts +0 -239
  462. package/src/lib/model/crm-csv.js +0 -467
  463. package/src/lib/model/crm-csv.js.map +0 -1
  464. package/src/lib/model/crm-deal-import.d.ts +0 -112
  465. package/src/lib/model/crm-deal-import.js +0 -263
  466. package/src/lib/model/crm-deal-import.js.map +0 -1
  467. package/src/lib/model/crm-import.d.ts +0 -250
  468. package/src/lib/model/crm-import.js +0 -467
  469. package/src/lib/model/crm-import.js.map +0 -1
  470. package/src/lib/model/crm-lead-import.d.ts +0 -138
  471. package/src/lib/model/crm-lead-import.js +0 -446
  472. package/src/lib/model/crm-lead-import.js.map +0 -1
  473. package/src/lib/model/crm-task-import.d.ts +0 -94
  474. package/src/lib/model/crm-task-import.js +0 -275
  475. package/src/lib/model/crm-task-import.js.map +0 -1
  476. package/src/lib/model/deals-csv.d.ts +0 -32
  477. package/src/lib/model/deals-csv.js +0 -36
  478. package/src/lib/model/deals-csv.js.map +0 -1
  479. package/src/lib/model/import-column-shapes.d.ts +0 -36
  480. package/src/lib/model/import-column-shapes.js +0 -76
  481. package/src/lib/model/import-column-shapes.js.map +0 -1
  482. package/src/lib/model/lead-source-values.d.ts +0 -82
  483. package/src/lib/model/lead-source-values.js +0 -149
  484. package/src/lib/model/lead-source-values.js.map +0 -1
  485. package/src/lib/model/leads-csv.d.ts +0 -40
  486. package/src/lib/model/leads-csv.js +0 -40
  487. package/src/lib/model/leads-csv.js.map +0 -1
  488. package/src/lib/model/tasks-csv.d.ts +0 -28
  489. package/src/lib/model/tasks-csv.js +0 -28
  490. package/src/lib/model/tasks-csv.js.map +0 -1
  491. package/src/lib/server/companies-import.d.ts +0 -57
  492. package/src/lib/server/companies-import.js +0 -242
  493. package/src/lib/server/companies-import.js.map +0 -1
  494. package/src/lib/server/contacts-import.d.ts +0 -109
  495. package/src/lib/server/contacts-import.js +0 -393
  496. package/src/lib/server/contacts-import.js.map +0 -1
  497. package/src/lib/server/deals-import.d.ts +0 -76
  498. package/src/lib/server/deals-import.js +0 -248
  499. package/src/lib/server/deals-import.js.map +0 -1
  500. package/src/lib/server/export-route.d.ts +0 -60
  501. package/src/lib/server/export-route.js +0 -447
  502. package/src/lib/server/export-route.js.map +0 -1
  503. package/src/lib/server/import-context.d.ts +0 -87
  504. package/src/lib/server/import-context.js +0 -182
  505. package/src/lib/server/import-context.js.map +0 -1
  506. package/src/lib/server/lead-source-values.d.ts +0 -50
  507. package/src/lib/server/lead-source-values.js +0 -191
  508. package/src/lib/server/lead-source-values.js.map +0 -1
  509. package/src/lib/server/leads-import.d.ts +0 -84
  510. package/src/lib/server/leads-import.js +0 -367
  511. package/src/lib/server/leads-import.js.map +0 -1
  512. package/src/lib/server/tasks-import.d.ts +0 -54
  513. package/src/lib/server/tasks-import.js +0 -160
  514. package/src/lib/server/tasks-import.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/model/deal-board-model.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * The pipeline's arithmetic and the stage editor's rules (AGL-2598), as pure\n * functions — no Firestore, no React — so the board, the table, the detail\n * page and the server route read one definition of \"what is this pipeline\n * worth\" and \"which stage edits are legal\".\n */\n\n// The module itself and never the `@aglyn/aglyn` barrel: this file is\n// reached from the server entry through the deal-stage route, and the barrel\n// carries client-only context modules the App Router graph refuses on a\n// server path (AGL-405/1349).\nimport {\n type CrmDeal,\n type CrmDealStage,\n type CrmPipeline,\n DEFAULT_DEAL_STAGES,\n dealStageById,\n isPipelineArchived,\n weightedDealAmountCents,\n} from '@aglyn/aglyn/app-utils/crm'\n\n/** A deal as the console reads it: the model plus its document id. */\nexport type DealDoc = CrmDeal & {\n $id: string\n /**\n * The contact's and the company's names, copied onto the deal when the\n * link is made. A board of two hundred cards cannot afford a keyed read per\n * card to caption each one, and the caption is allowed to lag a rename —\n * the link itself is the id, which never changes.\n */\n contactName?: string\n companyName?: string\n}\n\n/** A pipeline as the console reads it. */\nexport type PipelineDoc = CrmPipeline & { $id: string }\n\n/** The document id the seeded default pipeline is written under. */\nexport const DEFAULT_PIPELINE_ID = 'default'\n\n/** Name of the pipeline every org starts with. */\nexport const DEFAULT_PIPELINE_NAME = 'Sales'\n\nexport const PIPELINE_NAME_MAX = 60\n\n/** The pipelines a picker offers and a new deal may land in: the ones not archived. */\nexport function activePipelines<T extends Pick<CrmPipeline, 'archivedAt'>>(\n pipelines: readonly T[],\n): T[] {\n return pipelines.filter((pipeline) => !isPipelineArchived(pipeline))\n}\n\n/**\n * The pipeline a new deal lands in when nobody picks one: the active one\n * marked default, else the first active one, else nothing. An archived\n * pipeline is never the default even if its flag was left on — archiving\n * clears the flag, but a document written by an older console may not\n * have had it cleared, and a default nobody can create a deal in is a\n * board that opens on a pipeline the picker does not list.\n */\nexport function defaultPipelineOf<T extends Pick<CrmPipeline, 'archivedAt' | 'isDefault'>>(\n pipelines: readonly T[],\n): T | null {\n const active = activePipelines(pipelines)\n return active.find((pipeline) => pipeline.isDefault) ?? active[0] ?? null\n}\n\n/**\n * Why a pipeline cannot be created or renamed to `name`, or `null`.\n *\n * Names are unique among the ACTIVE pipelines, compared without case: the\n * picker lists them by name alone, and two rows reading \"Sales\" would be a\n * choice nobody can make. An archived pipeline's name is free to reuse —\n * \"Sales 2025\" retired and \"Sales\" started over is the point of archiving.\n */\nexport function pipelineNameProblem(\n name: string,\n pipelines: ReadonlyArray<Pick<CrmPipeline, 'name' | 'archivedAt'> & { $id: string }>,\n ownId?: string,\n): string | null {\n const trimmed = String(name ?? '').trim()\n if (!trimmed) return 'A pipeline needs a name.'\n const taken = activePipelines(pipelines).find(\n (pipeline) =>\n pipeline.$id !== ownId &&\n String(pipeline.name ?? '').trim().toLowerCase() === trimmed.toLowerCase(),\n )\n if (taken) return `Another pipeline is already called \"${taken.name}\".`\n return null\n}\n\n/**\n * Why a pipeline cannot be archived, or `null` when it can.\n *\n * `openDeals` is the server's count, asked for before this is called, the\n * way `stageRemovalRefusal` asks: an open deal in an archived pipeline is\n * a card on no board, and the count is taken at the click because a deal\n * can be created into the pipeline from another tab meanwhile. The default\n * pipeline cannot go while it is the default — a new deal has to land\n * somewhere — and the last active one cannot go at all, for the same\n * reason from the other side.\n */\nexport function pipelineArchiveRefusal(\n pipeline: Pick<CrmPipeline, 'archivedAt' | 'isDefault'> & { $id: string },\n pipelines: ReadonlyArray<Pick<CrmPipeline, 'archivedAt'> & { $id: string }>,\n openDeals: number,\n): string | null {\n if (isPipelineArchived(pipeline)) return 'This pipeline is already archived.'\n if (activePipelines(pipelines).filter((entry) => entry.$id !== pipeline.$id).length === 0) {\n return 'The last pipeline cannot be archived.'\n }\n if (pipeline.isDefault) {\n return 'This is the default pipeline. Make another one the default first.'\n }\n if (openDeals > 0) {\n return (\n `${openDeals.toLocaleString()} open ${openDeals === 1 ? 'deal is' : 'deals are'} ` +\n 'in this pipeline. Close or move them first, so none is left on a board nobody opens.'\n )\n }\n return null\n}\n\n/** What every pipeline write stamps besides its name. */\nexport interface PipelineWriteContext {\n visibleTo: readonly string[]\n hostId: string\n uid: string\n nowMs: number\n}\n\n/**\n * The document a new pipeline is created as: the name, a COPY of the\n * default stages, not the default, active. A copy for the reason\n * `DEFAULT_DEAL_STAGES` gives — a second pipeline seeded later must start\n * from the original set, whatever the first one's editor did to its own.\n */\nexport function newPipelineDocument(\n name: string,\n context: PipelineWriteContext,\n): Record<string, unknown> {\n return {\n name: String(name ?? '').trim().slice(0, PIPELINE_NAME_MAX),\n stages: DEFAULT_DEAL_STAGES.map((stage) => ({ ...stage })),\n isDefault: false,\n archivedAt: null,\n visibleTo: [...context.visibleTo],\n hostId: context.hostId,\n createdByUid: context.uid,\n createdAt: new Date(context.nowMs),\n updatedAt: new Date(context.nowMs),\n }\n}\n\n/**\n * Currencies the amount picker offers, lowercase ISO 4217 as the model\n * stores them. Short on purpose: a picker of a hundred and eighty currencies\n * is a scroll, and a deal in one outside this list is entered through the\n * API with whatever code it carries — the display below formats any code\n * `Intl` knows.\n */\nexport const DEAL_CURRENCIES = [\n 'usd',\n 'eur',\n 'gbp',\n 'cad',\n 'aud',\n 'nzd',\n 'chf',\n 'jpy',\n 'sek',\n 'nok',\n 'dkk',\n 'mxn',\n 'brl',\n 'inr',\n 'sgd',\n] as const\n\nexport const DEFAULT_DEAL_CURRENCY = 'usd'\n\n/** How a deal's `status` reads on screen. */\nexport const DEAL_STATUS_LABELS: Record<CrmDeal['status'], string> = {\n open: 'Open',\n won: 'Won',\n lost: 'Lost',\n}\n\n/** Cents → the reader's locale, or a plain number when the code is unknown. */\nexport function formatMoney(cents: number, currency: string | undefined): string {\n const code = String(currency || DEFAULT_DEAL_CURRENCY).toUpperCase()\n const amount = (Number(cents) || 0) / 100\n try {\n return new Intl.NumberFormat(undefined, {\n style: 'currency',\n currency: code,\n }).format(amount)\n } catch {\n return `${amount.toFixed(2)} ${code}`\n }\n}\n\n/**\n * A typed amount, as cents, or `null` when it is not a number.\n *\n * Accepts what people paste from an invoice — a thousands separator, a\n * currency symbol, surrounding space — and refuses a negative: a deal is\n * worth zero or more, and a negative amount here is a typo rather than a\n * refund. Rounded rather than truncated so `19.995` does not lose a cent.\n */\nexport function parseAmountInput(input: string): number | null {\n const cleaned = String(input ?? '')\n .replace(/[^0-9.-]/g, '')\n .trim()\n if (!cleaned) return null\n const value = Number(cleaned)\n if (!Number.isFinite(value) || value < 0) return null\n return Math.round(value * 100)\n}\n\n/** Cents → the string the amount field shows, `''` for nothing. */\nexport function amountInputValue(cents: number | undefined | null): string {\n if (typeof cents !== 'number' || !Number.isFinite(cents)) return ''\n return (cents / 100).toFixed(2)\n}\n\n/**\n * Epoch ms from whatever a timestamp field holds after a round trip — a\n * Firestore `Timestamp`, a `Date` a client just wrote, or a number.\n */\nexport function timestampMs(value: unknown): number | null {\n if (!value) return null\n if (typeof value === 'number') return Number.isFinite(value) ? value : null\n if (value instanceof Date) return value.getTime()\n const seconds = (value as { seconds?: unknown }).seconds\n if (typeof seconds === 'number') return seconds * 1000\n const toMillis = (value as { toMillis?: () => number }).toMillis\n if (typeof toMillis === 'function') return toMillis.call(value)\n return null\n}\n\n/** Whole days a deal has sat in its current stage, never negative. */\nexport function daysInStage(\n deal: Pick<CrmDeal, 'stageChangedAtMs' | 'createdAt'>,\n nowMs: number,\n): number {\n const since =\n typeof deal.stageChangedAtMs === 'number'\n ? deal.stageChangedAtMs\n : timestampMs(deal.createdAt)\n if (!since) return 0\n return Math.max(0, Math.floor((nowMs - since) / 86_400_000))\n}\n\n/** A sum per currency, because two currencies never add into one number. */\nexport type AmountByCurrency = Record<string, number>\n\nfunction addAmount(\n totals: AmountByCurrency,\n currency: string | undefined,\n cents: number,\n): void {\n const code = String(currency || DEFAULT_DEAL_CURRENCY).toLowerCase()\n totals[code] = (totals[code] ?? 0) + cents\n}\n\nexport interface BoardSummary {\n /** Deals whose `status` is `open`, whatever stage they sit in. */\n openCount: number\n /** Every open deal's full amount, per currency. */\n valueByCurrency: AmountByCurrency\n /** Every open deal's amount times its stage's probability, per currency. */\n weightedByCurrency: AmountByCurrency\n}\n\n/**\n * The three figures above the board.\n *\n * Closed deals are excluded from both totals rather than counted at full or\n * zero value: the summary describes what is still in play, and a won deal\n * counted in \"pipeline value\" would make the pipeline look larger every time\n * something closed. `weightedDealAmountCents` already answers zero for a\n * stage the pipeline no longer has.\n */\nexport function boardSummary(\n deals: readonly Pick<\n CrmDeal,\n 'status' | 'amountCents' | 'currency' | 'stageId'\n >[],\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n): BoardSummary {\n const summary: BoardSummary = {\n openCount: 0,\n valueByCurrency: {},\n weightedByCurrency: {},\n }\n for (const deal of deals) {\n if (deal.status !== 'open') continue\n summary.openCount += 1\n const amount = Math.max(0, Math.round(Number(deal.amountCents ?? 0) || 0))\n addAmount(summary.valueByCurrency, deal.currency, amount)\n addAmount(\n summary.weightedByCurrency,\n deal.currency,\n weightedDealAmountCents(deal, dealStageById(pipeline, deal.stageId)),\n )\n }\n return summary\n}\n\n/** `$1,200.00 · €300.00` — one figure per currency, largest first. */\nexport function formatAmountByCurrency(totals: AmountByCurrency): string {\n const entries = Object.entries(totals).sort((a, b) => b[1] - a[1])\n if (!entries.length) return formatMoney(0, DEFAULT_DEAL_CURRENCY)\n return entries.map(([code, cents]) => formatMoney(cents, code)).join(' · ')\n}\n\n/** The pipeline's stages in `order`, ties broken by position. */\nexport function sortedStages(\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n): CrmDealStage[] {\n return [...(pipeline?.stages ?? [])]\n .map((stage, index) => ({ stage, index }))\n .sort((a, b) => a.stage.order - b.stage.order || a.index - b.index)\n .map(({ stage }) => stage)\n}\n\n/** The stages a deal can be dragged between — everything that is not closed. */\nexport function openStages(\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n): CrmDealStage[] {\n return sortedStages(pipeline).filter((stage) => stage.kind === 'open')\n}\n\n/** The stage whose `kind` is `won` or `lost`, or null when the pipeline lacks one. */\nexport function closingStage(\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n kind: 'won' | 'lost',\n): CrmDealStage | null {\n return sortedStages(pipeline).find((stage) => stage.kind === kind) ?? null\n}\n\n/**\n * The id a new stage is stored under: the name as a slug, made unique\n * against the stages already there.\n *\n * A slug rather than a random id because the id appears in every deal that\n * sits in the stage and in every automation filter that names it, so a\n * reader looking at `stageId == \"demo-booked\"` should be able to tell which\n * stage that is without opening the pipeline. Uniqueness is what keeps a\n * second \"Demo\" from silently becoming the first.\n */\nexport function newStageId(\n name: string,\n existing: readonly CrmDealStage[],\n): string {\n const base =\n String(name ?? '')\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '')\n .slice(0, 40) || 'stage'\n const taken = new Set(existing.map((stage) => stage.id))\n if (!taken.has(base)) return base\n let suffix = 2\n while (taken.has(`${base}-${suffix}`)) suffix += 1\n return `${base}-${suffix}`\n}\n\n/**\n * Stages re-numbered 0..n in the order they are GIVEN.\n *\n * Positional on purpose: every caller hands this an array it has already\n * arranged — a swap, a splice, a filter over the sorted stages — and the\n * stored `order` on each entry is the arrangement being replaced. Sorting by\n * it here would put a swapped pair straight back where it was.\n */\nexport function renumberStages(\n stages: readonly CrmDealStage[],\n): CrmDealStage[] {\n return stages.map((stage, order) => ({ ...stage, order }))\n}\n\n/** 0–100, whole numbers; anything else becomes the nearest bound. */\nexport function clampProbability(value: unknown): number {\n const number = Math.round(Number(value))\n if (!Number.isFinite(number)) return 0\n return Math.min(100, Math.max(0, number))\n}\n\n/**\n * Add an open stage in front of the closing stages.\n *\n * Won and Lost stay last: a pipeline reads left to right as a progression,\n * and a stage added after \"Won\" would be a step past the end.\n */\nexport function addStage(\n stages: readonly CrmDealStage[],\n name: string,\n probability = 50,\n): CrmDealStage[] {\n const trimmed = String(name ?? '').trim()\n if (!trimmed) return [...stages]\n const sorted = sortedStages({ stages: [...stages] })\n const firstClosed = sorted.findIndex((stage) => stage.kind !== 'open')\n const at = firstClosed === -1 ? sorted.length : firstClosed\n const stage: CrmDealStage = {\n id: newStageId(trimmed, sorted),\n name: trimmed,\n order: at,\n probability: clampProbability(probability),\n kind: 'open',\n }\n sorted.splice(at, 0, stage)\n return renumberStages(sorted)\n}\n\nexport function renameStage(\n stages: readonly CrmDealStage[],\n stageId: string,\n name: string,\n): CrmDealStage[] {\n const trimmed = String(name ?? '').trim()\n return stages.map((stage) =>\n stage.id === stageId && trimmed ? { ...stage, name: trimmed } : stage,\n )\n}\n\n/**\n * Set an OPEN stage's probability. Won is 100 and Lost is 0 by definition —\n * `weightedDealAmountCents` reads the status before the stage, so a Won\n * stage at 60% would change nothing and only mislead the editor.\n */\nexport function setStageProbability(\n stages: readonly CrmDealStage[],\n stageId: string,\n probability: unknown,\n): CrmDealStage[] {\n return stages.map((stage) =>\n stage.id === stageId && stage.kind === 'open'\n ? { ...stage, probability: clampProbability(probability) }\n : stage,\n )\n}\n\n/**\n * Swap an open stage with its open neighbor. The closing stages are not in\n * the reorderable range, so an open stage can never be moved past Won.\n */\nexport function moveStage(\n stages: readonly CrmDealStage[],\n stageId: string,\n direction: 'up' | 'down',\n): CrmDealStage[] {\n const sorted = sortedStages({ stages: [...stages] })\n const index = sorted.findIndex((stage) => stage.id === stageId)\n if (index === -1 || sorted[index].kind !== 'open') return sorted\n const target = direction === 'up' ? index - 1 : index + 1\n if (target < 0 || target >= sorted.length) return sorted\n if (sorted[target].kind !== 'open') return sorted\n const swapped = [...sorted]\n const moving = swapped[index]\n swapped[index] = swapped[target]\n swapped[target] = moving\n return renumberStages(swapped)\n}\n\n/**\n * Why a stage cannot be removed, or `null` when it can.\n *\n * `dealsInStage` is the server's count, asked for before this is called: a\n * stage with deals in it cannot go, because every one of those deals would\n * be left pointing at a stage that no longer exists and worth nothing to the\n * forecast. The closing stages cannot go at all — a deal has to be able to\n * be won and lost.\n */\nexport function stageRemovalRefusal(\n stages: readonly CrmDealStage[],\n stageId: string,\n dealsInStage: number,\n): string | null {\n const stage = stages.find((entry) => entry.id === stageId)\n if (!stage) return 'This stage is no longer in the pipeline.'\n if (stage.kind !== 'open') {\n return `Every pipeline keeps its ${stage.kind === 'won' ? 'Won' : 'Lost'} stage.`\n }\n if (openStages({ stages: [...stages] }).length <= 1) {\n return 'A pipeline needs at least one open stage.'\n }\n if (dealsInStage > 0) {\n return (\n `${dealsInStage.toLocaleString()} ${dealsInStage === 1 ? 'deal is' : 'deals are'} ` +\n 'in this stage. Move them first, so none is left in a stage that no longer exists.'\n )\n }\n return null\n}\n\nexport function removeStage(\n stages: readonly CrmDealStage[],\n stageId: string,\n): CrmDealStage[] {\n return renumberStages(stages.filter((stage) => stage.id !== stageId))\n}\n\n/**\n * What a stored set of stages has to satisfy before it is written back:\n * unique ids, a name on every stage, exactly one Won and one Lost, at least\n * one open stage. The editor refuses a save that fails this rather than\n * writing a pipeline the board cannot draw.\n */\nexport function stagesProblem(stages: readonly CrmDealStage[]): string | null {\n if (!stages.length) return 'A pipeline needs stages.'\n const ids = new Set<string>()\n for (const stage of stages) {\n if (!stage.id) return 'Every stage needs an id.'\n if (ids.has(stage.id)) return `Two stages share the id \"${stage.id}\".`\n ids.add(stage.id)\n if (!String(stage.name ?? '').trim()) return 'Every stage needs a name.'\n }\n const won = stages.filter((stage) => stage.kind === 'won').length\n const lost = stages.filter((stage) => stage.kind === 'lost').length\n if (won !== 1) return 'A pipeline has exactly one Won stage.'\n if (lost !== 1) return 'A pipeline has exactly one Lost stage.'\n if (!stages.some((stage) => stage.kind === 'open')) {\n return 'A pipeline needs at least one open stage.'\n }\n return null\n}\n\n/**\n * The event a stage change emits, and the flat payload a workflow filter\n * reads (AGL-2598). Strings and numbers only — a workflow expression\n * evaluates over scalars, and an absent link is the empty string rather than\n * a missing key so `contactId == \"\"` is a filter somebody can write.\n */\nexport type DealEventName = 'dealStageChanged' | 'dealWon' | 'dealLost'\n\nexport function dealEventName(status: CrmDeal['status']): DealEventName {\n if (status === 'won') return 'dealWon'\n if (status === 'lost') return 'dealLost'\n return 'dealStageChanged'\n}\n\nexport function dealEventPayload(\n dealId: string,\n deal: Pick<\n CrmDeal,\n | 'title'\n | 'amountCents'\n | 'currency'\n | 'stageId'\n | 'ownerUid'\n | 'contactId'\n | 'companyId'\n | 'lostReason'\n >,\n previousStageId: string,\n): Record<string, string | number> {\n return {\n dealId,\n title: String(deal.title ?? ''),\n amountCents: Math.max(0, Math.round(Number(deal.amountCents ?? 0) || 0)),\n currency: String(deal.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n stageId: String(deal.stageId ?? ''),\n previousStageId: String(previousStageId ?? ''),\n ownerUid: String(deal.ownerUid ?? ''),\n contactId: String(deal.contactId ?? ''),\n companyId: String(deal.companyId ?? ''),\n ...(deal.lostReason ? { lostReason: String(deal.lostReason) } : {}),\n }\n}\n\n/** Initials for an avatar: the first letters of the first two words. */\nexport function initialsFor(label: string): string {\n const words = String(label ?? '')\n .trim()\n .split(/[\\s@._-]+/)\n .filter(Boolean)\n const letters = words\n .slice(0, 2)\n .map((word) => word[0])\n .join('')\n return (letters || '?').toUpperCase()\n}\n\n/**\n * A stable hue for one person, so an owner keeps one color across cards and\n * sessions. Hashing the label rather than picking from a palette is what\n * makes the same uid the same color on every machine.\n */\nexport function hueFor(label: string): number {\n let hash = 0\n for (let index = 0; index < label.length; index += 1) {\n hash = (hash * 31 + label.charCodeAt(index)) % 360\n }\n return hash\n}\n\n/** `2026-09-05` for a date input, from epoch ms; `''` for none. */\nexport function dateInputValue(ms: number | null | undefined): string {\n if (typeof ms !== 'number' || !Number.isFinite(ms)) return ''\n const at = new Date(ms)\n const offset = at.getTimezoneOffset() * 60_000\n return new Date(ms - offset).toISOString().slice(0, 10)\n}\n\n/**\n * Epoch ms at local noon of a typed `YYYY-MM-DD`, or `null`.\n *\n * Noon rather than midnight so the day survives a timezone round trip: a\n * close date stored at local midnight and read back one timezone west shows\n * the day before.\n */\nexport function dateInputMs(value: string): number | null {\n const match = /^(\\d{4})-(\\d{2})-(\\d{2})$/.exec(String(value ?? '').trim())\n if (!match) return null\n const at = new Date(\n Number(match[1]),\n Number(match[2]) - 1,\n Number(match[3]),\n 12,\n 0,\n 0,\n 0,\n )\n return Number.isNaN(at.getTime()) ? null : at.getTime()\n}\n"],"names":["DEFAULT_DEAL_STAGES","dealStageById","isPipelineArchived","weightedDealAmountCents","DEFAULT_PIPELINE_ID","DEFAULT_PIPELINE_NAME","PIPELINE_NAME_MAX","activePipelines","pipelines","filter","pipeline","defaultPipelineOf","active","find","isDefault","pipelineNameProblem","name","ownId","trimmed","String","trim","taken","$id","toLowerCase","pipelineArchiveRefusal","openDeals","entry","length","toLocaleString","newPipelineDocument","context","slice","stages","map","stage","archivedAt","visibleTo","hostId","createdByUid","uid","createdAt","Date","nowMs","updatedAt","DEAL_CURRENCIES","DEFAULT_DEAL_CURRENCY","DEAL_STATUS_LABELS","open","won","lost","formatMoney","cents","currency","code","toUpperCase","amount","Number","Intl","NumberFormat","undefined","style","format","toFixed","parseAmountInput","input","cleaned","replace","value","isFinite","Math","round","amountInputValue","timestampMs","getTime","seconds","toMillis","call","daysInStage","deal","since","stageChangedAtMs","max","floor","addAmount","totals","boardSummary","deals","summary","openCount","valueByCurrency","weightedByCurrency","status","amountCents","stageId","formatAmountByCurrency","entries","Object","sort","a","b","join","sortedStages","index","order","openStages","kind","closingStage","newStageId","existing","base","Set","id","has","suffix","renumberStages","clampProbability","number","min","addStage","probability","sorted","firstClosed","findIndex","at","splice","renameStage","setStageProbability","moveStage","direction","target","swapped","moving","stageRemovalRefusal","dealsInStage","removeStage","stagesProblem","ids","add","some","dealEventName","dealEventPayload","dealId","previousStageId","title","ownerUid","contactId","companyId","lostReason","initialsFor","label","words","split","Boolean","letters","word","hueFor","hash","charCodeAt","dateInputValue","ms","offset","getTimezoneOffset","toISOString","dateInputMs","match","exec","isNaN"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;CAKC,GAED,sEAAsE;AACtE,6EAA6E;AAC7E,wEAAwE;AACxE,8BAA8B;AAC9B,SAIEA,mBAAmB,EACnBC,aAAa,EACbC,kBAAkB,EAClBC,uBAAuB,QAClB,6BAA4B;AAkBnC,kEAAkE,GAClE,OAAO,MAAMC,sBAAsB,UAAS;AAE5C,gDAAgD,GAChD,OAAO,MAAMC,wBAAwB,QAAO;AAE5C,OAAO,MAAMC,oBAAoB,GAAE;AAEnC,qFAAqF,GACrF,OAAO,SAASC,gBACdC,SAAuB;IAEvB,OAAOA,UAAUC,MAAM,CAAC,CAACC,WAAa,CAACR,mBAAmBQ;AAC5D;AAEA;;;;;;;CAOC,GACD,OAAO,SAASC,kBACdH,SAAuB;QAGhBI,MAAAA;IADP,MAAMA,SAASL,gBAAgBC;IAC/B,QAAOI,QAAAA,eAAAA,OAAOC,IAAI,CAAC,CAACH,WAAaA,SAASI,SAAS,aAA5CF,eAAiDA,MAAM,CAAC,EAAE,YAA1DA,OAA8D;AACvE;AAEA;;;;;;;CAOC,GACD,OAAO,SAASG,oBACdC,IAAY,EACZR,SAAoF,EACpFS,KAAc;IAEd,MAAMC,UAAUC,OAAOH,eAAAA,OA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MAAM,OAAOkD,GAAGlD,OAAO;AACvD"}
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/model/deal-board-model.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * The pipeline's arithmetic and the stage editor's rules (AGL-2598), as pure\n * functions — no Firestore, no React — so the board, the table, the detail\n * page and the server route read one definition of \"what is this pipeline\n * worth\" and \"which stage edits are legal\".\n */\n\n// The module itself and never the `@aglyn/aglyn` barrel: this file is\n// reached from the server entry through the deal-stage route, and the barrel\n// carries client-only context modules the App Router graph refuses on a\n// server path (AGL-405/1349).\nimport {\n type CrmDeal,\n type CrmDealStage,\n type CrmForecastCategory,\n type CrmPipeline,\n DEFAULT_DEAL_STAGES,\n dealStageById,\n isCrmForecastCategory,\n isPipelineArchived,\n weightedDealAmountCents,\n} from '@aglyn/aglyn/app-utils/crm'\n\n/** A deal as the console reads it: the model plus its document id. */\nexport type DealDoc = CrmDeal & {\n $id: string\n /**\n * The contact's and the company's names, copied onto the deal when the\n * link is made. A board of two hundred cards cannot afford a keyed read per\n * card to caption each one, and the caption is allowed to lag a rename —\n * the link itself is the id, which never changes.\n */\n contactName?: string\n companyName?: string\n}\n\n/** A pipeline as the console reads it. */\nexport type PipelineDoc = CrmPipeline & { $id: string }\n\n/** The document id the seeded default pipeline is written under. */\nexport const DEFAULT_PIPELINE_ID = 'default'\n\n/** Name of the pipeline every org starts with. */\nexport const DEFAULT_PIPELINE_NAME = 'Sales'\n\nexport const PIPELINE_NAME_MAX = 60\n\n/** The pipelines a picker offers and a new deal may land in: the ones not archived. */\nexport function activePipelines<T extends Pick<CrmPipeline, 'archivedAt'>>(\n pipelines: readonly T[],\n): T[] {\n return pipelines.filter((pipeline) => !isPipelineArchived(pipeline))\n}\n\n/**\n * The pipeline a new deal lands in when nobody picks one: the active one\n * marked default, else the first active one, else nothing. An archived\n * pipeline is never the default even if its flag was left on — archiving\n * clears the flag, but a document written by an older console may not\n * have had it cleared, and a default nobody can create a deal in is a\n * board that opens on a pipeline the picker does not list.\n */\nexport function defaultPipelineOf<T extends Pick<CrmPipeline, 'archivedAt' | 'isDefault'>>(\n pipelines: readonly T[],\n): T | null {\n const active = activePipelines(pipelines)\n return active.find((pipeline) => pipeline.isDefault) ?? active[0] ?? null\n}\n\n/**\n * Why a pipeline cannot be created or renamed to `name`, or `null`.\n *\n * Names are unique among the ACTIVE pipelines, compared without case: the\n * picker lists them by name alone, and two rows reading \"Sales\" would be a\n * choice nobody can make. An archived pipeline's name is free to reuse —\n * \"Sales 2025\" retired and \"Sales\" started over is the point of archiving.\n */\nexport function pipelineNameProblem(\n name: string,\n pipelines: ReadonlyArray<Pick<CrmPipeline, 'name' | 'archivedAt'> & { $id: string }>,\n ownId?: string,\n): string | null {\n const trimmed = String(name ?? '').trim()\n if (!trimmed) return 'A pipeline needs a name.'\n const taken = activePipelines(pipelines).find(\n (pipeline) =>\n pipeline.$id !== ownId &&\n String(pipeline.name ?? '').trim().toLowerCase() === trimmed.toLowerCase(),\n )\n if (taken) return `Another pipeline is already called \"${taken.name}\".`\n return null\n}\n\n/**\n * Why a pipeline cannot be archived, or `null` when it can.\n *\n * `openDeals` is the server's count, asked for before this is called, the\n * way `stageRemovalRefusal` asks: an open deal in an archived pipeline is\n * a card on no board, and the count is taken at the click because a deal\n * can be created into the pipeline from another tab meanwhile. The default\n * pipeline cannot go while it is the default — a new deal has to land\n * somewhere — and the last active one cannot go at all, for the same\n * reason from the other side.\n */\nexport function pipelineArchiveRefusal(\n pipeline: Pick<CrmPipeline, 'archivedAt' | 'isDefault'> & { $id: string },\n pipelines: ReadonlyArray<Pick<CrmPipeline, 'archivedAt'> & { $id: string }>,\n openDeals: number,\n): string | null {\n if (isPipelineArchived(pipeline)) return 'This pipeline is already archived.'\n if (activePipelines(pipelines).filter((entry) => entry.$id !== pipeline.$id).length === 0) {\n return 'The last pipeline cannot be archived.'\n }\n if (pipeline.isDefault) {\n return 'This is the default pipeline. Make another one the default first.'\n }\n if (openDeals > 0) {\n return (\n `${openDeals.toLocaleString()} open ${openDeals === 1 ? 'deal is' : 'deals are'} ` +\n 'in this pipeline. Close or move them first, so none is left on a board nobody opens.'\n )\n }\n return null\n}\n\n/** What every pipeline write stamps besides its name. */\nexport interface PipelineWriteContext {\n visibleTo: readonly string[]\n hostId: string\n uid: string\n nowMs: number\n}\n\n/**\n * The document a new pipeline is created as: the name, a COPY of the\n * default stages, not the default, active. A copy for the reason\n * `DEFAULT_DEAL_STAGES` gives — a second pipeline seeded later must start\n * from the original set, whatever the first one's editor did to its own.\n */\nexport function newPipelineDocument(\n name: string,\n context: PipelineWriteContext,\n): Record<string, unknown> {\n return {\n name: String(name ?? '').trim().slice(0, PIPELINE_NAME_MAX),\n stages: DEFAULT_DEAL_STAGES.map((stage) => ({ ...stage })),\n isDefault: false,\n archivedAt: null,\n visibleTo: [...context.visibleTo],\n hostId: context.hostId,\n createdByUid: context.uid,\n createdAt: new Date(context.nowMs),\n updatedAt: new Date(context.nowMs),\n }\n}\n\n/**\n * Currencies the amount picker offers, lowercase ISO 4217 as the model\n * stores them. Short on purpose: a picker of a hundred and eighty currencies\n * is a scroll, and a deal in one outside this list is entered through the\n * API with whatever code it carries — the display below formats any code\n * `Intl` knows.\n */\nexport const DEAL_CURRENCIES = [\n 'usd',\n 'eur',\n 'gbp',\n 'cad',\n 'aud',\n 'nzd',\n 'chf',\n 'jpy',\n 'sek',\n 'nok',\n 'dkk',\n 'mxn',\n 'brl',\n 'inr',\n 'sgd',\n] as const\n\nexport const DEFAULT_DEAL_CURRENCY = 'usd'\n\n/** How a deal's `status` reads on screen. */\nexport const DEAL_STATUS_LABELS: Record<CrmDeal['status'], string> = {\n open: 'Open',\n won: 'Won',\n lost: 'Lost',\n}\n\n/** Cents → the reader's locale, or a plain number when the code is unknown. */\nexport function formatMoney(cents: number, currency: string | undefined): string {\n const code = String(currency || DEFAULT_DEAL_CURRENCY).toUpperCase()\n const amount = (Number(cents) || 0) / 100\n try {\n return new Intl.NumberFormat(undefined, {\n style: 'currency',\n currency: code,\n }).format(amount)\n } catch {\n return `${amount.toFixed(2)} ${code}`\n }\n}\n\n/**\n * A typed amount, as cents, or `null` when it is not a number.\n *\n * Accepts what people paste from an invoice — a thousands separator, a\n * currency symbol, surrounding space — and refuses a negative: a deal is\n * worth zero or more, and a negative amount here is a typo rather than a\n * refund. Rounded rather than truncated so `19.995` does not lose a cent.\n */\nexport function parseAmountInput(input: string): number | null {\n const cleaned = String(input ?? '')\n .replace(/[^0-9.-]/g, '')\n .trim()\n if (!cleaned) return null\n const value = Number(cleaned)\n if (!Number.isFinite(value) || value < 0) return null\n return Math.round(value * 100)\n}\n\n/** Cents → the string the amount field shows, `''` for nothing. */\nexport function amountInputValue(cents: number | undefined | null): string {\n if (typeof cents !== 'number' || !Number.isFinite(cents)) return ''\n return (cents / 100).toFixed(2)\n}\n\n/**\n * Epoch ms from whatever a timestamp field holds after a round trip — a\n * Firestore `Timestamp`, a `Date` a client just wrote, or a number.\n */\nexport function timestampMs(value: unknown): number | null {\n if (!value) return null\n if (typeof value === 'number') return Number.isFinite(value) ? value : null\n if (value instanceof Date) return value.getTime()\n const seconds = (value as { seconds?: unknown }).seconds\n if (typeof seconds === 'number') return seconds * 1000\n const toMillis = (value as { toMillis?: () => number }).toMillis\n if (typeof toMillis === 'function') return toMillis.call(value)\n return null\n}\n\n/** Whole days a deal has sat in its current stage, never negative. */\nexport function daysInStage(\n deal: Pick<CrmDeal, 'stageChangedAtMs' | 'createdAt'>,\n nowMs: number,\n): number {\n const since =\n typeof deal.stageChangedAtMs === 'number'\n ? deal.stageChangedAtMs\n : timestampMs(deal.createdAt)\n if (!since) return 0\n return Math.max(0, Math.floor((nowMs - since) / 86_400_000))\n}\n\n/** A sum per currency, because two currencies never add into one number. */\nexport type AmountByCurrency = Record<string, number>\n\nfunction addAmount(\n totals: AmountByCurrency,\n currency: string | undefined,\n cents: number,\n): void {\n const code = String(currency || DEFAULT_DEAL_CURRENCY).toLowerCase()\n totals[code] = (totals[code] ?? 0) + cents\n}\n\nexport interface BoardSummary {\n /** Deals whose `status` is `open`, whatever stage they sit in. */\n openCount: number\n /** Every open deal's full amount, per currency. */\n valueByCurrency: AmountByCurrency\n /** Every open deal's amount times its stage's probability, per currency. */\n weightedByCurrency: AmountByCurrency\n}\n\n/**\n * The three figures above the board.\n *\n * Closed deals are excluded from both totals rather than counted at full or\n * zero value: the summary describes what is still in play, and a won deal\n * counted in \"pipeline value\" would make the pipeline look larger every time\n * something closed. `weightedDealAmountCents` already answers zero for a\n * stage the pipeline no longer has.\n */\nexport function boardSummary(\n deals: readonly (Pick<CrmDeal, 'status' | 'amountCents' | 'currency' | 'stageId'> &\n Partial<Pick<CrmDeal, 'probability'>>)[],\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n): BoardSummary {\n const summary: BoardSummary = {\n openCount: 0,\n valueByCurrency: {},\n weightedByCurrency: {},\n }\n for (const deal of deals) {\n if (deal.status !== 'open') continue\n summary.openCount += 1\n const amount = Math.max(0, Math.round(Number(deal.amountCents ?? 0) || 0))\n addAmount(summary.valueByCurrency, deal.currency, amount)\n addAmount(\n summary.weightedByCurrency,\n deal.currency,\n weightedDealAmountCents(deal, dealStageById(pipeline, deal.stageId)),\n )\n }\n return summary\n}\n\n/** `$1,200.00 · €300.00` — one figure per currency, largest first. */\nexport function formatAmountByCurrency(totals: AmountByCurrency): string {\n const entries = Object.entries(totals).sort((a, b) => b[1] - a[1])\n if (!entries.length) return formatMoney(0, DEFAULT_DEAL_CURRENCY)\n return entries.map(([code, cents]) => formatMoney(cents, code)).join(' · ')\n}\n\n/** The pipeline's stages in `order`, ties broken by position. */\nexport function sortedStages(\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n): CrmDealStage[] {\n return [...(pipeline?.stages ?? [])]\n .map((stage, index) => ({ stage, index }))\n .sort((a, b) => a.stage.order - b.stage.order || a.index - b.index)\n .map(({ stage }) => stage)\n}\n\n/** The stages a deal can be dragged between — everything that is not closed. */\nexport function openStages(\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n): CrmDealStage[] {\n return sortedStages(pipeline).filter((stage) => stage.kind === 'open')\n}\n\n/** The stage whose `kind` is `won` or `lost`, or null when the pipeline lacks one. */\nexport function closingStage(\n pipeline: Pick<CrmPipeline, 'stages'> | null | undefined,\n kind: 'won' | 'lost',\n): CrmDealStage | null {\n return sortedStages(pipeline).find((stage) => stage.kind === kind) ?? null\n}\n\n/**\n * The id a new stage is stored under: the name as a slug, made unique\n * against the stages already there.\n *\n * A slug rather than a random id because the id appears in every deal that\n * sits in the stage and in every automation filter that names it, so a\n * reader looking at `stageId == \"demo-booked\"` should be able to tell which\n * stage that is without opening the pipeline. Uniqueness is what keeps a\n * second \"Demo\" from silently becoming the first.\n */\nexport function newStageId(\n name: string,\n existing: readonly CrmDealStage[],\n): string {\n const base =\n String(name ?? '')\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '')\n .slice(0, 40) || 'stage'\n const taken = new Set(existing.map((stage) => stage.id))\n if (!taken.has(base)) return base\n let suffix = 2\n while (taken.has(`${base}-${suffix}`)) suffix += 1\n return `${base}-${suffix}`\n}\n\n/**\n * Stages re-numbered 0..n in the order they are GIVEN.\n *\n * Positional on purpose: every caller hands this an array it has already\n * arranged — a swap, a splice, a filter over the sorted stages — and the\n * stored `order` on each entry is the arrangement being replaced. Sorting by\n * it here would put a swapped pair straight back where it was.\n */\nexport function renumberStages(\n stages: readonly CrmDealStage[],\n): CrmDealStage[] {\n return stages.map((stage, order) => ({ ...stage, order }))\n}\n\n/** 0–100, whole numbers; anything else becomes the nearest bound. */\nexport function clampProbability(value: unknown): number {\n const number = Math.round(Number(value))\n if (!Number.isFinite(number)) return 0\n return Math.min(100, Math.max(0, number))\n}\n\n/**\n * Add an open stage in front of the closing stages.\n *\n * Won and Lost stay last: a pipeline reads left to right as a progression,\n * and a stage added after \"Won\" would be a step past the end.\n */\nexport function addStage(\n stages: readonly CrmDealStage[],\n name: string,\n probability = 50,\n): CrmDealStage[] {\n const trimmed = String(name ?? '').trim()\n if (!trimmed) return [...stages]\n const sorted = sortedStages({ stages: [...stages] })\n const firstClosed = sorted.findIndex((stage) => stage.kind !== 'open')\n const at = firstClosed === -1 ? sorted.length : firstClosed\n const stage: CrmDealStage = {\n id: newStageId(trimmed, sorted),\n name: trimmed,\n order: at,\n probability: clampProbability(probability),\n kind: 'open',\n forecastCategory: 'pipeline',\n }\n sorted.splice(at, 0, stage)\n return renumberStages(sorted)\n}\n\nexport function renameStage(\n stages: readonly CrmDealStage[],\n stageId: string,\n name: string,\n): CrmDealStage[] {\n const trimmed = String(name ?? '').trim()\n return stages.map((stage) =>\n stage.id === stageId && trimmed ? { ...stage, name: trimmed } : stage,\n )\n}\n\n/**\n * Set an OPEN stage's probability. Won is 100 and Lost is 0 by definition —\n * `weightedDealAmountCents` reads the status before the stage, so a Won\n * stage at 60% would change nothing and only mislead the editor.\n */\nexport function setStageProbability(\n stages: readonly CrmDealStage[],\n stageId: string,\n probability: unknown,\n): CrmDealStage[] {\n return stages.map((stage) =>\n stage.id === stageId && stage.kind === 'open'\n ? { ...stage, probability: clampProbability(probability) }\n : stage,\n )\n}\n\n/**\n * Set an OPEN stage's forecast category (AGL-3516). Won is Closed and Lost\n * is Omitted by definition, for the reason {@link setStageProbability}\n * gives; a deal's own category is changed on the deal.\n */\nexport function setStageForecastCategory(\n stages: readonly CrmDealStage[],\n stageId: string,\n category: CrmForecastCategory | string,\n): CrmDealStage[] {\n if (!isCrmForecastCategory(category)) return [...stages]\n return stages.map((stage) =>\n stage.id === stageId && stage.kind === 'open'\n ? { ...stage, forecastCategory: category }\n : stage,\n )\n}\n\n/**\n * Swap an open stage with its open neighbor. The closing stages are not in\n * the reorderable range, so an open stage can never be moved past Won.\n */\nexport function moveStage(\n stages: readonly CrmDealStage[],\n stageId: string,\n direction: 'up' | 'down',\n): CrmDealStage[] {\n const sorted = sortedStages({ stages: [...stages] })\n const index = sorted.findIndex((stage) => stage.id === stageId)\n if (index === -1 || sorted[index].kind !== 'open') return sorted\n const target = direction === 'up' ? index - 1 : index + 1\n if (target < 0 || target >= sorted.length) return sorted\n if (sorted[target].kind !== 'open') return sorted\n const swapped = [...sorted]\n const moving = swapped[index]\n swapped[index] = swapped[target]\n swapped[target] = moving\n return renumberStages(swapped)\n}\n\n/**\n * Why a stage cannot be removed, or `null` when it can.\n *\n * `dealsInStage` is the server's count, asked for before this is called: a\n * stage with deals in it cannot go, because every one of those deals would\n * be left pointing at a stage that no longer exists and worth nothing to the\n * forecast. The closing stages cannot go at all — a deal has to be able to\n * be won and lost.\n */\nexport function stageRemovalRefusal(\n stages: readonly CrmDealStage[],\n stageId: string,\n dealsInStage: number,\n): string | null {\n const stage = stages.find((entry) => entry.id === stageId)\n if (!stage) return 'This stage is no longer in the pipeline.'\n if (stage.kind !== 'open') {\n return `Every pipeline keeps its ${stage.kind === 'won' ? 'Won' : 'Lost'} stage.`\n }\n if (openStages({ stages: [...stages] }).length <= 1) {\n return 'A pipeline needs at least one open stage.'\n }\n if (dealsInStage > 0) {\n return (\n `${dealsInStage.toLocaleString()} ${dealsInStage === 1 ? 'deal is' : 'deals are'} ` +\n 'in this stage. Move them first, so none is left in a stage that no longer exists.'\n )\n }\n return null\n}\n\nexport function removeStage(\n stages: readonly CrmDealStage[],\n stageId: string,\n): CrmDealStage[] {\n return renumberStages(stages.filter((stage) => stage.id !== stageId))\n}\n\n/**\n * What a stored set of stages has to satisfy before it is written back:\n * unique ids, a name on every stage, exactly one Won and one Lost, at least\n * one open stage. The editor refuses a save that fails this rather than\n * writing a pipeline the board cannot draw.\n */\nexport function stagesProblem(stages: readonly CrmDealStage[]): string | null {\n if (!stages.length) return 'A pipeline needs stages.'\n const ids = new Set<string>()\n for (const stage of stages) {\n if (!stage.id) return 'Every stage needs an id.'\n if (ids.has(stage.id)) return `Two stages share the id \"${stage.id}\".`\n ids.add(stage.id)\n if (!String(stage.name ?? '').trim()) return 'Every stage needs a name.'\n }\n const won = stages.filter((stage) => stage.kind === 'won').length\n const lost = stages.filter((stage) => stage.kind === 'lost').length\n if (won !== 1) return 'A pipeline has exactly one Won stage.'\n if (lost !== 1) return 'A pipeline has exactly one Lost stage.'\n if (!stages.some((stage) => stage.kind === 'open')) {\n return 'A pipeline needs at least one open stage.'\n }\n return null\n}\n\n/**\n * The event a stage change emits, and the flat payload a workflow filter\n * reads (AGL-2598). Strings and numbers only — a workflow expression\n * evaluates over scalars, and an absent link is the empty string rather than\n * a missing key so `contactId == \"\"` is a filter somebody can write.\n */\nexport type DealEventName = 'dealStageChanged' | 'dealWon' | 'dealLost'\n\nexport function dealEventName(status: CrmDeal['status']): DealEventName {\n if (status === 'won') return 'dealWon'\n if (status === 'lost') return 'dealLost'\n return 'dealStageChanged'\n}\n\nexport function dealEventPayload(\n dealId: string,\n deal: Pick<\n CrmDeal,\n | 'title'\n | 'amountCents'\n | 'currency'\n | 'stageId'\n | 'ownerUid'\n | 'contactId'\n | 'companyId'\n | 'lostReason'\n >,\n previousStageId: string,\n): Record<string, string | number> {\n return {\n dealId,\n title: String(deal.title ?? ''),\n amountCents: Math.max(0, Math.round(Number(deal.amountCents ?? 0) || 0)),\n currency: String(deal.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n stageId: String(deal.stageId ?? ''),\n previousStageId: String(previousStageId ?? ''),\n ownerUid: String(deal.ownerUid ?? ''),\n contactId: String(deal.contactId ?? ''),\n companyId: String(deal.companyId ?? ''),\n ...(deal.lostReason ? { lostReason: String(deal.lostReason) } : {}),\n }\n}\n\n/** Initials for an avatar: the first letters of the first two words. */\nexport function initialsFor(label: string): string {\n const words = String(label ?? '')\n .trim()\n .split(/[\\s@._-]+/)\n .filter(Boolean)\n const letters = words\n .slice(0, 2)\n .map((word) => word[0])\n .join('')\n return (letters || '?').toUpperCase()\n}\n\n/**\n * A stable hue for one person, so an owner keeps one color across cards and\n * sessions. Hashing the label rather than picking from a palette is what\n * makes the same uid the same color on every machine.\n */\nexport function hueFor(label: string): number {\n let hash = 0\n for (let index = 0; index < label.length; index += 1) {\n hash = (hash * 31 + label.charCodeAt(index)) % 360\n }\n return hash\n}\n\n/** `2026-09-05` for a date input, from epoch ms; `''` for none. */\nexport function dateInputValue(ms: number | null | undefined): string {\n if (typeof ms !== 'number' || !Number.isFinite(ms)) return ''\n const at = new Date(ms)\n const offset = at.getTimezoneOffset() * 60_000\n return new Date(ms - offset).toISOString().slice(0, 10)\n}\n\n/**\n * Epoch ms at local noon of a typed `YYYY-MM-DD`, or `null`.\n *\n * Noon rather than midnight so the day survives a timezone round trip: a\n * close date stored at local midnight and read back one timezone west shows\n * the day before.\n */\nexport function dateInputMs(value: string): number | null {\n const match = /^(\\d{4})-(\\d{2})-(\\d{2})$/.exec(String(value ?? '').trim())\n if (!match) return null\n const at = new Date(\n Number(match[1]),\n Number(match[2]) - 1,\n Number(match[3]),\n 12,\n 0,\n 0,\n 0,\n )\n return Number.isNaN(at.getTime()) ? null : at.getTime()\n}\n"],"names":["DEFAULT_DEAL_STAGES","dealStageById","isCrmForecastCategory","isPipelineArchived","weightedDealAmountCents","DEFAULT_PIPELINE_ID","DEFAULT_PIPELINE_NAME","PIPELINE_NAME_MAX","activePipelines","pipelines","filter","pipeline","defaultPipelineOf","active","find","isDefault","pipelineNameProblem","name","ownId","trimmed","String","trim","taken","$id","toLowerCase","pipelineArchiveRefusal","openDeals","entry","length","toLocaleString","newPipelineDocument","context","slice","stages","map","stage","archivedAt","visibleTo","hostId","createdByUid","uid","createdAt","Date","nowMs","updatedAt","DEAL_CURRENCIES","DEFAULT_DEAL_CURRENCY","DEAL_STATUS_LABELS","open","won","lost","formatMoney","cents","currency","code","toUpperCase","amount","Number","Intl","NumberFormat","undefined","style","format","toFixed","parseAmountInput","input","cleaned","replace","value","isFinite","Math","round","amountInputValue","timestampMs","getTime","seconds","toMillis","call","daysInStage","deal","since","stageChangedAtMs","max","floor","addAmount","totals","boardSummary","deals","summary","openCount","valueByCurrency","weightedByCurrency","status","amountCents","stageId","formatAmountByCurrency","entries","Object","sort","a","b","join","sortedStages","index","order","openStages","kind","closingStage","newStageId","existing","base","Set","id","has","suffix","renumberStages","clampProbability","number","min","addStage","probability","sorted","firstClosed","findIndex","at","forecastCategory","splice","renameStage","setStageProbability","setStageForecastCategory","category","moveStage","direction","target","swapped","moving","stageRemovalRefusal","dealsInStage","removeStage","stagesProblem","ids","add","some","dealEventName","dealEventPayload","dealId","previousStageId","title","ownerUid","contactId","companyId","lostReason","initialsFor","label","words","split","Boolean","letters","word","hueFor","hash","charCodeAt","dateInputValue","ms","offset","getTimezoneOffset","toISOString","dateInputMs","match","exec","isNaN"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;CAKC,GAED,sEAAsE;AACtE,6EAA6E;AAC7E,wEAAwE;AACxE,8BAA8B;AAC9B,SAKEA,mBAAmB,EACnBC,aAAa,EACbC,qBAAqB,EACrBC,kBAAkB,EAClBC,uBAAuB,QAClB,6BAA4B;AAkBnC,kEAAkE,GAClE,OAAO,MAAMC,sBAAsB,UAAS;AAE5C,gDAAgD,GAChD,OAAO,MAAMC,wBAAwB,QAAO;AAE5C,OAAO,MAAMC,oBAAoB,GAAE;AAEnC,qFAAqF,GACrF,OAAO,SAASC,gBACdC,SAAuB;IAEvB,OAAOA,UAAUC,MAAM,CAAC,CAACC,WAAa,CAACR,mBAAmBQ;AAC5D;AAEA;;;;;;;CAOC,GACD,OAAO,SAASC,kBACdH,SAAuB;QAGhBI,MAAAA;IADP,MA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CAAC1G,OAAOY,QAAQ,CAAC8F,KAAK,OAAO;IAC3D,MAAMvC,KAAK,IAAIlF,KAAKyH;IACpB,MAAMC,SAASxC,GAAGyC,iBAAiB,KAAK;IACxC,OAAO,IAAI3H,KAAKyH,KAAKC,QAAQE,WAAW,GAAGtI,KAAK,CAAC,GAAG;AACtD;AAEA;;;;;;CAMC,GACD,OAAO,SAASuI,YAAYnG,KAAa;IACvC,MAAMoG,QAAQ,4BAA4BC,IAAI,CAACrJ,OAAOgD,gBAAAA,QAAS,IAAI/C,IAAI;IACvE,IAAI,CAACmJ,OAAO,OAAO;IACnB,MAAM5C,KAAK,IAAIlF,KACbe,OAAO+G,KAAK,CAAC,EAAE,GACf/G,OAAO+G,KAAK,CAAC,EAAE,IAAI,GACnB/G,OAAO+G,KAAK,CAAC,EAAE,GACf,IACA,GACA,GACA;IAEF,OAAO/G,OAAOiH,KAAK,CAAC9C,GAAGlD,OAAO,MAAM,OAAOkD,GAAGlD,OAAO;AACvD"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Aglyn LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import { type CrmDealContactRole, type CrmPicklist } from '@aglyn/aglyn';
18
+ /** Why a contact cannot be added to the deal, or `null` when it can. */
19
+ export declare function dealContactRoleAddProblem(roles: readonly CrmDealContactRole[], contactId: string): string | null;
20
+ /** `roles` with one contact added — as the Primary, when asked, which every other row then is not. */
21
+ export declare function dealContactRolesAdded(roles: readonly CrmDealContactRole[], added: {
22
+ contactId: string;
23
+ role?: string;
24
+ primary: boolean;
25
+ }): CrmDealContactRole[];
26
+ /** `roles` with one contact's role set — `''` for none. */
27
+ export declare function dealContactRolesWithRole(roles: readonly CrmDealContactRole[], contactId: string, role: string): CrmDealContactRole[];
28
+ /**
29
+ * What a save of the card writes: the roles judged against the org's list
30
+ * (a contact's current role kept), `contactId` as their Primary — `null`
31
+ * for a field the caller deletes — and whether the Primary changed, which
32
+ * is when the name the drawer copied beside `contactId` goes stale.
33
+ */
34
+ export declare function dealContactRolesSave(deal: {
35
+ contactId?: unknown;
36
+ contactRoles?: unknown;
37
+ }, next: readonly CrmDealContactRole[], picklist: CrmPicklist): {
38
+ ok: true;
39
+ contactRoles: CrmDealContactRole[];
40
+ contactId: string | null;
41
+ primaryChanged: boolean;
42
+ } | {
43
+ ok: false;
44
+ error: string;
45
+ };
@@ -0,0 +1,73 @@
1
+ import { _ as _extends } from "@swc/helpers/_/_extends";
2
+ /**
3
+ * @license
4
+ * Copyright 2026 Aglyn LLC
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */ import { DEAL_CONTACT_ROLES_MAX, dealContactRoleFields, dealContactRolesOf, judgeDealContactRoles } from "@aglyn/aglyn";
18
+ /*
19
+ * THE DEAL PAGE'S CONTACT ROLES CARD (AGL-3521), as pure functions: what
20
+ * each edit makes of the list, and the fields one save writes.
21
+ */ /** Why a contact cannot be added to the deal, or `null` when it can. */ export function dealContactRoleAddProblem(roles, contactId) {
22
+ if (!contactId) return 'Pick a contact.';
23
+ if (roles.some((row)=>row.contactId === contactId)) return 'That contact is already on this deal.';
24
+ if (roles.length >= DEAL_CONTACT_ROLES_MAX) {
25
+ return `A deal names at most ${DEAL_CONTACT_ROLES_MAX} contacts.`;
26
+ }
27
+ return null;
28
+ }
29
+ /** `roles` with one contact added — as the Primary, when asked, which every other row then is not. */ export function dealContactRolesAdded(roles, added) {
30
+ const row = _extends({
31
+ contactId: added.contactId
32
+ }, added.role ? {
33
+ role: added.role
34
+ } : {}, {
35
+ primary: added.primary
36
+ });
37
+ return [
38
+ ...roles.map((entry)=>added.primary ? _extends({}, entry, {
39
+ primary: false
40
+ }) : entry),
41
+ row
42
+ ];
43
+ }
44
+ /** `roles` with one contact's role set — `''` for none. */ export function dealContactRolesWithRole(roles, contactId, role) {
45
+ return roles.map((row)=>{
46
+ if (row.contactId !== contactId) return row;
47
+ return _extends({
48
+ contactId: row.contactId
49
+ }, role ? {
50
+ role
51
+ } : {}, {
52
+ primary: row.primary
53
+ });
54
+ });
55
+ }
56
+ /**
57
+ * What a save of the card writes: the roles judged against the org's list
58
+ * (a contact's current role kept), `contactId` as their Primary — `null`
59
+ * for a field the caller deletes — and whether the Primary changed, which
60
+ * is when the name the drawer copied beside `contactId` goes stale.
61
+ */ export function dealContactRolesSave(deal, next, picklist) {
62
+ const judged = judgeDealContactRoles(picklist, next, dealContactRolesOf(deal));
63
+ if (judged.ok === false) return judged;
64
+ const fields = dealContactRoleFields(judged.roles);
65
+ const before = typeof deal.contactId === 'string' && deal.contactId ? deal.contactId : null;
66
+ return _extends({
67
+ ok: true
68
+ }, fields, {
69
+ primaryChanged: fields.contactId !== before
70
+ });
71
+ }
72
+
73
+ //# sourceMappingURL=deal-contact-roles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/model/deal-contact-roles.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n type CrmDealContactRole,\n type CrmPicklist,\n DEAL_CONTACT_ROLES_MAX,\n dealContactRoleFields,\n dealContactRolesOf,\n judgeDealContactRoles,\n} from '@aglyn/aglyn'\n\n/*\n * THE DEAL PAGE'S CONTACT ROLES CARD (AGL-3521), as pure functions: what\n * each edit makes of the list, and the fields one save writes.\n */\n\n/** Why a contact cannot be added to the deal, or `null` when it can. */\nexport function dealContactRoleAddProblem(\n roles: readonly CrmDealContactRole[],\n contactId: string,\n): string | null {\n if (!contactId) return 'Pick a contact.'\n if (roles.some((row) => row.contactId === contactId)) return 'That contact is already on this deal.'\n if (roles.length >= DEAL_CONTACT_ROLES_MAX) {\n return `A deal names at most ${DEAL_CONTACT_ROLES_MAX} contacts.`\n }\n return null\n}\n\n/** `roles` with one contact added — as the Primary, when asked, which every other row then is not. */\nexport function dealContactRolesAdded(\n roles: readonly CrmDealContactRole[],\n added: { contactId: string; role?: string; primary: boolean },\n): CrmDealContactRole[] {\n const row: CrmDealContactRole = {\n contactId: added.contactId,\n ...(added.role ? { role: added.role } : {}),\n primary: added.primary,\n }\n return [...roles.map((entry) => (added.primary ? { ...entry, primary: false } : entry)), row]\n}\n\n/** `roles` with one contact's role set — `''` for none. */\nexport function dealContactRolesWithRole(\n roles: readonly CrmDealContactRole[],\n contactId: string,\n role: string,\n): CrmDealContactRole[] {\n return roles.map((row) => {\n if (row.contactId !== contactId) return row\n return { contactId: row.contactId, ...(role ? { role } : {}), primary: row.primary }\n })\n}\n\n/**\n * What a save of the card writes: the roles judged against the org's list\n * (a contact's current role kept), `contactId` as their Primary — `null`\n * for a field the caller deletes — and whether the Primary changed, which\n * is when the name the drawer copied beside `contactId` goes stale.\n */\nexport function dealContactRolesSave(\n deal: { contactId?: unknown; contactRoles?: unknown },\n next: readonly CrmDealContactRole[],\n picklist: CrmPicklist,\n):\n | {\n ok: true\n contactRoles: CrmDealContactRole[]\n contactId: string | null\n primaryChanged: boolean\n }\n | { ok: false; error: string } {\n const judged = judgeDealContactRoles(picklist, next, dealContactRolesOf(deal))\n if (judged.ok === false) return judged\n const fields = dealContactRoleFields(judged.roles)\n const before = typeof deal.contactId === 'string' && deal.contactId ? deal.contactId : null\n return { ok: true, ...fields, primaryChanged: fields.contactId !== before }\n}\n"],"names":["DEAL_CONTACT_ROLES_MAX","dealContactRoleFields","dealContactRolesOf","judgeDealContactRoles","dealContactRoleAddProblem","roles","contactId","some","row","length","dealContactRolesAdded","added","role","primary","map","entry","dealContactRolesWithRole","dealContactRolesSave","deal","next","picklist","judged","ok","fields","before","primaryChanged"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAGEA,sBAAsB,EACtBC,qBAAqB,EACrBC,kBAAkB,EAClBC,qBAAqB,QAChB,eAAc;AAErB;;;CAGC,GAED,sEAAsE,GACtE,OAAO,SAASC,0BACdC,KAAoC,EACpCC,SAAiB;IAEjB,IAAI,CAACA,WAAW,OAAO;IACvB,IAAID,MAAME,IAAI,CAAC,CAACC,MAAQA,IAAIF,SAAS,KAAKA,YAAY,OAAO;IAC7D,IAAID,MAAMI,MAAM,IAAIT,wBAAwB;QAC1C,OAAO,CAAC,qBAAqB,EAAEA,uBAAuB,UAAU,CAAC;IACnE;IACA,OAAO;AACT;AAEA,oGAAoG,GACpG,OAAO,SAASU,sBACdL,KAAoC,EACpCM,KAA6D;IAE7D,MAAMH,MAA0B;QAC9BF,WAAWK,MAAML,SAAS;OACtBK,MAAMC,IAAI,GAAG;QAAEA,MAAMD,MAAMC,IAAI;IAAC,IAAI,CAAC;QACzCC,SAASF,MAAME,OAAO;;IAExB,OAAO;WAAIR,MAAMS,GAAG,CAAC,CAACC,QAAWJ,MAAME,OAAO,GAAG,aAAKE;gBAAOF,SAAS;iBAAUE;QAASP;KAAI;AAC/F;AAEA,yDAAyD,GACzD,OAAO,SAASQ,yBACdX,KAAoC,EACpCC,SAAiB,EACjBM,IAAY;IAEZ,OAAOP,MAAMS,GAAG,CAAC,CAACN;QAChB,IAAIA,IAAIF,SAAS,KAAKA,WAAW,OAAOE;QACxC,OAAO;YAAEF,WAAWE,IAAIF,SAAS;WAAMM,OAAO;YAAEA;QAAK,IAAI,CAAC;YAAIC,SAASL,IAAIK,OAAO;;IACpF;AACF;AAEA;;;;;CAKC,GACD,OAAO,SAASI,qBACdC,IAAqD,EACrDC,IAAmC,EACnCC,QAAqB;IASrB,MAAMC,SAASlB,sBAAsBiB,UAAUD,MAAMjB,mBAAmBgB;IACxE,IAAIG,OAAOC,EAAE,KAAK,OAAO,OAAOD;IAChC,MAAME,SAAStB,sBAAsBoB,OAAOhB,KAAK;IACjD,MAAMmB,SAAS,OAAON,KAAKZ,SAAS,KAAK,YAAYY,KAAKZ,SAAS,GAAGY,KAAKZ,SAAS,GAAG;IACvF,OAAO;QAAEgB,IAAI;OAASC;QAAQE,gBAAgBF,OAAOjB,SAAS,KAAKkB;;AACrE"}
@@ -14,6 +14,15 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ /**
18
+ * The deal drawer's values and the documents they become (AGL-2598), as pure
19
+ * functions — the drawer holds strings the way inputs do, and this module is
20
+ * the one place those strings turn into the typed fields a `CrmDeal` stores.
21
+ * Kept out of the component so the translation can be tested without a
22
+ * Firestore or a DOM, and so the contact card and the company card, which
23
+ * open the same drawer with a link preselected, build a deal the same way.
24
+ */
25
+ import { type CrmDeal, type CrmDealStage, type CrmForecastCategory, type CrmPicklist } from '@aglyn/aglyn';
17
26
  import { type DealDoc, type PipelineDoc } from './deal-board-model';
18
27
  /** What the drawer edits — strings as the inputs hold them. */
19
28
  export interface DealFormValues {
@@ -31,6 +40,17 @@ export interface DealFormValues {
31
40
  companyId: string;
32
41
  companyName: string;
33
42
  notes: string;
43
+ /** The `opportunityType` label, or empty. */
44
+ type: string;
45
+ /** The `leadSource` label, or empty. */
46
+ leadSource: string;
47
+ nextStep: string;
48
+ /** The override as typed, `35`; empty means the stage's. */
49
+ probability: string;
50
+ /** Empty means the stage's — what a save stamps when nobody picked one. */
51
+ forecastCategory: '' | CrmForecastCategory;
52
+ /** One of the org's campaigns, or empty. */
53
+ campaignId: string;
34
54
  }
35
55
  export declare const DEAL_TITLE_MAX = 120;
36
56
  export declare const DEAL_NOTES_MAX = 4000;
@@ -62,7 +82,34 @@ export interface DealWriteContext {
62
82
  hostId: string;
63
83
  uid: string;
64
84
  nowMs: number;
85
+ /**
86
+ * The stage the deal is in, whose forecast category a form left on
87
+ * "from the stage" stamps (AGL-3516). Absent, the field is left unset and
88
+ * every reader derives it from the stage.
89
+ */
90
+ stage?: Pick<CrmDealStage, 'kind' | 'forecastCategory'> | null;
65
91
  }
92
+ /**
93
+ * The form's two picklist fields judged against the org's lists (AGL-3516),
94
+ * the way every server door judges them: an active value is stored as the
95
+ * list spells it, a blank clears, the deal's own current value is kept, and
96
+ * anything else is refused naming what the list allows. A NEW deal with no
97
+ * Type takes the list's default, as Salesforce's does; the lead source is
98
+ * never defaulted — it records where a deal came from.
99
+ */
100
+ export declare function judgeDealFormPicklists(values: DealFormValues, lists: {
101
+ type: CrmPicklist;
102
+ leadSource: CrmPicklist;
103
+ }, options: {
104
+ current?: Partial<Pick<CrmDeal, 'type' | 'leadSource'>> | null;
105
+ created: boolean;
106
+ }): {
107
+ ok: true;
108
+ values: DealFormValues;
109
+ } | {
110
+ ok: false;
111
+ error: string;
112
+ };
66
113
  /**
67
114
  * The document a new deal is created as. Open, in the chosen stage, with
68
115
  * the stage clock started now so "days in stage" counts from creation.
@@ -81,6 +128,15 @@ export declare function dealDocumentFromForm(values: DealFormValues, context: De
81
128
  */
82
129
  export declare function dealPatchFromForm(values: DealFormValues, nowMs: number, options?: {
83
130
  amountDerived?: boolean;
131
+ stage?: DealWriteContext['stage'];
132
+ /**
133
+ * The deal as stored, whose contact roles a changed contact moves
134
+ * (AGL-3521): the contact picked becomes the Primary — added with no
135
+ * role when the deal did not name them — and a cleared contact leaves
136
+ * the deal with no Primary. An unchanged contact writes no roles, so a
137
+ * role edited on the deal's page meanwhile is not overwritten.
138
+ */
139
+ current?: Pick<CrmDeal, 'contactId' | 'contactRoles'> | null;
84
140
  }): {
85
141
  set: Record<string, unknown>;
86
142
  clear: string[];
@@ -21,7 +21,7 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
21
21
  * Kept out of the component so the translation can be tested without a
22
22
  * Firestore or a DOM, and so the contact card and the company card, which
23
23
  * open the same drawer with a link preselected, build a deal the same way.
24
- */ import { contactDisplayName, nameSearchKey, nameSearchToken } from "@aglyn/aglyn";
24
+ */ import { contactDisplayName, crmPicklistDefaultLabel, DEAL_NEXT_STEP_MAX, dealContactRolesOf, dealContactRolesWithPrimary, dealStageForecastCategory, isCrmForecastCategory, judgeCrmPicklistValue, nameSearchKey, nameSearchToken, readDealProbability } from "@aglyn/aglyn";
25
25
  import { amountInputValue, dateInputMs, dateInputValue, DEFAULT_DEAL_CURRENCY, openStages, parseAmountInput } from "./deal-board-model.js";
26
26
  export const DEAL_TITLE_MAX = 120;
27
27
  export const DEAL_NOTES_MAX = 4000;
@@ -47,11 +47,17 @@ export const DEAL_NOTES_MAX = 4000;
47
47
  contactName: '',
48
48
  companyId: '',
49
49
  companyName: '',
50
- notes: ''
50
+ notes: '',
51
+ type: '',
52
+ leadSource: '',
53
+ nextStep: '',
54
+ probability: '',
55
+ forecastCategory: '',
56
+ campaignId: ''
51
57
  }, defaults);
52
58
  }
53
59
  /** The form an existing deal opens as. */ export function dealFormFromDoc(deal) {
54
- var _deal_title, _deal_pipelineId, _deal_stageId, _deal_ownerUid, _deal_contactId, _deal_contactName, _deal_companyId, _deal_companyName, _deal_notes;
60
+ var _deal_title, _deal_pipelineId, _deal_stageId, _deal_ownerUid, _deal_contactId, _deal_contactName, _deal_companyId, _deal_companyName, _deal_notes, _deal_type, _deal_leadSource, _deal_nextStep, _deal_campaignId;
55
61
  return {
56
62
  title: String((_deal_title = deal.title) != null ? _deal_title : ''),
57
63
  pipelineId: String((_deal_pipelineId = deal.pipelineId) != null ? _deal_pipelineId : ''),
@@ -64,7 +70,13 @@ export const DEAL_NOTES_MAX = 4000;
64
70
  contactName: String((_deal_contactName = deal.contactName) != null ? _deal_contactName : ''),
65
71
  companyId: String((_deal_companyId = deal.companyId) != null ? _deal_companyId : ''),
66
72
  companyName: String((_deal_companyName = deal.companyName) != null ? _deal_companyName : ''),
67
- notes: String((_deal_notes = deal.notes) != null ? _deal_notes : '')
73
+ notes: String((_deal_notes = deal.notes) != null ? _deal_notes : ''),
74
+ type: String((_deal_type = deal.type) != null ? _deal_type : ''),
75
+ leadSource: String((_deal_leadSource = deal.leadSource) != null ? _deal_leadSource : ''),
76
+ nextStep: String((_deal_nextStep = deal.nextStep) != null ? _deal_nextStep : ''),
77
+ probability: typeof deal.probability === 'number' ? String(deal.probability) : '',
78
+ forecastCategory: isCrmForecastCategory(deal.forecastCategory) ? deal.forecastCategory : '',
79
+ campaignId: String((_deal_campaignId = deal.campaignId) != null ? _deal_campaignId : '')
68
80
  };
69
81
  }
70
82
  /**
@@ -84,11 +96,41 @@ export const DEAL_NOTES_MAX = 4000;
84
96
  if (values.expectedClose.trim() && dateInputMs(values.expectedClose) === null) {
85
97
  return 'The expected close has to be a date.';
86
98
  }
99
+ if (readDealProbability(values.probability) === undefined) {
100
+ return 'The probability has to be a whole number from 0 to 100.';
101
+ }
87
102
  if (mode === 'create' && (!values.pipelineId || !values.stageId)) {
88
103
  return 'Pick the stage the deal starts in.';
89
104
  }
90
105
  return null;
91
106
  }
107
+ /**
108
+ * The form's two picklist fields judged against the org's lists (AGL-3516),
109
+ * the way every server door judges them: an active value is stored as the
110
+ * list spells it, a blank clears, the deal's own current value is kept, and
111
+ * anything else is refused naming what the list allows. A NEW deal with no
112
+ * Type takes the list's default, as Salesforce's does; the lead source is
113
+ * never defaulted — it records where a deal came from.
114
+ */ export function judgeDealFormPicklists(values, lists, options) {
115
+ var _crmPicklistDefaultLabel, _type_value, _leadSource_value;
116
+ var _options_current, _options_current1;
117
+ const typed = values.type.trim() || (options.created ? (_crmPicklistDefaultLabel = crmPicklistDefaultLabel(lists.type)) != null ? _crmPicklistDefaultLabel : '' : '');
118
+ const type = judgeCrmPicklistValue('opportunityType', lists.type, typed, (_options_current = options.current) == null ? void 0 : _options_current.type);
119
+ if (type.ok === false) return type;
120
+ const leadSource = judgeCrmPicklistValue('leadSource', lists.leadSource, values.leadSource, (_options_current1 = options.current) == null ? void 0 : _options_current1.leadSource);
121
+ if (leadSource.ok === false) return leadSource;
122
+ return {
123
+ ok: true,
124
+ values: _extends({}, values, {
125
+ type: (_type_value = type.value) != null ? _type_value : '',
126
+ leadSource: (_leadSource_value = leadSource.value) != null ? _leadSource_value : ''
127
+ })
128
+ };
129
+ }
130
+ /** The forecast category a save stamps: the form's, else the stage's, else none. */ function forecastCategoryFor(values, stage) {
131
+ if (isCrmForecastCategory(values.forecastCategory)) return values.forecastCategory;
132
+ return stage ? dealStageForecastCategory(stage) : null;
133
+ }
92
134
  /**
93
135
  * The optional fields, as the values to SET and the keys to CLEAR.
94
136
  *
@@ -99,6 +141,7 @@ export const DEAL_NOTES_MAX = 4000;
99
141
  * either. On a create the cleared keys are simply not written; on an edit
100
142
  * the component turns them into field deletes.
101
143
  */ function optionalFields(values) {
144
+ var _readDealProbability;
102
145
  const set = {};
103
146
  const clear = [];
104
147
  const put = (key, value)=>{
@@ -113,6 +156,11 @@ export const DEAL_NOTES_MAX = 4000;
113
156
  put('companyId', values.companyId.trim());
114
157
  put('companyName', values.companyId.trim() ? values.companyName.trim() : '');
115
158
  put('notes', values.notes.trim().slice(0, DEAL_NOTES_MAX));
159
+ put('type', values.type.trim());
160
+ put('leadSource', values.leadSource.trim());
161
+ put('nextStep', values.nextStep.trim().replace(/\s+/g, ' ').slice(0, DEAL_NEXT_STEP_MAX));
162
+ put('probability', (_readDealProbability = readDealProbability(values.probability)) != null ? _readDealProbability : null);
163
+ put('campaignId', values.campaignId.trim());
116
164
  return {
117
165
  set,
118
166
  clear
@@ -124,15 +172,27 @@ export const DEAL_NOTES_MAX = 4000;
124
172
  */ export function dealDocumentFromForm(values, context) {
125
173
  const title = values.title.trim().slice(0, DEAL_TITLE_MAX);
126
174
  const { set } = optionalFields(values);
175
+ const forecastCategory = forecastCategoryFor(values, context.stage);
176
+ const contactId = values.contactId.trim();
127
177
  return _extends({
128
178
  title,
129
179
  titleLower: nameSearchKey(title),
130
180
  pipelineId: values.pipelineId,
131
181
  stageId: values.stageId,
132
- status: 'open',
182
+ status: 'open'
183
+ }, contactId ? {
184
+ contactRoles: [
185
+ {
186
+ contactId,
187
+ primary: true
188
+ }
189
+ ]
190
+ } : {}, {
133
191
  currency: String(values.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),
134
192
  stageChangedAtMs: context.nowMs
135
- }, set, {
193
+ }, forecastCategory ? {
194
+ forecastCategory
195
+ } : {}, set, {
136
196
  visibleTo: context.visibleTo,
137
197
  hostId: context.hostId,
138
198
  createdByUid: context.uid,
@@ -151,8 +211,20 @@ export const DEAL_NOTES_MAX = 4000;
151
211
  * form shows them read-only, and a patch that re-wrote the number it was
152
212
  * seeded with would overwrite a line added from another tab meanwhile.
153
213
  */ export function dealPatchFromForm(values, nowMs, options = {}) {
214
+ var _options_current_contactId;
154
215
  const title = values.title.trim().slice(0, DEAL_TITLE_MAX);
155
- const { set, clear } = optionalFields(values);
216
+ const optional = optionalFields(values);
217
+ const set = optional.set;
218
+ const { clear } = optional;
219
+ const contactId = values.contactId.trim();
220
+ if (options.current && contactId !== ((_options_current_contactId = options.current.contactId) != null ? _options_current_contactId : '')) {
221
+ set['contactRoles'] = dealContactRolesWithPrimary(dealContactRolesOf(options.current), contactId || null);
222
+ }
223
+ // The category picked, else the stage's again; with neither, the field
224
+ // goes and every reader derives it from the stage.
225
+ const forecastCategory = forecastCategoryFor(values, options.stage);
226
+ if (forecastCategory) set['forecastCategory'] = forecastCategory;
227
+ else clear.push('forecastCategory');
156
228
  if (options.amountDerived) {
157
229
  delete set['amountCents'];
158
230
  return {