@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.
- package/CHANGELOG.md +260 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +664 -2
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +332 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +664 -2
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +332 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/refundCommandEvidence.d.ts +14 -0
- package/dist/client/connect/refundCommandEvidence.d.ts.map +1 -0
- package/dist/client/connect/refundCommandEvidence.js +20 -0
- package/dist/client/connect/refundCommandEvidence.js.map +1 -0
- package/dist/client/connect/refundCommandNormalization.d.ts +11 -0
- package/dist/client/connect/refundCommandNormalization.d.ts.map +1 -0
- package/dist/client/connect/refundCommandNormalization.js +12 -0
- package/dist/client/connect/refundCommandNormalization.js.map +1 -0
- package/dist/client/financialObservations.d.ts +42 -0
- package/dist/client/financialObservations.d.ts.map +1 -0
- package/dist/client/financialObservations.js +58 -0
- package/dist/client/financialObservations.js.map +1 -0
- package/dist/client/financialOwnership.d.ts +52 -0
- package/dist/client/financialOwnership.d.ts.map +1 -0
- package/dist/client/financialOwnership.js +72 -0
- package/dist/client/financialOwnership.js.map +1 -0
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/observationPagination.d.ts +10 -0
- package/dist/client/observationPagination.d.ts.map +1 -0
- package/dist/client/observationPagination.js +12 -0
- package/dist/client/observationPagination.js.map +1 -0
- package/dist/client/operationEvidence.d.ts +37 -0
- package/dist/client/operationEvidence.d.ts.map +1 -0
- package/dist/client/operationEvidence.js +44 -0
- package/dist/client/operationEvidence.js.map +1 -0
- package/dist/client/publicTypes.d.ts +6 -0
- package/dist/client/publicTypes.d.ts.map +1 -1
- package/dist/client/readContracts.d.ts +31 -0
- package/dist/client/readContracts.d.ts.map +1 -1
- package/dist/client/readContracts.js +14 -0
- package/dist/client/readContracts.js.map +1 -1
- package/dist/client/transferObservations.d.ts +59 -0
- package/dist/client/transferObservations.d.ts.map +1 -0
- package/dist/client/transferObservations.js +82 -0
- package/dist/client/transferObservations.js.map +1 -0
- package/dist/component/_generated/component.d.ts +687 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
- package/dist/component/affiliateRecovery/mutations.js +4 -1
- package/dist/component/affiliateRecovery/mutations.js.map +1 -1
- package/dist/component/connect/mutations.d.ts.map +1 -1
- package/dist/component/connect/mutations.js +18 -2
- package/dist/component/connect/mutations.js.map +1 -1
- package/dist/component/connect/queries.d.ts +8 -0
- package/dist/component/connect/queries.d.ts.map +1 -1
- package/dist/component/connect/reversalClaims.d.ts.map +1 -1
- package/dist/component/connect/reversalClaims.js +7 -1
- package/dist/component/connect/reversalClaims.js.map +1 -1
- package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
- package/dist/component/connect/reversalReceipts.js +2 -0
- package/dist/component/connect/reversalReceipts.js.map +1 -1
- package/dist/component/connect/schema.d.ts +11 -1
- package/dist/component/connect/schema.d.ts.map +1 -1
- package/dist/component/connect/schema.js +9 -1
- package/dist/component/connect/schema.js.map +1 -1
- package/dist/component/connect/validators.d.ts +9 -1
- package/dist/component/connect/validators.d.ts.map +1 -1
- package/dist/component/connect/validators.js +2 -0
- package/dist/component/connect/validators.js.map +1 -1
- package/dist/component/earnings/pairedAdjustment.d.ts +12 -0
- package/dist/component/earnings/pairedAdjustment.d.ts.map +1 -0
- package/dist/component/earnings/pairedAdjustment.js +46 -0
- package/dist/component/earnings/pairedAdjustment.js.map +1 -0
- package/dist/component/earnings/triggers.d.ts +546 -2
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/earnings/triggers.js +17 -3
- package/dist/component/earnings/triggers.js.map +1 -1
- package/dist/component/financialObservations/continuation.d.ts +7 -0
- package/dist/component/financialObservations/continuation.d.ts.map +1 -0
- package/dist/component/financialObservations/continuation.js +131 -0
- package/dist/component/financialObservations/continuation.js.map +1 -0
- package/dist/component/financialObservations/generations.d.ts +6 -0
- package/dist/component/financialObservations/generations.d.ts.map +1 -0
- package/dist/component/financialObservations/generations.js +46 -0
- package/dist/component/financialObservations/generations.js.map +1 -0
- package/dist/component/financialObservations/mutations.d.ts +62 -0
- package/dist/component/financialObservations/mutations.d.ts.map +1 -0
- package/dist/component/financialObservations/mutations.js +69 -0
- package/dist/component/financialObservations/mutations.js.map +1 -0
- package/dist/component/financialObservations/queries.d.ts +307 -0
- package/dist/component/financialObservations/queries.d.ts.map +1 -0
- package/dist/component/financialObservations/queries.js +219 -0
- package/dist/component/financialObservations/queries.js.map +1 -0
- package/dist/component/financialObservations/schema.d.ts +182 -0
- package/dist/component/financialObservations/schema.d.ts.map +1 -0
- package/dist/component/financialObservations/schema.js +53 -0
- package/dist/component/financialObservations/schema.js.map +1 -0
- package/dist/component/financialObservations/settlement.d.ts +7 -0
- package/dist/component/financialObservations/settlement.d.ts.map +1 -0
- package/dist/component/financialObservations/settlement.js +118 -0
- package/dist/component/financialObservations/settlement.js.map +1 -0
- package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
- package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
- package/dist/component/financialObservations/settlementHelpers.js +70 -0
- package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
- package/dist/component/financialObservations/transferBatches.d.ts +12 -0
- package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
- package/dist/component/financialObservations/transferBatches.js +76 -0
- package/dist/component/financialObservations/transferBatches.js.map +1 -0
- package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
- package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
- package/dist/component/financialObservations/transferContinuation.js +35 -0
- package/dist/component/financialObservations/transferContinuation.js.map +1 -0
- package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
- package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
- package/dist/component/financialObservations/transferContinuationState.js +69 -0
- package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
- package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
- package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
- package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
- package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
- package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
- package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
- package/dist/component/financialObservations/transferHelpers.js +37 -0
- package/dist/component/financialObservations/transferHelpers.js.map +1 -0
- package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
- package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
- package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
- package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
- package/dist/component/financialObservations/transferSchema.d.ts +237 -0
- package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
- package/dist/component/financialObservations/transferSchema.js +65 -0
- package/dist/component/financialObservations/transferSchema.js.map +1 -0
- package/dist/component/financialObservations/transfers.d.ts +7 -0
- package/dist/component/financialObservations/transfers.d.ts.map +1 -0
- package/dist/component/financialObservations/transfers.js +79 -0
- package/dist/component/financialObservations/transfers.js.map +1 -0
- package/dist/component/financialOwnership/helpers.d.ts +37 -0
- package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
- package/dist/component/financialOwnership/helpers.js +76 -0
- package/dist/component/financialOwnership/helpers.js.map +1 -0
- package/dist/component/financialOwnership/mutations.d.ts +28 -0
- package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
- package/dist/component/financialOwnership/mutations.js +74 -0
- package/dist/component/financialOwnership/mutations.js.map +1 -0
- package/dist/component/financialOwnership/queries.d.ts +57 -0
- package/dist/component/financialOwnership/queries.d.ts.map +1 -0
- package/dist/component/financialOwnership/queries.js +50 -0
- package/dist/component/financialOwnership/queries.js.map +1 -0
- package/dist/component/financialOwnership/schema.d.ts +140 -0
- package/dist/component/financialOwnership/schema.d.ts.map +1 -0
- package/dist/component/financialOwnership/schema.js +35 -0
- package/dist/component/financialOwnership/schema.js.map +1 -0
- package/dist/component/operationEvidence/canonical.d.ts +4 -0
- package/dist/component/operationEvidence/canonical.d.ts.map +1 -0
- package/dist/component/operationEvidence/canonical.js +8 -0
- package/dist/component/operationEvidence/canonical.js.map +1 -0
- package/dist/component/operationEvidence/helpers.d.ts +87 -0
- package/dist/component/operationEvidence/helpers.d.ts.map +1 -0
- package/dist/component/operationEvidence/helpers.js +49 -0
- package/dist/component/operationEvidence/helpers.js.map +1 -0
- package/dist/component/operationEvidence/maintenance.d.ts +88 -0
- package/dist/component/operationEvidence/maintenance.d.ts.map +1 -0
- package/dist/component/operationEvidence/maintenance.js +59 -0
- package/dist/component/operationEvidence/maintenance.js.map +1 -0
- package/dist/component/operationEvidence/mutations.d.ts +97 -0
- package/dist/component/operationEvidence/mutations.d.ts.map +1 -0
- package/dist/component/operationEvidence/mutations.js +14 -0
- package/dist/component/operationEvidence/mutations.js.map +1 -0
- package/dist/component/operationEvidence/queries.d.ts +205 -0
- package/dist/component/operationEvidence/queries.d.ts.map +1 -0
- package/dist/component/operationEvidence/queries.js +75 -0
- package/dist/component/operationEvidence/queries.js.map +1 -0
- package/dist/component/operationEvidence/schema.d.ts +536 -0
- package/dist/component/operationEvidence/schema.d.ts.map +1 -0
- package/dist/component/operationEvidence/schema.js +53 -0
- package/dist/component/operationEvidence/schema.js.map +1 -0
- package/dist/component/refundHistory/commandBinding.d.ts +17 -0
- package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
- package/dist/component/refundHistory/commandBinding.js +41 -0
- package/dist/component/refundHistory/commandBinding.js.map +1 -0
- package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
- package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
- package/dist/component/refundHistory/commandEvidence.js +58 -0
- package/dist/component/refundHistory/commandEvidence.js.map +1 -0
- package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
- package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
- package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
- package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
- package/dist/component/refundHistory/commandFields.d.ts +8 -0
- package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
- package/dist/component/refundHistory/commandFields.js +9 -0
- package/dist/component/refundHistory/commandFields.js.map +1 -0
- package/dist/component/refundHistory/mutations.d.ts +8 -0
- package/dist/component/refundHistory/mutations.d.ts.map +1 -0
- package/dist/component/refundHistory/mutations.js +25 -0
- package/dist/component/refundHistory/mutations.js.map +1 -0
- package/dist/component/refundHistory/originalSeller.d.ts +5 -0
- package/dist/component/refundHistory/originalSeller.d.ts.map +1 -0
- package/dist/component/refundHistory/originalSeller.js +35 -0
- package/dist/component/refundHistory/originalSeller.js.map +1 -0
- package/dist/component/refundHistory/queries.d.ts +51 -0
- package/dist/component/refundHistory/queries.d.ts.map +1 -1
- package/dist/component/refundHistory/queries.js +14 -0
- package/dist/component/refundHistory/queries.js.map +1 -1
- package/dist/component/schema.d.ts +332 -1
- package/dist/component/schema.js +15 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/settlement/mutations.d.ts.map +1 -1
- package/dist/component/settlement/mutations.js +5 -1
- package/dist/component/settlement/mutations.js.map +1 -1
- package/dist/shared/refundCommandScope.d.ts +10 -0
- package/dist/shared/refundCommandScope.d.ts.map +1 -0
- package/dist/shared/refundCommandScope.js +9 -0
- package/dist/shared/refundCommandScope.js.map +1 -0
- package/docs/financial-observations.md +244 -0
- package/docs/financial-ownership.md +54 -0
- package/docs/refund-command-evidence.md +55 -0
- package/package.json +5 -2
- package/src/client/connect/refundCommandEvidence.ts +41 -0
- package/src/client/connect/refundCommandNormalization.ts +28 -0
- package/src/client/financialObservations.test.ts +109 -0
- package/src/client/financialObservations.ts +106 -0
- package/src/client/financialOwnership.test.ts +80 -0
- package/src/client/financialOwnership.ts +116 -0
- package/src/client/index.ts +2 -2
- package/src/client/observationPagination.ts +11 -0
- package/src/client/operationEvidence.test.ts +110 -0
- package/src/client/operationEvidence.ts +92 -0
- package/src/client/publicTypes.ts +45 -0
- package/src/client/readContracts.ts +25 -0
- package/src/client/refund-command-evidence.test.ts +68 -0
- package/src/client/refund-command-normalization.test.ts +61 -0
- package/src/client/transferContinuation.test.ts +94 -0
- package/src/client/transferObservations.test.ts +98 -0
- package/src/client/transferObservations.ts +180 -0
- package/src/component/_generated/api.ts +54 -0
- package/src/component/_generated/component.ts +797 -0
- package/src/component/affiliateRecovery/mutations.ts +10 -1
- package/src/component/connect/mutations.ts +29 -2
- package/src/component/connect/reversalClaims.ts +19 -1
- package/src/component/connect/reversalReceipts.ts +2 -0
- package/src/component/connect/schema.ts +9 -1
- package/src/component/connect/validators.ts +2 -0
- package/src/component/core/reset.test.ts +8 -1
- package/src/component/earnings/earnings-totals.test.ts +14 -10
- package/src/component/earnings/paired-adjustment.test.ts +258 -0
- package/src/component/earnings/pairedAdjustment.ts +77 -0
- package/src/component/earnings/triggers.ts +23 -11
- package/src/component/financialObservations/continuation.test.ts +445 -0
- package/src/component/financialObservations/continuation.ts +159 -0
- package/src/component/financialObservations/generations.ts +53 -0
- package/src/component/financialObservations/mutations.ts +75 -0
- package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
- package/src/component/financialObservations/queries.ts +246 -0
- package/src/component/financialObservations/schema.ts +68 -0
- package/src/component/financialObservations/settlement-observations.test.ts +86 -0
- package/src/component/financialObservations/settlement.ts +142 -0
- package/src/component/financialObservations/settlementHelpers.ts +92 -0
- package/src/component/financialObservations/transfer-batches.test.ts +61 -0
- package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
- package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
- package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
- package/src/component/financialObservations/transfer-observations.test.ts +212 -0
- package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
- package/src/component/financialObservations/transfer-writers.test.ts +162 -0
- package/src/component/financialObservations/transferBatches.ts +96 -0
- package/src/component/financialObservations/transferContinuation.ts +53 -0
- package/src/component/financialObservations/transferContinuationState.ts +85 -0
- package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
- package/src/component/financialObservations/transferHelpers.ts +38 -0
- package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
- package/src/component/financialObservations/transferSchema.ts +74 -0
- package/src/component/financialObservations/transfers.ts +99 -0
- package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
- package/src/component/financialOwnership/helpers.ts +98 -0
- package/src/component/financialOwnership/mutations.ts +83 -0
- package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
- package/src/component/financialOwnership/queries.ts +65 -0
- package/src/component/financialOwnership/schema.ts +35 -0
- package/src/component/operationEvidence/canonical.ts +10 -0
- package/src/component/operationEvidence/helpers.ts +65 -0
- package/src/component/operationEvidence/integrity.test.ts +257 -0
- package/src/component/operationEvidence/maintenance.test.ts +152 -0
- package/src/component/operationEvidence/maintenance.ts +89 -0
- package/src/component/operationEvidence/mutations.ts +14 -0
- package/src/component/operationEvidence/queries.ts +108 -0
- package/src/component/operationEvidence/reads.test.ts +140 -0
- package/src/component/operationEvidence/schema.ts +62 -0
- package/src/component/refundHistory/command-evidence.test.ts +288 -0
- package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
- package/src/component/refundHistory/command-normalization.test.ts +229 -0
- package/src/component/refundHistory/command-provenance.test.ts +59 -0
- package/src/component/refundHistory/commandBinding.ts +54 -0
- package/src/component/refundHistory/commandEvidence.ts +76 -0
- package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
- package/src/component/refundHistory/commandFields.ts +9 -0
- package/src/component/refundHistory/mutations.ts +33 -0
- package/src/component/refundHistory/original-seller-boundaries.test.ts +138 -0
- package/src/component/refundHistory/original-seller.test.ts +175 -0
- package/src/component/refundHistory/originalSeller.ts +48 -0
- package/src/component/refundHistory/queries.ts +15 -0
- package/src/component/schema.ts +28 -0
- package/src/component/settlement/mutations.ts +5 -1
- 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
|
+
}
|