@fantastic.dev/better-stripe 0.57.0 → 0.59.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 +12 -0
- package/README.md +3 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +356 -50
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +162 -9
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +356 -50
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +162 -9
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/usage/allowanceClient.d.ts +51 -0
- package/dist/client/usage/allowanceClient.d.ts.map +1 -0
- package/dist/client/usage/allowanceClient.js +64 -0
- package/dist/client/usage/allowanceClient.js.map +1 -0
- package/dist/client/usage/allowanceTerms.d.ts +64 -0
- package/dist/client/usage/allowanceTerms.d.ts.map +1 -0
- package/dist/client/usage/allowanceTerms.js +101 -0
- package/dist/client/usage/allowanceTerms.js.map +1 -0
- package/dist/client/usage/client.d.ts +2 -2
- package/dist/client/usage/client.d.ts.map +1 -1
- package/dist/client/usage/client.js +2 -2
- package/dist/client/usage/client.js.map +1 -1
- package/dist/client/usage/correctionClient.d.ts +33 -0
- package/dist/client/usage/correctionClient.d.ts.map +1 -0
- package/dist/client/usage/correctionClient.js +27 -0
- package/dist/client/usage/correctionClient.js.map +1 -0
- package/dist/client/usage/correctionEligibility.d.ts +4 -0
- package/dist/client/usage/correctionEligibility.d.ts.map +1 -0
- package/dist/client/usage/correctionEligibility.js +7 -0
- package/dist/client/usage/correctionEligibility.js.map +1 -0
- package/dist/client/usage/correctionInvoice.d.ts +9 -0
- package/dist/client/usage/correctionInvoice.d.ts.map +1 -0
- package/dist/client/usage/correctionInvoice.js +218 -0
- package/dist/client/usage/correctionInvoice.js.map +1 -0
- package/dist/client/usage/correctionInvoiceProof.d.ts +29 -0
- package/dist/client/usage/correctionInvoiceProof.d.ts.map +1 -0
- package/dist/client/usage/correctionInvoiceProof.js +41 -0
- package/dist/client/usage/correctionInvoiceProof.js.map +1 -0
- package/dist/client/usage/correctionProofs.d.ts +36 -0
- package/dist/client/usage/correctionProofs.d.ts.map +1 -0
- package/dist/client/usage/correctionProofs.js +41 -0
- package/dist/client/usage/correctionProofs.js.map +1 -0
- package/dist/client/usage/correctionProvider.d.ts +6 -0
- package/dist/client/usage/correctionProvider.d.ts.map +1 -0
- package/dist/client/usage/correctionProvider.js +243 -0
- package/dist/client/usage/correctionProvider.js.map +1 -0
- package/dist/client/usage/correctionRead.d.ts +27 -0
- package/dist/client/usage/correctionRead.d.ts.map +1 -0
- package/dist/client/usage/correctionRead.js +36 -0
- package/dist/client/usage/correctionRead.js.map +1 -0
- package/dist/client/usage/correctionTerms.d.ts +64 -0
- package/dist/client/usage/correctionTerms.d.ts.map +1 -0
- package/dist/client/usage/correctionTerms.js +65 -0
- package/dist/client/usage/correctionTerms.js.map +1 -0
- package/dist/client/usage/correctionTransport.fixtures.d.ts +37 -0
- package/dist/client/usage/correctionTransport.fixtures.d.ts.map +1 -0
- package/dist/client/usage/correctionTransport.fixtures.js +322 -0
- package/dist/client/usage/correctionTransport.fixtures.js.map +1 -0
- package/dist/client/usage/creditClient.d.ts +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/observationClient.d.ts +20 -0
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/client/usage/observationClient.js +20 -6
- package/dist/client/usage/observationClient.js.map +1 -1
- package/dist/client/usage/records.d.ts +1 -1
- package/dist/client/usage/records.d.ts.map +1 -1
- package/dist/client/usage/records.js.map +1 -1
- package/dist/client/usage/summary.d.ts +3 -0
- package/dist/client/usage/summary.d.ts.map +1 -1
- package/dist/client/usage/summary.js +9 -4
- package/dist/client/usage/summary.js.map +1 -1
- package/dist/client/usageExports.d.ts +4 -0
- package/dist/client/usageExports.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +20 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +324 -13
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/accountClosure/mutations.d.ts.map +1 -1
- package/dist/component/accountClosure/mutations.js +13 -0
- package/dist/component/accountClosure/mutations.js.map +1 -1
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/mutations.d.ts +43 -43
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/earnings/triggers.d.ts +288 -10
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/schema.d.ts +162 -9
- package/dist/component/schema.js +7 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/mutations.d.ts.map +1 -1
- package/dist/component/usage/mutations.js +4 -0
- package/dist/component/usage/mutations.js.map +1 -1
- package/dist/component/usage/queries.d.ts +2 -2
- package/dist/component/usage/schema.d.ts +21 -15
- package/dist/component/usage/schema.d.ts.map +1 -1
- package/dist/component/usage/schema.js +4 -2
- package/dist/component/usage/schema.js.map +1 -1
- package/dist/component/usageAllowance/helpers.d.ts +78 -0
- package/dist/component/usageAllowance/helpers.d.ts.map +1 -0
- package/dist/component/usageAllowance/helpers.js +99 -0
- package/dist/component/usageAllowance/helpers.js.map +1 -0
- package/dist/component/usageAllowance/mutations.d.ts +37 -0
- package/dist/component/usageAllowance/mutations.d.ts.map +1 -0
- package/dist/component/usageAllowance/mutations.js +99 -0
- package/dist/component/usageAllowance/mutations.js.map +1 -0
- package/dist/component/usageAllowance/queries.d.ts +36 -0
- package/dist/component/usageAllowance/queries.d.ts.map +1 -0
- package/dist/component/usageAllowance/queries.js +122 -0
- package/dist/component/usageAllowance/queries.js.map +1 -0
- package/dist/component/usageAllowance/reservations.d.ts +25 -0
- package/dist/component/usageAllowance/reservations.d.ts.map +1 -0
- package/dist/component/usageAllowance/reservations.js +150 -0
- package/dist/component/usageAllowance/reservations.js.map +1 -0
- package/dist/component/usageAllowance/schema.d.ts +77 -0
- package/dist/component/usageAllowance/schema.d.ts.map +1 -0
- package/dist/component/usageAllowance/schema.js +32 -0
- package/dist/component/usageAllowance/schema.js.map +1 -0
- package/dist/component/usageCorrections/accounting.d.ts +5 -0
- package/dist/component/usageCorrections/accounting.d.ts.map +1 -0
- package/dist/component/usageCorrections/accounting.js +55 -0
- package/dist/component/usageCorrections/accounting.js.map +1 -0
- package/dist/component/usageCorrections/cancellation.d.ts +6 -0
- package/dist/component/usageCorrections/cancellation.d.ts.map +1 -0
- package/dist/component/usageCorrections/cancellation.js +29 -0
- package/dist/component/usageCorrections/cancellation.js.map +1 -0
- package/dist/component/usageCorrections/helpers.d.ts +60 -0
- package/dist/component/usageCorrections/helpers.d.ts.map +1 -0
- package/dist/component/usageCorrections/helpers.js +52 -0
- package/dist/component/usageCorrections/helpers.js.map +1 -0
- package/dist/component/usageCorrections/invoice.d.ts +38 -0
- package/dist/component/usageCorrections/invoice.d.ts.map +1 -0
- package/dist/component/usageCorrections/invoice.js +82 -0
- package/dist/component/usageCorrections/invoice.js.map +1 -0
- package/dist/component/usageCorrections/mutations.d.ts +121 -0
- package/dist/component/usageCorrections/mutations.d.ts.map +1 -0
- package/dist/component/usageCorrections/mutations.js +256 -0
- package/dist/component/usageCorrections/mutations.js.map +1 -0
- package/dist/component/usageCorrections/queries.d.ts +115 -0
- package/dist/component/usageCorrections/queries.d.ts.map +1 -0
- package/dist/component/usageCorrections/queries.js +84 -0
- package/dist/component/usageCorrections/queries.js.map +1 -0
- package/dist/component/usageCorrections/schema.d.ts +248 -0
- package/dist/component/usageCorrections/schema.d.ts.map +1 -0
- package/dist/component/usageCorrections/schema.js +74 -0
- package/dist/component/usageCorrections/schema.js.map +1 -0
- package/dist/component/usageCredits/queries.d.ts +1 -1
- package/dist/component/usageCredits/queries.d.ts.map +1 -1
- package/dist/component/usageCredits/queries.js +6 -0
- package/dist/component/usageCredits/queries.js.map +1 -1
- package/dist/component/usageCredits/schema.d.ts +5 -5
- package/dist/component/usageCredits/schema.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.js +1 -1
- package/dist/component/usageCredits/schema.js.map +1 -1
- package/dist/component/usageDelivery/fields.d.ts +2 -2
- package/dist/component/usageDelivery/fields.d.ts.map +1 -1
- package/dist/component/usageDelivery/fields.js +1 -1
- package/dist/component/usageDelivery/fields.js.map +1 -1
- package/dist/component/usageDelivery/helpers.d.ts +7 -6
- package/dist/component/usageDelivery/helpers.d.ts.map +1 -1
- package/dist/component/usageDelivery/mutations.d.ts +6 -6
- package/dist/component/usageDelivery/mutations.d.ts.map +1 -1
- package/dist/component/usageDelivery/mutations.js +9 -1
- package/dist/component/usageDelivery/mutations.js.map +1 -1
- package/dist/component/usageDelivery/queries.d.ts +8 -6
- package/dist/component/usageDelivery/queries.d.ts.map +1 -1
- package/dist/component/usageDelivery/schema.d.ts +4 -4
- package/dist/component/usageDelivery/steps.d.ts +2 -2
- package/dist/component/usageDelivery/steps.js +1 -1
- package/dist/component/usageDelivery/steps.js.map +1 -1
- package/dist/component/usageObservations/queries.d.ts.map +1 -1
- package/dist/component/usageObservations/queries.js +4 -2
- package/dist/component/usageObservations/queries.js.map +1 -1
- package/dist/component/usageSubscriptions/retirement.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/retirement.js +24 -2
- package/dist/component/usageSubscriptions/retirement.js.map +1 -1
- package/docs/README.md +4 -0
- package/docs/usage-corrections.md +249 -0
- package/docs/usage-limits.md +217 -0
- package/package.json +3 -1
- package/src/client/usage/allowanceClient.test.ts +93 -0
- package/src/client/usage/allowanceClient.ts +134 -0
- package/src/client/usage/allowanceTerms.test.ts +131 -0
- package/src/client/usage/allowanceTerms.ts +172 -0
- package/src/client/usage/client.ts +2 -2
- package/src/client/usage/correctionClient.test.ts +153 -0
- package/src/client/usage/correctionClient.ts +62 -0
- package/src/client/usage/correctionCredits.test.ts +156 -0
- package/src/client/usage/correctionEligibility.ts +14 -0
- package/src/client/usage/correctionInvoice.test.ts +127 -0
- package/src/client/usage/correctionInvoice.ts +269 -0
- package/src/client/usage/correctionInvoiceProof.ts +40 -0
- package/src/client/usage/correctionProofs.ts +40 -0
- package/src/client/usage/correctionProvider.ts +326 -0
- package/src/client/usage/correctionRead.ts +73 -0
- package/src/client/usage/correctionRecovery.test.ts +113 -0
- package/src/client/usage/correctionTerms.ts +98 -0
- package/src/client/usage/correctionTransport.fixtures.ts +372 -0
- package/src/client/usage/index.ts +19 -0
- package/src/client/usage/observationClient.ts +20 -6
- package/src/client/usage/records.ts +7 -1
- package/src/client/usage/summary.ts +12 -4
- package/src/client/usageExports.ts +19 -0
- package/src/component/_generated/api.ts +20 -0
- package/src/component/_generated/component.ts +423 -1
- package/src/component/accountClosure/mutations.ts +17 -0
- package/src/component/schema.ts +14 -0
- package/src/component/usage/allowance.test.ts +435 -0
- package/src/component/usage/mutations.ts +13 -0
- package/src/component/usage/schema.ts +6 -1
- package/src/component/usageAllowance/helpers.ts +137 -0
- package/src/component/usageAllowance/mutations.ts +139 -0
- package/src/component/usageAllowance/queries.ts +140 -0
- package/src/component/usageAllowance/reservations.ts +220 -0
- package/src/component/usageAllowance/schema.ts +37 -0
- package/src/component/usageCorrections/accounting.ts +79 -0
- package/src/component/usageCorrections/cancellation.ts +32 -0
- package/src/component/usageCorrections/corrections.test.ts +389 -0
- package/src/component/usageCorrections/helpers.ts +57 -0
- package/src/component/usageCorrections/invoice.ts +103 -0
- package/src/component/usageCorrections/mutations.ts +278 -0
- package/src/component/usageCorrections/queries.ts +94 -0
- package/src/component/usageCorrections/schema.ts +90 -0
- package/src/component/usageCredits/queries.ts +4 -0
- package/src/component/usageCredits/schema.ts +1 -0
- package/src/component/usageDelivery/fields.ts +1 -0
- package/src/component/usageDelivery/mutations.ts +8 -3
- package/src/component/usageDelivery/steps.ts +1 -1
- package/src/component/usageObservations/queries.ts +5 -2
- package/src/component/usageSubscriptions/retirement.ts +31 -2
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation } from "../_generated/server.js";
|
|
3
|
+
import { limitPolicy } from "../../client/usage/allowanceTerms.js";
|
|
4
|
+
import {
|
|
5
|
+
terms,
|
|
6
|
+
json,
|
|
7
|
+
policyRow,
|
|
8
|
+
overlapping,
|
|
9
|
+
active,
|
|
10
|
+
history,
|
|
11
|
+
canonical,
|
|
12
|
+
scopeOverlap,
|
|
13
|
+
} from "./helpers.js";
|
|
14
|
+
import {
|
|
15
|
+
reserve as reserveUnits,
|
|
16
|
+
release as releaseUnits,
|
|
17
|
+
} from "./reservations.js";
|
|
18
|
+
import { scopeValidator } from "../usage/schema.js";
|
|
19
|
+
export const configure = mutation({
|
|
20
|
+
args: {
|
|
21
|
+
bindingJson: v.string(),
|
|
22
|
+
policyJson: v.string(),
|
|
23
|
+
expectedRevision: v.number(),
|
|
24
|
+
},
|
|
25
|
+
returns: v.object({ revision: v.number() }),
|
|
26
|
+
handler: async (ctx, args) => {
|
|
27
|
+
const original = terms(json(args.bindingJson)),
|
|
28
|
+
policy = limitPolicy(json(args.policyJson), original.binding),
|
|
29
|
+
policyJson = canonical(policy);
|
|
30
|
+
if (
|
|
31
|
+
!Number.isSafeInteger(args.expectedRevision) ||
|
|
32
|
+
args.expectedRevision < 0
|
|
33
|
+
)
|
|
34
|
+
throw new Error("Invalid allowance policy revision");
|
|
35
|
+
await active(ctx, original.binding);
|
|
36
|
+
const prior =
|
|
37
|
+
(await overlapping(
|
|
38
|
+
ctx,
|
|
39
|
+
original.nativeKey,
|
|
40
|
+
original.binding.subscription.period,
|
|
41
|
+
)) ??
|
|
42
|
+
(await scopeOverlap(
|
|
43
|
+
ctx,
|
|
44
|
+
original.scopeKey,
|
|
45
|
+
original.binding.subscription.period,
|
|
46
|
+
));
|
|
47
|
+
if (prior) {
|
|
48
|
+
if (
|
|
49
|
+
prior.key !== original.key ||
|
|
50
|
+
prior.bindingJson !== original.bindingJson
|
|
51
|
+
)
|
|
52
|
+
throw new Error("Overlapping allowance terms cannot reset usage");
|
|
53
|
+
if (prior.policyJson === policyJson) return { revision: prior.revision };
|
|
54
|
+
if (prior.revision !== args.expectedRevision)
|
|
55
|
+
throw new Error("Stale allowance policy revision");
|
|
56
|
+
await ctx.db.patch("usageAllowancePolicies", prior._id, {
|
|
57
|
+
policyJson,
|
|
58
|
+
revision: prior.revision + 1,
|
|
59
|
+
});
|
|
60
|
+
await history(ctx, prior, "policy", {
|
|
61
|
+
revision: prior.revision + 1,
|
|
62
|
+
policy,
|
|
63
|
+
previousPolicy: JSON.parse(prior.policyJson),
|
|
64
|
+
});
|
|
65
|
+
return { revision: prior.revision + 1 };
|
|
66
|
+
}
|
|
67
|
+
if (args.expectedRevision !== 0)
|
|
68
|
+
throw new Error("Stale allowance policy revision");
|
|
69
|
+
// Enable accounting before any accepted usage in this scope/period. Existing
|
|
70
|
+
// unaccounted history must be reconciled rather than silently granting units.
|
|
71
|
+
const existing = await ctx.db
|
|
72
|
+
.query("usageEvents")
|
|
73
|
+
.withIndex("by_scope_time", (q) =>
|
|
74
|
+
q
|
|
75
|
+
.eq("scopeKey", original.scopeKey)
|
|
76
|
+
.gte("occurredAt", original.binding.subscription.period.start)
|
|
77
|
+
.lt("occurredAt", original.binding.subscription.period.end),
|
|
78
|
+
)
|
|
79
|
+
.first();
|
|
80
|
+
if (existing)
|
|
81
|
+
throw new Error(
|
|
82
|
+
"Existing period usage requires reconciliation before allowance setup",
|
|
83
|
+
);
|
|
84
|
+
const id = await ctx.db.insert("usageAllowancePolicies", {
|
|
85
|
+
key: original.key,
|
|
86
|
+
nativeKey: original.nativeKey,
|
|
87
|
+
scopeKey: original.scopeKey,
|
|
88
|
+
bindingJson: original.bindingJson,
|
|
89
|
+
start: original.binding.subscription.period.start,
|
|
90
|
+
end: original.binding.subscription.period.end,
|
|
91
|
+
policyJson,
|
|
92
|
+
revision: 1,
|
|
93
|
+
consumed: "0",
|
|
94
|
+
reserved: "0",
|
|
95
|
+
historyCount: 0,
|
|
96
|
+
});
|
|
97
|
+
const row = await policyRow(ctx, original.key);
|
|
98
|
+
if (!row || row._id !== id)
|
|
99
|
+
throw new Error("Allowance setup invariant violated");
|
|
100
|
+
await history(ctx, row, "policy", { revision: 1, policy });
|
|
101
|
+
return { revision: 1 };
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
export const reserve = mutation({
|
|
105
|
+
args: { inputJson: v.string(), subscriptionJson: v.string() },
|
|
106
|
+
returns: v.union(
|
|
107
|
+
v.object({
|
|
108
|
+
state: v.literal("denied"),
|
|
109
|
+
reason: v.union(v.literal("not_entitled"), v.literal("hard_limit")),
|
|
110
|
+
revision: v.number(),
|
|
111
|
+
}),
|
|
112
|
+
v.object({
|
|
113
|
+
reservationId: v.id("usageReservations"),
|
|
114
|
+
state: v.union(
|
|
115
|
+
v.literal("reserved"),
|
|
116
|
+
v.literal("committed"),
|
|
117
|
+
v.literal("released"),
|
|
118
|
+
),
|
|
119
|
+
revision: v.number(),
|
|
120
|
+
}),
|
|
121
|
+
),
|
|
122
|
+
handler: (ctx, args) =>
|
|
123
|
+
reserveUnits(ctx, args.inputJson, args.subscriptionJson),
|
|
124
|
+
});
|
|
125
|
+
export const release = mutation({
|
|
126
|
+
args: {
|
|
127
|
+
scope: scopeValidator,
|
|
128
|
+
reservationId: v.id("usageReservations"),
|
|
129
|
+
reason: v.union(
|
|
130
|
+
v.literal("work_failed"),
|
|
131
|
+
v.literal("abandoned_before_work"),
|
|
132
|
+
),
|
|
133
|
+
},
|
|
134
|
+
returns: v.null(),
|
|
135
|
+
handler: async (ctx, args) => {
|
|
136
|
+
await releaseUnits(ctx, args.scope, args.reservationId, args.reason);
|
|
137
|
+
return null;
|
|
138
|
+
},
|
|
139
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { verifiedCancellation } from "../usageCorrections/cancellation.js";
|
|
2
|
+
import { paginationOptsValidator } from "convex/server";
|
|
3
|
+
import { v } from "convex/values";
|
|
4
|
+
import { query } from "../_generated/server.js";
|
|
5
|
+
import {
|
|
6
|
+
allowanceStatus,
|
|
7
|
+
limitPolicy,
|
|
8
|
+
} from "../../client/usage/allowanceTerms.js";
|
|
9
|
+
import { terms, json, policyRow, active } from "./helpers.js";
|
|
10
|
+
import { historyDoc } from "./schema.js";
|
|
11
|
+
export const status = query({
|
|
12
|
+
args: { bindingJson: v.string() },
|
|
13
|
+
returns: v.union(v.string(), v.null()),
|
|
14
|
+
handler: async (ctx, args) => {
|
|
15
|
+
const original = terms(json(args.bindingJson)),
|
|
16
|
+
row = await policyRow(ctx, original.key);
|
|
17
|
+
if (!row || row.bindingJson !== original.bindingJson) return null;
|
|
18
|
+
const result = allowanceStatus(
|
|
19
|
+
original.binding,
|
|
20
|
+
limitPolicy(JSON.parse(row.policyJson), original.binding),
|
|
21
|
+
row,
|
|
22
|
+
Math.floor(Date.now() / 1000),
|
|
23
|
+
);
|
|
24
|
+
try {
|
|
25
|
+
await active(ctx, original.binding);
|
|
26
|
+
} catch {
|
|
27
|
+
result.accepting = false;
|
|
28
|
+
}
|
|
29
|
+
return JSON.stringify(result);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
export const readHistory = query({
|
|
33
|
+
args: { bindingJson: v.string(), paginationOpts: paginationOptsValidator },
|
|
34
|
+
returns: v.object({
|
|
35
|
+
page: v.array(historyDoc),
|
|
36
|
+
isDone: v.boolean(),
|
|
37
|
+
continueCursor: v.string(),
|
|
38
|
+
}),
|
|
39
|
+
handler: async (ctx, args) => {
|
|
40
|
+
const original = terms(json(args.bindingJson)),
|
|
41
|
+
row = await policyRow(ctx, original.key);
|
|
42
|
+
if (!row || row.bindingJson !== original.bindingJson)
|
|
43
|
+
throw new Error("Allowance policy unavailable");
|
|
44
|
+
if (
|
|
45
|
+
!Number.isInteger(args.paginationOpts.numItems) ||
|
|
46
|
+
args.paginationOpts.numItems < 1 ||
|
|
47
|
+
args.paginationOpts.numItems > 100
|
|
48
|
+
)
|
|
49
|
+
throw new Error("Invalid allowance history page");
|
|
50
|
+
const result = await ctx.db
|
|
51
|
+
.query("usageAllowanceHistory")
|
|
52
|
+
.withIndex("by_key", (q) => q.eq("key", row.key))
|
|
53
|
+
.order("desc")
|
|
54
|
+
.paginate({
|
|
55
|
+
...args.paginationOpts,
|
|
56
|
+
maximumRowsRead: 200,
|
|
57
|
+
maximumBytesRead: 1024 * 1024,
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
page: result.page,
|
|
61
|
+
isDone: result.isDone,
|
|
62
|
+
continueCursor: result.continueCursor,
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
export const reconciliation = query({
|
|
68
|
+
args: { bindingJson: v.string() },
|
|
69
|
+
returns: v.object({
|
|
70
|
+
status: v.union(v.literal("matched"), v.literal("held")),
|
|
71
|
+
reason: v.string(),
|
|
72
|
+
consumedScaledQuantity: v.string(),
|
|
73
|
+
reservedScaledQuantity: v.string(),
|
|
74
|
+
acceptedScaledQuantity: v.string(),
|
|
75
|
+
repaired: v.literal(false),
|
|
76
|
+
}),
|
|
77
|
+
handler: async (ctx, args) => {
|
|
78
|
+
const original = terms(json(args.bindingJson)),
|
|
79
|
+
row = await policyRow(ctx, original.key);
|
|
80
|
+
if (!row || row.bindingJson !== original.bindingJson)
|
|
81
|
+
throw new Error("Allowance policy unavailable");
|
|
82
|
+
const events = await ctx.db
|
|
83
|
+
.query("usageEvents")
|
|
84
|
+
.withIndex("by_scope_time", (q) =>
|
|
85
|
+
q
|
|
86
|
+
.eq("scopeKey", row.scopeKey)
|
|
87
|
+
.gte("occurredAt", row.start)
|
|
88
|
+
.lt("occurredAt", row.end),
|
|
89
|
+
)
|
|
90
|
+
.take(501);
|
|
91
|
+
const reservations = await ctx.db
|
|
92
|
+
.query("usageReservations")
|
|
93
|
+
.withIndex("by_account_state", (q) =>
|
|
94
|
+
q.eq("accountKey", row.key).eq("state", "reserved"),
|
|
95
|
+
)
|
|
96
|
+
.take(501);
|
|
97
|
+
let accepted = 0n,
|
|
98
|
+
reserved = 0n,
|
|
99
|
+
complete = events.length <= 500 && reservations.length <= 500;
|
|
100
|
+
for (const event of events) {
|
|
101
|
+
const record = JSON.parse(event.recordJson);
|
|
102
|
+
const reservation = event.reservationId
|
|
103
|
+
? await ctx.db.get("usageReservations", event.reservationId)
|
|
104
|
+
: null;
|
|
105
|
+
if (
|
|
106
|
+
!reservation ||
|
|
107
|
+
reservation.accountKey !== row.key ||
|
|
108
|
+
reservation.state !== "committed" ||
|
|
109
|
+
reservation.eventId !== event._id ||
|
|
110
|
+
reservation.scaledQuantity !== record.scaledQuantity
|
|
111
|
+
)
|
|
112
|
+
complete = false;
|
|
113
|
+
const outbox = await ctx.db
|
|
114
|
+
.query("usageOutbox")
|
|
115
|
+
.withIndex("by_event", (q) => q.eq("eventId", event._id))
|
|
116
|
+
.unique();
|
|
117
|
+
const canceled = await verifiedCancellation(ctx, event, outbox);
|
|
118
|
+
if (canceled === "unverified") complete = false;
|
|
119
|
+
if (canceled !== "confirmed") accepted += BigInt(record.scaledQuantity);
|
|
120
|
+
}
|
|
121
|
+
for (const reservation of reservations)
|
|
122
|
+
reserved += BigInt(reservation.scaledQuantity);
|
|
123
|
+
const matches =
|
|
124
|
+
complete &&
|
|
125
|
+
accepted.toString() === row.consumed &&
|
|
126
|
+
reserved.toString() === row.reserved;
|
|
127
|
+
return {
|
|
128
|
+
status: matches ? ("matched" as const) : ("held" as const),
|
|
129
|
+
reason: matches
|
|
130
|
+
? "complete_local_accounting"
|
|
131
|
+
: complete
|
|
132
|
+
? "accounting_mismatch"
|
|
133
|
+
: "local_evidence_incomplete",
|
|
134
|
+
consumedScaledQuantity: row.consumed,
|
|
135
|
+
reservedScaledQuantity: row.reserved,
|
|
136
|
+
acceptedScaledQuantity: accepted.toString(),
|
|
137
|
+
repaired: false as const,
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
});
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import type { Value } from "convex/values";
|
|
2
|
+
import type { MutationCtx } from "../_generated/server.js";
|
|
3
|
+
import type { Doc, Id } from "../_generated/dataModel.js";
|
|
4
|
+
import {
|
|
5
|
+
prepareUsageRecord,
|
|
6
|
+
type PreparedUsageRecord,
|
|
7
|
+
type UsageScope,
|
|
8
|
+
} from "../../client/usage/records.js";
|
|
9
|
+
import {
|
|
10
|
+
allowanceStatus,
|
|
11
|
+
limitPolicy,
|
|
12
|
+
} from "../../client/usage/allowanceTerms.js";
|
|
13
|
+
import { parseUsageQuantity } from "../../client/usage/contracts.js";
|
|
14
|
+
import { subscriptionSnapshot, scopeKey } from "../usage/helpers.js";
|
|
15
|
+
import {
|
|
16
|
+
terms,
|
|
17
|
+
json,
|
|
18
|
+
canonical,
|
|
19
|
+
policyRow,
|
|
20
|
+
active,
|
|
21
|
+
history,
|
|
22
|
+
overlapping,
|
|
23
|
+
scopeOverlap,
|
|
24
|
+
configuredScope,
|
|
25
|
+
} from "./helpers.js";
|
|
26
|
+
export async function reserve(
|
|
27
|
+
ctx: MutationCtx,
|
|
28
|
+
inputJson: string,
|
|
29
|
+
subscriptionJson: string,
|
|
30
|
+
) {
|
|
31
|
+
const event = prepareUsageRecord(json(inputJson)),
|
|
32
|
+
subscription = subscriptionSnapshot(subscriptionJson),
|
|
33
|
+
original = terms({
|
|
34
|
+
scope: event.scope,
|
|
35
|
+
binding: event.binding,
|
|
36
|
+
subscription,
|
|
37
|
+
});
|
|
38
|
+
const input = canonical(event),
|
|
39
|
+
sub = canonical(subscription);
|
|
40
|
+
const prior = await ctx.db
|
|
41
|
+
.query("usageReservations")
|
|
42
|
+
.withIndex("by_scope_source", (q) =>
|
|
43
|
+
q
|
|
44
|
+
.eq("scopeKey", original.scopeKey)
|
|
45
|
+
.eq("sourceEventId", event.sourceEventId),
|
|
46
|
+
)
|
|
47
|
+
.unique();
|
|
48
|
+
if (prior) {
|
|
49
|
+
if (
|
|
50
|
+
prior.accountingKind !== "allowance" ||
|
|
51
|
+
prior.inputJson !== input ||
|
|
52
|
+
prior.subscriptionJson !== sub
|
|
53
|
+
)
|
|
54
|
+
throw new Error("Allowance reservation replay conflict");
|
|
55
|
+
return {
|
|
56
|
+
reservationId: prior._id,
|
|
57
|
+
state: prior.state,
|
|
58
|
+
revision: Number(prior.allocationsJson),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const accepted = await ctx.db
|
|
62
|
+
.query("usageEvents")
|
|
63
|
+
.withIndex("by_identity", (q) => q.eq("identity", event.identity))
|
|
64
|
+
.unique();
|
|
65
|
+
if (accepted) throw new Error("Accepted usage cannot be reserved again");
|
|
66
|
+
const row = await policyRow(ctx, original.key);
|
|
67
|
+
if (!row || row.bindingJson !== original.bindingJson)
|
|
68
|
+
throw new Error("Allowance policy unavailable");
|
|
69
|
+
await active(ctx, original.binding, event);
|
|
70
|
+
const policy = limitPolicy(JSON.parse(row.policyJson), original.binding),
|
|
71
|
+
status = allowanceStatus(
|
|
72
|
+
original.binding,
|
|
73
|
+
policy,
|
|
74
|
+
row,
|
|
75
|
+
Math.floor(Date.now() / 1000),
|
|
76
|
+
);
|
|
77
|
+
if (!policy.entitled)
|
|
78
|
+
return {
|
|
79
|
+
state: "denied" as const,
|
|
80
|
+
reason: "not_entitled" as const,
|
|
81
|
+
revision: row.revision,
|
|
82
|
+
};
|
|
83
|
+
if (
|
|
84
|
+
status.hardLimitRemainingQuantity !== null &&
|
|
85
|
+
BigInt(event.scaledQuantity) >
|
|
86
|
+
BigInt(
|
|
87
|
+
parseUsageQuantity(
|
|
88
|
+
status.hardLimitRemainingQuantity,
|
|
89
|
+
event.binding.plan.unit.precision,
|
|
90
|
+
).scaledQuantity,
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
return {
|
|
94
|
+
state: "denied" as const,
|
|
95
|
+
reason: "hard_limit" as const,
|
|
96
|
+
revision: row.revision,
|
|
97
|
+
};
|
|
98
|
+
const reserved = BigInt(row.reserved) + BigInt(event.scaledQuantity);
|
|
99
|
+
if (reserved + BigInt(row.consumed) > 999999999999999999n)
|
|
100
|
+
throw new Error("Allowance accounting quantity bound exceeded");
|
|
101
|
+
const reservationId = await ctx.db.insert("usageReservations", {
|
|
102
|
+
scopeKey: original.scopeKey,
|
|
103
|
+
sourceEventId: event.sourceEventId,
|
|
104
|
+
planJson: JSON.stringify(event.binding.plan),
|
|
105
|
+
scaledQuantity: event.scaledQuantity,
|
|
106
|
+
state: "reserved",
|
|
107
|
+
accountingKind: "allowance",
|
|
108
|
+
accountKey: row.key,
|
|
109
|
+
inputJson: input,
|
|
110
|
+
subscriptionJson: sub,
|
|
111
|
+
allocationsJson: String(row.revision),
|
|
112
|
+
});
|
|
113
|
+
await ctx.db.patch("usageAllowancePolicies", row._id, {
|
|
114
|
+
reserved: reserved.toString(),
|
|
115
|
+
});
|
|
116
|
+
await history(ctx, row, "reservation", {
|
|
117
|
+
reservationId,
|
|
118
|
+
event,
|
|
119
|
+
subscription,
|
|
120
|
+
revision: row.revision,
|
|
121
|
+
});
|
|
122
|
+
return { reservationId, state: "reserved" as const, revision: row.revision };
|
|
123
|
+
}
|
|
124
|
+
export async function requireReservation(
|
|
125
|
+
ctx: MutationCtx,
|
|
126
|
+
event: PreparedUsageRecord,
|
|
127
|
+
subscription: ReturnType<typeof subscriptionSnapshot>,
|
|
128
|
+
reservation: Doc<"usageReservations"> | null,
|
|
129
|
+
) {
|
|
130
|
+
if (event.binding.plan.model === "prepaid") return;
|
|
131
|
+
const original = terms({
|
|
132
|
+
scope: event.scope,
|
|
133
|
+
binding: event.binding,
|
|
134
|
+
subscription,
|
|
135
|
+
});
|
|
136
|
+
const row =
|
|
137
|
+
(await overlapping(ctx, original.nativeKey, subscription.period)) ??
|
|
138
|
+
(await scopeOverlap(ctx, original.scopeKey, subscription.period));
|
|
139
|
+
if (!row && (await configuredScope(ctx, original)))
|
|
140
|
+
throw new Error(
|
|
141
|
+
"New allowance period requires policy configuration and reservation",
|
|
142
|
+
);
|
|
143
|
+
if (
|
|
144
|
+
row &&
|
|
145
|
+
(row.bindingJson !== original.bindingJson ||
|
|
146
|
+
!reservation ||
|
|
147
|
+
reservation.accountingKind !== "allowance" ||
|
|
148
|
+
reservation.accountKey !== row.key)
|
|
149
|
+
)
|
|
150
|
+
throw new Error("Matching allowance reservation required");
|
|
151
|
+
}
|
|
152
|
+
export async function commit(
|
|
153
|
+
ctx: MutationCtx,
|
|
154
|
+
reservation: Doc<"usageReservations">,
|
|
155
|
+
event: Value,
|
|
156
|
+
subscription: Value,
|
|
157
|
+
eventId: Id<"usageEvents">,
|
|
158
|
+
) {
|
|
159
|
+
if (
|
|
160
|
+
reservation.accountingKind !== "allowance" ||
|
|
161
|
+
!reservation.accountKey ||
|
|
162
|
+
reservation.inputJson !== canonical(event) ||
|
|
163
|
+
reservation.subscriptionJson !== canonical(subscription)
|
|
164
|
+
)
|
|
165
|
+
throw new Error("Allowance reservation provenance mismatch");
|
|
166
|
+
const row = await policyRow(ctx, reservation.accountKey);
|
|
167
|
+
if (!row || BigInt(row.reserved) < BigInt(reservation.scaledQuantity))
|
|
168
|
+
throw new Error("Allowance reservation backing unavailable");
|
|
169
|
+
await ctx.db.patch("usageAllowancePolicies", row._id, {
|
|
170
|
+
reserved: (
|
|
171
|
+
BigInt(row.reserved) - BigInt(reservation.scaledQuantity)
|
|
172
|
+
).toString(),
|
|
173
|
+
consumed: (
|
|
174
|
+
BigInt(row.consumed) + BigInt(reservation.scaledQuantity)
|
|
175
|
+
).toString(),
|
|
176
|
+
});
|
|
177
|
+
await history(ctx, row, "commit", {
|
|
178
|
+
reservationId: reservation._id,
|
|
179
|
+
eventId,
|
|
180
|
+
revision: Number(reservation.allocationsJson),
|
|
181
|
+
scaledQuantity: reservation.scaledQuantity,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
export async function release(
|
|
185
|
+
ctx: MutationCtx,
|
|
186
|
+
scope: UsageScope,
|
|
187
|
+
reservationId: Id<"usageReservations">,
|
|
188
|
+
reason: string,
|
|
189
|
+
) {
|
|
190
|
+
const row = await ctx.db.get("usageReservations", reservationId);
|
|
191
|
+
if (
|
|
192
|
+
!row ||
|
|
193
|
+
row.scopeKey !== scopeKey(scope) ||
|
|
194
|
+
row.accountingKind !== "allowance" ||
|
|
195
|
+
!row.accountKey
|
|
196
|
+
)
|
|
197
|
+
throw new Error("Allowance reservation unavailable");
|
|
198
|
+
if (row.state === "committed" || row.eventId)
|
|
199
|
+
throw new Error("Committed usage requires an auditable correction");
|
|
200
|
+
if (row.state === "released") {
|
|
201
|
+
if (row.releaseReason !== reason)
|
|
202
|
+
throw new Error("Allowance release replay conflict");
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const policy = await policyRow(ctx, row.accountKey);
|
|
206
|
+
if (!policy || BigInt(policy.reserved) < BigInt(row.scaledQuantity))
|
|
207
|
+
throw new Error("Allowance reservation invariant violated");
|
|
208
|
+
await ctx.db.patch("usageAllowancePolicies", policy._id, {
|
|
209
|
+
reserved: (BigInt(policy.reserved) - BigInt(row.scaledQuantity)).toString(),
|
|
210
|
+
});
|
|
211
|
+
await ctx.db.patch("usageReservations", row._id, {
|
|
212
|
+
state: "released",
|
|
213
|
+
releaseReason: reason,
|
|
214
|
+
});
|
|
215
|
+
await history(ctx, policy, "release", {
|
|
216
|
+
reservationId,
|
|
217
|
+
reason,
|
|
218
|
+
scaledQuantity: row.scaledQuantity,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
export const fields = {
|
|
4
|
+
key: v.string(),
|
|
5
|
+
nativeKey: v.string(),
|
|
6
|
+
scopeKey: v.string(),
|
|
7
|
+
bindingJson: v.string(),
|
|
8
|
+
start: v.number(),
|
|
9
|
+
end: v.number(),
|
|
10
|
+
policyJson: v.string(),
|
|
11
|
+
revision: v.number(),
|
|
12
|
+
consumed: v.string(),
|
|
13
|
+
reserved: v.string(),
|
|
14
|
+
historyCount: v.number(),
|
|
15
|
+
};
|
|
16
|
+
export const policiesTable = defineTable(fields)
|
|
17
|
+
.index("by_key", ["key"])
|
|
18
|
+
.index("by_native_start", ["nativeKey", "start"])
|
|
19
|
+
.index("by_scope_start", ["scopeKey", "start"]);
|
|
20
|
+
export const historyFields = {
|
|
21
|
+
key: v.string(),
|
|
22
|
+
kind: v.union(
|
|
23
|
+
v.literal("policy"),
|
|
24
|
+
v.literal("reservation"),
|
|
25
|
+
v.literal("commit"),
|
|
26
|
+
v.literal("release"),
|
|
27
|
+
v.literal("correction"),
|
|
28
|
+
),
|
|
29
|
+
evidenceJson: v.string(),
|
|
30
|
+
at: v.number(),
|
|
31
|
+
};
|
|
32
|
+
export const historyDoc = v.object({
|
|
33
|
+
_id: v.id("usageAllowanceHistory"),
|
|
34
|
+
_creationTime: v.number(),
|
|
35
|
+
...historyFields,
|
|
36
|
+
});
|
|
37
|
+
export const historyTable = defineTable(historyFields).index("by_key", ["key"]);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { MutationCtx } from "../_generated/server.js";
|
|
2
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
3
|
+
import { creditAllocations } from "../usageCredits/reservations.js";
|
|
4
|
+
import { account, history as creditHistory } from "../usageCredits/helpers.js";
|
|
5
|
+
import {
|
|
6
|
+
policyRow,
|
|
7
|
+
history as allowanceHistory,
|
|
8
|
+
} from "../usageAllowance/helpers.js";
|
|
9
|
+
/** Called only for a full proven cancellation, in the same transaction as the terminal receipt. */
|
|
10
|
+
export async function restore(
|
|
11
|
+
ctx: MutationCtx,
|
|
12
|
+
correction: Doc<"usageCorrections">,
|
|
13
|
+
event: Doc<"usageEvents">,
|
|
14
|
+
) {
|
|
15
|
+
if (!event.reservationId) return;
|
|
16
|
+
const reservation = await ctx.db.get(
|
|
17
|
+
"usageReservations",
|
|
18
|
+
event.reservationId,
|
|
19
|
+
);
|
|
20
|
+
if (
|
|
21
|
+
!reservation ||
|
|
22
|
+
reservation.state !== "committed" ||
|
|
23
|
+
reservation.eventId !== event._id ||
|
|
24
|
+
reservation.scopeKey !== correction.scopeKey ||
|
|
25
|
+
reservation.scaledQuantity !== correction.scaledQuantity
|
|
26
|
+
)
|
|
27
|
+
throw new Error("Correction reservation provenance mismatch");
|
|
28
|
+
if (reservation.accountingKind === "allowance") {
|
|
29
|
+
const policy = await policyRow(ctx, reservation.accountKey!);
|
|
30
|
+
if (!policy || BigInt(policy.consumed) < BigInt(correction.scaledQuantity))
|
|
31
|
+
throw new Error("Correction allowance backing unavailable");
|
|
32
|
+
await ctx.db.patch("usageAllowancePolicies", policy._id, {
|
|
33
|
+
consumed: (
|
|
34
|
+
BigInt(policy.consumed) - BigInt(correction.scaledQuantity)
|
|
35
|
+
).toString(),
|
|
36
|
+
});
|
|
37
|
+
await allowanceHistory(ctx, policy, "correction", {
|
|
38
|
+
correctionKey: correction.key,
|
|
39
|
+
eventId: event._id,
|
|
40
|
+
scaledQuantity: correction.scaledQuantity,
|
|
41
|
+
cashCollection: "none",
|
|
42
|
+
});
|
|
43
|
+
} else if (reservation.accountingKind === "prepaid") {
|
|
44
|
+
const ledger = await account(ctx, reservation.accountKey!);
|
|
45
|
+
if (ledger.closed || ledger.frozenReason)
|
|
46
|
+
throw new Error("Correction credit account requires reconciliation");
|
|
47
|
+
const allocations = creditAllocations.parse(
|
|
48
|
+
JSON.parse(reservation.allocationsJson!),
|
|
49
|
+
);
|
|
50
|
+
for (const allocation of allocations) {
|
|
51
|
+
const grant = await ctx.db.get(
|
|
52
|
+
"usageCreditGrants",
|
|
53
|
+
allocation.grantId as import("../_generated/dataModel.js").Id<"usageCreditGrants">,
|
|
54
|
+
);
|
|
55
|
+
if (
|
|
56
|
+
!grant ||
|
|
57
|
+
grant.accountKey !== ledger.key ||
|
|
58
|
+
grant.frozen ||
|
|
59
|
+
grant.nativeExpired ||
|
|
60
|
+
(grant.expiresAt !== null && grant.expiresAt <= Date.now() / 1000) ||
|
|
61
|
+
BigInt(grant.consumed) < BigInt(allocation.quantity)
|
|
62
|
+
)
|
|
63
|
+
throw new Error("Correction original credit backing unavailable");
|
|
64
|
+
await ctx.db.patch("usageCreditGrants", grant._id, {
|
|
65
|
+
consumed: (
|
|
66
|
+
BigInt(grant.consumed) - BigInt(allocation.quantity)
|
|
67
|
+
).toString(),
|
|
68
|
+
available: (
|
|
69
|
+
BigInt(grant.available) + BigInt(allocation.quantity)
|
|
70
|
+
).toString(),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
await creditHistory(ctx, ledger.key, "correction", correction.key, {
|
|
74
|
+
eventId: event._id,
|
|
75
|
+
allocations,
|
|
76
|
+
cashCollection: "none",
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { QueryCtx } from "../_generated/server.js";
|
|
2
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
3
|
+
/** Never treat an unproven terminal projection as canceled billable quantity. */
|
|
4
|
+
export async function verifiedCancellation(
|
|
5
|
+
ctx: QueryCtx,
|
|
6
|
+
event: Doc<"usageEvents">,
|
|
7
|
+
outbox: Doc<"usageOutbox"> | null,
|
|
8
|
+
) {
|
|
9
|
+
if (outbox?.state !== "canceled") return "none" as const;
|
|
10
|
+
if (!outbox.correctionKey) return "unverified" as const;
|
|
11
|
+
const correction = await ctx.db
|
|
12
|
+
.query("usageCorrections")
|
|
13
|
+
.withIndex("by_key", (q) => q.eq("key", outbox.correctionKey!))
|
|
14
|
+
.unique();
|
|
15
|
+
const record = JSON.parse(event.recordJson);
|
|
16
|
+
return correction?.state === "confirmed" &&
|
|
17
|
+
correction.resolution !== "invoice_credit" &&
|
|
18
|
+
correction.eventId === event._id &&
|
|
19
|
+
correction.scopeKey === event.scopeKey &&
|
|
20
|
+
correction.recordJson === event.recordJson &&
|
|
21
|
+
correction.subscriptionJson === event.subscriptionJson &&
|
|
22
|
+
correction.scaledQuantity === record.scaledQuantity &&
|
|
23
|
+
correction.restoredQuantity === record.quantity
|
|
24
|
+
? ("confirmed" as const)
|
|
25
|
+
: ("unverified" as const);
|
|
26
|
+
}
|
|
27
|
+
export const unresolvedCorrectionStates = [
|
|
28
|
+
"requested",
|
|
29
|
+
"provider_accepted",
|
|
30
|
+
"held",
|
|
31
|
+
"invoice_adjustment_required",
|
|
32
|
+
] as const;
|