@fantastic.dev/better-stripe 0.59.0 → 0.60.1
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 +12 -0
- package/README.md +5 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +676 -50
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +322 -9
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +644 -18
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +338 -25
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/paymentSource/resolver.d.ts +12 -1
- package/dist/client/paymentSource/resolver.d.ts.map +1 -1
- package/dist/client/paymentSource/resolver.fixtures.d.ts +220 -0
- package/dist/client/paymentSource/resolver.fixtures.d.ts.map +1 -0
- package/dist/client/paymentSource/resolver.fixtures.js +189 -0
- package/dist/client/paymentSource/resolver.fixtures.js.map +1 -0
- package/dist/client/paymentSource/resolver.js +28 -3
- package/dist/client/paymentSource/resolver.js.map +1 -1
- package/dist/client/settlement/execution.d.ts +25 -0
- package/dist/client/settlement/execution.d.ts.map +1 -1
- package/dist/client/settlement/execution.js +2 -0
- package/dist/client/settlement/execution.js.map +1 -1
- package/dist/client/usage/catalogPrices.d.ts +1 -1
- package/dist/client/usage/catalogPrices.d.ts.map +1 -1
- package/dist/client/usage/catalogPrices.js +4 -4
- package/dist/client/usage/catalogPrices.js.map +1 -1
- package/dist/client/usage/client.d.ts +2 -2
- package/dist/client/usage/client.js +2 -2
- package/dist/client/usage/creditCapacity.d.ts.map +1 -1
- package/dist/client/usage/creditCapacity.js +7 -1
- package/dist/client/usage/creditCapacity.js.map +1 -1
- package/dist/client/usage/creditClient.d.ts +3 -0
- package/dist/client/usage/creditClient.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.js +4 -1
- package/dist/client/usage/creditDelivery.js.map +1 -1
- package/dist/client/usage/creditLifecycle.d.ts.map +1 -1
- package/dist/client/usage/creditLifecycle.js +2 -1
- package/dist/client/usage/creditLifecycle.js.map +1 -1
- package/dist/client/usage/creditNative.d.ts +3 -0
- package/dist/client/usage/creditNative.d.ts.map +1 -1
- package/dist/client/usage/creditNative.js +3 -0
- package/dist/client/usage/creditNative.js.map +1 -1
- package/dist/client/usage/creditProvider.d.ts.map +1 -1
- package/dist/client/usage/creditProvider.js +2 -1
- package/dist/client/usage/creditProvider.js.map +1 -1
- package/dist/client/usage/creditRead.d.ts +1 -0
- package/dist/client/usage/creditRead.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.js +11 -3
- package/dist/client/usage/creditReconciliation.js.map +1 -1
- package/dist/client/usage/creditRevenue.d.ts +22 -0
- package/dist/client/usage/creditRevenue.d.ts.map +1 -0
- package/dist/client/usage/creditRevenue.js +118 -0
- package/dist/client/usage/creditRevenue.js.map +1 -0
- package/dist/client/usage/creditSettlement.d.ts +29 -0
- package/dist/client/usage/creditSettlement.d.ts.map +1 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts +216 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -0
- package/dist/client/usage/creditSettlement.fixtures.js +59 -0
- package/dist/client/usage/creditSettlement.fixtures.js.map +1 -0
- package/dist/client/usage/creditSettlement.js +70 -0
- package/dist/client/usage/creditSettlement.js.map +1 -0
- package/dist/client/usage/creditTransport.fixtures.d.ts +8 -1
- package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.js +27 -5
- package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
- package/dist/client/usage/index.d.ts +4 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/client/usage/invoiceSale.d.ts +22 -0
- package/dist/client/usage/invoiceSale.d.ts.map +1 -0
- package/dist/client/usage/invoiceSale.fixtures.d.ts +285 -0
- package/dist/client/usage/invoiceSale.fixtures.d.ts.map +1 -0
- package/dist/client/usage/invoiceSale.fixtures.js +104 -0
- package/dist/client/usage/invoiceSale.fixtures.js.map +1 -0
- package/dist/client/usage/invoiceSale.js +117 -0
- package/dist/client/usage/invoiceSale.js.map +1 -0
- package/dist/client/usage/observationClient.d.ts +2 -2
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/client/usage/settlement.d.ts +32 -0
- package/dist/client/usage/settlement.d.ts.map +1 -0
- package/dist/client/usage/settlement.js +60 -0
- package/dist/client/usage/settlement.js.map +1 -0
- package/dist/client/usage/settlementClient.d.ts +51 -0
- package/dist/client/usage/settlementClient.d.ts.map +1 -0
- package/dist/client/usage/settlementClient.js +35 -0
- package/dist/client/usage/settlementClient.js.map +1 -0
- package/dist/client/usage/subscriptionCancellation.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCancellation.js +3 -2
- package/dist/client/usage/subscriptionCancellation.js.map +1 -1
- package/dist/client/usage/subscriptionCheckout.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCheckout.js +3 -2
- package/dist/client/usage/subscriptionCheckout.js.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.d.ts +1 -0
- package/dist/client/usage/subscriptionLifecycle.d.ts.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.js +3 -2
- package/dist/client/usage/subscriptionLifecycle.js.map +1 -1
- package/dist/client/usage/subscriptionProvider.d.ts +2 -0
- package/dist/client/usage/subscriptionProvider.d.ts.map +1 -1
- package/dist/client/usage/subscriptionProvider.js +9 -2
- package/dist/client/usage/subscriptionProvider.js.map +1 -1
- package/dist/component/_generated/component.d.ts +146 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/earnings/triggers.d.ts +172 -12
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -1
- package/dist/component/financialObservations/settlementHelpers.js +2 -0
- package/dist/component/financialObservations/settlementHelpers.js.map +1 -1
- package/dist/component/recoveryWorkflow/helpers.d.ts +25 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/mutations.d.ts +25 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/queries.d.ts +50 -0
- package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/schema.d.ts +566 -10
- package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -1
- package/dist/component/schema.d.ts +322 -9
- package/dist/component/settlement/mutations.d.ts +50 -0
- package/dist/component/settlement/mutations.d.ts.map +1 -1
- package/dist/component/settlement/mutations.js +8 -1
- package/dist/component/settlement/mutations.js.map +1 -1
- package/dist/component/settlement/queries.d.ts +25 -0
- package/dist/component/settlement/queries.d.ts.map +1 -1
- package/dist/component/settlement/schema.d.ts +128 -1
- package/dist/component/settlement/schema.d.ts.map +1 -1
- package/dist/component/settlement/schema.js +22 -0
- package/dist/component/settlement/schema.js.map +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +4212 -0
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -0
- package/dist/component/usage/allowance.fixtures.js +67 -0
- package/dist/component/usage/allowance.fixtures.js.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts +4235 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.js +89 -0
- package/dist/component/usageCredits/accounting.fixtures.js.map +1 -0
- package/dist/component/usageCredits/helpers.d.ts +7 -0
- package/dist/component/usageCredits/helpers.d.ts.map +1 -1
- package/dist/component/usageCredits/helpers.js +5 -0
- package/dist/component/usageCredits/helpers.js.map +1 -1
- package/dist/component/usageCredits/mutations.d.ts +6 -0
- package/dist/component/usageCredits/mutations.d.ts.map +1 -1
- package/dist/component/usageCredits/mutations.js +70 -7
- package/dist/component/usageCredits/mutations.js.map +1 -1
- package/dist/component/usageCredits/purchase.d.ts +1 -0
- package/dist/component/usageCredits/purchase.d.ts.map +1 -1
- package/dist/component/usageCredits/queries.d.ts +10 -0
- package/dist/component/usageCredits/queries.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.d.ts +24 -4
- package/dist/component/usageCredits/schema.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.js +4 -0
- package/dist/component/usageCredits/schema.js.map +1 -1
- package/dist/component/usageSubscriptions/history.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/history.js +3 -1
- package/dist/component/usageSubscriptions/history.js.map +1 -1
- package/dist/component/usageSubscriptions/mutations.d.ts +5 -0
- package/dist/component/usageSubscriptions/mutations.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/mutations.js +19 -0
- package/dist/component/usageSubscriptions/mutations.js.map +1 -1
- package/dist/component/usageSubscriptions/queries.d.ts +2 -0
- package/dist/component/usageSubscriptions/queries.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.d.ts +7 -2
- package/dist/component/usageSubscriptions/schema.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.js +1 -0
- package/dist/component/usageSubscriptions/schema.js.map +1 -1
- package/docs/README.md +4 -0
- package/docs/settlement.md +4 -0
- package/docs/usage-credits.md +6 -1
- package/docs/usage-security.md +130 -0
- package/docs/usage-settlement.md +162 -0
- package/package.json +3 -1
- package/src/client/index.ts +9 -0
- package/src/client/paymentSource/resolver.fixtures.ts +216 -0
- package/src/client/paymentSource/resolver.test.ts +11 -207
- package/src/client/paymentSource/resolver.ts +54 -2
- package/src/client/settlement/execution.ts +24 -0
- package/src/client/usage/catalogPrices.ts +4 -2
- package/src/client/usage/client.ts +2 -2
- package/src/client/usage/creditBackingIsolation.test.ts +110 -0
- package/src/client/usage/creditCapacity.test.ts +80 -1
- package/src/client/usage/creditCapacity.ts +10 -1
- package/src/client/usage/creditDelivery.ts +4 -0
- package/src/client/usage/creditLifecycle.ts +2 -0
- package/src/client/usage/creditNative.ts +5 -0
- package/src/client/usage/creditProofBounds.test.ts +187 -0
- package/src/client/usage/creditProvider.test.ts +74 -0
- package/src/client/usage/creditProvider.ts +2 -0
- package/src/client/usage/creditReconciliation.ts +18 -3
- package/src/client/usage/creditRevenue.test.ts +105 -0
- package/src/client/usage/creditRevenue.ts +163 -0
- package/src/client/usage/creditSettlement.fixtures.ts +70 -0
- package/src/client/usage/creditSettlement.test.ts +66 -0
- package/src/client/usage/creditSettlement.ts +94 -0
- package/src/client/usage/creditSettlementRecovery.test.ts +173 -0
- package/src/client/usage/creditTransport.fixtures.ts +31 -5
- package/src/client/usage/index.ts +9 -0
- package/src/client/usage/invoiceSale.fixtures.ts +103 -0
- package/src/client/usage/invoiceSale.test.ts +184 -0
- package/src/client/usage/invoiceSale.ts +155 -0
- package/src/client/usage/settlement.test.ts +179 -0
- package/src/client/usage/settlement.ts +81 -0
- package/src/client/usage/settlementClient.ts +84 -0
- package/src/client/usage/subscriptionCancellation.ts +3 -2
- package/src/client/usage/subscriptionCheckout.ts +7 -2
- package/src/client/usage/subscriptionClient.test.ts +60 -1
- package/src/client/usage/subscriptionLifecycle.ts +3 -2
- package/src/client/usage/subscriptionProvider.ts +27 -3
- package/src/component/_generated/component.ts +150 -1
- package/src/component/financialObservations/settlementHelpers.ts +2 -0
- package/src/component/settlement/mutations.ts +10 -1
- package/src/component/settlement/schema.ts +26 -0
- package/src/component/settlement/settlement-claims.test.ts +54 -0
- package/src/component/usage/allowance.fixtures.ts +77 -0
- package/src/component/usage/allowance.test.ts +3 -66
- package/src/component/usage/allowanceBounds.test.ts +79 -0
- package/src/component/usage/historyIsolation.test.ts +124 -0
- package/src/component/usageCredits/accounting.fixtures.ts +96 -0
- package/src/component/usageCredits/accounting.test.ts +176 -82
- package/src/component/usageCredits/freshness.test.ts +33 -0
- package/src/component/usageCredits/helpers.ts +8 -0
- package/src/component/usageCredits/mutations.ts +83 -5
- package/src/component/usageCredits/quotas.test.ts +63 -0
- package/src/component/usageCredits/schema.ts +10 -0
- package/src/component/usageSubscriptions/history.test.ts +57 -0
- package/src/component/usageSubscriptions/history.ts +3 -1
- package/src/component/usageSubscriptions/mutations.ts +23 -0
- package/src/component/usageSubscriptions/schema.ts +1 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { freshCreditProof } from "./helpers.js";
|
|
1
2
|
import { canonicalFingerprint } from "../operationEvidence/canonical.js";
|
|
2
3
|
import { accountTerms, grants } from "./helpers.js";
|
|
3
4
|
import {
|
|
@@ -109,6 +110,7 @@ export const freezePurchase = mutation({
|
|
|
109
110
|
throw new Error("Invalid original credit refund amount");
|
|
110
111
|
await ctx.db.patch("usageCreditPurchases", row._id, {
|
|
111
112
|
state: "frozen",
|
|
113
|
+
recoveryReason: args.reason,
|
|
112
114
|
refundedAmount: Math.max(
|
|
113
115
|
row.refundedAmount ?? 0,
|
|
114
116
|
args.refundedAmount ?? 0,
|
|
@@ -140,7 +142,12 @@ export const freezePurchase = mutation({
|
|
|
140
142
|
});
|
|
141
143
|
|
|
142
144
|
export const saveObservation = mutation({
|
|
143
|
-
args: {
|
|
145
|
+
args: {
|
|
146
|
+
inputJson: v.string(),
|
|
147
|
+
proofJson: v.string(),
|
|
148
|
+
startedAt: v.number(),
|
|
149
|
+
proofStartedAt: v.optional(v.number()),
|
|
150
|
+
},
|
|
144
151
|
returns: v.string(),
|
|
145
152
|
handler: async (ctx, args) => {
|
|
146
153
|
if (
|
|
@@ -149,6 +156,15 @@ export const saveObservation = mutation({
|
|
|
149
156
|
args.startedAt > Date.now() + 300000
|
|
150
157
|
)
|
|
151
158
|
throw new Error("Invalid credit observation time");
|
|
159
|
+
const proofStartedAt =
|
|
160
|
+
args.proofStartedAt ?? Math.min(args.startedAt, Date.now());
|
|
161
|
+
if (
|
|
162
|
+
!Number.isSafeInteger(proofStartedAt) ||
|
|
163
|
+
proofStartedAt < 0 ||
|
|
164
|
+
proofStartedAt > Date.now() ||
|
|
165
|
+
proofStartedAt > args.startedAt
|
|
166
|
+
)
|
|
167
|
+
throw new Error("Invalid credit proof wall time");
|
|
152
168
|
const parsed = await purchaseTerms(args.inputJson),
|
|
153
169
|
row = await purchase(ctx, parsed.key);
|
|
154
170
|
const proof = creditNativeObservation.parse(
|
|
@@ -183,11 +199,33 @@ export const saveObservation = mutation({
|
|
|
183
199
|
for (const tx of proof.transactions)
|
|
184
200
|
await history(ctx, row.accountKey, "observation", tx.id, tx, row.key);
|
|
185
201
|
const ledger = await account(ctx, row.accountKey);
|
|
186
|
-
|
|
202
|
+
const tiedConflict =
|
|
203
|
+
grant.observedAt === args.startedAt &&
|
|
204
|
+
(await canonicalFingerprint(proof)) !==
|
|
205
|
+
(await canonicalFingerprint(
|
|
206
|
+
grant.nativeObservationJson
|
|
207
|
+
? JSON.parse(grant.nativeObservationJson)
|
|
208
|
+
: null,
|
|
209
|
+
));
|
|
210
|
+
if ((grant.observedAt ?? -1) >= args.startedAt) {
|
|
211
|
+
result.backing = "held";
|
|
212
|
+
result.reason = "stale_native_observation";
|
|
213
|
+
if (tiedConflict) {
|
|
214
|
+
await ctx.db.patch("usageCreditGrants", grant._id, {
|
|
215
|
+
observationConflictAt: args.startedAt,
|
|
216
|
+
});
|
|
217
|
+
await ctx.db.patch("usageCreditAccounts", ledger._id, {
|
|
218
|
+
frozenReason: "observation_order_conflict",
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if ((grant.observedAt ?? -1) < args.startedAt) {
|
|
187
223
|
await ctx.db.patch("usageCreditGrants", grant._id, {
|
|
188
224
|
nativeUnused: proof.ledgerAmount > 0,
|
|
189
225
|
nativeObservationJson: JSON.stringify(proof),
|
|
190
226
|
observedAt: args.startedAt,
|
|
227
|
+
proofStartedAt,
|
|
228
|
+
observationConflictAt: undefined,
|
|
191
229
|
...(proof.expired > 0 || proof.voided > 0
|
|
192
230
|
? { frozen: true, nativeExpired: true }
|
|
193
231
|
: {}),
|
|
@@ -232,6 +270,8 @@ export const closeAccount = mutation({
|
|
|
232
270
|
);
|
|
233
271
|
const ledger = await account(ctx, original.key);
|
|
234
272
|
if (ledger.closed) return null;
|
|
273
|
+
if (ledger.frozenReason)
|
|
274
|
+
throw new Error("Credit recovery reconciliation required before closure");
|
|
235
275
|
const purchases = await ctx.db
|
|
236
276
|
.query("usageCreditPurchases")
|
|
237
277
|
.withIndex("by_account", (q) => q.eq("accountKey", original.key))
|
|
@@ -247,6 +287,14 @@ export const closeAccount = mutation({
|
|
|
247
287
|
: null;
|
|
248
288
|
const purchaseRow = await purchase(ctx, grant.purchaseKey),
|
|
249
289
|
terms = (await purchaseTerms(purchaseRow.termsJson)).terms;
|
|
290
|
+
if (
|
|
291
|
+
purchaseRow.state === "frozen" &&
|
|
292
|
+
(purchaseRow.recoveryReason !== "payment_refunded" ||
|
|
293
|
+
purchaseRow.refundedAmount !== terms.amount)
|
|
294
|
+
)
|
|
295
|
+
throw new Error(
|
|
296
|
+
"Original payment recovery requires reconciliation before closure",
|
|
297
|
+
);
|
|
250
298
|
if (
|
|
251
299
|
BigInt(grant.available) > 0n &&
|
|
252
300
|
!(
|
|
@@ -263,6 +311,8 @@ export const closeAccount = mutation({
|
|
|
263
311
|
: creditCost(terms.binding.plan, grant.consumed);
|
|
264
312
|
if (
|
|
265
313
|
BigInt(grant.reserved) !== 0n ||
|
|
314
|
+
grant.observationConflictAt !== undefined ||
|
|
315
|
+
!freshCreditProof(grant) ||
|
|
266
316
|
!proof?.complete ||
|
|
267
317
|
!proof.invoicesVerified ||
|
|
268
318
|
proof.ledgerAmount !== 0 ||
|
|
@@ -338,9 +388,10 @@ export const prepareRefund = mutation({
|
|
|
338
388
|
: null;
|
|
339
389
|
const terms = (await purchaseTerms(row.termsJson)).terms;
|
|
340
390
|
if (
|
|
391
|
+
grant.observationConflictAt !== undefined ||
|
|
341
392
|
!proof?.complete ||
|
|
342
393
|
!proof.invoicesVerified ||
|
|
343
|
-
|
|
394
|
+
!freshCreditProof(grant) ||
|
|
344
395
|
proof.ledgerAmount !== terms.amount ||
|
|
345
396
|
proof.applied !== 0 ||
|
|
346
397
|
proof.reinstated !== 0 ||
|
|
@@ -382,10 +433,11 @@ export const resumeAccount = mutation({
|
|
|
382
433
|
? creditNativeObservation.parse(JSON.parse(grant.nativeObservationJson))
|
|
383
434
|
: null;
|
|
384
435
|
if (
|
|
436
|
+
grant.observationConflictAt !== undefined ||
|
|
385
437
|
!proof ||
|
|
386
438
|
!proof.complete ||
|
|
387
439
|
!proof.invoicesVerified ||
|
|
388
|
-
|
|
440
|
+
!freshCreditProof(grant) ||
|
|
389
441
|
normalizeCreditState(terms, grant, proof, true).backing !==
|
|
390
442
|
"sufficient" ||
|
|
391
443
|
(row.state === "frozen" &&
|
|
@@ -398,7 +450,7 @@ export const resumeAccount = mutation({
|
|
|
398
450
|
);
|
|
399
451
|
evidence.push({
|
|
400
452
|
creditGrantId: grant.creditGrantId,
|
|
401
|
-
observedAt: grant.observedAt!,
|
|
453
|
+
observedAt: grant.proofStartedAt ?? grant.observedAt!,
|
|
402
454
|
});
|
|
403
455
|
}
|
|
404
456
|
await ctx.db.patch("usageCreditAccounts", ledger._id, {
|
|
@@ -449,3 +501,29 @@ export const cancelUnstarted = mutation({
|
|
|
449
501
|
return null;
|
|
450
502
|
},
|
|
451
503
|
});
|
|
504
|
+
|
|
505
|
+
/** Trusted host reserves a unique observation order before reading original native backing. */
|
|
506
|
+
export const beginObservation = mutation({
|
|
507
|
+
args: { inputJson: v.string() },
|
|
508
|
+
returns: v.number(),
|
|
509
|
+
handler: async (ctx, args) => {
|
|
510
|
+
const parsed = await purchaseTerms(args.inputJson),
|
|
511
|
+
row = await purchase(ctx, parsed.key);
|
|
512
|
+
const grant = await ctx.db
|
|
513
|
+
.query("usageCreditGrants")
|
|
514
|
+
.withIndex("by_purchase", (q) => q.eq("purchaseKey", row.key))
|
|
515
|
+
.unique();
|
|
516
|
+
if (!grant) throw new Error("Credit grant unavailable");
|
|
517
|
+
const startedAt = Math.max(
|
|
518
|
+
Date.now(),
|
|
519
|
+
(grant.observedAt ?? -1) + 1,
|
|
520
|
+
(grant.observationStartedAt ?? -1) + 1,
|
|
521
|
+
);
|
|
522
|
+
if (!Number.isSafeInteger(startedAt) || startedAt > Date.now() + 300000)
|
|
523
|
+
throw new Error("Credit observation order capacity exhausted");
|
|
524
|
+
await ctx.db.patch("usageCreditGrants", grant._id, {
|
|
525
|
+
observationStartedAt: startedAt,
|
|
526
|
+
});
|
|
527
|
+
return startedAt;
|
|
528
|
+
},
|
|
529
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { makeFunctionReference } from "convex/server";
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import { setupCredits } from "./accounting.fixtures.js";
|
|
4
|
+
const ref = (name: string) =>
|
|
5
|
+
makeFunctionReference<"mutation">(`usageCredits/mutations:${name}`);
|
|
6
|
+
afterEach(() => vi.useRealTimers());
|
|
7
|
+
it("accepts the 1000th retained purchase, preserves replay and rejects 1001 without writing", async () => {
|
|
8
|
+
const { t, purchase } = await setupCredits();
|
|
9
|
+
await t.run(async (ctx) => {
|
|
10
|
+
const row = (await ctx.db.query("usageCreditAccounts").first())!;
|
|
11
|
+
await ctx.db.patch(row._id, { purchaseCount: 999 });
|
|
12
|
+
});
|
|
13
|
+
const inputJson = JSON.stringify({
|
|
14
|
+
...purchase,
|
|
15
|
+
purchaseId: "last_retained",
|
|
16
|
+
});
|
|
17
|
+
const last = await t.mutation(ref("preparePurchase"), { inputJson });
|
|
18
|
+
expect(await t.mutation(ref("preparePurchase"), { inputJson })).toEqual(last);
|
|
19
|
+
await expect(
|
|
20
|
+
t.mutation(ref("preparePurchase"), {
|
|
21
|
+
inputJson: JSON.stringify({ ...purchase, purchaseId: "over_retained" }),
|
|
22
|
+
}),
|
|
23
|
+
).rejects.toThrow(/purchase quota/i);
|
|
24
|
+
await t.run(async (ctx) => {
|
|
25
|
+
expect(await ctx.db.query("usageCreditAccounts").first()).toMatchObject({
|
|
26
|
+
purchaseCount: 1000,
|
|
27
|
+
});
|
|
28
|
+
expect(await ctx.db.query("usageCreditPurchases").take(3)).toHaveLength(2);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
it("commits the 100000th credit audit entry and rolls back the next reservation", async () => {
|
|
32
|
+
const { t, event, subscription } = await setupCredits();
|
|
33
|
+
await t.run(async (ctx) => {
|
|
34
|
+
const row = (await ctx.db.query("usageCreditAccounts").first())!;
|
|
35
|
+
await ctx.db.patch(row._id, { historyCount: 99999 });
|
|
36
|
+
});
|
|
37
|
+
const args = {
|
|
38
|
+
inputJson: JSON.stringify({ ...event, quantity: "1" }),
|
|
39
|
+
subscriptionJson: JSON.stringify(subscription),
|
|
40
|
+
};
|
|
41
|
+
const last = await t.mutation(ref("reserve"), args);
|
|
42
|
+
expect(await t.mutation(ref("reserve"), args)).toEqual(last);
|
|
43
|
+
await expect(
|
|
44
|
+
t.mutation(ref("reserve"), {
|
|
45
|
+
...args,
|
|
46
|
+
inputJson: JSON.stringify({
|
|
47
|
+
...event,
|
|
48
|
+
quantity: "1",
|
|
49
|
+
sourceEventId: "over_retained",
|
|
50
|
+
}),
|
|
51
|
+
}),
|
|
52
|
+
).rejects.toThrow(/history.*quota/i);
|
|
53
|
+
await t.run(async (ctx) => {
|
|
54
|
+
expect(await ctx.db.query("usageCreditAccounts").first()).toMatchObject({
|
|
55
|
+
historyCount: 100000,
|
|
56
|
+
});
|
|
57
|
+
expect(await ctx.db.query("usageReservations").take(2)).toHaveLength(1);
|
|
58
|
+
expect(await ctx.db.query("usageCreditGrants").first()).toMatchObject({
|
|
59
|
+
available: "9",
|
|
60
|
+
reserved: "1",
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -33,6 +33,13 @@ export const purchaseFields = {
|
|
|
33
33
|
paymentKey: v.optional(v.string()),
|
|
34
34
|
paymentProofJson: v.optional(v.string()),
|
|
35
35
|
refundedAmount: v.optional(v.number()),
|
|
36
|
+
recoveryReason: v.optional(
|
|
37
|
+
v.union(
|
|
38
|
+
v.literal("payment_refunded"),
|
|
39
|
+
v.literal("payment_disputed"),
|
|
40
|
+
v.literal("native_state_changed"),
|
|
41
|
+
),
|
|
42
|
+
),
|
|
36
43
|
grantFirstAttemptAt: v.optional(v.number()),
|
|
37
44
|
creditGrantId: v.optional(v.string()),
|
|
38
45
|
};
|
|
@@ -65,6 +72,9 @@ export const grantFields = {
|
|
|
65
72
|
nativeExpired: v.optional(v.boolean()),
|
|
66
73
|
nativeObservationJson: v.optional(v.string()),
|
|
67
74
|
observedAt: v.optional(v.number()),
|
|
75
|
+
proofStartedAt: v.optional(v.number()),
|
|
76
|
+
observationStartedAt: v.optional(v.number()),
|
|
77
|
+
observationConflictAt: v.optional(v.number()),
|
|
68
78
|
};
|
|
69
79
|
export const grantDoc = v.object({
|
|
70
80
|
_id: v.id("usageCreditGrants"),
|
|
@@ -67,3 +67,60 @@ it("retains original and renewed snapshots, deduplicates replay and keeps newer
|
|
|
67
67
|
}),
|
|
68
68
|
).rejects.toThrow();
|
|
69
69
|
});
|
|
70
|
+
|
|
71
|
+
it("retains the newer projection when conflicting native observations have the same timestamp", async () => {
|
|
72
|
+
const t = convexTest(schema, modules),
|
|
73
|
+
now = Date.now();
|
|
74
|
+
const intent = await t.mutation(prepare, {
|
|
75
|
+
termsJson: JSON.stringify(request),
|
|
76
|
+
method: "direct",
|
|
77
|
+
});
|
|
78
|
+
const renewed = nativeSubscription();
|
|
79
|
+
renewed.items.data.forEach((item) => {
|
|
80
|
+
item.current_period_start = 1200;
|
|
81
|
+
item.current_period_end = 1800;
|
|
82
|
+
});
|
|
83
|
+
await t.mutation(save, {
|
|
84
|
+
key: intent.key,
|
|
85
|
+
nativeJson: JSON.stringify(renewed),
|
|
86
|
+
startedAt: now,
|
|
87
|
+
});
|
|
88
|
+
await t.mutation(save, {
|
|
89
|
+
key: intent.key,
|
|
90
|
+
nativeJson: JSON.stringify(nativeSubscription()),
|
|
91
|
+
startedAt: now,
|
|
92
|
+
});
|
|
93
|
+
const scope = {
|
|
94
|
+
stripeAccountId: "acct_platform",
|
|
95
|
+
customerAccount: "acct_buyer",
|
|
96
|
+
livemode: false,
|
|
97
|
+
};
|
|
98
|
+
const projection = await t.query(get, { scope, requestId: "signup1" });
|
|
99
|
+
expect(
|
|
100
|
+
JSON.parse(projection.viewJson).bindings[0].subscription.period.start,
|
|
101
|
+
).toBe(1200);
|
|
102
|
+
const page = await t.query(history, {
|
|
103
|
+
scope,
|
|
104
|
+
requestId: "signup1",
|
|
105
|
+
kind: "subscription",
|
|
106
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
107
|
+
});
|
|
108
|
+
expect(page.page).toHaveLength(2);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("reserves distinct ordered observations before native I/O in a concurrent clock tick", async () => {
|
|
112
|
+
const t = convexTest(schema, modules);
|
|
113
|
+
const intent = await t.mutation(prepare, {
|
|
114
|
+
termsJson: JSON.stringify(request),
|
|
115
|
+
method: "direct",
|
|
116
|
+
});
|
|
117
|
+
const begin = makeFunctionReference<"mutation">(
|
|
118
|
+
"usageSubscriptions/mutations:beginObservation",
|
|
119
|
+
);
|
|
120
|
+
const stamps = await Promise.all([
|
|
121
|
+
t.mutation(begin, { key: intent.key }),
|
|
122
|
+
t.mutation(begin, { key: intent.key }),
|
|
123
|
+
]);
|
|
124
|
+
expect(new Set(stamps).size).toBe(2);
|
|
125
|
+
expect(stamps.every(Number.isSafeInteger)).toBe(true);
|
|
126
|
+
});
|
|
@@ -58,7 +58,9 @@ export async function saveView(
|
|
|
58
58
|
view,
|
|
59
59
|
startedAt,
|
|
60
60
|
);
|
|
61
|
-
if ((intent.startedAt ?? -1)
|
|
61
|
+
if ((intent.startedAt ?? -1) >= startedAt && intent.viewJson)
|
|
62
|
+
return JSON.parse(intent.viewJson) as ReturnType<typeof subscriptionView>;
|
|
63
|
+
if ((intent.startedAt ?? -1) < startedAt)
|
|
62
64
|
await ctx.db.patch("usageSubscriptionIntents", intent._id, {
|
|
63
65
|
providerId: view.subscriptionId,
|
|
64
66
|
viewJson: JSON.stringify(view),
|
|
@@ -273,3 +273,26 @@ export const abandon = mutation({
|
|
|
273
273
|
return null;
|
|
274
274
|
},
|
|
275
275
|
});
|
|
276
|
+
|
|
277
|
+
/** Reserve a unique order before native I/O, including concurrent same-clock observations. */
|
|
278
|
+
export const beginObservation = mutation({
|
|
279
|
+
args: { key: v.string() },
|
|
280
|
+
returns: v.number(),
|
|
281
|
+
handler: async (ctx, args) => {
|
|
282
|
+
const intent = await ctx.db
|
|
283
|
+
.query("usageSubscriptionIntents")
|
|
284
|
+
.withIndex("by_key", (q) => q.eq("key", args.key))
|
|
285
|
+
.unique();
|
|
286
|
+
if (!intent) throw new Error("Subscription intent not found");
|
|
287
|
+
const startedAt = Math.max(
|
|
288
|
+
Date.now(),
|
|
289
|
+
(intent.startedAt ?? -1) + 1,
|
|
290
|
+
(intent.observationStartedAt ?? -1) + 1,
|
|
291
|
+
);
|
|
292
|
+
observationStart(startedAt);
|
|
293
|
+
await ctx.db.patch("usageSubscriptionIntents", intent._id, {
|
|
294
|
+
observationStartedAt: startedAt,
|
|
295
|
+
});
|
|
296
|
+
return startedAt;
|
|
297
|
+
},
|
|
298
|
+
});
|
|
@@ -11,6 +11,7 @@ export const intentFields = {
|
|
|
11
11
|
checkoutId: v.optional(v.string()),
|
|
12
12
|
viewJson: v.optional(v.string()),
|
|
13
13
|
startedAt: v.optional(v.number()),
|
|
14
|
+
observationStartedAt: v.optional(v.number()),
|
|
14
15
|
controlKey: v.optional(v.string()),
|
|
15
16
|
historyCount: v.optional(v.number()),
|
|
16
17
|
retiredAt: v.optional(v.number()),
|