@fantastic.dev/better-stripe 0.51.0 → 0.53.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 +20 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +210 -32
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +105 -16
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +178 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +89 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +5 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/products/catalogClient.d.ts +23 -0
- package/dist/client/products/catalogClient.d.ts.map +1 -1
- package/dist/client/products/catalogClient.js +32 -0
- package/dist/client/products/catalogClient.js.map +1 -1
- package/dist/client/products/fixedPrice.d.ts +4 -0
- package/dist/client/products/fixedPrice.d.ts.map +1 -0
- package/dist/client/products/fixedPrice.js +10 -0
- package/dist/client/products/fixedPrice.js.map +1 -0
- package/dist/client/products/products.d.ts.map +1 -1
- package/dist/client/products/products.js +3 -0
- package/dist/client/products/products.js.map +1 -1
- package/dist/client/usage/catalog.d.ts +7 -0
- package/dist/client/usage/catalog.d.ts.map +1 -0
- package/dist/client/usage/catalog.js +55 -0
- package/dist/client/usage/catalog.js.map +1 -0
- package/dist/client/usage/catalogLifecycle.d.ts +25 -0
- package/dist/client/usage/catalogLifecycle.d.ts.map +1 -0
- package/dist/client/usage/catalogLifecycle.js +64 -0
- package/dist/client/usage/catalogLifecycle.js.map +1 -0
- package/dist/client/usage/catalogMath.d.ts +3 -0
- package/dist/client/usage/catalogMath.d.ts.map +1 -0
- package/dist/client/usage/catalogMath.js +21 -0
- package/dist/client/usage/catalogMath.js.map +1 -0
- package/dist/client/usage/catalogPrices.d.ts +5 -0
- package/dist/client/usage/catalogPrices.d.ts.map +1 -0
- package/dist/client/usage/catalogPrices.js +178 -0
- package/dist/client/usage/catalogPrices.js.map +1 -0
- package/dist/client/usage/catalogRead.d.ts +25 -0
- package/dist/client/usage/catalogRead.d.ts.map +1 -0
- package/dist/client/usage/catalogRead.js +71 -0
- package/dist/client/usage/catalogRead.js.map +1 -0
- package/dist/client/usage/catalogTypes.d.ts +97 -0
- package/dist/client/usage/catalogTypes.d.ts.map +1 -0
- package/dist/client/usage/catalogTypes.js +106 -0
- package/dist/client/usage/catalogTypes.js.map +1 -0
- package/dist/client/usage/client.d.ts +37 -0
- package/dist/client/usage/client.d.ts.map +1 -0
- package/dist/client/usage/client.js +59 -0
- package/dist/client/usage/client.js.map +1 -0
- 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 +1 -1
- package/dist/client/usage/records.js.map +1 -1
- package/dist/client/webhooks/processors.d.ts.map +1 -1
- package/dist/client/webhooks/processors.js +3 -0
- package/dist/client/webhooks/processors.js.map +1 -1
- package/dist/component/_generated/api.d.ts +10 -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 +141 -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 +43 -43
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +224 -16
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/schema.d.ts +89 -0
- package/dist/component/schema.js +7 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/helpers.d.ts +13 -0
- package/dist/component/usage/helpers.d.ts.map +1 -0
- package/dist/component/usage/helpers.js +34 -0
- package/dist/component/usage/helpers.js.map +1 -0
- package/dist/component/usage/mutations.d.ts +14 -0
- package/dist/component/usage/mutations.d.ts.map +1 -0
- package/dist/component/usage/mutations.js +124 -0
- package/dist/component/usage/mutations.js.map +1 -0
- package/dist/component/usage/queries.d.ts +62 -0
- package/dist/component/usage/queries.d.ts.map +1 -0
- package/dist/component/usage/queries.js +65 -0
- package/dist/component/usage/queries.js.map +1 -0
- package/dist/component/usage/schema.d.ts +110 -0
- package/dist/component/usage/schema.d.ts.map +1 -0
- package/dist/component/usage/schema.js +54 -0
- package/dist/component/usage/schema.js.map +1 -0
- package/dist/component/usageCatalog/mutations.d.ts +25 -0
- package/dist/component/usageCatalog/mutations.d.ts.map +1 -0
- package/dist/component/usageCatalog/mutations.js +75 -0
- package/dist/component/usageCatalog/mutations.js.map +1 -0
- package/dist/component/usageCatalog/queries.d.ts +33 -0
- package/dist/component/usageCatalog/queries.d.ts.map +1 -0
- package/dist/component/usageCatalog/queries.js +26 -0
- package/dist/component/usageCatalog/queries.js.map +1 -0
- package/dist/component/usageCatalog/schema.d.ts +57 -0
- package/dist/component/usageCatalog/schema.d.ts.map +1 -0
- package/dist/component/usageCatalog/schema.js +22 -0
- package/dist/component/usageCatalog/schema.js.map +1 -0
- package/docs/README.md +5 -1
- package/docs/metered-usage.md +2 -2
- package/docs/metering-setup.md +4 -1
- package/docs/usage-catalog.md +106 -0
- package/docs/usage-ledger.md +62 -0
- package/package.json +4 -2
- package/src/client/index.ts +12 -0
- package/src/client/products/catalogClient.ts +77 -0
- package/src/client/products/fixedPrice.test.ts +57 -0
- package/src/client/products/fixedPrice.ts +13 -0
- package/src/client/products/products.ts +2 -0
- package/src/client/usage/catalog.test.ts +464 -0
- package/src/client/usage/catalog.ts +117 -0
- package/src/client/usage/catalogLifecycle.ts +128 -0
- package/src/client/usage/catalogMath.test.ts +13 -0
- package/src/client/usage/catalogMath.ts +22 -0
- package/src/client/usage/catalogPrices.ts +251 -0
- package/src/client/usage/catalogRead.ts +98 -0
- package/src/client/usage/catalogTypes.ts +170 -0
- package/src/client/usage/client.ts +97 -0
- package/src/client/usage/ledger-client.test.ts +74 -0
- package/src/client/usage/records.ts +1 -1
- package/src/client/webhooks/processors.ts +2 -0
- package/src/component/_generated/api.ts +10 -0
- package/src/component/_generated/component.ts +184 -0
- package/src/component/schema.ts +12 -0
- package/src/component/usage/helpers.ts +33 -0
- package/src/component/usage/ledger.test.ts +310 -0
- package/src/component/usage/mutations.ts +139 -0
- package/src/component/usage/queries.ts +69 -0
- package/src/component/usage/schema.ts +65 -0
- package/src/component/usageCatalog/mutations.ts +79 -0
- package/src/component/usageCatalog/queries.ts +32 -0
- package/src/component/usageCatalog/schema.ts +21 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import type { FunctionHandle } from "convex/server";
|
|
3
|
+
import { mutation } from "../_generated/server.js";
|
|
4
|
+
import {
|
|
5
|
+
prepareUsageRecord,
|
|
6
|
+
type PreparedUsageRecord,
|
|
7
|
+
} from "../../client/usage/records.js";
|
|
8
|
+
import { defineUsagePlan } from "../../client/usage/contracts.js";
|
|
9
|
+
import { assertUsageReplay } from "../../client/usage/boundaries.js";
|
|
10
|
+
import { scopeKey, subscriptionSnapshot } from "./helpers.js";
|
|
11
|
+
|
|
12
|
+
/** Trusted host mutation. Authenticate, authorize scope and verify native binding first. */
|
|
13
|
+
export const record = mutation({
|
|
14
|
+
args: {
|
|
15
|
+
inputJson: v.string(),
|
|
16
|
+
subscriptionJson: v.string(),
|
|
17
|
+
dispatch: v.optional(v.string()),
|
|
18
|
+
reservationId: v.optional(v.id("usageReservations")),
|
|
19
|
+
quota: v.optional(v.number()),
|
|
20
|
+
},
|
|
21
|
+
returns: v.object({
|
|
22
|
+
eventId: v.id("usageEvents"),
|
|
23
|
+
identity: v.string(),
|
|
24
|
+
acceptedAt: v.number(),
|
|
25
|
+
delivery: v.literal("queued"),
|
|
26
|
+
}),
|
|
27
|
+
handler: async (ctx, args) => {
|
|
28
|
+
if (new TextEncoder().encode(args.inputJson).length > 8192)
|
|
29
|
+
throw new Error("Usage payload exceeds size limit");
|
|
30
|
+
const event = prepareUsageRecord(JSON.parse(args.inputJson));
|
|
31
|
+
const key = scopeKey(event.scope);
|
|
32
|
+
const subscription = subscriptionSnapshot(args.subscriptionJson);
|
|
33
|
+
const subscriptionJson = JSON.stringify(subscription);
|
|
34
|
+
const existing = await ctx.db
|
|
35
|
+
.query("usageEvents")
|
|
36
|
+
.withIndex("by_identity", (q) => q.eq("identity", event.identity))
|
|
37
|
+
.unique();
|
|
38
|
+
if (existing) {
|
|
39
|
+
if (
|
|
40
|
+
args.reservationId !== undefined &&
|
|
41
|
+
args.reservationId !== existing.reservationId
|
|
42
|
+
)
|
|
43
|
+
throw new Error("Conflicting usage reservation replay");
|
|
44
|
+
assertUsageReplay(
|
|
45
|
+
JSON.parse(existing.recordJson) as PreparedUsageRecord,
|
|
46
|
+
event,
|
|
47
|
+
);
|
|
48
|
+
if (existing.subscriptionJson !== subscriptionJson)
|
|
49
|
+
throw new Error("Conflicting usage replay");
|
|
50
|
+
const outbox = await ctx.db
|
|
51
|
+
.query("usageOutbox")
|
|
52
|
+
.withIndex("by_event", (q) => q.eq("eventId", existing._id))
|
|
53
|
+
.unique();
|
|
54
|
+
if (!outbox) throw new Error("Usage outbox invariant violated");
|
|
55
|
+
// Acceptance is an immutable receipt; subsequent delivery is read separately.
|
|
56
|
+
return {
|
|
57
|
+
eventId: existing._id,
|
|
58
|
+
identity: existing.identity,
|
|
59
|
+
acceptedAt: existing.acceptedAt,
|
|
60
|
+
delivery: "queued" as const,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const now = Math.floor(Date.now() / 1000);
|
|
64
|
+
if (event.occurredAt < now - 35 * 86400 || event.occurredAt > now + 300)
|
|
65
|
+
throw new Error("Usage occurrence is outside the native delivery window");
|
|
66
|
+
if (
|
|
67
|
+
event.binding.stripePriceId !== subscription.stripePriceId ||
|
|
68
|
+
event.occurredAt < subscription.period.start ||
|
|
69
|
+
event.occurredAt >= subscription.period.end
|
|
70
|
+
)
|
|
71
|
+
throw new Error("Usage does not match its subscription period/price");
|
|
72
|
+
const quota = args.quota ?? 100000;
|
|
73
|
+
if (!Number.isInteger(quota) || quota < 1 || quota > 100000)
|
|
74
|
+
throw new Error("Invalid usage scope quota");
|
|
75
|
+
const counter = await ctx.db
|
|
76
|
+
.query("usageScopeCounters")
|
|
77
|
+
.withIndex("by_scope", (q) => q.eq("scopeKey", key))
|
|
78
|
+
.unique();
|
|
79
|
+
if ((counter?.count ?? 0) >= quota)
|
|
80
|
+
throw new Error("Usage scope quota exhausted");
|
|
81
|
+
const reservation = args.reservationId
|
|
82
|
+
? await ctx.db.get("usageReservations", args.reservationId)
|
|
83
|
+
: null;
|
|
84
|
+
if (args.reservationId !== undefined && !reservation)
|
|
85
|
+
throw new Error("Usage reservation is unavailable");
|
|
86
|
+
if (event.binding.plan.model === "prepaid" && !reservation)
|
|
87
|
+
throw new Error("A matching usage reservation is required");
|
|
88
|
+
if (
|
|
89
|
+
reservation &&
|
|
90
|
+
(reservation.scopeKey !== key ||
|
|
91
|
+
reservation.sourceEventId !== event.sourceEventId ||
|
|
92
|
+
JSON.stringify(defineUsagePlan(JSON.parse(reservation.planJson))) !==
|
|
93
|
+
JSON.stringify(event.binding.plan) ||
|
|
94
|
+
reservation.scaledQuantity !== event.scaledQuantity ||
|
|
95
|
+
reservation.state !== "reserved")
|
|
96
|
+
)
|
|
97
|
+
throw new Error("Usage reservation does not match event");
|
|
98
|
+
const eventId = await ctx.db.insert("usageEvents", {
|
|
99
|
+
reservationId: args.reservationId,
|
|
100
|
+
scopeKey: key,
|
|
101
|
+
identity: event.identity,
|
|
102
|
+
recordJson: JSON.stringify(event),
|
|
103
|
+
subscriptionJson,
|
|
104
|
+
occurredAt: event.occurredAt,
|
|
105
|
+
acceptedAt: now,
|
|
106
|
+
});
|
|
107
|
+
await ctx.db.insert("usageOutbox", {
|
|
108
|
+
eventId,
|
|
109
|
+
scopeKey: key,
|
|
110
|
+
state: "queued",
|
|
111
|
+
attempts: 0,
|
|
112
|
+
nextAttemptAt: now,
|
|
113
|
+
providerIdentifier: `bs_usage_${eventId}`,
|
|
114
|
+
dispatch: args.dispatch,
|
|
115
|
+
});
|
|
116
|
+
if (counter)
|
|
117
|
+
await ctx.db.patch("usageScopeCounters", counter._id, {
|
|
118
|
+
count: counter.count + 1,
|
|
119
|
+
});
|
|
120
|
+
else await ctx.db.insert("usageScopeCounters", { scopeKey: key, count: 1 });
|
|
121
|
+
if (reservation)
|
|
122
|
+
await ctx.db.patch("usageReservations", reservation._id, {
|
|
123
|
+
state: "committed",
|
|
124
|
+
eventId,
|
|
125
|
+
});
|
|
126
|
+
if (args.dispatch)
|
|
127
|
+
await ctx.scheduler.runAfter(
|
|
128
|
+
0,
|
|
129
|
+
args.dispatch as FunctionHandle<"action">,
|
|
130
|
+
{ eventId },
|
|
131
|
+
);
|
|
132
|
+
return {
|
|
133
|
+
eventId,
|
|
134
|
+
identity: event.identity,
|
|
135
|
+
acceptedAt: now,
|
|
136
|
+
delivery: "queued" as const,
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import {
|
|
3
|
+
paginationOptsValidator,
|
|
4
|
+
paginationResultValidator,
|
|
5
|
+
} from "convex/server";
|
|
6
|
+
import { paginator } from "convex-helpers/server/pagination";
|
|
7
|
+
import { query } from "../_generated/server.js";
|
|
8
|
+
import schema from "../schema.js";
|
|
9
|
+
import { eventDoc, scopeValidator, deliveryValidator } from "./schema.js";
|
|
10
|
+
import { scopeKey } from "./helpers.js";
|
|
11
|
+
import { validateUsagePageRequest } from "../../client/usage/boundaries.js";
|
|
12
|
+
|
|
13
|
+
const view = v.object({ event: eventDoc, delivery: deliveryValidator });
|
|
14
|
+
export const get = query({
|
|
15
|
+
args: { scope: scopeValidator, eventId: v.id("usageEvents") },
|
|
16
|
+
returns: v.union(view, v.null()),
|
|
17
|
+
handler: async (ctx, args) => {
|
|
18
|
+
const key = scopeKey(args.scope);
|
|
19
|
+
const event = await ctx.db.get("usageEvents", args.eventId);
|
|
20
|
+
if (!event || event.scopeKey !== key) return null;
|
|
21
|
+
const outbox = await ctx.db
|
|
22
|
+
.query("usageOutbox")
|
|
23
|
+
.withIndex("by_event", (q) => q.eq("eventId", event._id))
|
|
24
|
+
.unique();
|
|
25
|
+
if (!outbox) throw new Error("Usage outbox invariant violated");
|
|
26
|
+
return { event, delivery: outbox.state };
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
export const read = query({
|
|
30
|
+
args: {
|
|
31
|
+
scope: scopeValidator,
|
|
32
|
+
period: v.object({ start: v.number(), end: v.number() }),
|
|
33
|
+
paginationOpts: paginationOptsValidator,
|
|
34
|
+
},
|
|
35
|
+
returns: paginationResultValidator(view),
|
|
36
|
+
handler: async (ctx, args) => {
|
|
37
|
+
const key = scopeKey(args.scope);
|
|
38
|
+
const request = validateUsagePageRequest({
|
|
39
|
+
period: args.period,
|
|
40
|
+
limit: args.paginationOpts.numItems,
|
|
41
|
+
cursor: args.paginationOpts.cursor,
|
|
42
|
+
});
|
|
43
|
+
const result = await paginator(ctx.db, schema)
|
|
44
|
+
.query("usageEvents")
|
|
45
|
+
.withIndex("by_scope_time", (q) =>
|
|
46
|
+
q
|
|
47
|
+
.eq("scopeKey", key)
|
|
48
|
+
.gte("occurredAt", request.period.start)
|
|
49
|
+
.lt("occurredAt", request.period.end),
|
|
50
|
+
)
|
|
51
|
+
.order("desc")
|
|
52
|
+
.paginate({
|
|
53
|
+
...args.paginationOpts,
|
|
54
|
+
maximumRowsRead: 200,
|
|
55
|
+
maximumBytesRead: 1000000,
|
|
56
|
+
});
|
|
57
|
+
const page = await Promise.all(
|
|
58
|
+
result.page.map(async (event) => {
|
|
59
|
+
const outbox = await ctx.db
|
|
60
|
+
.query("usageOutbox")
|
|
61
|
+
.withIndex("by_event", (q) => q.eq("eventId", event._id))
|
|
62
|
+
.unique();
|
|
63
|
+
if (!outbox) throw new Error("Usage outbox invariant violated");
|
|
64
|
+
return { event, delivery: outbox.state };
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
return { ...result, page };
|
|
68
|
+
},
|
|
69
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
export const scopeValidator = v.object({
|
|
5
|
+
stripeAccountId: v.string(),
|
|
6
|
+
customerAccount: v.string(),
|
|
7
|
+
meterId: v.string(),
|
|
8
|
+
livemode: v.boolean(),
|
|
9
|
+
});
|
|
10
|
+
export const deliveryValidator = v.union(
|
|
11
|
+
v.literal("queued"),
|
|
12
|
+
v.literal("sending"),
|
|
13
|
+
v.literal("provider_accepted"),
|
|
14
|
+
v.literal("rejected"),
|
|
15
|
+
v.literal("unknown"),
|
|
16
|
+
v.literal("held"),
|
|
17
|
+
);
|
|
18
|
+
export const eventFields = {
|
|
19
|
+
reservationId: v.optional(v.id("usageReservations")),
|
|
20
|
+
scopeKey: v.string(),
|
|
21
|
+
identity: v.string(),
|
|
22
|
+
recordJson: v.string(),
|
|
23
|
+
subscriptionJson: v.string(),
|
|
24
|
+
occurredAt: v.number(),
|
|
25
|
+
acceptedAt: v.number(),
|
|
26
|
+
};
|
|
27
|
+
export const eventDoc = v.object({
|
|
28
|
+
_id: v.id("usageEvents"),
|
|
29
|
+
_creationTime: v.number(),
|
|
30
|
+
...eventFields,
|
|
31
|
+
});
|
|
32
|
+
export const usageEventsTable = defineTable(eventFields)
|
|
33
|
+
.index("by_identity", ["identity"])
|
|
34
|
+
.index("by_scope_time", ["scopeKey", "occurredAt"]);
|
|
35
|
+
export const usageOutboxTable = defineTable({
|
|
36
|
+
eventId: v.id("usageEvents"),
|
|
37
|
+
scopeKey: v.string(),
|
|
38
|
+
state: deliveryValidator,
|
|
39
|
+
attempts: v.number(),
|
|
40
|
+
nextAttemptAt: v.number(),
|
|
41
|
+
providerIdentifier: v.string(),
|
|
42
|
+
dispatch: v.optional(v.string()),
|
|
43
|
+
})
|
|
44
|
+
.index("by_event", ["eventId"])
|
|
45
|
+
.index("by_state_next", ["state", "nextAttemptAt"])
|
|
46
|
+
.index("by_scope_state", ["scopeKey", "state"]);
|
|
47
|
+
export const usageScopeCountersTable = defineTable({
|
|
48
|
+
scopeKey: v.string(),
|
|
49
|
+
count: v.number(),
|
|
50
|
+
}).index("by_scope", ["scopeKey"]);
|
|
51
|
+
// Only accounting APIs may create reservations. There is no public arbitrary
|
|
52
|
+
// reservation/grant insertion API. Acceptance commits a matching reservation
|
|
53
|
+
// in the same transaction as its immutable event and outbox intent.
|
|
54
|
+
export const usageReservationsTable = defineTable({
|
|
55
|
+
scopeKey: v.string(),
|
|
56
|
+
sourceEventId: v.string(),
|
|
57
|
+
planJson: v.string(),
|
|
58
|
+
scaledQuantity: v.string(),
|
|
59
|
+
state: v.union(
|
|
60
|
+
v.literal("reserved"),
|
|
61
|
+
v.literal("committed"),
|
|
62
|
+
v.literal("released"),
|
|
63
|
+
),
|
|
64
|
+
eventId: v.optional(v.id("usageEvents")),
|
|
65
|
+
}).index("by_scope_source", ["scopeKey", "sourceEventId"]);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation } from "../_generated/server.js";
|
|
3
|
+
import { doc, kind } from "./schema.js";
|
|
4
|
+
function bounded(value: string, max: number) {
|
|
5
|
+
if (!value || new TextEncoder().encode(value).length > max)
|
|
6
|
+
throw new Error("Invalid usage catalog size");
|
|
7
|
+
}
|
|
8
|
+
export const prepare = mutation({
|
|
9
|
+
args: { scopeKey: v.string(), kind, key: v.string(), termsJson: v.string() },
|
|
10
|
+
returns: doc,
|
|
11
|
+
handler: async (ctx, args) => {
|
|
12
|
+
bounded(args.scopeKey, 256);
|
|
13
|
+
bounded(args.key, 512);
|
|
14
|
+
bounded(args.termsJson, 8192);
|
|
15
|
+
const existing = await ctx.db
|
|
16
|
+
.query("usageCatalog")
|
|
17
|
+
.withIndex("by_scope_kind_key", (q) =>
|
|
18
|
+
q
|
|
19
|
+
.eq("scopeKey", args.scopeKey)
|
|
20
|
+
.eq("kind", args.kind)
|
|
21
|
+
.eq("key", args.key),
|
|
22
|
+
)
|
|
23
|
+
.unique();
|
|
24
|
+
if (existing) {
|
|
25
|
+
if (existing.termsJson !== args.termsJson)
|
|
26
|
+
throw new Error("Immutable usage catalog terms conflict");
|
|
27
|
+
return existing;
|
|
28
|
+
}
|
|
29
|
+
const id = await ctx.db.insert("usageCatalog", {
|
|
30
|
+
...args,
|
|
31
|
+
firstAttemptAt: Math.floor(Date.now() / 1000),
|
|
32
|
+
});
|
|
33
|
+
const result = await ctx.db.get("usageCatalog", id);
|
|
34
|
+
if (!result) throw new Error("Usage catalog intent unavailable");
|
|
35
|
+
return result;
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
export const complete = mutation({
|
|
39
|
+
args: {
|
|
40
|
+
scopeKey: v.string(),
|
|
41
|
+
kind,
|
|
42
|
+
key: v.string(),
|
|
43
|
+
providerId: v.string(),
|
|
44
|
+
observationJson: v.string(),
|
|
45
|
+
},
|
|
46
|
+
returns: v.null(),
|
|
47
|
+
handler: async (ctx, args) => {
|
|
48
|
+
bounded(args.providerId, 100);
|
|
49
|
+
bounded(args.observationJson, 8192);
|
|
50
|
+
const row = await ctx.db
|
|
51
|
+
.query("usageCatalog")
|
|
52
|
+
.withIndex("by_scope_kind_key", (q) =>
|
|
53
|
+
q
|
|
54
|
+
.eq("scopeKey", args.scopeKey)
|
|
55
|
+
.eq("kind", args.kind)
|
|
56
|
+
.eq("key", args.key),
|
|
57
|
+
)
|
|
58
|
+
.unique();
|
|
59
|
+
if (!row || (row.providerId && row.providerId !== args.providerId))
|
|
60
|
+
throw new Error("Usage catalog reassignment rejected");
|
|
61
|
+
const existing = await ctx.db
|
|
62
|
+
.query("usageCatalog")
|
|
63
|
+
.withIndex("by_scope_kind_provider", (q) =>
|
|
64
|
+
q
|
|
65
|
+
.eq("scopeKey", args.scopeKey)
|
|
66
|
+
.eq("kind", args.kind)
|
|
67
|
+
.eq("providerId", args.providerId),
|
|
68
|
+
)
|
|
69
|
+
.unique();
|
|
70
|
+
if (existing && existing._id !== row._id)
|
|
71
|
+
throw new Error("Usage catalog ownership conflict");
|
|
72
|
+
await ctx.db.patch("usageCatalog", row._id, {
|
|
73
|
+
providerId: args.providerId,
|
|
74
|
+
observationJson: args.observationJson,
|
|
75
|
+
observedAt: Math.floor(Date.now() / 1000),
|
|
76
|
+
});
|
|
77
|
+
return null;
|
|
78
|
+
},
|
|
79
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { query } from "../_generated/server.js";
|
|
3
|
+
import { kind, doc } from "./schema.js";
|
|
4
|
+
export const get = query({
|
|
5
|
+
args: { scopeKey: v.string(), kind, key: v.string() },
|
|
6
|
+
returns: v.union(doc, v.null()),
|
|
7
|
+
handler: async (ctx, args) =>
|
|
8
|
+
ctx.db
|
|
9
|
+
.query("usageCatalog")
|
|
10
|
+
.withIndex("by_scope_kind_key", (q) =>
|
|
11
|
+
q
|
|
12
|
+
.eq("scopeKey", args.scopeKey)
|
|
13
|
+
.eq("kind", args.kind)
|
|
14
|
+
.eq("key", args.key),
|
|
15
|
+
)
|
|
16
|
+
.unique(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const getByProvider = query({
|
|
20
|
+
args: { scopeKey: v.string(), kind, providerId: v.string() },
|
|
21
|
+
returns: v.union(doc, v.null()),
|
|
22
|
+
handler: async (ctx, args) =>
|
|
23
|
+
ctx.db
|
|
24
|
+
.query("usageCatalog")
|
|
25
|
+
.withIndex("by_scope_kind_provider", (q) =>
|
|
26
|
+
q
|
|
27
|
+
.eq("scopeKey", args.scopeKey)
|
|
28
|
+
.eq("kind", args.kind)
|
|
29
|
+
.eq("providerId", args.providerId),
|
|
30
|
+
)
|
|
31
|
+
.unique(),
|
|
32
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
export const kind = v.union(v.literal("meter"), v.literal("price"));
|
|
4
|
+
export const fields = {
|
|
5
|
+
observedAt: v.optional(v.number()),
|
|
6
|
+
scopeKey: v.string(),
|
|
7
|
+
kind,
|
|
8
|
+
key: v.string(),
|
|
9
|
+
termsJson: v.string(),
|
|
10
|
+
firstAttemptAt: v.number(),
|
|
11
|
+
providerId: v.optional(v.string()),
|
|
12
|
+
observationJson: v.optional(v.string()),
|
|
13
|
+
};
|
|
14
|
+
export const doc = v.object({
|
|
15
|
+
_id: v.id("usageCatalog"),
|
|
16
|
+
_creationTime: v.number(),
|
|
17
|
+
...fields,
|
|
18
|
+
});
|
|
19
|
+
export const usageCatalogTable = defineTable(fields)
|
|
20
|
+
.index("by_scope_kind_key", ["scopeKey", "kind", "key"])
|
|
21
|
+
.index("by_scope_kind_provider", ["scopeKey", "kind", "providerId"]);
|