@fantastic.dev/better-stripe 0.34.0 → 0.37.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 (308) hide show
  1. package/CHANGELOG.md +260 -0
  2. package/README.md +1 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +664 -2
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +332 -1
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +664 -2
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +332 -1
  10. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  11. package/dist/client/connect/refundCommandEvidence.d.ts +14 -0
  12. package/dist/client/connect/refundCommandEvidence.d.ts.map +1 -0
  13. package/dist/client/connect/refundCommandEvidence.js +20 -0
  14. package/dist/client/connect/refundCommandEvidence.js.map +1 -0
  15. package/dist/client/connect/refundCommandNormalization.d.ts +11 -0
  16. package/dist/client/connect/refundCommandNormalization.d.ts.map +1 -0
  17. package/dist/client/connect/refundCommandNormalization.js +12 -0
  18. package/dist/client/connect/refundCommandNormalization.js.map +1 -0
  19. package/dist/client/financialObservations.d.ts +42 -0
  20. package/dist/client/financialObservations.d.ts.map +1 -0
  21. package/dist/client/financialObservations.js +58 -0
  22. package/dist/client/financialObservations.js.map +1 -0
  23. package/dist/client/financialOwnership.d.ts +52 -0
  24. package/dist/client/financialOwnership.d.ts.map +1 -0
  25. package/dist/client/financialOwnership.js +72 -0
  26. package/dist/client/financialOwnership.js.map +1 -0
  27. package/dist/client/index.d.ts +2 -2
  28. package/dist/client/index.d.ts.map +1 -1
  29. package/dist/client/index.js +2 -2
  30. package/dist/client/index.js.map +1 -1
  31. package/dist/client/observationPagination.d.ts +10 -0
  32. package/dist/client/observationPagination.d.ts.map +1 -0
  33. package/dist/client/observationPagination.js +12 -0
  34. package/dist/client/observationPagination.js.map +1 -0
  35. package/dist/client/operationEvidence.d.ts +37 -0
  36. package/dist/client/operationEvidence.d.ts.map +1 -0
  37. package/dist/client/operationEvidence.js +44 -0
  38. package/dist/client/operationEvidence.js.map +1 -0
  39. package/dist/client/publicTypes.d.ts +6 -0
  40. package/dist/client/publicTypes.d.ts.map +1 -1
  41. package/dist/client/readContracts.d.ts +31 -0
  42. package/dist/client/readContracts.d.ts.map +1 -1
  43. package/dist/client/readContracts.js +14 -0
  44. package/dist/client/readContracts.js.map +1 -1
  45. package/dist/client/transferObservations.d.ts +59 -0
  46. package/dist/client/transferObservations.d.ts.map +1 -0
  47. package/dist/client/transferObservations.js +82 -0
  48. package/dist/client/transferObservations.js.map +1 -0
  49. package/dist/component/_generated/component.d.ts +687 -0
  50. package/dist/component/_generated/component.d.ts.map +1 -1
  51. package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
  52. package/dist/component/affiliateRecovery/mutations.js +4 -1
  53. package/dist/component/affiliateRecovery/mutations.js.map +1 -1
  54. package/dist/component/connect/mutations.d.ts.map +1 -1
  55. package/dist/component/connect/mutations.js +18 -2
  56. package/dist/component/connect/mutations.js.map +1 -1
  57. package/dist/component/connect/queries.d.ts +8 -0
  58. package/dist/component/connect/queries.d.ts.map +1 -1
  59. package/dist/component/connect/reversalClaims.d.ts.map +1 -1
  60. package/dist/component/connect/reversalClaims.js +7 -1
  61. package/dist/component/connect/reversalClaims.js.map +1 -1
  62. package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
  63. package/dist/component/connect/reversalReceipts.js +2 -0
  64. package/dist/component/connect/reversalReceipts.js.map +1 -1
  65. package/dist/component/connect/schema.d.ts +11 -1
  66. package/dist/component/connect/schema.d.ts.map +1 -1
  67. package/dist/component/connect/schema.js +9 -1
  68. package/dist/component/connect/schema.js.map +1 -1
  69. package/dist/component/connect/validators.d.ts +9 -1
  70. package/dist/component/connect/validators.d.ts.map +1 -1
  71. package/dist/component/connect/validators.js +2 -0
  72. package/dist/component/connect/validators.js.map +1 -1
  73. package/dist/component/earnings/pairedAdjustment.d.ts +12 -0
  74. package/dist/component/earnings/pairedAdjustment.d.ts.map +1 -0
  75. package/dist/component/earnings/pairedAdjustment.js +46 -0
  76. package/dist/component/earnings/pairedAdjustment.js.map +1 -0
  77. package/dist/component/earnings/triggers.d.ts +546 -2
  78. package/dist/component/earnings/triggers.d.ts.map +1 -1
  79. package/dist/component/earnings/triggers.js +17 -3
  80. package/dist/component/earnings/triggers.js.map +1 -1
  81. package/dist/component/financialObservations/continuation.d.ts +7 -0
  82. package/dist/component/financialObservations/continuation.d.ts.map +1 -0
  83. package/dist/component/financialObservations/continuation.js +131 -0
  84. package/dist/component/financialObservations/continuation.js.map +1 -0
  85. package/dist/component/financialObservations/generations.d.ts +6 -0
  86. package/dist/component/financialObservations/generations.d.ts.map +1 -0
  87. package/dist/component/financialObservations/generations.js +46 -0
  88. package/dist/component/financialObservations/generations.js.map +1 -0
  89. package/dist/component/financialObservations/mutations.d.ts +62 -0
  90. package/dist/component/financialObservations/mutations.d.ts.map +1 -0
  91. package/dist/component/financialObservations/mutations.js +69 -0
  92. package/dist/component/financialObservations/mutations.js.map +1 -0
  93. package/dist/component/financialObservations/queries.d.ts +307 -0
  94. package/dist/component/financialObservations/queries.d.ts.map +1 -0
  95. package/dist/component/financialObservations/queries.js +219 -0
  96. package/dist/component/financialObservations/queries.js.map +1 -0
  97. package/dist/component/financialObservations/schema.d.ts +182 -0
  98. package/dist/component/financialObservations/schema.d.ts.map +1 -0
  99. package/dist/component/financialObservations/schema.js +53 -0
  100. package/dist/component/financialObservations/schema.js.map +1 -0
  101. package/dist/component/financialObservations/settlement.d.ts +7 -0
  102. package/dist/component/financialObservations/settlement.d.ts.map +1 -0
  103. package/dist/component/financialObservations/settlement.js +118 -0
  104. package/dist/component/financialObservations/settlement.js.map +1 -0
  105. package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
  106. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
  107. package/dist/component/financialObservations/settlementHelpers.js +70 -0
  108. package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
  109. package/dist/component/financialObservations/transferBatches.d.ts +12 -0
  110. package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
  111. package/dist/component/financialObservations/transferBatches.js +76 -0
  112. package/dist/component/financialObservations/transferBatches.js.map +1 -0
  113. package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
  114. package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
  115. package/dist/component/financialObservations/transferContinuation.js +35 -0
  116. package/dist/component/financialObservations/transferContinuation.js.map +1 -0
  117. package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
  118. package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
  119. package/dist/component/financialObservations/transferContinuationState.js +69 -0
  120. package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
  121. package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
  122. package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
  123. package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
  124. package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
  125. package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
  126. package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
  127. package/dist/component/financialObservations/transferHelpers.js +37 -0
  128. package/dist/component/financialObservations/transferHelpers.js.map +1 -0
  129. package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
  130. package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
  131. package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
  132. package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
  133. package/dist/component/financialObservations/transferSchema.d.ts +237 -0
  134. package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
  135. package/dist/component/financialObservations/transferSchema.js +65 -0
  136. package/dist/component/financialObservations/transferSchema.js.map +1 -0
  137. package/dist/component/financialObservations/transfers.d.ts +7 -0
  138. package/dist/component/financialObservations/transfers.d.ts.map +1 -0
  139. package/dist/component/financialObservations/transfers.js +79 -0
  140. package/dist/component/financialObservations/transfers.js.map +1 -0
  141. package/dist/component/financialOwnership/helpers.d.ts +37 -0
  142. package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
  143. package/dist/component/financialOwnership/helpers.js +76 -0
  144. package/dist/component/financialOwnership/helpers.js.map +1 -0
  145. package/dist/component/financialOwnership/mutations.d.ts +28 -0
  146. package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
  147. package/dist/component/financialOwnership/mutations.js +74 -0
  148. package/dist/component/financialOwnership/mutations.js.map +1 -0
  149. package/dist/component/financialOwnership/queries.d.ts +57 -0
  150. package/dist/component/financialOwnership/queries.d.ts.map +1 -0
  151. package/dist/component/financialOwnership/queries.js +50 -0
  152. package/dist/component/financialOwnership/queries.js.map +1 -0
  153. package/dist/component/financialOwnership/schema.d.ts +140 -0
  154. package/dist/component/financialOwnership/schema.d.ts.map +1 -0
  155. package/dist/component/financialOwnership/schema.js +35 -0
  156. package/dist/component/financialOwnership/schema.js.map +1 -0
  157. package/dist/component/operationEvidence/canonical.d.ts +4 -0
  158. package/dist/component/operationEvidence/canonical.d.ts.map +1 -0
  159. package/dist/component/operationEvidence/canonical.js +8 -0
  160. package/dist/component/operationEvidence/canonical.js.map +1 -0
  161. package/dist/component/operationEvidence/helpers.d.ts +87 -0
  162. package/dist/component/operationEvidence/helpers.d.ts.map +1 -0
  163. package/dist/component/operationEvidence/helpers.js +49 -0
  164. package/dist/component/operationEvidence/helpers.js.map +1 -0
  165. package/dist/component/operationEvidence/maintenance.d.ts +88 -0
  166. package/dist/component/operationEvidence/maintenance.d.ts.map +1 -0
  167. package/dist/component/operationEvidence/maintenance.js +59 -0
  168. package/dist/component/operationEvidence/maintenance.js.map +1 -0
  169. package/dist/component/operationEvidence/mutations.d.ts +97 -0
  170. package/dist/component/operationEvidence/mutations.d.ts.map +1 -0
  171. package/dist/component/operationEvidence/mutations.js +14 -0
  172. package/dist/component/operationEvidence/mutations.js.map +1 -0
  173. package/dist/component/operationEvidence/queries.d.ts +205 -0
  174. package/dist/component/operationEvidence/queries.d.ts.map +1 -0
  175. package/dist/component/operationEvidence/queries.js +75 -0
  176. package/dist/component/operationEvidence/queries.js.map +1 -0
  177. package/dist/component/operationEvidence/schema.d.ts +536 -0
  178. package/dist/component/operationEvidence/schema.d.ts.map +1 -0
  179. package/dist/component/operationEvidence/schema.js +53 -0
  180. package/dist/component/operationEvidence/schema.js.map +1 -0
  181. package/dist/component/refundHistory/commandBinding.d.ts +17 -0
  182. package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
  183. package/dist/component/refundHistory/commandBinding.js +41 -0
  184. package/dist/component/refundHistory/commandBinding.js.map +1 -0
  185. package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
  186. package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
  187. package/dist/component/refundHistory/commandEvidence.js +58 -0
  188. package/dist/component/refundHistory/commandEvidence.js.map +1 -0
  189. package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
  190. package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
  191. package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
  192. package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
  193. package/dist/component/refundHistory/commandFields.d.ts +8 -0
  194. package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
  195. package/dist/component/refundHistory/commandFields.js +9 -0
  196. package/dist/component/refundHistory/commandFields.js.map +1 -0
  197. package/dist/component/refundHistory/mutations.d.ts +8 -0
  198. package/dist/component/refundHistory/mutations.d.ts.map +1 -0
  199. package/dist/component/refundHistory/mutations.js +25 -0
  200. package/dist/component/refundHistory/mutations.js.map +1 -0
  201. package/dist/component/refundHistory/originalSeller.d.ts +5 -0
  202. package/dist/component/refundHistory/originalSeller.d.ts.map +1 -0
  203. package/dist/component/refundHistory/originalSeller.js +35 -0
  204. package/dist/component/refundHistory/originalSeller.js.map +1 -0
  205. package/dist/component/refundHistory/queries.d.ts +51 -0
  206. package/dist/component/refundHistory/queries.d.ts.map +1 -1
  207. package/dist/component/refundHistory/queries.js +14 -0
  208. package/dist/component/refundHistory/queries.js.map +1 -1
  209. package/dist/component/schema.d.ts +332 -1
  210. package/dist/component/schema.js +15 -0
  211. package/dist/component/schema.js.map +1 -1
  212. package/dist/component/settlement/mutations.d.ts.map +1 -1
  213. package/dist/component/settlement/mutations.js +5 -1
  214. package/dist/component/settlement/mutations.js.map +1 -1
  215. package/dist/shared/refundCommandScope.d.ts +10 -0
  216. package/dist/shared/refundCommandScope.d.ts.map +1 -0
  217. package/dist/shared/refundCommandScope.js +9 -0
  218. package/dist/shared/refundCommandScope.js.map +1 -0
  219. package/docs/financial-observations.md +244 -0
  220. package/docs/financial-ownership.md +54 -0
  221. package/docs/refund-command-evidence.md +55 -0
  222. package/package.json +5 -2
  223. package/src/client/connect/refundCommandEvidence.ts +41 -0
  224. package/src/client/connect/refundCommandNormalization.ts +28 -0
  225. package/src/client/financialObservations.test.ts +109 -0
  226. package/src/client/financialObservations.ts +106 -0
  227. package/src/client/financialOwnership.test.ts +80 -0
  228. package/src/client/financialOwnership.ts +116 -0
  229. package/src/client/index.ts +2 -2
  230. package/src/client/observationPagination.ts +11 -0
  231. package/src/client/operationEvidence.test.ts +110 -0
  232. package/src/client/operationEvidence.ts +92 -0
  233. package/src/client/publicTypes.ts +45 -0
  234. package/src/client/readContracts.ts +25 -0
  235. package/src/client/refund-command-evidence.test.ts +68 -0
  236. package/src/client/refund-command-normalization.test.ts +61 -0
  237. package/src/client/transferContinuation.test.ts +94 -0
  238. package/src/client/transferObservations.test.ts +98 -0
  239. package/src/client/transferObservations.ts +180 -0
  240. package/src/component/_generated/api.ts +54 -0
  241. package/src/component/_generated/component.ts +797 -0
  242. package/src/component/affiliateRecovery/mutations.ts +10 -1
  243. package/src/component/connect/mutations.ts +29 -2
  244. package/src/component/connect/reversalClaims.ts +19 -1
  245. package/src/component/connect/reversalReceipts.ts +2 -0
  246. package/src/component/connect/schema.ts +9 -1
  247. package/src/component/connect/validators.ts +2 -0
  248. package/src/component/core/reset.test.ts +8 -1
  249. package/src/component/earnings/earnings-totals.test.ts +14 -10
  250. package/src/component/earnings/paired-adjustment.test.ts +258 -0
  251. package/src/component/earnings/pairedAdjustment.ts +77 -0
  252. package/src/component/earnings/triggers.ts +23 -11
  253. package/src/component/financialObservations/continuation.test.ts +445 -0
  254. package/src/component/financialObservations/continuation.ts +159 -0
  255. package/src/component/financialObservations/generations.ts +53 -0
  256. package/src/component/financialObservations/mutations.ts +75 -0
  257. package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
  258. package/src/component/financialObservations/queries.ts +246 -0
  259. package/src/component/financialObservations/schema.ts +68 -0
  260. package/src/component/financialObservations/settlement-observations.test.ts +86 -0
  261. package/src/component/financialObservations/settlement.ts +142 -0
  262. package/src/component/financialObservations/settlementHelpers.ts +92 -0
  263. package/src/component/financialObservations/transfer-batches.test.ts +61 -0
  264. package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
  265. package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
  266. package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
  267. package/src/component/financialObservations/transfer-observations.test.ts +212 -0
  268. package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
  269. package/src/component/financialObservations/transfer-writers.test.ts +162 -0
  270. package/src/component/financialObservations/transferBatches.ts +96 -0
  271. package/src/component/financialObservations/transferContinuation.ts +53 -0
  272. package/src/component/financialObservations/transferContinuationState.ts +85 -0
  273. package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
  274. package/src/component/financialObservations/transferHelpers.ts +38 -0
  275. package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
  276. package/src/component/financialObservations/transferSchema.ts +74 -0
  277. package/src/component/financialObservations/transfers.ts +99 -0
  278. package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
  279. package/src/component/financialOwnership/helpers.ts +98 -0
  280. package/src/component/financialOwnership/mutations.ts +83 -0
  281. package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
  282. package/src/component/financialOwnership/queries.ts +65 -0
  283. package/src/component/financialOwnership/schema.ts +35 -0
  284. package/src/component/operationEvidence/canonical.ts +10 -0
  285. package/src/component/operationEvidence/helpers.ts +65 -0
  286. package/src/component/operationEvidence/integrity.test.ts +257 -0
  287. package/src/component/operationEvidence/maintenance.test.ts +152 -0
  288. package/src/component/operationEvidence/maintenance.ts +89 -0
  289. package/src/component/operationEvidence/mutations.ts +14 -0
  290. package/src/component/operationEvidence/queries.ts +108 -0
  291. package/src/component/operationEvidence/reads.test.ts +140 -0
  292. package/src/component/operationEvidence/schema.ts +62 -0
  293. package/src/component/refundHistory/command-evidence.test.ts +288 -0
  294. package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
  295. package/src/component/refundHistory/command-normalization.test.ts +229 -0
  296. package/src/component/refundHistory/command-provenance.test.ts +59 -0
  297. package/src/component/refundHistory/commandBinding.ts +54 -0
  298. package/src/component/refundHistory/commandEvidence.ts +76 -0
  299. package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
  300. package/src/component/refundHistory/commandFields.ts +9 -0
  301. package/src/component/refundHistory/mutations.ts +33 -0
  302. package/src/component/refundHistory/original-seller-boundaries.test.ts +138 -0
  303. package/src/component/refundHistory/original-seller.test.ts +175 -0
  304. package/src/component/refundHistory/originalSeller.ts +48 -0
  305. package/src/component/refundHistory/queries.ts +15 -0
  306. package/src/component/schema.ts +28 -0
  307. package/src/component/settlement/mutations.ts +5 -1
  308. package/src/shared/refundCommandScope.ts +19 -0
@@ -0,0 +1,229 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+
7
+ const normalize = makeFunctionReference<
8
+ "mutation",
9
+ { limit: number },
10
+ { normalized: number; hasMore: boolean }
11
+ >("refundHistory/mutations:normalizeRefundCommandBindings");
12
+ const scope = {
13
+ commandKey: "dojo-refund:legacy_command:v1",
14
+ accountId: "acct_original",
15
+ stripePaymentIntentId: "pi_original",
16
+ stripeChargeId: "ch_original",
17
+ currency: "jpy",
18
+ };
19
+ const refund = {
20
+ stripeRefundId: "re_legacy",
21
+ accountId: scope.accountId,
22
+ stripePaymentIntentId: scope.stripePaymentIntentId,
23
+ stripeChargeId: scope.stripeChargeId,
24
+ currency: scope.currency,
25
+ amount: 500,
26
+ status: "pending" as const,
27
+ metadata: { betterStripeRefundCommand: scope.commandKey },
28
+ };
29
+
30
+ it("drains only the bounded pending rows across multiple invocations", async () => {
31
+ const t = convexTest(schema, modules);
32
+ const ids = await t.run(async (ctx) => {
33
+ const ids = [];
34
+ for (let i = 0; i < 3; i++)
35
+ ids.push(
36
+ await ctx.db.insert("refunds", {
37
+ ...refund,
38
+ stripeRefundId: `re_legacy${i}`,
39
+ }),
40
+ );
41
+ return ids;
42
+ });
43
+ expect(await t.mutation(normalize, { limit: 2 })).toEqual({
44
+ normalized: 2,
45
+ hasMore: true,
46
+ });
47
+ const rows = await t.run(async (ctx) =>
48
+ Promise.all(ids.map((id) => ctx.db.get("refunds", id))),
49
+ );
50
+ expect(
51
+ rows.filter((row) => row?.refundCommandNormalizationVersion === 1),
52
+ ).toHaveLength(2);
53
+ expect(await t.mutation(normalize, { limit: 2 })).toEqual({
54
+ normalized: 1,
55
+ hasMore: false,
56
+ });
57
+ expect(await t.mutation(normalize, { limit: 2 })).toEqual({
58
+ normalized: 0,
59
+ hasMore: false,
60
+ });
61
+ });
62
+
63
+ it.each([0, -1, 101, 1.5, NaN, Infinity])(
64
+ "rejects invalid limit %s before modifying rows",
65
+ async (limit) => {
66
+ const t = convexTest(schema, modules);
67
+ const id = await t.run((ctx) => ctx.db.insert("refunds", refund));
68
+ const before = await t.run((ctx) => ctx.db.get("refunds", id));
69
+ await expect(t.mutation(normalize, { limit })).rejects.toThrow();
70
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toEqual(before);
71
+ },
72
+ );
73
+
74
+ it("keeps old plus new duplicates held until normalization, then ambiguous", async () => {
75
+ const t = convexTest(schema, modules);
76
+ await t.run((ctx) => ctx.db.insert("refunds", refund));
77
+ await t.mutation(api.connect.mutations.upsertRefund, {
78
+ ...refund,
79
+ stripeRefundId: "re_new",
80
+ });
81
+ expect(
82
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
83
+ ).toEqual({ status: "unavailable", reason: "normalization_incomplete" });
84
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
85
+ normalized: 1,
86
+ hasMore: false,
87
+ });
88
+ expect(
89
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
90
+ ).toEqual({ status: "unavailable", reason: "ambiguous" });
91
+ });
92
+
93
+ it.each([
94
+ undefined,
95
+ {},
96
+ { betterStripeRefundCommand: 123 },
97
+ { betterStripeRefundCommand: scope.commandKey },
98
+ ])(
99
+ "normalizes current legacy metadata without touching original financial fields: %j",
100
+ async (metadata) => {
101
+ const t = convexTest(schema, modules);
102
+ const id = await t.run((ctx) =>
103
+ ctx.db.insert("refunds", { ...refund, metadata }),
104
+ );
105
+ const before = await t.run((ctx) => ctx.db.get("refunds", id));
106
+ const started = Date.now();
107
+ await t.mutation(normalize, { limit: 1 });
108
+ const after = await t.run((ctx) => ctx.db.get("refunds", id));
109
+ const {
110
+ refundCommandNormalizationVersion,
111
+ refundCommandKey,
112
+ refundCommandConflict,
113
+ refundCommandObservedAt,
114
+ ...raw
115
+ } = after!;
116
+ expect(raw).toEqual(before);
117
+ expect(refundCommandNormalizationVersion).toBe(1);
118
+ expect(refundCommandConflict).toBe(
119
+ metadata?.betterStripeRefundCommand === 123,
120
+ );
121
+ if (typeof metadata?.betterStripeRefundCommand === "string") {
122
+ expect(refundCommandKey).toBe(scope.commandKey);
123
+ expect(refundCommandObservedAt).toBeGreaterThanOrEqual(started);
124
+ expect(refundCommandObservedAt).toBeLessThanOrEqual(Date.now());
125
+ } else {
126
+ expect(refundCommandKey).toBeUndefined();
127
+ expect(refundCommandObservedAt).toBeUndefined();
128
+ }
129
+ await t.mutation(normalize, { limit: 1 });
130
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toEqual(after);
131
+ },
132
+ );
133
+
134
+ it("preserves first-known projection and latches contradictory legacy raw metadata", async () => {
135
+ const t = convexTest(schema, modules);
136
+ const id = await t.run((ctx) =>
137
+ ctx.db.insert("refunds", {
138
+ ...refund,
139
+ refundCommandKey: "dojo-refund:older_command:v1",
140
+ refundCommandObservedAt: 42,
141
+ }),
142
+ );
143
+ await t.mutation(normalize, { limit: 1 });
144
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toMatchObject({
145
+ refundCommandKey: "dojo-refund:older_command:v1",
146
+ refundCommandObservedAt: 42,
147
+ refundCommandConflict: true,
148
+ metadata: refund.metadata,
149
+ });
150
+ });
151
+
152
+ it("retains late enrichment and conflict behavior after a missing-metadata legacy drain", async () => {
153
+ const t = convexTest(schema, modules);
154
+ await t.run((ctx) => ctx.db.insert("refunds", { ...refund, metadata: {} }));
155
+ await t.mutation(normalize, { limit: 1 });
156
+ expect(
157
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
158
+ ).toEqual({ status: "unavailable", reason: "missing" });
159
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
160
+ expect(
161
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
162
+ ).toMatchObject({ status: "observed" });
163
+ await t.mutation(api.connect.mutations.upsertRefund, {
164
+ ...refund,
165
+ metadata: {},
166
+ });
167
+ expect(
168
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
169
+ ).toEqual({ status: "unavailable", reason: "correlation_conflict" });
170
+ });
171
+
172
+ it("serializes concurrent bounded drains and never rewrites normalized rows", async () => {
173
+ const t = convexTest(schema, modules);
174
+ const ids = await t.run(async (ctx) => {
175
+ const ids = [];
176
+ for (let i = 0; i < 3; i++)
177
+ ids.push(
178
+ await ctx.db.insert("refunds", {
179
+ ...refund,
180
+ stripeRefundId: `re_concurrent${i}`,
181
+ }),
182
+ );
183
+ return ids;
184
+ });
185
+ const results = await Promise.all([
186
+ t.mutation(normalize, { limit: 2 }),
187
+ t.mutation(normalize, { limit: 2 }),
188
+ ]);
189
+ expect(results.reduce((total, result) => total + result.normalized, 0)).toBe(
190
+ 3,
191
+ );
192
+ for (const id of ids)
193
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toMatchObject({
194
+ refundCommandNormalizationVersion: 1,
195
+ });
196
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
197
+ normalized: 0,
198
+ hasMore: false,
199
+ });
200
+ });
201
+
202
+ it("accepts the maximum batch and continues one remaining row without a lifetime cap", async () => {
203
+ const t = convexTest(schema, modules);
204
+ await t.run(async (ctx) => {
205
+ for (let i = 0; i < 101; i++)
206
+ await ctx.db.insert("refunds", {
207
+ ...refund,
208
+ stripeRefundId: `re_max${i}`,
209
+ });
210
+ });
211
+ expect(await t.mutation(normalize, { limit: 100 })).toEqual({
212
+ normalized: 100,
213
+ hasMore: true,
214
+ });
215
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
216
+ normalized: 1,
217
+ hasMore: false,
218
+ });
219
+ await t.run((ctx) =>
220
+ ctx.db.insert("refunds", { ...refund, stripeRefundId: "re_late" }),
221
+ );
222
+ expect(
223
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
224
+ ).toEqual({ status: "unavailable", reason: "normalization_incomplete" });
225
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
226
+ normalized: 1,
227
+ hasMore: false,
228
+ });
229
+ });
@@ -0,0 +1,59 @@
1
+ import { convexTest } from "convex-test";
2
+ import { expect, it } from "vitest";
3
+ import { api } from "../_generated/api.js";
4
+ import { schema, modules } from "../../test.js";
5
+ import { refundCommandBinding } from "./commandBinding.js";
6
+ const key = "dojo-refund:provenance_command:v1";
7
+ const metadata = { betterStripeRefundCommand: key };
8
+ const scope = {
9
+ commandKey: key,
10
+ accountId: "acct_original",
11
+ stripePaymentIntentId: "pi_original",
12
+ stripeChargeId: "ch_original",
13
+ currency: "jpy",
14
+ };
15
+ const read = api.refundHistory.queries.getRefundCommandEvidence;
16
+
17
+ it("preserves first observed binding time without treating legacy insertion as provider creation", () => {
18
+ const first = refundCommandBinding({ metadata }, {}, 2000);
19
+ expect(first.refundCommandObservedAt).toBe(2000);
20
+ const later = refundCommandBinding(
21
+ { ...first, metadata },
22
+ { metadata },
23
+ 3000,
24
+ );
25
+ expect(later.refundCommandObservedAt).toBe(2000);
26
+ });
27
+
28
+ it.each([-1, NaN, Infinity, Number.MAX_SAFE_INTEGER])(
29
+ "holds malformed or future observed binding time %s",
30
+ async (observedAt) => {
31
+ const t = convexTest(schema, modules);
32
+ await t.mutation(api.connect.mutations.upsertRefund, {
33
+ stripeRefundId: "re_original",
34
+ accountId: scope.accountId,
35
+ stripePaymentIntentId: scope.stripePaymentIntentId,
36
+ stripeChargeId: scope.stripeChargeId,
37
+ currency: "jpy",
38
+ amount: 125,
39
+ status: "succeeded",
40
+ metadata,
41
+ });
42
+ await t.run(async (ctx) => {
43
+ const row = await ctx.db
44
+ .query("refunds")
45
+ .withIndex("by_stripe_refund_id", (q) =>
46
+ q.eq("stripeRefundId", "re_original"),
47
+ )
48
+ .unique();
49
+ if (!row) throw new Error("Missing QA refund");
50
+ await ctx.db.patch("refunds", row._id, {
51
+ refundCommandObservedAt: observedAt,
52
+ });
53
+ });
54
+ expect(await t.query(read, scope)).toEqual({
55
+ status: "unavailable",
56
+ reason: "invalid_receipt",
57
+ });
58
+ },
59
+ );
@@ -0,0 +1,54 @@
1
+ type Existing = {
2
+ metadata?: unknown;
3
+ refundCommandKey?: string;
4
+ refundCommandConflict?: boolean;
5
+ refundCommandObservedAt?: number;
6
+ };
7
+ function keyFrom(metadata: unknown) {
8
+ if (
9
+ metadata === null ||
10
+ typeof metadata !== "object" ||
11
+ Array.isArray(metadata)
12
+ )
13
+ return { kind: "missing" as const };
14
+ const key = (metadata as Record<string, unknown>).betterStripeRefundCommand;
15
+ if (key === undefined) return { kind: "missing" as const };
16
+ return typeof key === "string" && /^[A-Za-z0-9:_-]{16,200}$/.test(key)
17
+ ? { kind: "valid" as const, key }
18
+ : { kind: "invalid" as const };
19
+ }
20
+
21
+ /** First known metadata is observed before incoming changes; it is not provider-creation provenance. */
22
+ export function refundCommandBinding(
23
+ existing: Existing | null,
24
+ incoming: { metadata?: unknown },
25
+ now: number,
26
+ ) {
27
+ const old = keyFrom(existing?.metadata);
28
+ const next = Object.prototype.hasOwnProperty.call(incoming, "metadata")
29
+ ? keyFrom(incoming.metadata)
30
+ : undefined;
31
+ const first =
32
+ existing?.refundCommandKey ?? (old.kind === "valid" ? old.key : undefined);
33
+ const key = first ?? (next?.kind === "valid" ? next.key : undefined);
34
+ const conflict =
35
+ existing?.refundCommandConflict === true ||
36
+ old.kind === "invalid" ||
37
+ next?.kind === "invalid" ||
38
+ (existing?.refundCommandKey !== undefined &&
39
+ old.kind === "valid" &&
40
+ old.key !== existing.refundCommandKey) ||
41
+ (first !== undefined &&
42
+ next !== undefined &&
43
+ (next.kind !== "valid" || next.key !== first));
44
+ return {
45
+ refundCommandNormalizationVersion: 1 as const,
46
+ refundCommandConflict: conflict,
47
+ ...(key
48
+ ? {
49
+ refundCommandKey: key,
50
+ refundCommandObservedAt: existing?.refundCommandObservedAt ?? now,
51
+ }
52
+ : {}),
53
+ };
54
+ }
@@ -0,0 +1,76 @@
1
+ import { originalRefundSeller } from "./originalSeller.js";
2
+ import type { QueryCtx } from "../_generated/server.js";
3
+ import {
4
+ assertRefundCommandScope,
5
+ type RefundCommandScope,
6
+ } from "../../shared/refundCommandScope.js";
7
+
8
+ /** Bounded global normalization and exact-key proof, never a provider history or retry claim. */
9
+ export async function readRefundCommandEvidence(
10
+ ctx: QueryCtx,
11
+ scope: RefundCommandScope,
12
+ ) {
13
+ assertRefundCommandScope(scope);
14
+ const legacy = await ctx.db
15
+ .query("refunds")
16
+ .withIndex("by_refund_command_normalization_version", (q) =>
17
+ q.eq("refundCommandNormalizationVersion", undefined),
18
+ )
19
+ .take(1);
20
+ if (legacy.length)
21
+ return {
22
+ status: "unavailable" as const,
23
+ reason: "normalization_incomplete" as const,
24
+ };
25
+ const rows = await ctx.db
26
+ .query("refunds")
27
+ .withIndex("by_refund_command_key", (q) =>
28
+ q.eq("refundCommandKey", scope.commandKey),
29
+ )
30
+ .take(2);
31
+ if (rows.length > 1)
32
+ return { status: "unavailable" as const, reason: "ambiguous" as const };
33
+ const row = rows[0];
34
+ if (!row)
35
+ return { status: "unavailable" as const, reason: "missing" as const };
36
+ if (row.refundCommandConflict)
37
+ return {
38
+ status: "unavailable" as const,
39
+ reason: "correlation_conflict" as const,
40
+ };
41
+ const accountId =
42
+ row.accountId === undefined
43
+ ? await originalRefundSeller(ctx, scope)
44
+ : row.accountId;
45
+ if (
46
+ accountId !== scope.accountId ||
47
+ row.stripePaymentIntentId !== scope.stripePaymentIntentId ||
48
+ row.stripeChargeId !== scope.stripeChargeId ||
49
+ row.currency !== scope.currency
50
+ )
51
+ return {
52
+ status: "unavailable" as const,
53
+ reason: "scope_mismatch" as const,
54
+ };
55
+ if (
56
+ !/^re_[A-Za-z0-9_]{1,252}$/.test(row.stripeRefundId) ||
57
+ !Number.isSafeInteger(row.amount) ||
58
+ row.amount <= 0 ||
59
+ !Number.isSafeInteger(row.refundCommandObservedAt) ||
60
+ (row.refundCommandObservedAt ?? -1) < 0 ||
61
+ (row.refundCommandObservedAt ?? Infinity) > Date.now()
62
+ )
63
+ return {
64
+ status: "unavailable" as const,
65
+ reason: "invalid_receipt" as const,
66
+ };
67
+ const identity = await ctx.db
68
+ .query("refunds")
69
+ .withIndex("by_stripe_refund_id", (q) =>
70
+ q.eq("stripeRefundId", row.stripeRefundId),
71
+ )
72
+ .take(2);
73
+ if (identity.length !== 1 || identity[0]?._id !== row._id)
74
+ return { status: "unavailable" as const, reason: "ambiguous" as const };
75
+ return { status: "observed" as const, refund: { ...row, accountId } };
76
+ }
@@ -0,0 +1,18 @@
1
+ import { v } from "convex/values";
2
+ import { refundDocValidator } from "../connect/validators.js";
3
+
4
+ /** Observation and coverage only; no state here means provider absence or retry authority. */
5
+ export const refundCommandEvidenceValidator = v.union(
6
+ v.object({ status: v.literal("observed"), refund: refundDocValidator }),
7
+ v.object({
8
+ status: v.literal("unavailable"),
9
+ reason: v.union(
10
+ v.literal("missing"),
11
+ v.literal("normalization_incomplete"),
12
+ v.literal("ambiguous"),
13
+ v.literal("correlation_conflict"),
14
+ v.literal("scope_mismatch"),
15
+ v.literal("invalid_receipt"),
16
+ ),
17
+ }),
18
+ );
@@ -0,0 +1,9 @@
1
+ import { v } from "convex/values";
2
+
3
+ /** Correlation projection only; raw provider refund fields remain authoritative. */
4
+ export const refundCommandFields = {
5
+ refundCommandNormalizationVersion: v.optional(v.literal(1)),
6
+ refundCommandKey: v.optional(v.string()),
7
+ refundCommandConflict: v.optional(v.boolean()),
8
+ refundCommandObservedAt: v.optional(v.number()),
9
+ };
@@ -0,0 +1,33 @@
1
+ import { v } from "convex/values";
2
+ import { mutation } from "../_generated/server.js";
3
+ import { refundCommandBinding } from "./commandBinding.js";
4
+
5
+ /** Trusted-host projection backfill; completion covers current component rows, never provider history. */
6
+ export const normalizeRefundCommandBindings = mutation({
7
+ args: { limit: v.number() },
8
+ returns: v.object({ normalized: v.number(), hasMore: v.boolean() }),
9
+ handler: async (ctx, { limit }) => {
10
+ if (!Number.isInteger(limit) || limit < 1 || limit > 100)
11
+ throw new Error("Normalization limit must be an integer from 1 to 100");
12
+ const rows = await ctx.db
13
+ .query("refunds")
14
+ .withIndex("by_refund_command_normalization_version", (q) =>
15
+ q.eq("refundCommandNormalizationVersion", undefined),
16
+ )
17
+ .take(limit);
18
+ const now = Date.now();
19
+ for (const row of rows)
20
+ await ctx.db.patch(
21
+ "refunds",
22
+ row._id,
23
+ refundCommandBinding(row, {}, now),
24
+ );
25
+ const pending = await ctx.db
26
+ .query("refunds")
27
+ .withIndex("by_refund_command_normalization_version", (q) =>
28
+ q.eq("refundCommandNormalizationVersion", undefined),
29
+ )
30
+ .take(1);
31
+ return { normalized: rows.length, hasMore: pending.length > 0 };
32
+ },
33
+ });
@@ -0,0 +1,138 @@
1
+ import { convexTest } from "convex-test";
2
+ import { expect, it } from "vitest";
3
+ import { api } from "../_generated/api.js";
4
+ import { schema, modules } from "../../test.js";
5
+
6
+ const scope = {
7
+ commandKey: "dojo-refund:original_store_gap:v1",
8
+ accountId: "acct_original_store",
9
+ stripePaymentIntentId: "pi_original_split",
10
+ stripeChargeId: "ch_original_split",
11
+ currency: "usd",
12
+ };
13
+ const refund = {
14
+ stripeRefundId: "re_original_split",
15
+ stripePaymentIntentId: scope.stripePaymentIntentId,
16
+ stripeChargeId: scope.stripeChargeId,
17
+ amount: 50,
18
+ currency: "usd",
19
+ status: "pending" as const,
20
+ metadata: { betterStripeRefundCommand: scope.commandKey },
21
+ };
22
+ const payment = {
23
+ stripePaymentIntentId: scope.stripePaymentIntentId,
24
+ userId: "original_buyer",
25
+ accountId: "acct_original_buyer",
26
+ amount: 1000,
27
+ currency: "usd",
28
+ status: "succeeded" as const,
29
+ chargeType: "separate" as const,
30
+ splitRecipients: [
31
+ {
32
+ destinationAccountId: scope.accountId,
33
+ role: "store" as const,
34
+ amount: 700,
35
+ },
36
+ {
37
+ destinationAccountId: "acct_affiliate",
38
+ role: "affiliate" as const,
39
+ amount: 300,
40
+ },
41
+ ],
42
+ };
43
+ const read = api.refundHistory.queries.getRefundCommandEvidence;
44
+
45
+ const allocation = {
46
+ sourceChargeId: scope.stripeChargeId,
47
+ paymentIntentId: scope.stripePaymentIntentId,
48
+ currency: scope.currency,
49
+ gross: 1000,
50
+ processingFee: 0,
51
+ platformNet: 0,
52
+ transfers: payment.splitRecipients,
53
+ };
54
+ it.each([
55
+ { paymentIntentId: undefined },
56
+ { paymentIntentId: "pi_foreign" },
57
+ { sourceChargeId: "ch_foreign" },
58
+ { currency: "eur" },
59
+ { transfers: [] },
60
+ { transfers: [payment.splitRecipients[1]!] },
61
+ {
62
+ transfers: [
63
+ ...payment.splitRecipients,
64
+ {
65
+ destinationAccountId: "acct_second_store",
66
+ role: "store" as const,
67
+ amount: 0,
68
+ },
69
+ ],
70
+ },
71
+ {
72
+ transfers: [
73
+ ...payment.splitRecipients,
74
+ {
75
+ destinationAccountId: scope.accountId,
76
+ role: "affiliate" as const,
77
+ amount: 0,
78
+ },
79
+ ],
80
+ },
81
+ {
82
+ transfers: [
83
+ { ...payment.splitRecipients[0]!, destinationAccountId: "malformed" },
84
+ ],
85
+ },
86
+ { transfers: [{ ...payment.splitRecipients[0]!, amount: 0.5 }] },
87
+ { transfers: [{ ...payment.splitRecipients[0]!, amount: -1 }] },
88
+ ])(
89
+ "holds malformed, missing or contradictory immutable allocation %#",
90
+ async (patch) => {
91
+ const t = convexTest(schema, modules);
92
+ await t.mutation(api.connect.mutations.upsertPayment, payment);
93
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
94
+ await t.run((ctx) =>
95
+ ctx.db.insert("settlementAllocations", { ...allocation, ...patch }),
96
+ );
97
+ expect(await t.query(read, scope)).toMatchObject({
98
+ status: "unavailable",
99
+ reason: "scope_mismatch",
100
+ });
101
+ },
102
+ );
103
+ it("rejects duplicate exact-charge allocations before selecting any original store", async () => {
104
+ const t = convexTest(schema, modules);
105
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
106
+ await t.run(async (ctx) => {
107
+ await ctx.db.insert("settlementAllocations", allocation);
108
+ await ctx.db.insert("settlementAllocations", {
109
+ ...allocation,
110
+ transfers: [
111
+ {
112
+ destinationAccountId: "acct_other_store",
113
+ role: "store",
114
+ amount: 1000,
115
+ },
116
+ ],
117
+ });
118
+ });
119
+ expect(await t.query(read, scope)).toMatchObject({ status: "unavailable" });
120
+ });
121
+ it.each(["stripePaymentIntentId", "stripeChargeId", "currency"] as const)(
122
+ "never qualifies foreign receipt %s",
123
+ async (field) => {
124
+ const t = convexTest(schema, modules);
125
+ await t.mutation(
126
+ api.settlement.mutations.reserveSettlementAllocation,
127
+ allocation,
128
+ );
129
+ await t.mutation(api.connect.mutations.upsertRefund, {
130
+ ...refund,
131
+ [field]: field === "currency" ? "eur" : "foreign",
132
+ });
133
+ expect(await t.query(read, scope)).toMatchObject({
134
+ status: "unavailable",
135
+ reason: "scope_mismatch",
136
+ });
137
+ },
138
+ );