@fantastic.dev/better-stripe 0.34.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.
- package/CHANGELOG.md +35 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +486 -2
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +243 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +486 -2
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +243 -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/publicTypes.d.ts +5 -0
- package/dist/client/publicTypes.d.ts.map +1 -1
- package/dist/client/readContracts.d.ts +29 -0
- package/dist/client/readContracts.d.ts.map +1 -1
- package/dist/client/readContracts.js +10 -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 +506 -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/triggers.d.ts +426 -2
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/earnings/triggers.js +7 -1
- 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/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- 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 +54 -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/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 +243 -1
- package/dist/component/schema.js +12 -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 +159 -0
- package/docs/financial-ownership.md +54 -0
- package/docs/refund-command-evidence.md +49 -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/publicTypes.ts +37 -0
- package/src/client/readContracts.ts +21 -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 +44 -0
- package/src/component/_generated/component.ts +590 -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/triggers.ts +9 -1
- 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/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 +71 -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/queries.ts +15 -0
- package/src/component/schema.ts +22 -0
- package/src/component/settlement/mutations.ts +5 -1
- package/src/shared/refundCommandScope.ts +19 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
2
|
+
import type { MutationCtx } from "../_generated/server.js";
|
|
3
|
+
import {
|
|
4
|
+
hasInvalidLegIdentity,
|
|
5
|
+
invalidate,
|
|
6
|
+
legOwnership,
|
|
7
|
+
settlementFingerprint,
|
|
8
|
+
} from "./settlementHelpers.js";
|
|
9
|
+
|
|
10
|
+
type Observation = Doc<"financialSourceObservations">;
|
|
11
|
+
const batchLimit = 100;
|
|
12
|
+
async function hold(ctx: MutationCtx, marker: Observation) {
|
|
13
|
+
await ctx.db.patch("financialSourceObservations", marker._id, {
|
|
14
|
+
isContradictory: true,
|
|
15
|
+
generation: Number.isSafeInteger(marker.generation + 1)
|
|
16
|
+
? marker.generation + 1
|
|
17
|
+
: 0,
|
|
18
|
+
continuation: {
|
|
19
|
+
state: "held",
|
|
20
|
+
nextLeg: marker.indexedLegCount,
|
|
21
|
+
validatedLegs: marker.continuation?.validatedLegs ?? 0,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async function indexBatch(
|
|
26
|
+
ctx: MutationCtx,
|
|
27
|
+
row: Doc<"settlementAllocations">,
|
|
28
|
+
marker: Observation,
|
|
29
|
+
) {
|
|
30
|
+
const start = marker.continuation?.nextLeg ?? 0;
|
|
31
|
+
const end = Math.min(start + batchLimit, row.transfers.length);
|
|
32
|
+
const existing = await ctx.db
|
|
33
|
+
.query("financialSourceLegs")
|
|
34
|
+
.withIndex("by_allocation_id_and_leg_index", (q) =>
|
|
35
|
+
q.eq("allocationId", row._id).gte("legIndex", start),
|
|
36
|
+
)
|
|
37
|
+
.take(1);
|
|
38
|
+
if (existing.length) return hold(ctx, marker);
|
|
39
|
+
const legs: Omit<Doc<"financialSourceLegs">, "_id" | "_creationTime">[] = [];
|
|
40
|
+
for (let i = start; i < end; i++) {
|
|
41
|
+
const original = row.transfers[i]!;
|
|
42
|
+
const leg = {
|
|
43
|
+
allocationId: row._id,
|
|
44
|
+
sourceChargeId: row.sourceChargeId,
|
|
45
|
+
legIndex: i,
|
|
46
|
+
bucketKey: original.destinationAccountId,
|
|
47
|
+
role: original.role,
|
|
48
|
+
...(await legOwnership(ctx, original.destinationAccountId, undefined)),
|
|
49
|
+
};
|
|
50
|
+
legs.push(leg);
|
|
51
|
+
await ctx.db.insert("financialSourceLegs", leg);
|
|
52
|
+
}
|
|
53
|
+
await invalidate(ctx, legs);
|
|
54
|
+
await ctx.db.patch("financialSourceObservations", marker._id, {
|
|
55
|
+
generation: marker.generation + 1,
|
|
56
|
+
indexedLegCount: end,
|
|
57
|
+
continuation: {
|
|
58
|
+
state: end === row.transfers.length ? "validating" : "indexing",
|
|
59
|
+
nextLeg: end,
|
|
60
|
+
validatedLegs: 0,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async function validateBatch(
|
|
65
|
+
ctx: MutationCtx,
|
|
66
|
+
row: Doc<"settlementAllocations">,
|
|
67
|
+
marker: Observation,
|
|
68
|
+
) {
|
|
69
|
+
const start = marker.continuation!.validatedLegs;
|
|
70
|
+
const legs = await ctx.db
|
|
71
|
+
.query("financialSourceLegs")
|
|
72
|
+
.withIndex("by_allocation_id_and_leg_index", (q) => {
|
|
73
|
+
const allocation = q.eq("allocationId", row._id);
|
|
74
|
+
return start === 0 ? allocation : allocation.gte("legIndex", start);
|
|
75
|
+
})
|
|
76
|
+
.take(batchLimit + 1);
|
|
77
|
+
const batch = legs.slice(0, batchLimit);
|
|
78
|
+
if (
|
|
79
|
+
hasInvalidLegIdentity(row, legs) ||
|
|
80
|
+
batch.some((leg, i) => leg.legIndex !== start + i) ||
|
|
81
|
+
(legs.length <= batchLimit && start + batch.length !== row.transfers.length)
|
|
82
|
+
)
|
|
83
|
+
return hold(ctx, marker);
|
|
84
|
+
for (const leg of batch) {
|
|
85
|
+
const ownership = await legOwnership(ctx, leg.bucketKey, leg);
|
|
86
|
+
if (
|
|
87
|
+
JSON.stringify(ownership) !==
|
|
88
|
+
JSON.stringify({
|
|
89
|
+
registrationId: leg.registrationId,
|
|
90
|
+
userId: leg.userId,
|
|
91
|
+
orgId: leg.orgId,
|
|
92
|
+
ownershipStatus: leg.ownershipStatus,
|
|
93
|
+
})
|
|
94
|
+
) {
|
|
95
|
+
await ctx.db.patch("financialSourceLegs", leg._id, ownership);
|
|
96
|
+
await invalidate(ctx, [leg, { ...leg, ...ownership }]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const validatedLegs = start + batch.length;
|
|
100
|
+
await ctx.db.patch("financialSourceObservations", marker._id, {
|
|
101
|
+
generation: marker.generation + 1,
|
|
102
|
+
continuation: {
|
|
103
|
+
state: legs.length <= batchLimit ? "complete" : "validating",
|
|
104
|
+
nextLeg: marker.indexedLegCount,
|
|
105
|
+
validatedLegs,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/** One atomic bounded step. Callers drain explicitly; a stale acknowledgement never advances twice. */
|
|
110
|
+
export async function continueSettlementObservation(
|
|
111
|
+
ctx: MutationCtx,
|
|
112
|
+
marker: Observation,
|
|
113
|
+
expectedGeneration: number,
|
|
114
|
+
) {
|
|
115
|
+
if (
|
|
116
|
+
marker.generation !== expectedGeneration ||
|
|
117
|
+
marker.isContradictory ||
|
|
118
|
+
!marker.isOverflow ||
|
|
119
|
+
marker.continuation?.state === "held"
|
|
120
|
+
)
|
|
121
|
+
return;
|
|
122
|
+
const row = await ctx.db.get("settlementAllocations", marker.allocationId);
|
|
123
|
+
const candidates = await ctx.db
|
|
124
|
+
.query("settlementAllocations")
|
|
125
|
+
.withIndex("by_source_charge", (q) =>
|
|
126
|
+
q.eq("sourceChargeId", marker.sourceChargeId),
|
|
127
|
+
)
|
|
128
|
+
.take(2);
|
|
129
|
+
const cursor = marker.continuation;
|
|
130
|
+
if (
|
|
131
|
+
!row ||
|
|
132
|
+
candidates.length !== 1 ||
|
|
133
|
+
candidates[0]?._id !== row._id ||
|
|
134
|
+
marker.sourceChargeId !== row.sourceChargeId ||
|
|
135
|
+
marker.fingerprint !== (await settlementFingerprint(row)) ||
|
|
136
|
+
marker.legCount !== row.transfers.length ||
|
|
137
|
+
!Number.isSafeInteger(marker.generation + 1) ||
|
|
138
|
+
marker.generation < 1 ||
|
|
139
|
+
!Number.isInteger(marker.indexedLegCount) ||
|
|
140
|
+
marker.indexedLegCount < 0 ||
|
|
141
|
+
marker.indexedLegCount > row.transfers.length ||
|
|
142
|
+
(cursor
|
|
143
|
+
? cursor.nextLeg !== marker.indexedLegCount ||
|
|
144
|
+
!Number.isInteger(cursor.validatedLegs) ||
|
|
145
|
+
cursor.validatedLegs < 0 ||
|
|
146
|
+
cursor.validatedLegs > cursor.nextLeg ||
|
|
147
|
+
(cursor.state === "indexing" && cursor.validatedLegs !== 0) ||
|
|
148
|
+
(cursor.state === "validating" &&
|
|
149
|
+
cursor.nextLeg !== row.transfers.length) ||
|
|
150
|
+
(cursor.state === "complete" &&
|
|
151
|
+
(cursor.nextLeg !== row.transfers.length ||
|
|
152
|
+
cursor.validatedLegs !== row.transfers.length))
|
|
153
|
+
: marker.indexedLegCount !== 0)
|
|
154
|
+
)
|
|
155
|
+
return hold(ctx, marker);
|
|
156
|
+
if (cursor?.state === "complete") return;
|
|
157
|
+
if (cursor?.state === "validating") await validateBatch(ctx, row, marker);
|
|
158
|
+
else await indexBatch(ctx, row, marker);
|
|
159
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { MutationCtx, QueryCtx } from "../_generated/server.js";
|
|
2
|
+
|
|
3
|
+
export function bucketScope(bucketKey: string) {
|
|
4
|
+
return JSON.stringify(["bucket", bucketKey]);
|
|
5
|
+
}
|
|
6
|
+
export function ownerScope(userId: string, orgId?: string) {
|
|
7
|
+
return JSON.stringify(["owner", userId, orgId ?? null]);
|
|
8
|
+
}
|
|
9
|
+
export async function observationGeneration(ctx: QueryCtx, scopeKey: string) {
|
|
10
|
+
const rows = await ctx.db
|
|
11
|
+
.query("financialObservationGenerations")
|
|
12
|
+
.withIndex("by_scope_key", (q) => q.eq("scopeKey", scopeKey))
|
|
13
|
+
.take(2);
|
|
14
|
+
if (
|
|
15
|
+
rows.length > 1 ||
|
|
16
|
+
(rows[0] &&
|
|
17
|
+
(!Number.isSafeInteger(rows[0].generation) || rows[0].generation < 1))
|
|
18
|
+
)
|
|
19
|
+
return null;
|
|
20
|
+
return rows[0]?.generation ?? 0;
|
|
21
|
+
}
|
|
22
|
+
export async function advanceObservationGeneration(
|
|
23
|
+
ctx: MutationCtx,
|
|
24
|
+
scopeKey: string,
|
|
25
|
+
) {
|
|
26
|
+
const rows = await ctx.db
|
|
27
|
+
.query("financialObservationGenerations")
|
|
28
|
+
.withIndex("by_scope_key", (q) => q.eq("scopeKey", scopeKey))
|
|
29
|
+
.take(2);
|
|
30
|
+
if (rows.length > 1) return;
|
|
31
|
+
if (
|
|
32
|
+
rows[0] &&
|
|
33
|
+
(!Number.isSafeInteger(rows[0].generation) || rows[0].generation < 1)
|
|
34
|
+
)
|
|
35
|
+
return;
|
|
36
|
+
const generation = (rows[0]?.generation ?? 0) + 1;
|
|
37
|
+
if (!Number.isSafeInteger(generation)) {
|
|
38
|
+
if (rows[0])
|
|
39
|
+
await ctx.db.patch("financialObservationGenerations", rows[0]._id, {
|
|
40
|
+
generation: 0,
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (rows[0])
|
|
45
|
+
await ctx.db.patch("financialObservationGenerations", rows[0]._id, {
|
|
46
|
+
generation,
|
|
47
|
+
});
|
|
48
|
+
else
|
|
49
|
+
await ctx.db.insert("financialObservationGenerations", {
|
|
50
|
+
scopeKey,
|
|
51
|
+
generation,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { continueTransferBatch } from "./transferContinuation.js";
|
|
2
|
+
import { transferReaderState } from "./transferBatches.js";
|
|
3
|
+
import { transferBatchDoc } from "./transferSchema.js";
|
|
4
|
+
import { v } from "convex/values";
|
|
5
|
+
import { mutation } from "../_generated/server.js";
|
|
6
|
+
import { sourceDoc } from "./schema.js";
|
|
7
|
+
import { continueSettlementObservation } from "./continuation.js";
|
|
8
|
+
/** Trusted host mutation. Host must authorize access and explicitly drain bounded steps. */
|
|
9
|
+
export const resume = mutation({
|
|
10
|
+
args: {
|
|
11
|
+
allocationId: v.id("settlementAllocations"),
|
|
12
|
+
expectedGeneration: v.number(),
|
|
13
|
+
},
|
|
14
|
+
returns: v.object({
|
|
15
|
+
coverage: v.literal("unknown"),
|
|
16
|
+
observation: v.union(sourceDoc, v.null()),
|
|
17
|
+
}),
|
|
18
|
+
handler: async (ctx, args) => {
|
|
19
|
+
if (
|
|
20
|
+
!Number.isSafeInteger(args.expectedGeneration) ||
|
|
21
|
+
args.expectedGeneration < 1
|
|
22
|
+
)
|
|
23
|
+
throw new Error("Invalid observation generation");
|
|
24
|
+
const markers = await ctx.db
|
|
25
|
+
.query("financialSourceObservations")
|
|
26
|
+
.withIndex("by_allocation_id", (q) =>
|
|
27
|
+
q.eq("allocationId", args.allocationId),
|
|
28
|
+
)
|
|
29
|
+
.take(2);
|
|
30
|
+
const marker = markers.length === 1 ? markers[0]! : null;
|
|
31
|
+
if (marker)
|
|
32
|
+
await continueSettlementObservation(ctx, marker, args.expectedGeneration);
|
|
33
|
+
const current = marker
|
|
34
|
+
? await ctx.db.get("financialSourceObservations", marker._id)
|
|
35
|
+
: null;
|
|
36
|
+
return {
|
|
37
|
+
coverage: "unknown" as const,
|
|
38
|
+
observation:
|
|
39
|
+
current &&
|
|
40
|
+
Number.isSafeInteger(current.generation) &&
|
|
41
|
+
current.generation >= 1
|
|
42
|
+
? current
|
|
43
|
+
: null,
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/** Trusted host continuation; caller selects identity and generation, never cursor state. */
|
|
49
|
+
export const resumeTransferBatch = mutation({
|
|
50
|
+
args: {
|
|
51
|
+
batchId: v.id("financialTransferBatches"),
|
|
52
|
+
expectedGeneration: v.number(),
|
|
53
|
+
},
|
|
54
|
+
returns: v.object({
|
|
55
|
+
coverage: v.literal("unknown"),
|
|
56
|
+
producerGeneration: v.union(v.number(), v.null()),
|
|
57
|
+
hasDeferredWork: v.boolean(),
|
|
58
|
+
batch: v.union(transferBatchDoc, v.null()),
|
|
59
|
+
}),
|
|
60
|
+
handler: async (ctx, args) => {
|
|
61
|
+
if (
|
|
62
|
+
!Number.isSafeInteger(args.expectedGeneration) ||
|
|
63
|
+
args.expectedGeneration < 1
|
|
64
|
+
)
|
|
65
|
+
throw new Error("Invalid observation generation");
|
|
66
|
+
const batch = await ctx.db.get("financialTransferBatches", args.batchId);
|
|
67
|
+
if (batch) await continueTransferBatch(ctx, batch, args.expectedGeneration);
|
|
68
|
+
return {
|
|
69
|
+
...(await transferReaderState(ctx)),
|
|
70
|
+
batch: batch
|
|
71
|
+
? await ctx.db.get("financialTransferBatches", batch._id)
|
|
72
|
+
: null,
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
});
|