@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,395 @@
1
+ // @vitest-environment edge-runtime
2
+ import { convexTest } from "convex-test";
3
+ import { makeFunctionReference } from "convex/server";
4
+ import { expect, it } from "vitest";
5
+ import type { Doc } from "./_generated/dataModel.js";
6
+ import schema from "./schema.js";
7
+ import { transferFingerprint } from "./payoutRelease/helpers.js";
8
+ const modules = import.meta.glob("./**/*.*s");
9
+ const m = (name: string) =>
10
+ makeFunctionReference<"mutation">(`payoutSnapshots/mutations:${name}`);
11
+ const cm = (name: string) =>
12
+ makeFunctionReference<"mutation">(`connect/mutations:${name}`);
13
+ const scope = {
14
+ accountId: "acct_one",
15
+ ownerKey: "owner",
16
+ operationKey: "collect-one",
17
+ };
18
+ async function setup(count = 151) {
19
+ const t = convexTest(schema, modules);
20
+ await t.mutation(cm("upsertTransfer"), {
21
+ stripeTransferId: "tr_one",
22
+ destinationAccountId: scope.accountId,
23
+ sourceChargeId: "ch_one",
24
+ amount: 1000,
25
+ currency: "usd",
26
+ role: "affiliate",
27
+ status: "paid",
28
+ });
29
+ await t.run(async (ctx) => {
30
+ for (let i = 1; i < count; i++)
31
+ await ctx.db.insert("transfers", {
32
+ stripeTransferId: `tr_${i}`,
33
+ destinationAccountId: scope.accountId,
34
+ amount: 1000,
35
+ currency: "usd",
36
+ role: "store",
37
+ status: "paid",
38
+ });
39
+ });
40
+ const begin = () =>
41
+ t.mutation(m("begin"), { ...scope, expectedRevision: -1 });
42
+ const step = (expectedStep: number, batchSize = 25) =>
43
+ t.mutation(m("advance"), { ...scope, expectedStep, batchSize });
44
+ async function finish(state: Doc<"payoutSnapshots">) {
45
+ for (
46
+ let i = 0;
47
+ i < 300 && state.phase !== "local_ready" && state.phase !== "invalidated";
48
+ i++
49
+ )
50
+ state = await step(state.step);
51
+ return state;
52
+ }
53
+ return { t, begin, step, finish };
54
+ }
55
+ it("collects more than 100 sources across durable pages without declaring partial eligibility", async () => {
56
+ const s = await setup(501);
57
+ let state = await s.begin();
58
+ expect(state).toMatchObject({ phase: "sources", sourceCount: 0 });
59
+ state = await s.step(state.step);
60
+ expect(state).toMatchObject({ phase: "sources", sourceCount: 25 });
61
+ state = await s.finish(state);
62
+ expect(state).toMatchObject({
63
+ phase: "local_ready",
64
+ sourceCount: 501,
65
+ allocationCount: 0,
66
+ claimCount: 0,
67
+ });
68
+ expect(
69
+ await s.t.run((ctx) => ctx.db.query("payoutSnapshotSources").collect()),
70
+ ).toHaveLength(501);
71
+ });
72
+ it("duplicate worker steps are idempotent", async () => {
73
+ const s = await setup();
74
+ await s.begin();
75
+ const states = await Promise.all([s.step(0), s.step(0)]);
76
+ expect(states.map((x) => x.step)).toEqual([1, 1]);
77
+ expect(
78
+ await s.t.run((ctx) => ctx.db.query("payoutSnapshotSources").collect()),
79
+ ).toHaveLength(25);
80
+ await expect(s.step(3)).rejects.toThrow(/step/i);
81
+ });
82
+ it("new positive income does not restart an older collection", async () => {
83
+ const s = await setup();
84
+ let state = await s.begin();
85
+ state = await s.step(0);
86
+ await s.t.mutation(cm("upsertTransfer"), {
87
+ stripeTransferId: "tr_later",
88
+ destinationAccountId: scope.accountId,
89
+ amount: 1000,
90
+ currency: "usd",
91
+ role: "affiliate",
92
+ status: "paid",
93
+ });
94
+ state = await s.finish(state);
95
+ expect(state).toMatchObject({
96
+ phase: "local_ready",
97
+ sourceCount: 151,
98
+ deferredPositiveChanges: true,
99
+ });
100
+ });
101
+ it("repairs a refund in an already-collected source without discarding other pages", async () => {
102
+ const s = await setup();
103
+ let state = await s.begin();
104
+ state = await s.step(0);
105
+ await s.t.mutation(cm("recordTransferReversal"), {
106
+ stripeTransferId: "tr_one",
107
+ reversedAmount: 100,
108
+ });
109
+ state = await s.finish(state);
110
+ expect(state).toMatchObject({
111
+ phase: "local_ready",
112
+ sourceCount: 151,
113
+ riskRevision: 1,
114
+ });
115
+ const original = await s.t.run((ctx) =>
116
+ ctx.db
117
+ .query("transfers")
118
+ .withIndex("by_stripe_transfer_id", (q) =>
119
+ q.eq("stripeTransferId", "tr_one"),
120
+ )
121
+ .unique(),
122
+ );
123
+ const cached = await s.t.run((ctx) =>
124
+ ctx.db
125
+ .query("payoutSnapshotSources")
126
+ .withIndex("by_snapshot_transfer", (q) =>
127
+ q.eq("snapshotId", state._id).eq("stripeTransferId", "tr_one"),
128
+ )
129
+ .unique(),
130
+ );
131
+ expect(cached?.ledgerFingerprint).toBe(transferFingerprint(original!));
132
+ });
133
+ it("retains cursor progress across a restarted caller", async () => {
134
+ const s = await setup();
135
+ await s.begin();
136
+ const first = await s.step(0);
137
+ const resumed = await s.begin();
138
+ expect(resumed.step).toBe(first.step);
139
+ expect(await s.finish(resumed)).toMatchObject({
140
+ phase: "local_ready",
141
+ sourceCount: 151,
142
+ });
143
+ });
144
+ it("collects all historical claims beyond the old 100-claim limit", async () => {
145
+ const s = await setup(1);
146
+ await s.t.run(async (ctx) => {
147
+ await ctx.db.insert("payoutSourceAllocations", {
148
+ accountId: scope.accountId,
149
+ ownerKey: scope.ownerKey,
150
+ stripeTransferId: "tr_one",
151
+ currency: "usd",
152
+ paidAmount: 201,
153
+ reservedAmount: 0,
154
+ });
155
+ for (let i = 0; i < 201; i++)
156
+ await ctx.db.insert("payoutReleaseClaims", {
157
+ accountId: scope.accountId,
158
+ ownerKey: scope.ownerKey,
159
+ operationKey: `old_${i}`,
160
+ expectedRevision: 0,
161
+ currency: "usd",
162
+ amount: 1,
163
+ allocations: [
164
+ { stripeTransferId: "tr_one", amount: 1, role: "affiliate" },
165
+ ],
166
+ observations: [],
167
+ status: "paid",
168
+ stripePayoutId: `po_${i}`,
169
+ attemptedAt: Date.now() - 1000,
170
+ });
171
+ });
172
+ const state = await s.finish(await s.begin());
173
+ expect(state).toMatchObject({
174
+ phase: "local_ready",
175
+ claimCount: 201,
176
+ allocationCount: 1,
177
+ });
178
+ expect(
179
+ await s.t.run((ctx) => ctx.db.query("payoutSnapshotClaims").collect()),
180
+ ).toHaveLength(201);
181
+ });
182
+ it("holds if an allocated source is missing", async () => {
183
+ const s = await setup(1);
184
+ await s.t.run((ctx) =>
185
+ ctx.db.insert("payoutSourceAllocations", {
186
+ accountId: scope.accountId,
187
+ ownerKey: scope.ownerKey,
188
+ stripeTransferId: "tr_missing",
189
+ currency: "usd",
190
+ paidAmount: 100,
191
+ reservedAmount: 0,
192
+ }),
193
+ );
194
+ expect(await s.finish(await s.begin())).toMatchObject({
195
+ phase: "invalidated",
196
+ reason: "source_missing",
197
+ });
198
+ });
199
+ it("never skips a missing risk-journal event", async () => {
200
+ const s = await setup();
201
+ let state = await s.begin();
202
+ state = await s.step(0);
203
+ await s.t.mutation(cm("recordTransferReversal"), {
204
+ stripeTransferId: "tr_one",
205
+ reversedAmount: 100,
206
+ });
207
+ await s.t.mutation(cm("recordTransferReversal"), {
208
+ stripeTransferId: "tr_one",
209
+ reversedAmount: 200,
210
+ });
211
+ await s.t.run(async (ctx) => {
212
+ const event = await ctx.db.query("payoutRiskEvents").first();
213
+ await ctx.db.delete("payoutRiskEvents", event!._id);
214
+ });
215
+ expect(await s.finish(state)).toMatchObject({
216
+ phase: "invalidated",
217
+ reason: "risk_history_incomplete",
218
+ });
219
+ });
220
+ it("policy revision changes stop a collection before it can be consumed", async () => {
221
+ const s = await setup();
222
+ let state = await s.begin();
223
+ await s.t.run((ctx) =>
224
+ ctx.db.insert("payoutPolicies", {
225
+ accountId: scope.accountId,
226
+ ownerKey: scope.ownerKey,
227
+ revision: 0,
228
+ mode: "stripe_managed",
229
+ status: "ready",
230
+ operationKey: "default",
231
+ country: "US",
232
+ thresholds: [],
233
+ }),
234
+ );
235
+ state = await s.step(state.step);
236
+ expect(state).toMatchObject({
237
+ phase: "invalidated",
238
+ reason: "policy_changed",
239
+ });
240
+ });
241
+ it("snapshot identity cannot be reused for another owner", async () => {
242
+ const s = await setup();
243
+ await s.begin();
244
+ await expect(
245
+ s.t.mutation(m("begin"), {
246
+ ...scope,
247
+ ownerKey: "other",
248
+ expectedRevision: -1,
249
+ }),
250
+ ).rejects.toThrow(/identity/i);
251
+ });
252
+ it("a paid-allocation mismatch never becomes local ready", async () => {
253
+ const s = await setup(1);
254
+ await s.t.run((ctx) =>
255
+ ctx.db.insert("payoutSourceAllocations", {
256
+ accountId: scope.accountId,
257
+ ownerKey: scope.ownerKey,
258
+ stripeTransferId: "tr_one",
259
+ currency: "usd",
260
+ paidAmount: 100,
261
+ reservedAmount: 0,
262
+ }),
263
+ );
264
+ expect(await s.finish(await s.begin())).toMatchObject({
265
+ phase: "invalidated",
266
+ reason: "source_allocation_mismatch",
267
+ });
268
+ });
269
+ it("repairs a returned historical payout and its paid source rollup together", async () => {
270
+ const s = await setup(1);
271
+ await s.t.run(async (ctx) => {
272
+ await ctx.db.insert("payoutSourceAllocations", {
273
+ accountId: scope.accountId,
274
+ ownerKey: scope.ownerKey,
275
+ stripeTransferId: "tr_one",
276
+ currency: "usd",
277
+ paidAmount: 1000,
278
+ reservedAmount: 0,
279
+ });
280
+ await ctx.db.insert("payoutReleaseClaims", {
281
+ accountId: scope.accountId,
282
+ ownerKey: scope.ownerKey,
283
+ operationKey: "old",
284
+ expectedRevision: 0,
285
+ currency: "usd",
286
+ amount: 1000,
287
+ allocations: [{ stripeTransferId: "tr_one", amount: 1000 }],
288
+ observations: [],
289
+ status: "paid",
290
+ stripePayoutId: "po_old",
291
+ attemptedAt: Date.now() - 1000,
292
+ });
293
+ });
294
+ let state = await s.finish(await s.begin());
295
+ expect(state.phase).toBe("local_ready");
296
+ await s.t.mutation(
297
+ makeFunctionReference<"mutation">("payoutRelease/mutations:recordOutcome"),
298
+ {
299
+ accountId: scope.accountId,
300
+ ownerKey: scope.ownerKey,
301
+ operationKey: "old",
302
+ stripePayoutId: "po_old",
303
+ amount: 1000,
304
+ currency: "usd",
305
+ providerStatus: "failed",
306
+ returnTransactionId: "txn_return",
307
+ },
308
+ );
309
+ state = await s.step(state.step);
310
+ state = await s.finish(state);
311
+ expect(state.phase).toBe("local_ready");
312
+ const source = await s.t.run((ctx) =>
313
+ ctx.db.query("payoutSnapshotSources").first(),
314
+ );
315
+ expect(source).toMatchObject({
316
+ paidAmount: 0,
317
+ expectedPaid: 0,
318
+ reservedAmount: 0,
319
+ expectedReserved: 0,
320
+ });
321
+ });
322
+ it("discarding a read-only collection removes caches without touching financial records", async () => {
323
+ const s = await setup(151);
324
+ let state = await s.finish(await s.begin());
325
+ const before = await s.t.run((ctx) =>
326
+ ctx.db.query("payoutFinancialRevisions").collect(),
327
+ );
328
+ for (let i = 0; i < 30 && state; i++)
329
+ state = await s.t.mutation(m("discard"), scope);
330
+ expect(state).toBeNull();
331
+ expect(
332
+ await s.t.run((ctx) => ctx.db.query("payoutSnapshotSources").collect()),
333
+ ).toEqual([]);
334
+ expect(
335
+ await s.t.run((ctx) => ctx.db.query("transfers").collect()),
336
+ ).toHaveLength(151);
337
+ expect(
338
+ await s.t.run((ctx) => ctx.db.query("payoutFinancialRevisions").collect()),
339
+ ).toEqual(before);
340
+ });
341
+ it("a financially bound collection cannot be discarded or expired as a read-only lease", async () => {
342
+ const s = await setup(1);
343
+ const state = await s.begin();
344
+ await s.t.run((ctx) =>
345
+ ctx.db.patch("payoutSnapshots", state._id, {
346
+ readOnly: false,
347
+ expiresAt: Date.now() - 1000,
348
+ }),
349
+ );
350
+ await expect(s.t.mutation(m("discard"), scope)).rejects.toThrow(/bound/i);
351
+ await expect(s.step(state.step)).rejects.toThrow(/bound/i);
352
+ });
353
+ it("retains source provenance even for a returned payout with no currently-paid principal", async () => {
354
+ const s = await setup(1);
355
+ await s.t.run((ctx) =>
356
+ ctx.db.insert("payoutReleaseClaims", {
357
+ accountId: scope.accountId,
358
+ ownerKey: scope.ownerKey,
359
+ operationKey: "returned",
360
+ expectedRevision: 0,
361
+ currency: "usd",
362
+ amount: 1000,
363
+ allocations: [{ stripeTransferId: "tr_missing", amount: 1000 }],
364
+ observations: [],
365
+ status: "returned",
366
+ stripePayoutId: "po_returned",
367
+ returnTransactionId: "txn_return",
368
+ attemptedAt: Date.now() - 1000,
369
+ }),
370
+ );
371
+ expect(await s.finish(await s.begin())).toMatchObject({
372
+ phase: "invalidated",
373
+ reason: "source_missing",
374
+ });
375
+ });
376
+
377
+ it("retains a risk-journal continuation when changes exceed one page", async () => {
378
+ const s = await setup(1);
379
+ let state = await s.begin();
380
+ while (state.phase !== "repair") state = await s.step(state.step);
381
+ for (let reversedAmount = 1; reversedAmount <= 31; reversedAmount++)
382
+ await s.t.mutation(cm("recordTransferReversal"), {
383
+ stripeTransferId: "tr_one",
384
+ reversedAmount,
385
+ });
386
+ state = await s.step(state.step);
387
+ expect(state).toMatchObject({ phase: "repair", riskRevision: 25 });
388
+ state = await s.step(state.step);
389
+ expect(state).toMatchObject({ phase: "check", riskRevision: 31 });
390
+ expect(await s.finish(state)).toMatchObject({
391
+ phase: "local_ready",
392
+ riskRevision: 31,
393
+ sourceCount: 1,
394
+ });
395
+ });
@@ -0,0 +1,165 @@
1
+ // @vitest-environment edge-runtime
2
+ import { convexTest } from "convex-test";
3
+ import { makeFunctionReference } from "convex/server";
4
+ import { expect, it, vi, afterEach } from "vitest";
5
+ import schema from "./schema.js";
6
+ const modules = import.meta.glob("./**/*.*s");
7
+ const m = (name: string) =>
8
+ makeFunctionReference<"mutation">(`payoutWorker/mutations:${name}`);
9
+ const q = (name: string) =>
10
+ makeFunctionReference<"query">(`payoutWorker/queries:${name}`);
11
+ const scope = { accountId: "acct_one", ownerKey: "owner", currency: "usd" };
12
+ const cadenceMs = 300000;
13
+ afterEach(() => vi.useRealTimers());
14
+ async function setup() {
15
+ const t = convexTest(schema, modules);
16
+ await t.run((ctx) =>
17
+ ctx.db.insert("payoutPolicies", {
18
+ accountId: scope.accountId,
19
+ ownerKey: scope.ownerKey,
20
+ mode: "custom",
21
+ status: "ready",
22
+ revision: 0,
23
+ operationKey: "enable",
24
+ country: "US",
25
+ thresholds: [{ currency: "usd", amount: 1000 }],
26
+ holdDeadline: Date.now() + 86400000,
27
+ }),
28
+ );
29
+ const begin = (extra = {}) =>
30
+ t.mutation(m("begin"), { ...scope, cadenceMs, ...extra });
31
+ return { t, begin };
32
+ }
33
+ it("leases one automatic attempt and stores the next attempt without creating a money claim", async () => {
34
+ const { t, begin } = await setup();
35
+ const first = await begin();
36
+ expect(first.status).toBe("run");
37
+ expect(first.job.operationKey).toMatch(/^bs:auto:/);
38
+ expect(await begin()).toMatchObject({
39
+ status: "held",
40
+ reason: "worker_busy",
41
+ });
42
+ const finished = await t.mutation(m("finish"), {
43
+ ...scope,
44
+ generation: first.job.generation,
45
+ operationKey: first.job.operationKey,
46
+ reason: "below_threshold",
47
+ });
48
+ expect(finished).toMatchObject({
49
+ lastReason: "below_threshold",
50
+ hasOperation: false,
51
+ });
52
+ expect(finished.nextAttemptAt).toBeGreaterThan(Date.now());
53
+ expect(await begin()).toMatchObject({ status: "held", reason: "not_due" });
54
+ expect(
55
+ await t.run((ctx) => ctx.db.query("payoutReleaseClaims").collect()),
56
+ ).toEqual([]);
57
+ });
58
+ it("resumes the same operation after a crashed lease and rejects a stale completion", async () => {
59
+ vi.useFakeTimers();
60
+ const { t, begin } = await setup();
61
+ const first = await begin();
62
+ vi.advanceTimersByTime(600001);
63
+ const resumed = await begin();
64
+ expect(resumed.job.operationKey).toBe(first.job.operationKey);
65
+ expect(resumed.job.generation).toBe(first.job.generation + 1);
66
+ await expect(
67
+ t.mutation(m("finish"), {
68
+ ...scope,
69
+ generation: first.job.generation,
70
+ operationKey: first.job.operationKey,
71
+ reason: "paid",
72
+ }),
73
+ ).rejects.toThrow(/generation/i);
74
+ });
75
+ it("keeps an uncertain attempted payout attached to the job across retries and opt-out", async () => {
76
+ vi.useFakeTimers();
77
+ const { t, begin } = await setup();
78
+ const first = await begin();
79
+ await t.run(async (ctx) => {
80
+ await ctx.db.insert("payoutReleaseClaims", {
81
+ ...scope,
82
+ operationKey: first.job.operationKey,
83
+ expectedRevision: 0,
84
+ amount: 1000,
85
+ allocations: [],
86
+ observations: [],
87
+ status: "reserved",
88
+ attemptedAt: Date.now(),
89
+ });
90
+ const policy = await ctx.db.query("payoutPolicies").first();
91
+ await ctx.db.patch("payoutPolicies", policy!._id, {
92
+ mode: "stripe_managed",
93
+ thresholds: [],
94
+ revision: 1,
95
+ });
96
+ });
97
+ const finished = await t.mutation(m("finish"), {
98
+ ...scope,
99
+ generation: first.job.generation,
100
+ operationKey: first.job.operationKey,
101
+ reason: "payout_unidentified",
102
+ });
103
+ expect(finished.operationKey).toBe(first.job.operationKey);
104
+ vi.advanceTimersByTime(cadenceMs);
105
+ const resumed = await begin();
106
+ expect(resumed).toMatchObject({ status: "run", reconcileOnly: true });
107
+ expect(resumed.job.operationKey).toBe(first.job.operationKey);
108
+ });
109
+ it("never starts a new payout for default, changing, expired or unconfigured policies", async () => {
110
+ const { t, begin } = await setup();
111
+ for (const change of [
112
+ { mode: "stripe_managed" as const },
113
+ { mode: "custom" as const, status: "changing" as const },
114
+ { status: "ready" as const, holdDeadline: Date.now() - 1 },
115
+ { holdDeadline: Date.now() + 86400000, thresholds: [] },
116
+ ]) {
117
+ await t.run(async (ctx) => {
118
+ const p = await ctx.db.query("payoutPolicies").first();
119
+ await ctx.db.patch("payoutPolicies", p!._id, change);
120
+ });
121
+ expect((await begin()).status).toBe("held");
122
+ }
123
+ });
124
+ it("scopes worker state and rejects invalid cadence before creating a job", async () => {
125
+ const { t, begin } = await setup();
126
+ await expect(begin({ ownerKey: "foreign" })).rejects.toThrow(/owner/i);
127
+ for (const value of [0, -1, 1000, NaN, 86400001])
128
+ await expect(begin({ cadenceMs: value })).rejects.toThrow();
129
+ expect(
130
+ await t.run((ctx) => ctx.db.query("payoutWorkerJobs").collect()),
131
+ ).toEqual([]);
132
+ });
133
+ it("completion replay preserves the original next attempt time", async () => {
134
+ vi.useFakeTimers();
135
+ const { t, begin } = await setup();
136
+ const started = await begin();
137
+ const args = {
138
+ ...scope,
139
+ generation: started.job.generation,
140
+ operationKey: started.job.operationKey,
141
+ reason: "below_threshold",
142
+ };
143
+ const first = await t.mutation(m("finish"), args);
144
+ vi.advanceTimersByTime(1000);
145
+ expect(await t.mutation(m("finish"), args)).toEqual(first);
146
+ await expect(
147
+ t.mutation(m("finish"), { ...args, reason: "paid" }),
148
+ ).rejects.toThrow(/result/i);
149
+ });
150
+ it("discovers custom policies and retained jobs on separate bounded pages", async () => {
151
+ const { t, begin } = await setup();
152
+ await begin();
153
+ const policies = await t.query(q("policies"), {
154
+ paginationOpts: { cursor: null, numItems: 1 },
155
+ });
156
+ expect(policies.page).toHaveLength(1);
157
+ expect(policies.page[0].accountId).toBe(scope.accountId);
158
+ const jobs = await t.query(q("recovering"), {
159
+ paginationOpts: { cursor: null, numItems: 1 },
160
+ });
161
+ expect(jobs.page).toHaveLength(1);
162
+ await expect(
163
+ t.query(q("get"), { ...scope, ownerKey: "foreign" }),
164
+ ).rejects.toThrow(/owner/i);
165
+ });
@@ -69,7 +69,9 @@ export const operationDoc = v.object({
69
69
  });
70
70
  export const policiesTable = defineTable(policyFields)
71
71
  .index("by_account", ["accountId"])
72
- .index("by_deadline", ["holdDeadline"]);
72
+ .index("by_deadline", ["holdDeadline"])
73
+ .index("by_status", ["status"])
74
+ .index("by_mode_status", ["mode", "status"]);
73
75
  export const operationsTable = defineTable(operationFields).index(
74
76
  "by_operation",
75
77
  ["operationKey"],
@@ -1,10 +1,14 @@
1
+ import {
2
+ reserveRecoveryGuard,
3
+ releaseRecoveryGuard,
4
+ } from "../affiliatePayouts/recipientGuard.js";
1
5
  import {
2
6
  historicalBatchArgs,
3
7
  adoptHistoricalBatch as importHistoricalBatch,
4
8
  } from "./adoption.js";
5
9
  import { readFees, recordFees } from "./fees.js";
6
10
  import { v } from "convex/values";
7
- import { mutation } from "../_generated/server.js";
11
+ import { mutation } from "../earnings/triggers.js";
8
12
  import { identity, observation, claimDoc } from "./schema.js";
9
13
  import {
10
14
  claimByKey,
@@ -110,6 +114,7 @@ export const claim = mutation({
110
114
  sources,
111
115
  });
112
116
  if (plan.status === "held") return plan;
117
+ await reserveRecoveryGuard(ctx, args, plan.amount);
113
118
  for (const allocation of plan.allocations) {
114
119
  const prior = await sourceById(ctx, allocation.stripeTransferId);
115
120
  if (prior)
@@ -155,6 +160,7 @@ export const beginAttempt = mutation({
155
160
  if (feeRevision !== (claim.feeRevision ?? 0))
156
161
  throw new Error("Payout fee revision changed after reservation.");
157
162
  await checkSnapshot(ctx, claim.accountId, claim.observations, true);
163
+ await reserveRecoveryGuard(ctx, claim, claim.amount);
158
164
  await ctx.db.patch("payoutReleaseClaims", claim._id, {
159
165
  attemptedAt: Date.now(),
160
166
  });
@@ -174,6 +180,7 @@ export const cancelUnattempted = mutation({
174
180
  throw new Error("An attempted payout claim cannot be canceled.");
175
181
  if (claim.status === "canceled") return claim;
176
182
  await moveAllocations(ctx, claim, "release");
183
+ await releaseRecoveryGuard(ctx, claim);
177
184
  await ctx.db.patch("payoutReleaseClaims", claim._id, {
178
185
  status: "canceled",
179
186
  });
@@ -257,6 +264,7 @@ export const recordOutcome = mutation({
257
264
  if (claim.status === "paid" && args.providerStatus !== "paid" && !returned)
258
265
  return claim;
259
266
  await recordFees(ctx, claim, args, returned);
267
+ await releaseRecoveryGuard(ctx, claim);
260
268
  if (returned) {
261
269
  await moveAllocations(
262
270
  ctx,
@@ -329,6 +337,7 @@ export const recordRejection = mutation({
329
337
  if (adoptedRejection && adoptedRejection._id !== claim._id)
330
338
  throw new Error("Payout rejection receipt is already allocated.");
331
339
  await moveAllocations(ctx, claim, "release");
340
+ await releaseRecoveryGuard(ctx, claim);
332
341
  await ctx.db.patch("payoutReleaseClaims", claim._id, {
333
342
  status: "rejected",
334
343
  rejection: { requestId: args.requestId, code: args.code },
@@ -1,3 +1,5 @@
1
+ import { paginator } from "convex-helpers/server/pagination";
2
+ import schema from "../schema.js";
1
3
  import { transferDocValidator } from "../connect/validators.js";
2
4
  import { readFees } from "./fees.js";
3
5
  import { paginationOptsValidator } from "convex/server";
@@ -43,7 +45,7 @@ export const pending = query({
43
45
  args.paginationOpts.numItems > 100
44
46
  )
45
47
  throw new Error("Invalid payout claim page.");
46
- const result = await ctx.db
48
+ const result = await paginator(ctx.db, schema)
47
49
  .query("payoutReleaseClaims")
48
50
  .withIndex("by_account_status", (q) =>
49
51
  q.eq("accountId", args.accountId).eq("status", "reserved"),
@@ -53,6 +53,7 @@ export const claimDoc = v.object({
53
53
  export const claimsTable = defineTable(claimFields)
54
54
  .index("by_operation", ["operationKey"])
55
55
  .index("by_account_status", ["accountId", "status"])
56
+ .index("by_status", ["status"])
56
57
  .index("by_provider_payout", ["stripePayoutId"])
57
58
  .index("by_debit_receipt", ["debitTransactionId"])
58
59
  .index("by_return_receipt", ["returnTransactionId"])