@fantastic.dev/better-stripe 0.23.2 → 0.25.0

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 (358) hide show
  1. package/CHANGELOG.md +27 -2
  2. package/README.md +46 -5
  3. package/dist/client/affiliateRecovery/disputes.d.ts +18 -0
  4. package/dist/client/affiliateRecovery/disputes.d.ts.map +1 -0
  5. package/dist/client/affiliateRecovery/disputes.js +33 -0
  6. package/dist/client/affiliateRecovery/disputes.js.map +1 -0
  7. package/dist/client/affiliateRecovery/engine.d.ts +29 -0
  8. package/dist/client/affiliateRecovery/engine.d.ts.map +1 -0
  9. package/dist/client/affiliateRecovery/engine.js +116 -0
  10. package/dist/client/affiliateRecovery/engine.js.map +1 -0
  11. package/dist/client/affiliateRecovery/payoutGate.d.ts +24 -0
  12. package/dist/client/affiliateRecovery/payoutGate.d.ts.map +1 -0
  13. package/dist/client/affiliateRecovery/payoutGate.js +30 -0
  14. package/dist/client/affiliateRecovery/payoutGate.js.map +1 -0
  15. package/dist/client/affiliateRecovery/provider.d.ts +14 -0
  16. package/dist/client/affiliateRecovery/provider.d.ts.map +1 -0
  17. package/dist/client/affiliateRecovery/provider.js +96 -0
  18. package/dist/client/affiliateRecovery/provider.js.map +1 -0
  19. package/dist/client/affiliateRecovery/review.d.ts +28 -0
  20. package/dist/client/affiliateRecovery/review.d.ts.map +1 -0
  21. package/dist/client/affiliateRecovery/review.js +86 -0
  22. package/dist/client/affiliateRecovery/review.js.map +1 -0
  23. package/dist/client/affiliateRecovery/validators.d.ts +402 -0
  24. package/dist/client/affiliateRecovery/validators.d.ts.map +1 -0
  25. package/dist/client/affiliateRecovery/validators.js +27 -0
  26. package/dist/client/affiliateRecovery/validators.js.map +1 -0
  27. package/dist/client/affiliateRecovery.d.ts +131 -0
  28. package/dist/client/affiliateRecovery.d.ts.map +1 -0
  29. package/dist/client/affiliateRecovery.js +69 -0
  30. package/dist/client/affiliateRecovery.js.map +1 -0
  31. package/dist/client/billing/checkout.d.ts +3 -43
  32. package/dist/client/billing/checkout.d.ts.map +1 -1
  33. package/dist/client/billing/checkout.js +5 -260
  34. package/dist/client/billing/checkout.js.map +1 -1
  35. package/dist/client/billing/checkoutRequest.d.ts +129 -0
  36. package/dist/client/billing/checkoutRequest.d.ts.map +1 -0
  37. package/dist/client/billing/checkoutRequest.js +263 -0
  38. package/dist/client/billing/checkoutRequest.js.map +1 -0
  39. package/dist/client/billing/preparedCheckout.d.ts +22 -0
  40. package/dist/client/billing/preparedCheckout.d.ts.map +1 -0
  41. package/dist/client/billing/preparedCheckout.js +86 -0
  42. package/dist/client/billing/preparedCheckout.js.map +1 -0
  43. package/dist/client/checkoutRequests.d.ts +80 -0
  44. package/dist/client/checkoutRequests.d.ts.map +1 -0
  45. package/dist/client/checkoutRequests.js +28 -0
  46. package/dist/client/checkoutRequests.js.map +1 -0
  47. package/dist/client/connect/commissionReceipts.d.ts +4 -0
  48. package/dist/client/connect/commissionReceipts.d.ts.map +1 -0
  49. package/dist/client/connect/commissionReceipts.js +17 -0
  50. package/dist/client/connect/commissionReceipts.js.map +1 -0
  51. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +958 -2
  52. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  53. package/dist/client/connect/payoutCollection.d.ts +21 -0
  54. package/dist/client/connect/payoutCollection.d.ts.map +1 -0
  55. package/dist/client/connect/payoutCollection.js +47 -0
  56. package/dist/client/connect/payoutCollection.js.map +1 -0
  57. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +1805 -0
  58. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -0
  59. package/dist/client/connect/payoutExecutionTestHelpers.js +194 -0
  60. package/dist/client/connect/payoutExecutionTestHelpers.js.map +1 -0
  61. package/dist/client/connect/payoutSyncTestHelpers.d.ts +479 -1
  62. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  63. package/dist/client/connect/payoutWorker.d.ts +48 -0
  64. package/dist/client/connect/payoutWorker.d.ts.map +1 -0
  65. package/dist/client/connect/payoutWorker.js +78 -0
  66. package/dist/client/connect/payoutWorker.js.map +1 -0
  67. package/dist/client/connect/payouts.d.ts +3 -1
  68. package/dist/client/connect/payouts.d.ts.map +1 -1
  69. package/dist/client/connect/payouts.js +45 -12
  70. package/dist/client/connect/payouts.js.map +1 -1
  71. package/dist/client/connect/refundPrincipal.d.ts +4 -0
  72. package/dist/client/connect/refundPrincipal.d.ts.map +1 -0
  73. package/dist/client/connect/refundPrincipal.js +21 -0
  74. package/dist/client/connect/refundPrincipal.js.map +1 -0
  75. package/dist/client/connect/reversalExecution.d.ts +47 -0
  76. package/dist/client/connect/reversalExecution.d.ts.map +1 -0
  77. package/dist/client/connect/reversalExecution.js +84 -0
  78. package/dist/client/connect/reversalExecution.js.map +1 -0
  79. package/dist/client/connect/transfers.d.ts +8 -4
  80. package/dist/client/connect/transfers.d.ts.map +1 -1
  81. package/dist/client/connect/transfers.js +52 -57
  82. package/dist/client/connect/transfers.js.map +1 -1
  83. package/dist/client/index.d.ts +7 -11
  84. package/dist/client/index.d.ts.map +1 -1
  85. package/dist/client/index.js +10 -10
  86. package/dist/client/index.js.map +1 -1
  87. package/dist/client/payoutPolicy.d.ts +67 -0
  88. package/dist/client/payoutPolicy.d.ts.map +1 -1
  89. package/dist/client/payoutPolicy.js +10 -0
  90. package/dist/client/payoutPolicy.js.map +1 -1
  91. package/dist/client/readContracts.d.ts +11 -1
  92. package/dist/client/readContracts.d.ts.map +1 -1
  93. package/dist/client/readContracts.js +10 -0
  94. package/dist/client/readContracts.js.map +1 -1
  95. package/dist/client/settlement/execution.d.ts.map +1 -1
  96. package/dist/client/settlement/execution.js +30 -3
  97. package/dist/client/settlement/execution.js.map +1 -1
  98. package/dist/client/settlement/reconciliation.d.ts.map +1 -1
  99. package/dist/client/settlement/reconciliation.js +16 -2
  100. package/dist/client/settlement/reconciliation.js.map +1 -1
  101. package/dist/client/webhooks/disputeRecovery.d.ts +4 -0
  102. package/dist/client/webhooks/disputeRecovery.d.ts.map +1 -0
  103. package/dist/client/webhooks/disputeRecovery.js +40 -0
  104. package/dist/client/webhooks/disputeRecovery.js.map +1 -0
  105. package/dist/client/webhooks/processors.d.ts.map +1 -1
  106. package/dist/client/webhooks/processors.js +14 -29
  107. package/dist/client/webhooks/processors.js.map +1 -1
  108. package/dist/component/_generated/component.d.ts +784 -0
  109. package/dist/component/_generated/component.d.ts.map +1 -1
  110. package/dist/component/affiliateEarnings/mutations.d.ts +22 -0
  111. package/dist/component/affiliateEarnings/mutations.d.ts.map +1 -0
  112. package/dist/component/affiliateEarnings/mutations.js +160 -0
  113. package/dist/component/affiliateEarnings/mutations.js.map +1 -0
  114. package/dist/component/affiliatePayouts/mutations.d.ts +25 -0
  115. package/dist/component/affiliatePayouts/mutations.d.ts.map +1 -0
  116. package/dist/component/affiliatePayouts/mutations.js +104 -0
  117. package/dist/component/affiliatePayouts/mutations.js.map +1 -0
  118. package/dist/component/affiliatePayouts/queries.d.ts +23 -0
  119. package/dist/component/affiliatePayouts/queries.d.ts.map +1 -0
  120. package/dist/component/affiliatePayouts/queries.js +23 -0
  121. package/dist/component/affiliatePayouts/queries.js.map +1 -0
  122. package/dist/component/affiliatePayouts/recipientGuard.d.ts +12 -0
  123. package/dist/component/affiliatePayouts/recipientGuard.d.ts.map +1 -0
  124. package/dist/component/affiliatePayouts/recipientGuard.js +26 -0
  125. package/dist/component/affiliatePayouts/recipientGuard.js.map +1 -0
  126. package/dist/component/affiliateRecovery/accounting.d.ts +28 -0
  127. package/dist/component/affiliateRecovery/accounting.d.ts.map +1 -0
  128. package/dist/component/affiliateRecovery/accounting.js +71 -0
  129. package/dist/component/affiliateRecovery/accounting.js.map +1 -0
  130. package/dist/component/affiliateRecovery/mutations.d.ts +65 -0
  131. package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -0
  132. package/dist/component/affiliateRecovery/mutations.js +250 -0
  133. package/dist/component/affiliateRecovery/mutations.js.map +1 -0
  134. package/dist/component/affiliateRecovery/queries.d.ts +305 -0
  135. package/dist/component/affiliateRecovery/queries.d.ts.map +1 -0
  136. package/dist/component/affiliateRecovery/queries.js +113 -0
  137. package/dist/component/affiliateRecovery/queries.js.map +1 -0
  138. package/dist/component/affiliateRecovery/schema.d.ts +171 -0
  139. package/dist/component/affiliateRecovery/schema.d.ts.map +1 -0
  140. package/dist/component/affiliateRecovery/schema.js +71 -0
  141. package/dist/component/affiliateRecovery/schema.js.map +1 -0
  142. package/dist/component/connect/mutations.d.ts +36 -23
  143. package/dist/component/connect/mutations.d.ts.map +1 -1
  144. package/dist/component/connect/mutations.js +7 -124
  145. package/dist/component/connect/mutations.js.map +1 -1
  146. package/dist/component/connect/queries.d.ts +10 -0
  147. package/dist/component/connect/queries.d.ts.map +1 -1
  148. package/dist/component/connect/queries.js +3 -40
  149. package/dist/component/connect/queries.js.map +1 -1
  150. package/dist/component/connect/reversalClaims.d.ts +169 -0
  151. package/dist/component/connect/reversalClaims.d.ts.map +1 -0
  152. package/dist/component/connect/reversalClaims.js +176 -0
  153. package/dist/component/connect/reversalClaims.js.map +1 -0
  154. package/dist/component/connect/reversalDiagnostics.d.ts +59 -0
  155. package/dist/component/connect/reversalDiagnostics.d.ts.map +1 -0
  156. package/dist/component/connect/reversalDiagnostics.js +50 -0
  157. package/dist/component/connect/reversalDiagnostics.js.map +1 -0
  158. package/dist/component/connect/reversalReceipts.d.ts +27 -0
  159. package/dist/component/connect/reversalReceipts.d.ts.map +1 -0
  160. package/dist/component/connect/reversalReceipts.js +85 -0
  161. package/dist/component/connect/reversalReceipts.js.map +1 -0
  162. package/dist/component/connect/schema.d.ts +31 -1
  163. package/dist/component/connect/schema.d.ts.map +1 -1
  164. package/dist/component/connect/schema.js +3 -1
  165. package/dist/component/connect/schema.js.map +1 -1
  166. package/dist/component/connect/validators.d.ts +49 -1
  167. package/dist/component/connect/validators.d.ts.map +1 -1
  168. package/dist/component/connect/validators.js +2 -0
  169. package/dist/component/connect/validators.js.map +1 -1
  170. package/dist/component/core/mutations.d.ts.map +1 -1
  171. package/dist/component/core/mutations.js +2 -0
  172. package/dist/component/core/mutations.js.map +1 -1
  173. package/dist/component/core/resetSafety.d.ts +4 -0
  174. package/dist/component/core/resetSafety.d.ts.map +1 -0
  175. package/dist/component/core/resetSafety.js +54 -0
  176. package/dist/component/core/resetSafety.js.map +1 -0
  177. package/dist/component/earnings/triggers.d.ts +828 -2
  178. package/dist/component/earnings/triggers.d.ts.map +1 -1
  179. package/dist/component/earnings/triggers.js +2 -0
  180. package/dist/component/earnings/triggers.js.map +1 -1
  181. package/dist/component/payoutPolicy/schema.d.ts +2 -0
  182. package/dist/component/payoutPolicy/schema.d.ts.map +1 -1
  183. package/dist/component/payoutPolicy/schema.js +3 -1
  184. package/dist/component/payoutPolicy/schema.js.map +1 -1
  185. package/dist/component/payoutRelease/mutations.d.ts.map +1 -1
  186. package/dist/component/payoutRelease/mutations.js +7 -1
  187. package/dist/component/payoutRelease/mutations.js.map +1 -1
  188. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  189. package/dist/component/payoutRelease/queries.js +3 -1
  190. package/dist/component/payoutRelease/queries.js.map +1 -1
  191. package/dist/component/payoutRelease/schema.d.ts +1 -0
  192. package/dist/component/payoutRelease/schema.d.ts.map +1 -1
  193. package/dist/component/payoutRelease/schema.js +1 -0
  194. package/dist/component/payoutRelease/schema.js.map +1 -1
  195. package/dist/component/payoutSnapshots/localState.d.ts +89 -0
  196. package/dist/component/payoutSnapshots/localState.d.ts.map +1 -0
  197. package/dist/component/payoutSnapshots/localState.js +186 -0
  198. package/dist/component/payoutSnapshots/localState.js.map +1 -0
  199. package/dist/component/payoutSnapshots/mutations.d.ts +108 -0
  200. package/dist/component/payoutSnapshots/mutations.d.ts.map +1 -0
  201. package/dist/component/payoutSnapshots/mutations.js +263 -0
  202. package/dist/component/payoutSnapshots/mutations.js.map +1 -0
  203. package/dist/component/payoutSnapshots/revisions.d.ts +12 -0
  204. package/dist/component/payoutSnapshots/revisions.d.ts.map +1 -0
  205. package/dist/component/payoutSnapshots/revisions.js +173 -0
  206. package/dist/component/payoutSnapshots/revisions.js.map +1 -0
  207. package/dist/component/payoutSnapshots/schema.d.ts +285 -0
  208. package/dist/component/payoutSnapshots/schema.d.ts.map +1 -0
  209. package/dist/component/payoutSnapshots/schema.js +108 -0
  210. package/dist/component/payoutSnapshots/schema.js.map +1 -0
  211. package/dist/component/payoutSnapshots/scope.d.ts +7 -0
  212. package/dist/component/payoutSnapshots/scope.d.ts.map +1 -0
  213. package/dist/component/payoutSnapshots/scope.js +8 -0
  214. package/dist/component/payoutSnapshots/scope.js.map +1 -0
  215. package/dist/component/payoutWorker/helpers.d.ts +30 -0
  216. package/dist/component/payoutWorker/helpers.d.ts.map +1 -0
  217. package/dist/component/payoutWorker/helpers.js +21 -0
  218. package/dist/component/payoutWorker/helpers.js.map +1 -0
  219. package/dist/component/payoutWorker/mutations.d.ts +68 -0
  220. package/dist/component/payoutWorker/mutations.d.ts.map +1 -0
  221. package/dist/component/payoutWorker/mutations.js +161 -0
  222. package/dist/component/payoutWorker/mutations.js.map +1 -0
  223. package/dist/component/payoutWorker/queries.d.ts +123 -0
  224. package/dist/component/payoutWorker/queries.d.ts.map +1 -0
  225. package/dist/component/payoutWorker/queries.js +61 -0
  226. package/dist/component/payoutWorker/queries.js.map +1 -0
  227. package/dist/component/payoutWorker/schema.d.ts +91 -0
  228. package/dist/component/payoutWorker/schema.d.ts.map +1 -0
  229. package/dist/component/payoutWorker/schema.js +27 -0
  230. package/dist/component/payoutWorker/schema.js.map +1 -0
  231. package/dist/component/schema.d.ts +479 -1
  232. package/dist/component/schema.js +19 -0
  233. package/dist/component/schema.js.map +1 -1
  234. package/dist/component/settlement/clawbackSchema.d.ts +161 -0
  235. package/dist/component/settlement/clawbackSchema.d.ts.map +1 -0
  236. package/dist/component/settlement/clawbackSchema.js +73 -0
  237. package/dist/component/settlement/clawbackSchema.js.map +1 -0
  238. package/dist/component/settlement/clawbacks.d.ts +55 -0
  239. package/dist/component/settlement/clawbacks.d.ts.map +1 -0
  240. package/dist/component/settlement/clawbacks.js +146 -0
  241. package/dist/component/settlement/clawbacks.js.map +1 -0
  242. package/dist/component/settlement/mutations.d.ts +16 -0
  243. package/dist/component/settlement/mutations.d.ts.map +1 -1
  244. package/dist/component/settlement/mutations.js +48 -0
  245. package/dist/component/settlement/mutations.js.map +1 -1
  246. package/dist/component/settlement/queries.d.ts +53 -0
  247. package/dist/component/settlement/queries.d.ts.map +1 -1
  248. package/dist/component/settlement/queries.js +26 -0
  249. package/dist/component/settlement/queries.js.map +1 -1
  250. package/dist/react/workflows/AffiliateRecovery.d.ts +46 -0
  251. package/dist/react/workflows/AffiliateRecovery.d.ts.map +1 -0
  252. package/dist/react/workflows/AffiliateRecovery.js +84 -0
  253. package/dist/react/workflows/AffiliateRecovery.js.map +1 -0
  254. package/dist/react/workflows/index.d.ts +2 -0
  255. package/dist/react/workflows/index.d.ts.map +1 -1
  256. package/dist/react/workflows/index.js +1 -0
  257. package/dist/react/workflows/index.js.map +1 -1
  258. package/dist/testing/affiliate-recovery.d.ts +10 -0
  259. package/dist/testing/affiliate-recovery.d.ts.map +1 -0
  260. package/dist/testing/affiliate-recovery.js +126 -0
  261. package/dist/testing/affiliate-recovery.js.map +1 -0
  262. package/dist/testing/index.d.ts +2 -0
  263. package/dist/testing/index.d.ts.map +1 -1
  264. package/dist/testing/index.js +1 -0
  265. package/dist/testing/index.js.map +1 -1
  266. package/docs/affiliate-clawbacks.md +306 -0
  267. package/docs/read-contracts.md +48 -0
  268. package/docs/settlement.md +145 -1
  269. package/package.json +2 -1
  270. package/src/client/affiliateRecovery/disputes.ts +62 -0
  271. package/src/client/affiliateRecovery/engine.ts +196 -0
  272. package/src/client/affiliateRecovery/payoutGate.ts +65 -0
  273. package/src/client/affiliateRecovery/provider.ts +127 -0
  274. package/src/client/affiliateRecovery/review.ts +117 -0
  275. package/src/client/affiliateRecovery/validators.ts +38 -0
  276. package/src/client/affiliateRecovery.ts +173 -0
  277. package/src/client/billing/checkout.ts +11 -359
  278. package/src/client/billing/checkoutRequest.ts +362 -0
  279. package/src/client/billing/prepared-checkout.test.ts +430 -0
  280. package/src/client/billing/preparedCheckout.ts +143 -0
  281. package/src/client/checkoutRequests.ts +40 -0
  282. package/src/client/connect/commissionReceipts.test.ts +157 -0
  283. package/src/client/connect/commissionReceipts.ts +23 -0
  284. package/src/client/connect/payoutCollection.test.ts +74 -0
  285. package/src/client/connect/payoutCollection.ts +82 -0
  286. package/src/client/connect/payoutExecution.test.ts +1 -211
  287. package/src/client/connect/payoutExecutionTestHelpers.ts +213 -0
  288. package/src/client/connect/payoutWorker.test.ts +145 -0
  289. package/src/client/connect/payoutWorker.ts +144 -0
  290. package/src/client/connect/payouts.ts +75 -15
  291. package/src/client/connect/refundPrincipal.test.ts +46 -0
  292. package/src/client/connect/refundPrincipal.ts +26 -0
  293. package/src/client/connect/reversalExecution.ts +130 -0
  294. package/src/client/connect/transfers.test.ts +3 -3
  295. package/src/client/connect/transfers.ts +93 -92
  296. package/src/client/index.ts +16 -28
  297. package/src/client/payoutPolicy.ts +26 -1
  298. package/src/client/readContracts.ts +23 -0
  299. package/src/client/settlement/execution.test.ts +5 -0
  300. package/src/client/settlement/execution.ts +50 -3
  301. package/src/client/settlement/reconciliation.ts +20 -2
  302. package/src/client/webhooks/disputeRecovery.ts +74 -0
  303. package/src/client/webhooks/processors.test.ts +36 -53
  304. package/src/client/webhooks/processors.ts +18 -31
  305. package/src/component/_generated/api.ts +40 -0
  306. package/src/component/_generated/component.ts +975 -1
  307. package/src/component/affiliate-clawbacks.test.ts +370 -0
  308. package/src/component/affiliate-component-pagination.test.ts +83 -0
  309. package/src/component/affiliate-direct-payout.test.ts +160 -0
  310. package/src/component/affiliate-recovery-lifecycle.test.ts +587 -0
  311. package/src/component/affiliateEarnings/mutations.ts +196 -0
  312. package/src/component/affiliatePayouts/mutations.ts +121 -0
  313. package/src/component/affiliatePayouts/queries.ts +28 -0
  314. package/src/component/affiliatePayouts/recipientGuard.ts +42 -0
  315. package/src/component/affiliateRecovery/accounting.ts +111 -0
  316. package/src/component/affiliateRecovery/mutations.ts +275 -0
  317. package/src/component/affiliateRecovery/queries.ts +138 -0
  318. package/src/component/affiliateRecovery/schema.ts +86 -0
  319. package/src/component/connect/mutations.ts +9 -160
  320. package/src/component/connect/queries.ts +4 -47
  321. package/src/component/connect/reversalClaims.ts +232 -0
  322. package/src/component/connect/reversalDiagnostics.ts +61 -0
  323. package/src/component/connect/reversalReceipts.ts +147 -0
  324. package/src/component/connect/schema.ts +3 -3
  325. package/src/component/connect/validators.ts +2 -0
  326. package/src/component/core/mutations.ts +2 -0
  327. package/src/component/core/resetSafety.ts +67 -0
  328. package/src/component/earnings/triggers.ts +2 -0
  329. package/src/component/payout-recovery-integration.test.ts +172 -0
  330. package/src/component/payout-reset-safety.test.ts +148 -0
  331. package/src/component/payout-revisions.test.ts +125 -0
  332. package/src/component/payout-snapshots.test.ts +395 -0
  333. package/src/component/payout-worker.test.ts +165 -0
  334. package/src/component/payoutPolicy/schema.ts +3 -1
  335. package/src/component/payoutRelease/mutations.ts +10 -1
  336. package/src/component/payoutRelease/queries.ts +3 -1
  337. package/src/component/payoutRelease/schema.ts +1 -0
  338. package/src/component/payoutSnapshots/localState.ts +238 -0
  339. package/src/component/payoutSnapshots/mutations.ts +275 -0
  340. package/src/component/payoutSnapshots/revisions.ts +247 -0
  341. package/src/component/payoutSnapshots/schema.ts +125 -0
  342. package/src/component/payoutSnapshots/scope.ts +7 -0
  343. package/src/component/payoutWorker/helpers.ts +30 -0
  344. package/src/component/payoutWorker/mutations.ts +181 -0
  345. package/src/component/payoutWorker/queries.ts +64 -0
  346. package/src/component/payoutWorker/schema.ts +26 -0
  347. package/src/component/public.test.ts +2 -2
  348. package/src/component/schema.ts +36 -0
  349. package/src/component/settlement/clawbackSchema.ts +78 -0
  350. package/src/component/settlement/clawbacks.ts +233 -0
  351. package/src/component/settlement/mutations.ts +54 -0
  352. package/src/component/settlement/queries.ts +35 -0
  353. package/src/component/summary-arithmetic.test.ts +9 -4
  354. package/src/react/workflows/AffiliateRecovery.test.tsx +106 -0
  355. package/src/react/workflows/AffiliateRecovery.tsx +273 -0
  356. package/src/react/workflows/index.ts +9 -0
  357. package/src/testing/affiliate-recovery.ts +137 -0
  358. package/src/testing/index.ts +2 -0
@@ -0,0 +1,113 @@
1
+ import { paginator } from "convex-helpers/server/pagination";
2
+ import schema from "../schema.js";
3
+ import { paginationOptsValidator, paginationResultValidator, } from "convex/server";
4
+ import { v } from "convex/values";
5
+ import { query } from "../_generated/server.js";
6
+ import { obligationFields } from "../settlement/clawbackSchema.js";
7
+ import { balanceFields, operationFields, earningFields } from "./schema.js";
8
+ export const obligationDoc = v.object({
9
+ ...obligationFields,
10
+ _id: v.id("affiliateObligations"),
11
+ _creationTime: v.number(),
12
+ });
13
+ export const getObligation = query({
14
+ args: { obligationId: v.id("affiliateObligations") },
15
+ returns: v.union(obligationDoc, v.null()),
16
+ handler: (ctx, args) => ctx.db.get("affiliateObligations", args.obligationId),
17
+ });
18
+ export const chargeObligations = query({
19
+ args: { sourceChargeId: v.string() },
20
+ returns: v.array(obligationDoc),
21
+ handler: async (ctx, args) => {
22
+ const rows = await ctx.db
23
+ .query("affiliateObligations")
24
+ .withIndex("by_charge", (q) => q.eq("sourceChargeId", args.sourceChargeId))
25
+ .take(1001);
26
+ if (rows.length > 1000)
27
+ throw new Error("Affiliate charge recovery limit exceeded.");
28
+ return rows;
29
+ },
30
+ });
31
+ export const getEarning = query({
32
+ args: { sourceChargeId: v.string(), affiliateAccountId: v.string() },
33
+ returns: v.union(v.object({
34
+ ...earningFields,
35
+ _id: v.id("affiliateEarnings"),
36
+ _creationTime: v.number(),
37
+ }), v.null()),
38
+ handler: (ctx, args) => ctx.db
39
+ .query("affiliateEarnings")
40
+ .withIndex("by_charge_affiliate", (q) => q
41
+ .eq("sourceChargeId", args.sourceChargeId)
42
+ .eq("affiliateAccountId", args.affiliateAccountId))
43
+ .unique(),
44
+ });
45
+ export const recoverySnapshot = query({
46
+ args: {
47
+ affiliateAccountId: v.string(),
48
+ currency: v.string(),
49
+ paginationOpts: paginationOptsValidator,
50
+ },
51
+ returns: v.object({
52
+ balance: v.union(v.object({
53
+ ...balanceFields,
54
+ _id: v.id("affiliateRecoveryBalances"),
55
+ _creationTime: v.number(),
56
+ }), v.null()),
57
+ obligations: paginationResultValidator(obligationDoc),
58
+ }),
59
+ handler: async (ctx, args) => ({
60
+ balance: await ctx.db
61
+ .query("affiliateRecoveryBalances")
62
+ .withIndex("by_affiliate_currency", (q) => q
63
+ .eq("affiliateAccountId", args.affiliateAccountId)
64
+ .eq("currency", args.currency))
65
+ .unique(),
66
+ obligations: await paginator(ctx.db, schema)
67
+ .query("affiliateObligations")
68
+ .withIndex("by_affiliate_currency", (q) => q
69
+ .eq("affiliateAccountId", args.affiliateAccountId)
70
+ .eq("currency", args.currency))
71
+ .paginate({
72
+ ...args.paginationOpts,
73
+ numItems: Math.min(100, args.paginationOpts.numItems),
74
+ }),
75
+ }),
76
+ });
77
+ export const operationHistory = query({
78
+ args: {
79
+ obligationId: v.id("affiliateObligations"),
80
+ paginationOpts: paginationOptsValidator,
81
+ },
82
+ returns: paginationResultValidator(v.object({
83
+ ...operationFields,
84
+ _id: v.id("affiliateRecoveryOperations"),
85
+ _creationTime: v.number(),
86
+ })),
87
+ handler: (ctx, args) => paginator(ctx.db, schema)
88
+ .query("affiliateRecoveryOperations")
89
+ .withIndex("by_obligation", (q) => q.eq("obligationId", args.obligationId))
90
+ .paginate({
91
+ ...args.paginationOpts,
92
+ numItems: Math.min(100, args.paginationOpts.numItems),
93
+ }),
94
+ });
95
+ export const pendingOperations = query({
96
+ args: {
97
+ paginationOpts: paginationOptsValidator,
98
+ state: v.union(v.literal("pending"), v.literal("unknown")),
99
+ },
100
+ returns: paginationResultValidator(v.object({
101
+ ...operationFields,
102
+ _id: v.id("affiliateRecoveryOperations"),
103
+ _creationTime: v.number(),
104
+ })),
105
+ handler: (ctx, args) => paginator(ctx.db, schema)
106
+ .query("affiliateRecoveryOperations")
107
+ .withIndex("by_state", (q) => q.eq("state", args.state))
108
+ .paginate({
109
+ ...args.paginationOpts,
110
+ numItems: Math.min(50, args.paginationOpts.numItems),
111
+ }),
112
+ });
113
+ //# sourceMappingURL=queries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../../src/component/affiliateRecovery/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAC7D,OAAO,MAAM,MAAM,cAAc,CAAC;AAClC,OAAO,EACL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5E,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,GAAG,gBAAgB;IACnB,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,sBAAsB,CAAC;IACjC,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;CAC1B,CAAC,CAAC;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAC;IACjC,IAAI,EAAE,EAAE,YAAY,EAAE,CAAC,CAAC,EAAE,CAAC,sBAAsB,CAAC,EAAE;IACpD,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,aAAa,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACzC,OAAO,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,sBA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C,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;KAC3D;IACD,OAAO,EAAE,yBAAyB,CAChC,CAAC,CAAC,MAAM,CAAC;QACP,GAAG,eAAe;QAClB,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,6BAA6B,CAAC;QACxC,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;KAC1B,CAAC,CACH;IACD,OAAO,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CACrB,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC;SACtB,KAAK,CAAC,6BAA6B,CAAC;SACpC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;SACvD,QAAQ,CAAC;QACR,GAAG,IAAI,CAAC,cAAc;QACtB,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC;KACrD,CAAC;CACP,CAAC,CAAC"}
@@ -0,0 +1,171 @@
1
+ export declare const recoveryState: import("convex/values").VUnion<"canceled" | "failed" | "insufficient_balance" | "pending" | "succeeded" | "unknown" | "unsupported", [import("convex/values").VLiteral<"pending", "required">, import("convex/values").VLiteral<"unknown", "required">, import("convex/values").VLiteral<"insufficient_balance", "required">, import("convex/values").VLiteral<"unsupported", "required">, import("convex/values").VLiteral<"failed", "required">, import("convex/values").VLiteral<"succeeded", "required">, import("convex/values").VLiteral<"canceled", "required">], "required", never>;
2
+ export declare const operationFields: {
3
+ obligationId: import("convex/values").VId<import("convex/values").GenericId<"affiliateObligations">, "required">;
4
+ sourceEventId: import("convex/values").VString<string | undefined, "optional">;
5
+ kind: import("convex/values").VUnion<"recovery" | "restoration", [import("convex/values").VLiteral<"recovery", "required">, import("convex/values").VLiteral<"restoration", "required">], "required", never>;
6
+ amount: import("convex/values").VFloat64<number, "required">;
7
+ state: import("convex/values").VUnion<"canceled" | "failed" | "insufficient_balance" | "pending" | "succeeded" | "unknown" | "unsupported", [import("convex/values").VLiteral<"pending", "required">, import("convex/values").VLiteral<"unknown", "required">, import("convex/values").VLiteral<"insufficient_balance", "required">, import("convex/values").VLiteral<"unsupported", "required">, import("convex/values").VLiteral<"failed", "required">, import("convex/values").VLiteral<"succeeded", "required">, import("convex/values").VLiteral<"canceled", "required">], "required", never>;
8
+ receiptId: import("convex/values").VString<string | undefined, "optional">;
9
+ error: import("convex/values").VString<string | undefined, "optional">;
10
+ attempted: import("convex/values").VBoolean<boolean, "required">;
11
+ providerBaseline: import("convex/values").VFloat64<number | undefined, "optional">;
12
+ createdAt: import("convex/values").VFloat64<number, "required">;
13
+ };
14
+ export declare const operationsTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
15
+ amount: number;
16
+ attempted: boolean;
17
+ createdAt: number;
18
+ error?: string | undefined;
19
+ kind: "recovery" | "restoration";
20
+ obligationId: import("convex/values").GenericId<"affiliateObligations">;
21
+ providerBaseline?: number | undefined;
22
+ receiptId?: string | undefined;
23
+ sourceEventId?: string | undefined;
24
+ state: "canceled" | "failed" | "insufficient_balance" | "pending" | "succeeded" | "unknown" | "unsupported";
25
+ }, {
26
+ obligationId: import("convex/values").VId<import("convex/values").GenericId<"affiliateObligations">, "required">;
27
+ sourceEventId: import("convex/values").VString<string | undefined, "optional">;
28
+ kind: import("convex/values").VUnion<"recovery" | "restoration", [import("convex/values").VLiteral<"recovery", "required">, import("convex/values").VLiteral<"restoration", "required">], "required", never>;
29
+ amount: import("convex/values").VFloat64<number, "required">;
30
+ state: import("convex/values").VUnion<"canceled" | "failed" | "insufficient_balance" | "pending" | "succeeded" | "unknown" | "unsupported", [import("convex/values").VLiteral<"pending", "required">, import("convex/values").VLiteral<"unknown", "required">, import("convex/values").VLiteral<"insufficient_balance", "required">, import("convex/values").VLiteral<"unsupported", "required">, import("convex/values").VLiteral<"failed", "required">, import("convex/values").VLiteral<"succeeded", "required">, import("convex/values").VLiteral<"canceled", "required">], "required", never>;
31
+ receiptId: import("convex/values").VString<string | undefined, "optional">;
32
+ error: import("convex/values").VString<string | undefined, "optional">;
33
+ attempted: import("convex/values").VBoolean<boolean, "required">;
34
+ providerBaseline: import("convex/values").VFloat64<number | undefined, "optional">;
35
+ createdAt: import("convex/values").VFloat64<number, "required">;
36
+ }, "required", "amount" | "attempted" | "createdAt" | "error" | "kind" | "obligationId" | "providerBaseline" | "receiptId" | "sourceEventId" | "state">, {
37
+ by_obligation: ["obligationId", "_creationTime"];
38
+ by_state: ["state", "_creationTime"];
39
+ }, {}, {}>;
40
+ export declare const earningFields: {
41
+ sourceChargeId: import("convex/values").VString<string, "required">;
42
+ affiliateAccountId: import("convex/values").VString<string, "required">;
43
+ currency: import("convex/values").VString<string, "required">;
44
+ originalCommission: import("convex/values").VFloat64<number, "required">;
45
+ withheldAmount: import("convex/values").VFloat64<number, "required">;
46
+ releaseAmount: import("convex/values").VFloat64<number, "required">;
47
+ sourceInvoiceId: import("convex/values").VString<string | undefined, "optional">;
48
+ paymentIntentId: import("convex/values").VString<string | undefined, "optional">;
49
+ stripeTransferId: import("convex/values").VString<string | undefined, "optional">;
50
+ status: import("convex/values").VUnion<"paid" | "reserved", [import("convex/values").VLiteral<"reserved", "required">, import("convex/values").VLiteral<"paid", "required">], "required", never>;
51
+ };
52
+ export declare const earningsTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
53
+ affiliateAccountId: string;
54
+ currency: string;
55
+ originalCommission: number;
56
+ paymentIntentId?: string | undefined;
57
+ releaseAmount: number;
58
+ sourceChargeId: string;
59
+ sourceInvoiceId?: string | undefined;
60
+ status: "paid" | "reserved";
61
+ stripeTransferId?: string | undefined;
62
+ withheldAmount: number;
63
+ }, {
64
+ sourceChargeId: import("convex/values").VString<string, "required">;
65
+ affiliateAccountId: import("convex/values").VString<string, "required">;
66
+ currency: import("convex/values").VString<string, "required">;
67
+ originalCommission: import("convex/values").VFloat64<number, "required">;
68
+ withheldAmount: import("convex/values").VFloat64<number, "required">;
69
+ releaseAmount: import("convex/values").VFloat64<number, "required">;
70
+ sourceInvoiceId: import("convex/values").VString<string | undefined, "optional">;
71
+ paymentIntentId: import("convex/values").VString<string | undefined, "optional">;
72
+ stripeTransferId: import("convex/values").VString<string | undefined, "optional">;
73
+ status: import("convex/values").VUnion<"paid" | "reserved", [import("convex/values").VLiteral<"reserved", "required">, import("convex/values").VLiteral<"paid", "required">], "required", never>;
74
+ }, "required", "affiliateAccountId" | "currency" | "originalCommission" | "paymentIntentId" | "releaseAmount" | "sourceChargeId" | "sourceInvoiceId" | "status" | "stripeTransferId" | "withheldAmount">, {
75
+ by_affiliate_currency: ["affiliateAccountId", "currency", "_creationTime"];
76
+ by_charge_affiliate: ["sourceChargeId", "affiliateAccountId", "_creationTime"];
77
+ }, {}, {}>;
78
+ export declare const offsetsTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
79
+ amount: number;
80
+ earningId: import("convex/values").GenericId<"affiliateEarnings">;
81
+ obligationId: import("convex/values").GenericId<"affiliateObligations">;
82
+ }, {
83
+ earningId: import("convex/values").VId<import("convex/values").GenericId<"affiliateEarnings">, "required">;
84
+ obligationId: import("convex/values").VId<import("convex/values").GenericId<"affiliateObligations">, "required">;
85
+ amount: import("convex/values").VFloat64<number, "required">;
86
+ }, "required", "amount" | "earningId" | "obligationId">, {
87
+ by_earning: ["earningId", "_creationTime"];
88
+ by_obligation: ["obligationId", "_creationTime"];
89
+ }, {}, {}>;
90
+ export declare const balanceFields: {
91
+ activePayoutKey: import("convex/values").VString<string | undefined, "optional">;
92
+ pendingPayoutAmount: import("convex/values").VFloat64<number | undefined, "optional">;
93
+ affiliateAccountId: import("convex/values").VString<string, "required">;
94
+ currency: import("convex/values").VString<string, "required">;
95
+ originalCommission: import("convex/values").VFloat64<number, "required">;
96
+ recoveredAmount: import("convex/values").VFloat64<number, "required">;
97
+ offsetAmount: import("convex/values").VFloat64<number, "required">;
98
+ restoredAmount: import("convex/values").VFloat64<number, "required">;
99
+ outstandingDebt: import("convex/values").VFloat64<number, "required">;
100
+ reservedRecovery: import("convex/values").VFloat64<number, "required">;
101
+ reservedRestoration: import("convex/values").VFloat64<number, "required">;
102
+ restorationDue: import("convex/values").VFloat64<number, "required">;
103
+ withheldAmount: import("convex/values").VFloat64<number, "required">;
104
+ netPayable: import("convex/values").VFloat64<number, "required">;
105
+ };
106
+ export declare const balancesTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
107
+ activePayoutKey?: string | undefined;
108
+ affiliateAccountId: string;
109
+ currency: string;
110
+ netPayable: number;
111
+ offsetAmount: number;
112
+ originalCommission: number;
113
+ outstandingDebt: number;
114
+ pendingPayoutAmount?: number | undefined;
115
+ recoveredAmount: number;
116
+ reservedRecovery: number;
117
+ reservedRestoration: number;
118
+ restorationDue: number;
119
+ restoredAmount: number;
120
+ withheldAmount: number;
121
+ }, {
122
+ activePayoutKey: import("convex/values").VString<string | undefined, "optional">;
123
+ pendingPayoutAmount: import("convex/values").VFloat64<number | undefined, "optional">;
124
+ affiliateAccountId: import("convex/values").VString<string, "required">;
125
+ currency: import("convex/values").VString<string, "required">;
126
+ originalCommission: import("convex/values").VFloat64<number, "required">;
127
+ recoveredAmount: import("convex/values").VFloat64<number, "required">;
128
+ offsetAmount: import("convex/values").VFloat64<number, "required">;
129
+ restoredAmount: import("convex/values").VFloat64<number, "required">;
130
+ outstandingDebt: import("convex/values").VFloat64<number, "required">;
131
+ reservedRecovery: import("convex/values").VFloat64<number, "required">;
132
+ reservedRestoration: import("convex/values").VFloat64<number, "required">;
133
+ restorationDue: import("convex/values").VFloat64<number, "required">;
134
+ withheldAmount: import("convex/values").VFloat64<number, "required">;
135
+ netPayable: import("convex/values").VFloat64<number, "required">;
136
+ }, "required", "activePayoutKey" | "affiliateAccountId" | "currency" | "netPayable" | "offsetAmount" | "originalCommission" | "outstandingDebt" | "pendingPayoutAmount" | "recoveredAmount" | "reservedRecovery" | "reservedRestoration" | "restorationDue" | "restoredAmount" | "withheldAmount">, {
137
+ by_affiliate_currency: ["affiliateAccountId", "currency", "_creationTime"];
138
+ }, {}, {}>;
139
+ export declare const payoutReleaseFields: {
140
+ affiliateAccountId: import("convex/values").VString<string, "required">;
141
+ currency: import("convex/values").VString<string, "required">;
142
+ operationKey: import("convex/values").VString<string, "required">;
143
+ amount: import("convex/values").VFloat64<number, "required">;
144
+ minimumAmount: import("convex/values").VFloat64<number, "required">;
145
+ state: import("convex/values").VUnion<"failed" | "pending" | "submitted" | "unknown", [import("convex/values").VLiteral<"pending", "required">, import("convex/values").VLiteral<"unknown", "required">, import("convex/values").VLiteral<"submitted", "required">, import("convex/values").VLiteral<"failed", "required">], "required", never>;
146
+ stripePayoutId: import("convex/values").VString<string | undefined, "optional">;
147
+ createdAt: import("convex/values").VFloat64<number, "required">;
148
+ };
149
+ export declare const payoutReleasesTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
150
+ affiliateAccountId: string;
151
+ amount: number;
152
+ createdAt: number;
153
+ currency: string;
154
+ minimumAmount: number;
155
+ operationKey: string;
156
+ state: "failed" | "pending" | "submitted" | "unknown";
157
+ stripePayoutId?: string | undefined;
158
+ }, {
159
+ affiliateAccountId: import("convex/values").VString<string, "required">;
160
+ currency: import("convex/values").VString<string, "required">;
161
+ operationKey: import("convex/values").VString<string, "required">;
162
+ amount: import("convex/values").VFloat64<number, "required">;
163
+ minimumAmount: import("convex/values").VFloat64<number, "required">;
164
+ state: import("convex/values").VUnion<"failed" | "pending" | "submitted" | "unknown", [import("convex/values").VLiteral<"pending", "required">, import("convex/values").VLiteral<"unknown", "required">, import("convex/values").VLiteral<"submitted", "required">, import("convex/values").VLiteral<"failed", "required">], "required", never>;
165
+ stripePayoutId: import("convex/values").VString<string | undefined, "optional">;
166
+ createdAt: import("convex/values").VFloat64<number, "required">;
167
+ }, "required", "affiliateAccountId" | "amount" | "createdAt" | "currency" | "minimumAmount" | "operationKey" | "state" | "stripePayoutId">, {
168
+ by_key: ["operationKey", "_creationTime"];
169
+ by_state: ["state", "_creationTime"];
170
+ }, {}, {}>;
171
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/component/affiliateRecovery/schema.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,6jBAQzB,CAAC;AACF,eAAO,MAAM,eAAe;IAC1B,YAAY;IACZ,aAAa;IACb,IAAI;IACJ,MAAM;IACN,KAAK;IACL,SAAS;IACT,KAAK;IACL,SAAS;IACT,gBAAgB;IAChB,SAAS;CACV,CAAC;AACF,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;UAEG,CAAC;AAChC,eAAO,MAAM,aAAa;IACxB,cAAc;IACd,kBAAkB;IAClB,QAAQ;IACR,kBAAkB;IAClB,cAAc;IACd,aAAa;IACb,eAAe;IACf,eAAe;IACf,gBAAgB;IAChB,MAAM;CACP,CAAC;AACF,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;UAE2C,CAAC;AACtE,eAAO,MAAM,YAAY;;;;;;;;;;;UAMkB,CAAC;AAC5C,eAAO,MAAM,aAAa;IACxB,eAAe;IACf,mBAAmB;IACnB,kBAAkB;IAClB,QAAQ;IACR,kBAAkB;IAClB,eAAe;IACf,YAAY;IACZ,cAAc;IACd,eAAe;IACf,gBAAgB;IAChB,mBAAmB;IACnB,cAAc;IACd,cAAc;IACd,UAAU;CACX,CAAC;AACF,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAGzB,CAAC;AACF,eAAO,MAAM,mBAAmB;IAC9B,kBAAkB;IAClB,QAAQ;IACR,YAAY;IACZ,MAAM;IACN,aAAa;IACb,KAAK;IAML,cAAc;IACd,SAAS;CACV,CAAC;AACF,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;UAED,CAAC"}
@@ -0,0 +1,71 @@
1
+ import { defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ export const recoveryState = v.union(v.literal("pending"), v.literal("unknown"), v.literal("insufficient_balance"), v.literal("unsupported"), v.literal("failed"), v.literal("succeeded"), v.literal("canceled"));
4
+ export const operationFields = {
5
+ obligationId: v.id("affiliateObligations"),
6
+ sourceEventId: v.optional(v.string()),
7
+ kind: v.union(v.literal("recovery"), v.literal("restoration")),
8
+ amount: v.number(),
9
+ state: recoveryState,
10
+ receiptId: v.optional(v.string()),
11
+ error: v.optional(v.string()),
12
+ attempted: v.boolean(),
13
+ providerBaseline: v.optional(v.number()),
14
+ createdAt: v.number(),
15
+ };
16
+ export const operationsTable = defineTable(operationFields)
17
+ .index("by_obligation", ["obligationId"])
18
+ .index("by_state", ["state"]);
19
+ export const earningFields = {
20
+ sourceChargeId: v.string(),
21
+ affiliateAccountId: v.string(),
22
+ currency: v.string(),
23
+ originalCommission: v.number(),
24
+ withheldAmount: v.number(),
25
+ releaseAmount: v.number(),
26
+ sourceInvoiceId: v.optional(v.string()),
27
+ paymentIntentId: v.optional(v.string()),
28
+ stripeTransferId: v.optional(v.string()),
29
+ status: v.union(v.literal("reserved"), v.literal("paid")),
30
+ };
31
+ export const earningsTable = defineTable(earningFields)
32
+ .index("by_charge_affiliate", ["sourceChargeId", "affiliateAccountId"])
33
+ .index("by_affiliate_currency", ["affiliateAccountId", "currency"]);
34
+ export const offsetsTable = defineTable({
35
+ earningId: v.id("affiliateEarnings"),
36
+ obligationId: v.id("affiliateObligations"),
37
+ amount: v.number(),
38
+ })
39
+ .index("by_earning", ["earningId"])
40
+ .index("by_obligation", ["obligationId"]);
41
+ export const balanceFields = {
42
+ activePayoutKey: v.optional(v.string()),
43
+ pendingPayoutAmount: v.optional(v.number()),
44
+ affiliateAccountId: v.string(),
45
+ currency: v.string(),
46
+ originalCommission: v.number(),
47
+ recoveredAmount: v.number(),
48
+ offsetAmount: v.number(),
49
+ restoredAmount: v.number(),
50
+ outstandingDebt: v.number(),
51
+ reservedRecovery: v.number(),
52
+ reservedRestoration: v.number(),
53
+ restorationDue: v.number(),
54
+ withheldAmount: v.number(),
55
+ netPayable: v.number(),
56
+ };
57
+ export const balancesTable = defineTable(balanceFields).index("by_affiliate_currency", ["affiliateAccountId", "currency"]);
58
+ export const payoutReleaseFields = {
59
+ affiliateAccountId: v.string(),
60
+ currency: v.string(),
61
+ operationKey: v.string(),
62
+ amount: v.number(),
63
+ minimumAmount: v.number(),
64
+ state: v.union(v.literal("pending"), v.literal("unknown"), v.literal("submitted"), v.literal("failed")),
65
+ stripePayoutId: v.optional(v.string()),
66
+ createdAt: v.number(),
67
+ };
68
+ export const payoutReleasesTable = defineTable(payoutReleaseFields)
69
+ .index("by_key", ["operationKey"])
70
+ .index("by_state", ["state"]);
71
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/component/affiliateRecovery/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAClC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,sBAAsB,CAAC,EACjC,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC,EACxB,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EACnB,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,EACtB,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CACtB,CAAC;AACF,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,YAAY,EAAE,CAAC,CAAC,EAAE,CAAC,sBAAsB,CAAC;IAC1C,aAAa,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACrC,IAAI,EAAE,CAAC,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EAAE,CAAC,CAAC,MAAM,EAAE;IAC1B,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;IAC1B,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;CACvB,CAAC;AACF,MAAM,CAAC,MAAM,aAAa,GAAG,WAAW,CAAC,aAAa,CAAC,CAAC,KAAK,CAC3D,uBAAuB,EACvB,CAAC,oBAAoB,EAAE,UAAU,CAAC,CACnC,CAAC;AACF,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,kBAAkB,EAAE,CAAC,CAAC,MAAM,EAAE;IAC9B,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE;IACxB,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE;IAClB,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;IACzB,KAAK,EAAE,CAAC,CAAC,KAAK,CACZ,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,EACtB,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CACpB;IACD,cAAc,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACtC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC;AACF,MAAM,CAAC,MAAM,mBAAmB,GAAG,WAAW,CAAC,mBAAmB,CAAC;KAChE,KAAK,CAAC,QAAQ,EAAE,CAAC,cAAc,CAAC,CAAC;KACjC,KAAK,CAAC,UAAU,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC"}
@@ -99,29 +99,18 @@ export declare const upsertTransfer: import("convex/server").RegisteredMutation<
99
99
  * is the new cumulative total (matches Stripe's `Transfer.amount_reversed`).
100
100
  */
101
101
  export declare const recordTransferReversal: import("convex/server").RegisteredMutation<"public", {
102
- reversedAmount: number;
103
102
  stripeTransferId: string;
103
+ reversedAmount: number;
104
+ receipt?: {
105
+ stripeReversalId: string;
106
+ operationId: string;
107
+ amount: number;
108
+ };
104
109
  }, Promise<null>>;
105
- /**
106
- * Atomically claim reversal slices for one operation (a dispute or refund
107
- * clawback) BEFORE any money moves (BTS-63). Runs in a single transaction, so
108
- * concurrent operations serialize here instead of racing between the ledger
109
- * read and the Stripe call:
110
- *
111
- * - First claim for an `operationId`: computes slices against each leg's
112
- * claim frontier (`max(reversedAmount, reversalClaimedAmount)`), advances
113
- * the frontier, and records the slices — atomically. A concurrent distinct
114
- * operation claiming the same target gets an EMPTY claim, not a duplicate.
115
- * - Any later call with the same `operationId` (webhook redelivery, retry
116
- * after a partial failure) replays the recorded slices verbatim, so the
117
- * caller re-sends byte-identical Stripe params under the same idempotency
118
- * keys no matter how the ledger moved in between.
119
- *
120
- * Each returned slice carries the leg's current CONFIRMED `reversedAmount` so
121
- * the executor can skip slices whose money already moved and was recorded.
122
- */
123
110
  export declare const claimReversalSlices: import("convex/server").RegisteredMutation<"public", {
124
- mode: {
111
+ operationId: string;
112
+ sourceChargeId: string;
113
+ mode: import("convex/values").Infer<import("convex/values").VUnion<{
125
114
  kind: "full";
126
115
  } | {
127
116
  kind: "percent";
@@ -133,13 +122,37 @@ export declare const claimReversalSlices: import("convex/server").RegisteredMuta
133
122
  amountRefunded: number;
134
123
  chargeAmount: number;
135
124
  kind: "fraction";
136
- };
137
- operationId: string;
138
- sourceChargeId: string;
125
+ }, [import("convex/values").VObject<{
126
+ kind: "full";
127
+ }, {
128
+ kind: import("convex/values").VLiteral<"full", "required">;
129
+ }, "required", "kind">, import("convex/values").VObject<{
130
+ kind: "percent";
131
+ percent: number;
132
+ }, {
133
+ kind: import("convex/values").VLiteral<"percent", "required">;
134
+ percent: import("convex/values").VFloat64<number, "required">;
135
+ }, "required", "kind" | "percent">, import("convex/values").VObject<{
136
+ amount: number;
137
+ kind: "amount";
138
+ }, {
139
+ kind: import("convex/values").VLiteral<"amount", "required">;
140
+ amount: import("convex/values").VFloat64<number, "required">;
141
+ }, "required", "amount" | "kind">, import("convex/values").VObject<{
142
+ amountRefunded: number;
143
+ chargeAmount: number;
144
+ kind: "fraction";
145
+ }, {
146
+ kind: import("convex/values").VLiteral<"fraction", "required">;
147
+ chargeAmount: import("convex/values").VFloat64<number, "required">;
148
+ amountRefunded: import("convex/values").VFloat64<number, "required">;
149
+ }, "required", "amountRefunded" | "chargeAmount" | "kind">], "required", "amount" | "amountRefunded" | "chargeAmount" | "kind" | "percent">>;
150
+ commissionExposure?: import("convex/values").Infer<typeof import("../settlement/clawbackSchema.js").commissionExposureValidator>;
139
151
  }, Promise<{
140
152
  replay: boolean;
141
153
  slices: {
142
154
  confirmed: number;
155
+ commissionTracked?: boolean | undefined;
143
156
  from: number;
144
157
  stripeTransferId: string;
145
158
  to: number;
@@ -1 +1 @@
1
- {"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../../src/component/connect/mutations.ts"],"names":[],"mappings":"AAkBA,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;iBA6FxB,CAAC;AAIH,eAAO,MAAM,YAAY;;;;;;;;;;;iBAOvB,CAAC;AAIH,eAAO,MAAM,YAAY;;;;;;;;;;;iBAiEvB,CAAC;AAEH;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,sBAAsB;;;;iBAwDjC,CAAC;AAIH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;iBAsBxB,CAAC;AAIH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;iBAmCzB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,sBAAsB;;;iBAqCjC,CAAC;AAcH;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;GA0F9B,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;0BA+EM,MAAM;YAAM,MAAM;;GAWvD,CAAC"}
1
+ {"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../../src/component/connect/mutations.ts"],"names":[],"mappings":"AAqBA,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;iBA6FxB,CAAC;AAIH,eAAO,MAAM,YAAY;;;;;;;;;;;iBAOvB,CAAC;AAIH,eAAO,MAAM,YAAY;;;;;;;;;;;iBAiEvB,CAAC;AAEH;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,sBAAsB;;;;iBAwDjC,CAAC;AAIH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;iBAsBxB,CAAC;AAIH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;iBAoCzB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,sBAAsB;;;;;;;;iBAElC,CAAC;AAEF,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAA0C,CAAC;AAE3E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;0BA+EM,MAAM;YAAM,MAAM;;GAWvD,CAAC"}
@@ -1,8 +1,11 @@
1
+ import { syncCommissionTransfer } from "../settlement/clawbacks.js";
2
+ import { recordTransferReversalDefinition } from "./reversalReceipts.js";
3
+ import { claimReversalSlicesDefinition } from "./reversalClaims.js";
1
4
  import { writePayout } from "./payoutWrites.js";
2
5
  import { v } from "convex/values";
3
6
  import { mutation } from "../earnings/triggers.js";
4
7
  import { feeRoutingFields } from "../lib/fees";
5
- import { computeReversalSlices, deriveReversalState } from "../lib/reversals";
8
+ import { deriveReversalState } from "../lib/reversals";
6
9
  import { paymentSeller } from "../lib/paymentSeller";
7
10
  import { disputeFields, paymentStatusValidator, payoutFields, refundFields, transferFields, } from "./validators";
8
11
  // Payment mutations
@@ -239,6 +242,7 @@ export const upsertTransfer = mutation({
239
242
  // shrink reversedAmount or flip a fully-reversed row back to paid.
240
243
  const reversedAmount = Math.max(existing.reversedAmount ?? 0, args.reversedAmount ?? 0);
241
244
  const derived = deriveReversalState(args.amount, reversedAmount, args.status);
245
+ await syncCommissionTransfer(ctx, { ...existing, ...args, ...derived });
242
246
  await ctx.db.patch("transfers", existing._id, { ...args, ...derived });
243
247
  }
244
248
  else {
@@ -253,129 +257,8 @@ export const upsertTransfer = mutation({
253
257
  * cumulative `reversedAmount` and a derived `reversalStatus`. `reversedAmount`
254
258
  * is the new cumulative total (matches Stripe's `Transfer.amount_reversed`).
255
259
  */
256
- export const recordTransferReversal = mutation({
257
- args: {
258
- stripeTransferId: v.string(),
259
- reversedAmount: v.number(),
260
- },
261
- returns: v.null(),
262
- handler: async (ctx, args) => {
263
- const transfer = await ctx.db
264
- .query("transfers")
265
- .withIndex("by_stripe_transfer_id", (q) => q.eq("stripeTransferId", args.stripeTransferId))
266
- .first();
267
- if (!transfer) {
268
- throw new Error(`Cannot record reversal: no transfer found for ${args.stripeTransferId}`);
269
- }
270
- // `reversedAmount` is a cumulative total (matches Stripe's
271
- // `Transfer.amount_reversed`). Keep it monotonic so a stale/out-of-order
272
- // event can't shrink it or undo a full reversal.
273
- const reversedAmount = Math.max(transfer.reversedAmount ?? 0, args.reversedAmount);
274
- const derived = deriveReversalState(transfer.amount, reversedAmount, transfer.status);
275
- await ctx.db.patch("transfers", transfer._id, derived);
276
- return null;
277
- },
278
- });
279
- /** How `claimReversalSlices` sizes each leg's slice. */
280
- const reversalModeValidator = v.union(v.object({ kind: v.literal("full") }), v.object({ kind: v.literal("percent"), percent: v.number() }), v.object({ kind: v.literal("amount"), amount: v.number() }), v.object({
281
- kind: v.literal("fraction"),
282
- chargeAmount: v.number(),
283
- amountRefunded: v.number(),
284
- }));
285
- /**
286
- * Atomically claim reversal slices for one operation (a dispute or refund
287
- * clawback) BEFORE any money moves (BTS-63). Runs in a single transaction, so
288
- * concurrent operations serialize here instead of racing between the ledger
289
- * read and the Stripe call:
290
- *
291
- * - First claim for an `operationId`: computes slices against each leg's
292
- * claim frontier (`max(reversedAmount, reversalClaimedAmount)`), advances
293
- * the frontier, and records the slices — atomically. A concurrent distinct
294
- * operation claiming the same target gets an EMPTY claim, not a duplicate.
295
- * - Any later call with the same `operationId` (webhook redelivery, retry
296
- * after a partial failure) replays the recorded slices verbatim, so the
297
- * caller re-sends byte-identical Stripe params under the same idempotency
298
- * keys no matter how the ledger moved in between.
299
- *
300
- * Each returned slice carries the leg's current CONFIRMED `reversedAmount` so
301
- * the executor can skip slices whose money already moved and was recorded.
302
- */
303
- export const claimReversalSlices = mutation({
304
- args: {
305
- operationId: v.string(),
306
- sourceChargeId: v.string(),
307
- mode: reversalModeValidator,
308
- },
309
- returns: v.object({
310
- replay: v.boolean(),
311
- slices: v.array(v.object({
312
- stripeTransferId: v.string(),
313
- from: v.number(),
314
- to: v.number(),
315
- confirmed: v.number(),
316
- })),
317
- }),
318
- handler: async (ctx, args) => {
319
- const confirmedFor = async (stripeTransferId) => {
320
- const row = await ctx.db
321
- .query("transfers")
322
- .withIndex("by_stripe_transfer_id", (q) => q.eq("stripeTransferId", stripeTransferId))
323
- .first();
324
- return row?.reversedAmount ?? 0;
325
- };
326
- const existingOp = await ctx.db
327
- .query("transferReversalOps")
328
- .withIndex("by_operation_id", (q) => q.eq("operationId", args.operationId))
329
- .first();
330
- if (existingOp) {
331
- const slices = [];
332
- for (const s of existingOp.slices) {
333
- slices.push({
334
- ...s,
335
- confirmed: await confirmedFor(s.stripeTransferId),
336
- });
337
- }
338
- return { replay: true, slices };
339
- }
340
- // Original split legs only — reinstatement payouts are audit rows and
341
- // must never be re-reversed (mirrors listTransfersByCharge).
342
- // Claim every original split leg, including those beyond the former
343
- // 50-row preview. Read/write limits abort the transaction before a partial
344
- // claim can commit; no Stripe call happens until this mutation succeeds.
345
- const rows = [];
346
- for await (const transfer of ctx.db
347
- .query("transfers")
348
- .withIndex("by_source_charge_id", (q) => q.eq("sourceChargeId", args.sourceChargeId))) {
349
- if (!transfer.reinstatement)
350
- rows.push(transfer);
351
- }
352
- const byId = new Map(rows.map((r) => [r.stripeTransferId, r]));
353
- const slices = computeReversalSlices(rows.map((t) => ({
354
- stripeTransferId: t.stripeTransferId,
355
- amount: t.amount,
356
- frontier: Math.max(t.reversedAmount ?? 0, t.reversalClaimedAmount ?? 0),
357
- })), args.mode);
358
- for (const s of slices) {
359
- await ctx.db.patch("transfers", byId.get(s.stripeTransferId)._id, {
360
- reversalClaimedAmount: s.to,
361
- });
362
- }
363
- // Record the operation even when it claims nothing, so a redelivery is a
364
- // deterministic no-op instead of a recomputation against newer state.
365
- await ctx.db.insert("transferReversalOps", {
366
- operationId: args.operationId,
367
- sourceChargeId: args.sourceChargeId,
368
- slices,
369
- });
370
- return {
371
- replay: false,
372
- slices: slices.map((s) => ({
373
- ...s,
374
- confirmed: byId.get(s.stripeTransferId).reversedAmount ?? 0,
375
- })),
376
- };
377
- },
378
- });
260
+ export const recordTransferReversal = mutation(recordTransferReversalDefinition);
261
+ export const claimReversalSlices = mutation(claimReversalSlicesDefinition);
379
262
  /**
380
263
  * Release a permanently-dead reversal claim (BTS-74): rewind each of the
381
264
  * operation's legs' claim frontier back to the pre-claim amount and delete the