@fantastic.dev/better-stripe 0.33.0 → 0.35.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 (314) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +30 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +522 -2
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +261 -1
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +522 -2
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +261 -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/core/accountClosures.d.ts +15 -0
  20. package/dist/client/core/accountClosures.d.ts.map +1 -0
  21. package/dist/client/core/accountClosures.js +10 -0
  22. package/dist/client/core/accountClosures.js.map +1 -0
  23. package/dist/client/core/accounts.d.ts.map +1 -1
  24. package/dist/client/core/accounts.js +15 -23
  25. package/dist/client/core/accounts.js.map +1 -1
  26. package/dist/client/core/personalAccountMapping.d.ts +8 -0
  27. package/dist/client/core/personalAccountMapping.d.ts.map +1 -0
  28. package/dist/client/core/personalAccountMapping.js +11 -0
  29. package/dist/client/core/personalAccountMapping.js.map +1 -0
  30. package/dist/client/financialObservations.d.ts +42 -0
  31. package/dist/client/financialObservations.d.ts.map +1 -0
  32. package/dist/client/financialObservations.js +58 -0
  33. package/dist/client/financialObservations.js.map +1 -0
  34. package/dist/client/financialOwnership.d.ts +52 -0
  35. package/dist/client/financialOwnership.d.ts.map +1 -0
  36. package/dist/client/financialOwnership.js +72 -0
  37. package/dist/client/financialOwnership.js.map +1 -0
  38. package/dist/client/index.d.ts +57 -4
  39. package/dist/client/index.d.ts.map +1 -1
  40. package/dist/client/index.js +15 -2
  41. package/dist/client/index.js.map +1 -1
  42. package/dist/client/observationPagination.d.ts +10 -0
  43. package/dist/client/observationPagination.d.ts.map +1 -0
  44. package/dist/client/observationPagination.js +12 -0
  45. package/dist/client/observationPagination.js.map +1 -0
  46. package/dist/client/publicTypes.d.ts +10 -0
  47. package/dist/client/publicTypes.d.ts.map +1 -0
  48. package/dist/client/publicTypes.js +2 -0
  49. package/dist/client/publicTypes.js.map +1 -0
  50. package/dist/client/readContracts.d.ts +29 -0
  51. package/dist/client/readContracts.d.ts.map +1 -1
  52. package/dist/client/readContracts.js +10 -0
  53. package/dist/client/readContracts.js.map +1 -1
  54. package/dist/client/transferObservations.d.ts +59 -0
  55. package/dist/client/transferObservations.d.ts.map +1 -0
  56. package/dist/client/transferObservations.js +82 -0
  57. package/dist/client/transferObservations.js.map +1 -0
  58. package/dist/component/_generated/component.d.ts +594 -0
  59. package/dist/component/_generated/component.d.ts.map +1 -1
  60. package/dist/component/accountClosure/helpers.d.ts +18 -0
  61. package/dist/component/accountClosure/helpers.d.ts.map +1 -0
  62. package/dist/component/accountClosure/helpers.js +18 -0
  63. package/dist/component/accountClosure/helpers.js.map +1 -0
  64. package/dist/component/accountClosure/mutations.d.ts +20 -0
  65. package/dist/component/accountClosure/mutations.d.ts.map +1 -0
  66. package/dist/component/accountClosure/mutations.js +55 -0
  67. package/dist/component/accountClosure/mutations.js.map +1 -0
  68. package/dist/component/accountClosure/queries.d.ts +34 -0
  69. package/dist/component/accountClosure/queries.d.ts.map +1 -0
  70. package/dist/component/accountClosure/queries.js +36 -0
  71. package/dist/component/accountClosure/queries.js.map +1 -0
  72. package/dist/component/accountClosure/schema.d.ts +46 -0
  73. package/dist/component/accountClosure/schema.d.ts.map +1 -0
  74. package/dist/component/accountClosure/schema.js +20 -0
  75. package/dist/component/accountClosure/schema.js.map +1 -0
  76. package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
  77. package/dist/component/affiliateRecovery/mutations.js +4 -1
  78. package/dist/component/affiliateRecovery/mutations.js.map +1 -1
  79. package/dist/component/connect/mutations.d.ts.map +1 -1
  80. package/dist/component/connect/mutations.js +18 -2
  81. package/dist/component/connect/mutations.js.map +1 -1
  82. package/dist/component/connect/queries.d.ts +8 -0
  83. package/dist/component/connect/queries.d.ts.map +1 -1
  84. package/dist/component/connect/reversalClaims.d.ts.map +1 -1
  85. package/dist/component/connect/reversalClaims.js +7 -1
  86. package/dist/component/connect/reversalClaims.js.map +1 -1
  87. package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
  88. package/dist/component/connect/reversalReceipts.js +2 -0
  89. package/dist/component/connect/reversalReceipts.js.map +1 -1
  90. package/dist/component/connect/schema.d.ts +11 -1
  91. package/dist/component/connect/schema.d.ts.map +1 -1
  92. package/dist/component/connect/schema.js +9 -1
  93. package/dist/component/connect/schema.js.map +1 -1
  94. package/dist/component/connect/validators.d.ts +9 -1
  95. package/dist/component/connect/validators.d.ts.map +1 -1
  96. package/dist/component/connect/validators.js +2 -0
  97. package/dist/component/connect/validators.js.map +1 -1
  98. package/dist/component/core/mutations.d.ts.map +1 -1
  99. package/dist/component/core/mutations.js +5 -0
  100. package/dist/component/core/mutations.js.map +1 -1
  101. package/dist/component/core/personalAccountMapping.d.ts +95 -0
  102. package/dist/component/core/personalAccountMapping.d.ts.map +1 -0
  103. package/dist/component/core/personalAccountMapping.js +54 -0
  104. package/dist/component/core/personalAccountMapping.js.map +1 -0
  105. package/dist/component/core/queries.d.ts +27 -0
  106. package/dist/component/core/queries.d.ts.map +1 -1
  107. package/dist/component/core/queries.js +7 -0
  108. package/dist/component/core/queries.js.map +1 -1
  109. package/dist/component/earnings/triggers.d.ts +468 -2
  110. package/dist/component/earnings/triggers.d.ts.map +1 -1
  111. package/dist/component/earnings/triggers.js +7 -1
  112. package/dist/component/earnings/triggers.js.map +1 -1
  113. package/dist/component/financialObservations/continuation.d.ts +7 -0
  114. package/dist/component/financialObservations/continuation.d.ts.map +1 -0
  115. package/dist/component/financialObservations/continuation.js +131 -0
  116. package/dist/component/financialObservations/continuation.js.map +1 -0
  117. package/dist/component/financialObservations/generations.d.ts +6 -0
  118. package/dist/component/financialObservations/generations.d.ts.map +1 -0
  119. package/dist/component/financialObservations/generations.js +46 -0
  120. package/dist/component/financialObservations/generations.js.map +1 -0
  121. package/dist/component/financialObservations/mutations.d.ts +62 -0
  122. package/dist/component/financialObservations/mutations.d.ts.map +1 -0
  123. package/dist/component/financialObservations/mutations.js +69 -0
  124. package/dist/component/financialObservations/mutations.js.map +1 -0
  125. package/dist/component/financialObservations/queries.d.ts +307 -0
  126. package/dist/component/financialObservations/queries.d.ts.map +1 -0
  127. package/dist/component/financialObservations/queries.js +219 -0
  128. package/dist/component/financialObservations/queries.js.map +1 -0
  129. package/dist/component/financialObservations/schema.d.ts +182 -0
  130. package/dist/component/financialObservations/schema.d.ts.map +1 -0
  131. package/dist/component/financialObservations/schema.js +53 -0
  132. package/dist/component/financialObservations/schema.js.map +1 -0
  133. package/dist/component/financialObservations/settlement.d.ts +7 -0
  134. package/dist/component/financialObservations/settlement.d.ts.map +1 -0
  135. package/dist/component/financialObservations/settlement.js +118 -0
  136. package/dist/component/financialObservations/settlement.js.map +1 -0
  137. package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
  138. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
  139. package/dist/component/financialObservations/settlementHelpers.js +70 -0
  140. package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
  141. package/dist/component/financialObservations/transferBatches.d.ts +12 -0
  142. package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
  143. package/dist/component/financialObservations/transferBatches.js +76 -0
  144. package/dist/component/financialObservations/transferBatches.js.map +1 -0
  145. package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
  146. package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
  147. package/dist/component/financialObservations/transferContinuation.js +35 -0
  148. package/dist/component/financialObservations/transferContinuation.js.map +1 -0
  149. package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
  150. package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
  151. package/dist/component/financialObservations/transferContinuationState.js +69 -0
  152. package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
  153. package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
  154. package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
  155. package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
  156. package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
  157. package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
  158. package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
  159. package/dist/component/financialObservations/transferHelpers.js +37 -0
  160. package/dist/component/financialObservations/transferHelpers.js.map +1 -0
  161. package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
  162. package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
  163. package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
  164. package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
  165. package/dist/component/financialObservations/transferSchema.d.ts +237 -0
  166. package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
  167. package/dist/component/financialObservations/transferSchema.js +65 -0
  168. package/dist/component/financialObservations/transferSchema.js.map +1 -0
  169. package/dist/component/financialObservations/transfers.d.ts +7 -0
  170. package/dist/component/financialObservations/transfers.d.ts.map +1 -0
  171. package/dist/component/financialObservations/transfers.js +79 -0
  172. package/dist/component/financialObservations/transfers.js.map +1 -0
  173. package/dist/component/financialOwnership/helpers.d.ts +37 -0
  174. package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
  175. package/dist/component/financialOwnership/helpers.js +76 -0
  176. package/dist/component/financialOwnership/helpers.js.map +1 -0
  177. package/dist/component/financialOwnership/mutations.d.ts +28 -0
  178. package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
  179. package/dist/component/financialOwnership/mutations.js +74 -0
  180. package/dist/component/financialOwnership/mutations.js.map +1 -0
  181. package/dist/component/financialOwnership/queries.d.ts +57 -0
  182. package/dist/component/financialOwnership/queries.d.ts.map +1 -0
  183. package/dist/component/financialOwnership/queries.js +50 -0
  184. package/dist/component/financialOwnership/queries.js.map +1 -0
  185. package/dist/component/financialOwnership/schema.d.ts +140 -0
  186. package/dist/component/financialOwnership/schema.d.ts.map +1 -0
  187. package/dist/component/financialOwnership/schema.js +35 -0
  188. package/dist/component/financialOwnership/schema.js.map +1 -0
  189. package/dist/component/payoutRelease/queries.d.ts +2 -2
  190. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  191. package/dist/component/refundHistory/commandBinding.d.ts +17 -0
  192. package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
  193. package/dist/component/refundHistory/commandBinding.js +41 -0
  194. package/dist/component/refundHistory/commandBinding.js.map +1 -0
  195. package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
  196. package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
  197. package/dist/component/refundHistory/commandEvidence.js +54 -0
  198. package/dist/component/refundHistory/commandEvidence.js.map +1 -0
  199. package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
  200. package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
  201. package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
  202. package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
  203. package/dist/component/refundHistory/commandFields.d.ts +8 -0
  204. package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
  205. package/dist/component/refundHistory/commandFields.js +9 -0
  206. package/dist/component/refundHistory/commandFields.js.map +1 -0
  207. package/dist/component/refundHistory/mutations.d.ts +8 -0
  208. package/dist/component/refundHistory/mutations.d.ts.map +1 -0
  209. package/dist/component/refundHistory/mutations.js +25 -0
  210. package/dist/component/refundHistory/mutations.js.map +1 -0
  211. package/dist/component/refundHistory/queries.d.ts +51 -0
  212. package/dist/component/refundHistory/queries.d.ts.map +1 -1
  213. package/dist/component/refundHistory/queries.js +14 -0
  214. package/dist/component/refundHistory/queries.js.map +1 -1
  215. package/dist/component/schema.d.ts +261 -1
  216. package/dist/component/schema.js +14 -0
  217. package/dist/component/schema.js.map +1 -1
  218. package/dist/component/settlement/mutations.d.ts.map +1 -1
  219. package/dist/component/settlement/mutations.js +5 -1
  220. package/dist/component/settlement/mutations.js.map +1 -1
  221. package/dist/shared/refundCommandScope.d.ts +10 -0
  222. package/dist/shared/refundCommandScope.d.ts.map +1 -0
  223. package/dist/shared/refundCommandScope.js +9 -0
  224. package/dist/shared/refundCommandScope.js.map +1 -0
  225. package/docs/financial-observations.md +159 -0
  226. package/docs/financial-ownership.md +54 -0
  227. package/docs/personal-account-mapping.md +54 -0
  228. package/docs/refund-command-evidence.md +49 -0
  229. package/package.json +6 -2
  230. package/src/client/connect/refundCommandEvidence.ts +41 -0
  231. package/src/client/connect/refundCommandNormalization.ts +28 -0
  232. package/src/client/core/account-close-orchestration.test.ts +131 -0
  233. package/src/client/core/account-closure.test.ts +317 -0
  234. package/src/client/core/accountClosures.ts +37 -0
  235. package/src/client/core/accounts.test.ts +0 -111
  236. package/src/client/core/accounts.ts +24 -29
  237. package/src/client/core/personal-account-mapping.test.ts +71 -0
  238. package/src/client/core/personalAccountMapping.ts +23 -0
  239. package/src/client/financialObservations.test.ts +109 -0
  240. package/src/client/financialObservations.ts +106 -0
  241. package/src/client/financialOwnership.test.ts +80 -0
  242. package/src/client/financialOwnership.ts +116 -0
  243. package/src/client/index.ts +38 -35
  244. package/src/client/observationPagination.ts +11 -0
  245. package/src/client/publicTypes.ts +72 -0
  246. package/src/client/readContracts.ts +21 -0
  247. package/src/client/refund-command-evidence.test.ts +68 -0
  248. package/src/client/refund-command-normalization.test.ts +61 -0
  249. package/src/client/transferContinuation.test.ts +94 -0
  250. package/src/client/transferObservations.test.ts +98 -0
  251. package/src/client/transferObservations.ts +180 -0
  252. package/src/component/_generated/api.ts +52 -0
  253. package/src/component/_generated/component.ts +716 -0
  254. package/src/component/accountClosure/helpers.ts +21 -0
  255. package/src/component/accountClosure/mutations.ts +56 -0
  256. package/src/component/accountClosure/queries.ts +43 -0
  257. package/src/component/accountClosure/schema.ts +20 -0
  258. package/src/component/affiliateRecovery/mutations.ts +10 -1
  259. package/src/component/connect/mutations.ts +29 -2
  260. package/src/component/connect/reversalClaims.ts +19 -1
  261. package/src/component/connect/reversalReceipts.ts +2 -0
  262. package/src/component/connect/schema.ts +9 -1
  263. package/src/component/connect/validators.ts +2 -0
  264. package/src/component/core/mutations.ts +3 -0
  265. package/src/component/core/personal-account-mapping.test.ts +173 -0
  266. package/src/component/core/personalAccountMapping.ts +81 -0
  267. package/src/component/core/queries.ts +11 -0
  268. package/src/component/core/reset.test.ts +8 -1
  269. package/src/component/earnings/earnings-totals.test.ts +14 -10
  270. package/src/component/earnings/triggers.ts +9 -1
  271. package/src/component/financialObservations/continuation.test.ts +445 -0
  272. package/src/component/financialObservations/continuation.ts +159 -0
  273. package/src/component/financialObservations/generations.ts +53 -0
  274. package/src/component/financialObservations/mutations.ts +75 -0
  275. package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
  276. package/src/component/financialObservations/queries.ts +246 -0
  277. package/src/component/financialObservations/schema.ts +68 -0
  278. package/src/component/financialObservations/settlement-observations.test.ts +86 -0
  279. package/src/component/financialObservations/settlement.ts +142 -0
  280. package/src/component/financialObservations/settlementHelpers.ts +92 -0
  281. package/src/component/financialObservations/transfer-batches.test.ts +61 -0
  282. package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
  283. package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
  284. package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
  285. package/src/component/financialObservations/transfer-observations.test.ts +212 -0
  286. package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
  287. package/src/component/financialObservations/transfer-writers.test.ts +162 -0
  288. package/src/component/financialObservations/transferBatches.ts +96 -0
  289. package/src/component/financialObservations/transferContinuation.ts +53 -0
  290. package/src/component/financialObservations/transferContinuationState.ts +85 -0
  291. package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
  292. package/src/component/financialObservations/transferHelpers.ts +38 -0
  293. package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
  294. package/src/component/financialObservations/transferSchema.ts +74 -0
  295. package/src/component/financialObservations/transfers.ts +99 -0
  296. package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
  297. package/src/component/financialOwnership/helpers.ts +98 -0
  298. package/src/component/financialOwnership/mutations.ts +83 -0
  299. package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
  300. package/src/component/financialOwnership/queries.ts +65 -0
  301. package/src/component/financialOwnership/schema.ts +35 -0
  302. package/src/component/refundHistory/command-evidence.test.ts +288 -0
  303. package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
  304. package/src/component/refundHistory/command-normalization.test.ts +229 -0
  305. package/src/component/refundHistory/command-provenance.test.ts +59 -0
  306. package/src/component/refundHistory/commandBinding.ts +54 -0
  307. package/src/component/refundHistory/commandEvidence.ts +71 -0
  308. package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
  309. package/src/component/refundHistory/commandFields.ts +9 -0
  310. package/src/component/refundHistory/mutations.ts +33 -0
  311. package/src/component/refundHistory/queries.ts +15 -0
  312. package/src/component/schema.ts +24 -0
  313. package/src/component/settlement/mutations.ts +5 -1
  314. package/src/shared/refundCommandScope.ts +19 -0
@@ -0,0 +1,212 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+ import { modules, schema } from "../../test.js";
5
+ import { recordTransferReversalDefinition } from "../connect/reversalReceipts.js";
6
+ import { claimReversalSlicesDefinition } from "../connect/reversalClaims.js";
7
+
8
+ const upsert = makeFunctionReference<"mutation">(
9
+ "connect/mutations:upsertTransfer",
10
+ );
11
+ const getTransfer = makeFunctionReference<"query">(
12
+ "connect/queries:getTransferByStripeId",
13
+ );
14
+ const source = makeFunctionReference<"query">(
15
+ "financialObservations/queries:transfer",
16
+ );
17
+ const register = makeFunctionReference<"mutation">(
18
+ "financialOwnership/mutations:register",
19
+ );
20
+ const release = makeFunctionReference<"mutation">(
21
+ "connect/mutations:releaseReversalClaim",
22
+ );
23
+ function transfer() {
24
+ return {
25
+ stripeTransferId: "tr_observed",
26
+ sourceChargeId: "ch_observed",
27
+ destinationAccountId: "opaque",
28
+ amount: 100,
29
+ currency: "usd",
30
+ role: "store" as const,
31
+ status: "paid" as const,
32
+ metadata: { a: 1, b: 2 },
33
+ };
34
+ }
35
+ it("observes the actual upsert with immutable ownership and stable matching replay", async () => {
36
+ const t = convexTest(schema, modules);
37
+ await t.mutation(register, {
38
+ bucketKey: "opaque",
39
+ kind: "logical",
40
+ userId: "owner",
41
+ orgId: "org",
42
+ source: { integration: "host", recordId: "fact" },
43
+ });
44
+ await t.mutation(upsert, transfer());
45
+ const row = await t.query(getTransfer, { stripeTransferId: "tr_observed" });
46
+ const before = await t.query(source, { transferId: row._id });
47
+ expect(before).toMatchObject({
48
+ coverage: "unknown",
49
+ observation: {
50
+ transferId: row._id,
51
+ bucketKey: "opaque",
52
+ userId: "owner",
53
+ orgId: "org",
54
+ ownershipStatus: "registered",
55
+ generation: 1,
56
+ isContradictory: false,
57
+ },
58
+ });
59
+ await t.mutation(upsert, { ...transfer(), metadata: { b: 2, a: 1 } });
60
+ expect(await t.query(source, { transferId: row._id })).toEqual(before);
61
+ expect(
62
+ await t.query(getTransfer, { stripeTransferId: "tr_observed" }),
63
+ ).toEqual(row);
64
+ });
65
+ it("observes confirmed reversal facts even through the actual raw-context handler", async () => {
66
+ const t = convexTest(schema, modules);
67
+ await t.mutation(upsert, transfer());
68
+ const row = await t.query(getTransfer, { stripeTransferId: "tr_observed" });
69
+ const before = await t.query(source, { transferId: row._id });
70
+ await t.run((ctx) =>
71
+ recordTransferReversalDefinition.handler(ctx, {
72
+ stripeTransferId: "tr_observed",
73
+ reversedAmount: 25,
74
+ }),
75
+ );
76
+ const after = await t.query(source, { transferId: row._id });
77
+ expect(after).toMatchObject({
78
+ coverage: "unknown",
79
+ observation: { generation: 2 },
80
+ });
81
+ expect(after.observation.fingerprint).toBe(before.observation.fingerprint);
82
+ expect(after.observation.observedFingerprint).not.toBe(
83
+ before.observation.observedFingerprint,
84
+ );
85
+ expect(
86
+ await t.query(getTransfer, { stripeTransferId: "tr_observed" }),
87
+ ).toMatchObject({ amount: 100, currency: "usd", reversedAmount: 25 });
88
+ });
89
+ it("observes raw claimed frontiers and actual release without changing earnings totals", async () => {
90
+ const t = convexTest(schema, modules);
91
+ await t.mutation(upsert, transfer());
92
+ const row = await t.query(getTransfer, { stripeTransferId: "tr_observed" });
93
+ const totals = await t.run((ctx) => ctx.db.query("currencyTotals").take(2));
94
+ await t.run((ctx) =>
95
+ claimReversalSlicesDefinition.handler(ctx, {
96
+ operationId: "op_observed",
97
+ sourceChargeId: "ch_observed",
98
+ mode: { kind: "full" },
99
+ }),
100
+ );
101
+ expect(await t.query(source, { transferId: row._id })).toMatchObject({
102
+ observation: { generation: 2 },
103
+ });
104
+ const result = await t.mutation(release, {
105
+ operationId: "op_observed",
106
+ verified: [{ stripeTransferId: "tr_observed", amountReversed: 0 }],
107
+ });
108
+ expect(result).toEqual({
109
+ released: true,
110
+ rewound: [{ stripeTransferId: "tr_observed", to: 0 }],
111
+ });
112
+ expect(await t.query(source, { transferId: row._id })).toMatchObject({
113
+ observation: { generation: 3 },
114
+ });
115
+ const afterTotals = await t.run((ctx) =>
116
+ ctx.db.query("currencyTotals").take(2),
117
+ );
118
+ const financialTotals = (rows: typeof totals) =>
119
+ rows.map(({ _id, _creationTime, ...fields }) => {
120
+ void _id;
121
+ void _creationTime;
122
+ return fields;
123
+ });
124
+ expect(financialTotals(afterTotals)).toEqual(financialTotals(totals));
125
+ expect(
126
+ await t.query(getTransfer, { stripeTransferId: "tr_observed" }),
127
+ ).toMatchObject({ amount: 100, currency: "usd", reversalClaimedAmount: 0 });
128
+ });
129
+ it("does not rewrite an originally missing charge reference when later provider facts enrich the financial row", async () => {
130
+ const t = convexTest(schema, modules);
131
+ const { sourceChargeId, ...initial } = transfer();
132
+ void sourceChargeId;
133
+ await t.mutation(upsert, initial);
134
+ const row = await t.query(getTransfer, { stripeTransferId: "tr_observed" });
135
+ expect(
136
+ (await t.query(source, { transferId: row._id })).observation,
137
+ ).not.toHaveProperty("sourceChargeId");
138
+ await t.mutation(upsert, transfer());
139
+ const result = await t.query(source, { transferId: row._id });
140
+ expect(result.observation).toMatchObject({
141
+ generation: 2,
142
+ isContradictory: true,
143
+ });
144
+ expect(result.observation).not.toHaveProperty("sourceChargeId");
145
+ expect(
146
+ await t.query(getTransfer, { stripeTransferId: "tr_observed" }),
147
+ ).toMatchObject({ sourceChargeId: "ch_observed", amount: 100 });
148
+ });
149
+ it("indexes owner and bucket scopes separately, preserving historical pins after replacement", async () => {
150
+ const t = convexTest(schema, modules);
151
+ await t.mutation(register, {
152
+ bucketKey: "opaque",
153
+ kind: "logical",
154
+ userId: "owner",
155
+ orgId: "org",
156
+ source: { integration: "host", recordId: "fact" },
157
+ });
158
+ await t.mutation(upsert, transfer());
159
+ const bucket = makeFunctionReference<"query">(
160
+ "financialObservations/queries:transferBucket",
161
+ );
162
+ const owner = makeFunctionReference<"query">(
163
+ "financialObservations/queries:transferOwner",
164
+ );
165
+ const paginationOpts = { numItems: 25, cursor: null };
166
+ expect(
167
+ await t.query(bucket, { bucketKey: "opaque", paginationOpts }),
168
+ ).toMatchObject({
169
+ coverage: "unknown",
170
+ generation: 1,
171
+ page: [expect.objectContaining({ userId: "owner", orgId: "org" })],
172
+ });
173
+ expect(
174
+ await t.query(owner, { userId: "owner", paginationOpts }),
175
+ ).toMatchObject({ page: [] });
176
+ expect(
177
+ await t.query(owner, { userId: "owner", orgId: "org", paginationOpts }),
178
+ ).toMatchObject({ page: [expect.objectContaining({ userId: "owner" })] });
179
+ await t.run(async (ctx) => {
180
+ const row = await ctx.db
181
+ .query("financialOwnership")
182
+ .withIndex("by_bucket_key", (q) => q.eq("bucketKey", "opaque"))
183
+ .unique();
184
+ await ctx.db.delete("financialOwnership", row!._id);
185
+ });
186
+ await t.mutation(register, {
187
+ bucketKey: "opaque",
188
+ kind: "logical",
189
+ userId: "replacement",
190
+ orgId: "org",
191
+ source: { integration: "host", recordId: "new" },
192
+ });
193
+ await t.mutation(upsert, transfer());
194
+ expect(
195
+ await t.query(owner, { userId: "owner", orgId: "org", paginationOpts }),
196
+ ).toMatchObject({
197
+ generation: 2,
198
+ page: [
199
+ expect.objectContaining({
200
+ userId: "owner",
201
+ ownershipStatus: "contradictory",
202
+ }),
203
+ ],
204
+ });
205
+ expect(
206
+ await t.query(owner, {
207
+ userId: "replacement",
208
+ orgId: "org",
209
+ paginationOpts,
210
+ }),
211
+ ).toMatchObject({ page: [] });
212
+ });
@@ -0,0 +1,181 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+ import { modules, schema } from "../../test.js";
5
+ const release = makeFunctionReference<"mutation">(
6
+ "connect/mutations:releaseReversalClaim",
7
+ );
8
+ const batches = makeFunctionReference<"query">(
9
+ "financialObservations/queries:transferBatches",
10
+ );
11
+ it.each([true, false])(
12
+ "preserves the exact first-overflow 101-leg release and defers compact references without earnings-hook leakage (totals recorded: %s)",
13
+ async (hasTotals) => {
14
+ const t = convexTest(schema, modules);
15
+ const fixture = await t.run(async (ctx) => {
16
+ const ids = [];
17
+ for (let i = 0; i < 101; i++)
18
+ ids.push(
19
+ await ctx.db.insert("transfers", {
20
+ stripeTransferId: `tr_legacy_${i}`,
21
+ sourceChargeId: "ch_legacy",
22
+ destinationAccountId: "opaque_legacy",
23
+ amount: 1,
24
+ currency: "usd",
25
+ role: "store",
26
+ status: "paid",
27
+ reversalClaimedAmount: 1,
28
+ ...(hasTotals ? { totalsRecorded: true as const } : {}),
29
+ }),
30
+ );
31
+ if (hasTotals)
32
+ await ctx.db.insert("currencyTotals", {
33
+ accountId: "opaque_legacy",
34
+ currency: "usd",
35
+ kind: "earnings",
36
+ gross: 101,
37
+ reversed: 0,
38
+ paidOut: 0,
39
+ count: 101,
40
+ });
41
+ const operationId = await ctx.db.insert("transferReversalOps", {
42
+ operationId: "op_legacy",
43
+ sourceChargeId: "ch_legacy",
44
+ slices: Array.from({ length: 101 }, (_, i) => ({
45
+ stripeTransferId: `tr_legacy_${i}`,
46
+ from: 0,
47
+ to: 1,
48
+ })),
49
+ });
50
+ return { ids, operationId };
51
+ });
52
+ const result = await t.mutation(release, {
53
+ operationId: "op_legacy",
54
+ verified: Array.from({ length: 101 }, (_, i) => ({
55
+ stripeTransferId: `tr_legacy_${i}`,
56
+ amountReversed: 0,
57
+ })),
58
+ });
59
+ expect(result.rewound).toHaveLength(101);
60
+ expect(result.released).toBe(true);
61
+ const diagnostic = await t.query(batches, {
62
+ operationId: "op_legacy",
63
+ paginationOpts: { numItems: 25, cursor: null },
64
+ });
65
+ const readTransfer = makeFunctionReference<"query">(
66
+ "financialObservations/queries:transfer",
67
+ );
68
+ expect(
69
+ await t.query(readTransfer, { transferId: fixture.ids[0] }),
70
+ ).toMatchObject({
71
+ coverage: "unknown",
72
+ observation: null,
73
+ hasDeferredWork: true,
74
+ producerGeneration: 1,
75
+ });
76
+ expect(diagnostic.page).toHaveLength(1);
77
+ expect(diagnostic.page[0].transferIds).toHaveLength(101);
78
+ expect(
79
+ diagnostic.page[0].transferIds.every(
80
+ (id: string, i: number) => id === fixture.ids[i],
81
+ ),
82
+ ).toBe(true);
83
+ expect(diagnostic).toMatchObject({
84
+ coverage: "unknown",
85
+ hasDeferredWork: true,
86
+ page: [
87
+ {
88
+ originalOperationId: fixture.operationId,
89
+ operationId: "op_legacy",
90
+ sourceChargeId: "ch_legacy",
91
+ generation: 1,
92
+ state: "pending",
93
+ isOverflow: true,
94
+ referenceCount: 101,
95
+ },
96
+ ],
97
+ });
98
+ expect(
99
+ await t.run((ctx) =>
100
+ ctx.db.query("financialTransferObservations").take(1),
101
+ ),
102
+ ).toEqual([]);
103
+ const rows = await t.run((ctx) => ctx.db.query("transfers").take(102));
104
+ expect(rows).toHaveLength(101);
105
+ expect(
106
+ rows.every(
107
+ (row) =>
108
+ row.amount === 1 &&
109
+ row.currency === "usd" &&
110
+ row.sourceChargeId === "ch_legacy" &&
111
+ row.destinationAccountId === "opaque_legacy" &&
112
+ row.reversalClaimedAmount === 0,
113
+ ),
114
+ ).toBe(true);
115
+ expect(
116
+ await t.run((ctx) =>
117
+ ctx.db.get("transferReversalOps", fixture.operationId),
118
+ ),
119
+ ).toBeNull();
120
+ expect(
121
+ await t.run((ctx) => ctx.db.query("currencyTotals").take(1)),
122
+ ).toEqual([
123
+ expect.objectContaining({
124
+ gross: 101,
125
+ count: 101,
126
+ reversed: 0,
127
+ paidOut: 0,
128
+ }),
129
+ ]);
130
+ },
131
+ );
132
+ it("defers every claim reference above the diagnostic bound without changing the claim", async () => {
133
+ const t = convexTest(schema, modules);
134
+ const ids = await t.run(async (ctx) => {
135
+ const ids = [];
136
+ for (let i = 0; i < 101; i++)
137
+ ids.push(
138
+ await ctx.db.insert("transfers", {
139
+ stripeTransferId: `tr_claim_${i}`,
140
+ sourceChargeId: "ch_claim",
141
+ destinationAccountId: "opaque",
142
+ amount: 1,
143
+ currency: "usd",
144
+ status: "paid",
145
+ totalsRecorded: true,
146
+ }),
147
+ );
148
+ return ids;
149
+ });
150
+ const claim = makeFunctionReference<"mutation">(
151
+ "connect/mutations:claimReversalSlices",
152
+ );
153
+ const args = {
154
+ operationId: "op_claim",
155
+ sourceChargeId: "ch_claim",
156
+ mode: { kind: "full" },
157
+ };
158
+ const result = await t.mutation(claim, args);
159
+ expect(result.slices).toHaveLength(101);
160
+ const diagnostic = await t.query(batches, {
161
+ operationId: "op_claim",
162
+ paginationOpts: { numItems: 25, cursor: null },
163
+ });
164
+ expect(diagnostic.page).toHaveLength(1);
165
+ expect(diagnostic.page[0]).toMatchObject({
166
+ producer: "transfer_frontier_claim",
167
+ referenceCount: 101,
168
+ transferIds: ids,
169
+ isContradictory: false,
170
+ });
171
+ expect(
172
+ await t.run((ctx) => ctx.db.query("financialTransferObservations").take(1)),
173
+ ).toEqual([]);
174
+ expect((await t.mutation(claim, args)).replay).toBe(true);
175
+ expect(
176
+ await t.query(batches, {
177
+ operationId: "op_claim",
178
+ paginationOpts: { numItems: 25, cursor: null },
179
+ }),
180
+ ).toEqual(diagnostic);
181
+ });
@@ -0,0 +1,162 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+ import { schema, modules } from "../../test.js";
5
+ import {
6
+ applyCommissionExposure,
7
+ applyObligationExposure,
8
+ } from "../settlement/clawbacks.js";
9
+ const upsert = makeFunctionReference<"mutation">(
10
+ "connect/mutations:upsertTransfer",
11
+ );
12
+ const source = makeFunctionReference<"query">(
13
+ "financialObservations/queries:transfer",
14
+ );
15
+ const get = makeFunctionReference<"query">(
16
+ "connect/queries:getTransferByStripeId",
17
+ );
18
+ const reserve = makeFunctionReference<"mutation">(
19
+ "affiliateRecovery/mutations:reserveOperation",
20
+ );
21
+ const begin = makeFunctionReference<"mutation">(
22
+ "affiliateRecovery/mutations:beginOperation",
23
+ );
24
+ const complete = makeFunctionReference<"mutation">(
25
+ "affiliateRecovery/mutations:completeOperation",
26
+ );
27
+ async function recovered() {
28
+ const t = convexTest(schema, modules);
29
+ await t.mutation(upsert, {
30
+ stripeTransferId: "tr_affiliate",
31
+ sourceChargeId: "ch_affiliate",
32
+ destinationAccountId: "opaque_affiliate",
33
+ amount: 100,
34
+ currency: "usd",
35
+ role: "affiliate",
36
+ status: "paid",
37
+ });
38
+ const row = await t.query(get, { stripeTransferId: "tr_affiliate" });
39
+ const obligationId = await t.run(async (ctx) => {
40
+ await applyCommissionExposure(ctx, row, {
41
+ kind: "dispute",
42
+ chargeAmount: 1000,
43
+ principal: 200,
44
+ finalized: false,
45
+ disputeState: "open",
46
+ disputeId: "dp_observed",
47
+ });
48
+ return (await ctx.db
49
+ .query("affiliateObligations")
50
+ .withIndex("by_transfer", (q) => q.eq("stripeTransferId", "tr_affiliate"))
51
+ .unique())!._id;
52
+ });
53
+ const op = await t.mutation(reserve, {
54
+ obligationId,
55
+ kind: "recovery",
56
+ now: 0,
57
+ });
58
+ expect(op.amount).toBe(20);
59
+ await t.mutation(begin, { operationId: op._id });
60
+ await t.mutation(complete, {
61
+ operationId: op._id,
62
+ state: "succeeded",
63
+ receiptId: "re_recovered",
64
+ confirmedTransferReversed: 20,
65
+ });
66
+ return { t, row, obligationId };
67
+ }
68
+ it("observes actual direct affiliate recovery patches without changing original transfer amounts", async () => {
69
+ const { t, row } = await recovered();
70
+ expect(await t.query(source, { transferId: row._id })).toMatchObject({
71
+ coverage: "unknown",
72
+ observation: {
73
+ generation: 2,
74
+ bucketKey: "opaque_affiliate",
75
+ ownershipStatus: "unknown",
76
+ },
77
+ });
78
+ expect(
79
+ await t.query(get, { stripeTransferId: "tr_affiliate" }),
80
+ ).toMatchObject({ amount: 100, currency: "usd", reversedAmount: 20 });
81
+ });
82
+ it("observes actual direct restoration inserts and retains the original source reference", async () => {
83
+ const { t, row, obligationId } = await recovered();
84
+ await t.run(async (ctx) => {
85
+ const obligation = await ctx.db.get("affiliateObligations", obligationId);
86
+ await applyObligationExposure(ctx, obligation!, {
87
+ kind: "dispute",
88
+ chargeAmount: 1000,
89
+ principal: 200,
90
+ finalized: false,
91
+ disputeState: "won",
92
+ disputeId: "dp_observed",
93
+ });
94
+ });
95
+ const op = await t.mutation(reserve, {
96
+ obligationId,
97
+ kind: "restoration",
98
+ now: 1,
99
+ });
100
+ expect(op.amount).toBe(20);
101
+ await t.mutation(begin, { operationId: op._id });
102
+ await t.mutation(complete, {
103
+ operationId: op._id,
104
+ state: "succeeded",
105
+ receiptId: "tr_restored",
106
+ });
107
+ const restored = await t.query(get, { stripeTransferId: "tr_restored" });
108
+ expect(restored).toMatchObject({
109
+ amount: 20,
110
+ currency: "usd",
111
+ sourceChargeId: "ch_affiliate",
112
+ reinstatement: true,
113
+ role: "affiliate",
114
+ });
115
+ expect(await t.query(source, { transferId: restored._id })).toMatchObject({
116
+ coverage: "unknown",
117
+ observation: {
118
+ generation: 1,
119
+ bucketKey: "opaque_affiliate",
120
+ sourceChargeId: "ch_affiliate",
121
+ },
122
+ });
123
+ expect(
124
+ await t.query(get, { stripeTransferId: "tr_affiliate" }),
125
+ ).toMatchObject({
126
+ amount: row.amount,
127
+ currency: row.currency,
128
+ reversedAmount: 20,
129
+ });
130
+ });
131
+ it("observes the actual derived totalsRecorded inner write through bounded earnings backfill", async () => {
132
+ const t = convexTest(schema, modules);
133
+ const transferId = await t.run((ctx) =>
134
+ ctx.db.insert("transfers", {
135
+ stripeTransferId: "tr_legacy_backfill",
136
+ sourceChargeId: "ch_backfill",
137
+ destinationAccountId: "opaque_backfill",
138
+ amount: 100,
139
+ currency: "usd",
140
+ role: "store",
141
+ status: "paid",
142
+ reversalClaimedAmount: 7,
143
+ }),
144
+ );
145
+ expect((await t.query(source, { transferId })).observation).toBeNull();
146
+ const backfill = makeFunctionReference<"mutation">(
147
+ "earnings/mutations:backfillCurrencyTotals",
148
+ );
149
+ await t.mutation(backfill, { kind: "earnings", cursor: null });
150
+ expect(await t.query(source, { transferId })).toMatchObject({
151
+ coverage: "unknown",
152
+ observation: { generation: 1, bucketKey: "opaque_backfill" },
153
+ });
154
+ expect(
155
+ await t.run((ctx) => ctx.db.get("transfers", transferId)),
156
+ ).toMatchObject({
157
+ amount: 100,
158
+ currency: "usd",
159
+ reversalClaimedAmount: 7,
160
+ totalsRecorded: true,
161
+ });
162
+ });
@@ -0,0 +1,96 @@
1
+ import type { Doc, Id } from "../_generated/dataModel.js";
2
+ import type { MutationCtx, QueryCtx } from "../_generated/server.js";
3
+ import {
4
+ advanceObservationGeneration,
5
+ observationGeneration,
6
+ } from "./generations.js";
7
+ import {
8
+ transferFingerprint,
9
+ transferProducerScope,
10
+ } from "./transferHelpers.js";
11
+ export const synchronousTransferFactLimit = 100;
12
+ /** Preserve compact source references atomically before the original operation is deleted. */
13
+ export async function deferTransferFacts(
14
+ ctx: MutationCtx,
15
+ operation: Doc<"transferReversalOps">,
16
+ transferIds: Id<"transfers">[],
17
+ producer:
18
+ | "transfer_frontier_release"
19
+ | "transfer_frontier_claim" = "transfer_frontier_release",
20
+ ) {
21
+ const existing = await ctx.db
22
+ .query("financialTransferBatches")
23
+ .withIndex("by_original_operation_producer", (q) =>
24
+ q.eq("originalOperationId", operation._id).eq("producer", producer),
25
+ )
26
+ .take(2);
27
+ const fingerprint = await transferFingerprint({
28
+ operation,
29
+ transferIds,
30
+ producer,
31
+ });
32
+ const prior = existing[0];
33
+ const isContradictory =
34
+ !!prior && (prior.isContradictory || prior.fingerprint !== fingerprint);
35
+ if (
36
+ existing.length === 1 &&
37
+ prior.observedFingerprint === fingerprint &&
38
+ prior.isContradictory === isContradictory
39
+ )
40
+ return;
41
+ if (existing.length === 1) {
42
+ const generation =
43
+ Number.isSafeInteger(prior.generation) &&
44
+ prior.generation >= 1 &&
45
+ Number.isSafeInteger(prior.generation + 1)
46
+ ? prior.generation + 1
47
+ : 0;
48
+ await ctx.db.patch("financialTransferBatches", prior._id, {
49
+ observedFingerprint: fingerprint,
50
+ generation,
51
+ isContradictory: isContradictory || generation === 0,
52
+ ...(isContradictory || generation === 0
53
+ ? {
54
+ state: "pending" as const,
55
+ ...(prior.continuation
56
+ ? {
57
+ continuation: {
58
+ ...prior.continuation,
59
+ state: "held" as const,
60
+ },
61
+ }
62
+ : {}),
63
+ }
64
+ : {}),
65
+ });
66
+ }
67
+ if (existing.length === 0)
68
+ await ctx.db.insert("financialTransferBatches", {
69
+ originalOperationId: operation._id,
70
+ operationId: operation.operationId,
71
+ sourceChargeId: operation.sourceChargeId,
72
+ producer,
73
+ producerVersion: 1,
74
+ fingerprint,
75
+ observedFingerprint: fingerprint,
76
+ isContradictory: false,
77
+ generation: 1,
78
+ referenceCount: transferIds.length,
79
+ transferIds,
80
+ isOverflow: true,
81
+ state: "pending",
82
+ });
83
+ await advanceObservationGeneration(ctx, transferProducerScope);
84
+ }
85
+ /** Global invalidation keeps a deferred batch visible even when its owner scopes are not indexed yet. */
86
+ export async function transferReaderState(ctx: QueryCtx) {
87
+ const pending = await ctx.db
88
+ .query("financialTransferBatches")
89
+ .withIndex("by_state", (q) => q.eq("state", "pending"))
90
+ .take(1);
91
+ return {
92
+ coverage: "unknown" as const,
93
+ producerGeneration: await observationGeneration(ctx, transferProducerScope),
94
+ hasDeferredWork: pending.length > 0,
95
+ };
96
+ }
@@ -0,0 +1,53 @@
1
+ import type { MutationCtx } from "../_generated/server.js";
2
+ import { observationGeneration } from "./generations.js";
3
+ import { transferProducerScope } from "./transferHelpers.js";
4
+ import {
5
+ holdTransferBatch,
6
+ isInvalidTransferCursor,
7
+ transferBatchFingerprint,
8
+ type TransferBatch,
9
+ } from "./transferContinuationState.js";
10
+ import {
11
+ indexTransferReferences,
12
+ validateTransferReferences,
13
+ } from "./transferContinuationSteps.js";
14
+ /** A stale acknowledgement never advances twice; inter-commit supersession never resets a prefix. */
15
+ export async function continueTransferBatch(
16
+ ctx: MutationCtx,
17
+ marker: TransferBatch,
18
+ expectedGeneration: number,
19
+ ) {
20
+ if (
21
+ marker.generation !== expectedGeneration ||
22
+ marker.isContradictory ||
23
+ marker.continuation?.state === "held"
24
+ )
25
+ return;
26
+ const fingerprint = await transferBatchFingerprint(marker);
27
+ const aliases = await ctx.db
28
+ .query("financialTransferBatches")
29
+ .withIndex("by_original_operation_producer", (q) =>
30
+ q
31
+ .eq("originalOperationId", marker.originalOperationId)
32
+ .eq("producer", marker.producer),
33
+ )
34
+ .take(2);
35
+ if (
36
+ aliases.length !== 1 ||
37
+ aliases[0]._id !== marker._id ||
38
+ isInvalidTransferCursor(marker, fingerprint)
39
+ )
40
+ return holdTransferBatch(ctx, marker);
41
+ // Completion is historical only. A later financial fact does not rewrite that receipt.
42
+ if (marker.state === "complete") return;
43
+ const generation = await observationGeneration(ctx, transferProducerScope);
44
+ if (
45
+ generation === null ||
46
+ (marker.continuation &&
47
+ marker.continuation.producerGeneration !== generation)
48
+ )
49
+ return holdTransferBatch(ctx, marker);
50
+ if (marker.continuation?.state === "validating")
51
+ await validateTransferReferences(ctx, marker);
52
+ else await indexTransferReferences(ctx, marker, fingerprint);
53
+ }