@fantastic.dev/better-stripe 0.53.0 → 0.55.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 +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +226 -34
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +97 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +226 -34
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +97 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +8 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +1 -0
- package/dist/client/index.js.map +1 -1
- 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 +3 -3
- package/dist/client/usage/client.js.map +1 -1
- package/dist/client/usage/deliveryClient.d.ts +64 -0
- package/dist/client/usage/deliveryClient.d.ts.map +1 -0
- package/dist/client/usage/deliveryClient.js +60 -0
- package/dist/client/usage/deliveryClient.js.map +1 -0
- package/dist/client/usage/deliveryProtocol.d.ts +130 -0
- package/dist/client/usage/deliveryProtocol.d.ts.map +1 -0
- package/dist/client/usage/deliveryProtocol.js +21 -0
- package/dist/client/usage/deliveryProtocol.js.map +1 -0
- package/dist/client/usage/deliveryProvider.d.ts +32 -0
- package/dist/client/usage/deliveryProvider.d.ts.map +1 -0
- package/dist/client/usage/deliveryProvider.js +127 -0
- package/dist/client/usage/deliveryProvider.js.map +1 -0
- package/dist/client/usage/index.d.ts +8 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/client/usage/index.js +1 -0
- package/dist/client/usage/index.js.map +1 -1
- package/dist/client/usage/invoiceObservation.d.ts +38 -0
- package/dist/client/usage/invoiceObservation.d.ts.map +1 -0
- package/dist/client/usage/invoiceObservation.js +157 -0
- package/dist/client/usage/invoiceObservation.js.map +1 -0
- package/dist/client/usage/observationClient.d.ts +83 -0
- package/dist/client/usage/observationClient.d.ts.map +1 -0
- package/dist/client/usage/observationClient.js +157 -0
- package/dist/client/usage/observationClient.js.map +1 -0
- package/dist/client/usage/observationTypes.d.ts +63 -0
- package/dist/client/usage/observationTypes.d.ts.map +1 -0
- package/dist/client/usage/observationTypes.js +59 -0
- package/dist/client/usage/observationTypes.js.map +1 -0
- package/dist/client/usage/providerObservation.d.ts +4 -0
- package/dist/client/usage/providerObservation.d.ts.map +1 -0
- package/dist/client/usage/providerObservation.js +96 -0
- package/dist/client/usage/providerObservation.js.map +1 -0
- package/dist/client/usage/reconciliation.d.ts +12 -0
- package/dist/client/usage/reconciliation.d.ts.map +1 -0
- package/dist/client/usage/reconciliation.js +48 -0
- package/dist/client/usage/reconciliation.js.map +1 -0
- package/dist/client/usage/summary.d.ts +50 -0
- package/dist/client/usage/summary.d.ts.map +1 -0
- package/dist/client/usage/summary.js +110 -0
- package/dist/client/usage/summary.js.map +1 -0
- package/dist/client/webhooks/handler.d.ts.map +1 -1
- package/dist/client/webhooks/handler.js +2 -1
- package/dist/client/webhooks/handler.js.map +1 -1
- package/dist/client/webhooks/meterErrors.d.ts +5 -0
- package/dist/client/webhooks/meterErrors.d.ts.map +1 -0
- package/dist/client/webhooks/meterErrors.js +98 -0
- package/dist/client/webhooks/meterErrors.js.map +1 -0
- package/dist/client/webhooks/v2.d.ts.map +1 -1
- package/dist/client/webhooks/v2.js +5 -0
- package/dist/client/webhooks/v2.js.map +1 -1
- package/dist/component/_generated/api.d.ts +22 -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 +265 -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/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +206 -18
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/schema.d.ts +97 -1
- package/dist/component/schema.js +6 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/schema.d.ts +101 -1
- package/dist/component/usage/schema.d.ts.map +1 -1
- package/dist/component/usage/schema.js +11 -2
- package/dist/component/usage/schema.js.map +1 -1
- package/dist/component/usageDelivery/bridge.d.ts +25 -0
- package/dist/component/usageDelivery/bridge.d.ts.map +1 -0
- package/dist/component/usageDelivery/bridge.js +45 -0
- package/dist/component/usageDelivery/bridge.js.map +1 -0
- package/dist/component/usageDelivery/fields.d.ts +79 -0
- package/dist/component/usageDelivery/fields.d.ts.map +1 -0
- package/dist/component/usageDelivery/fields.js +39 -0
- package/dist/component/usageDelivery/fields.js.map +1 -0
- package/dist/component/usageDelivery/helpers.d.ts +84 -0
- package/dist/component/usageDelivery/helpers.d.ts.map +1 -0
- package/dist/component/usageDelivery/helpers.js +63 -0
- package/dist/component/usageDelivery/helpers.js.map +1 -0
- package/dist/component/usageDelivery/mutations.d.ts +97 -0
- package/dist/component/usageDelivery/mutations.d.ts.map +1 -0
- package/dist/component/usageDelivery/mutations.js +160 -0
- package/dist/component/usageDelivery/mutations.js.map +1 -0
- package/dist/component/usageDelivery/queries.d.ts +115 -0
- package/dist/component/usageDelivery/queries.d.ts.map +1 -0
- package/dist/component/usageDelivery/queries.js +22 -0
- package/dist/component/usageDelivery/queries.js.map +1 -0
- package/dist/component/usageDelivery/schema.d.ts +79 -0
- package/dist/component/usageDelivery/schema.d.ts.map +1 -0
- package/dist/component/usageDelivery/schema.js +30 -0
- package/dist/component/usageDelivery/schema.js.map +1 -0
- package/dist/component/usageDelivery/steps.d.ts +85 -0
- package/dist/component/usageDelivery/steps.d.ts.map +1 -0
- package/dist/component/usageDelivery/steps.js +161 -0
- package/dist/component/usageDelivery/steps.js.map +1 -0
- package/dist/component/usageDelivery/workflow.d.ts +15 -0
- package/dist/component/usageDelivery/workflow.d.ts.map +1 -0
- package/dist/component/usageDelivery/workflow.js +28 -0
- package/dist/component/usageDelivery/workflow.js.map +1 -0
- package/dist/component/usageObservations/helpers.d.ts +38 -0
- package/dist/component/usageObservations/helpers.d.ts.map +1 -0
- package/dist/component/usageObservations/helpers.js +80 -0
- package/dist/component/usageObservations/helpers.js.map +1 -0
- package/dist/component/usageObservations/history.d.ts +6 -0
- package/dist/component/usageObservations/history.d.ts.map +1 -0
- package/dist/component/usageObservations/history.js +29 -0
- package/dist/component/usageObservations/history.js.map +1 -0
- package/dist/component/usageObservations/mutations.d.ts +6 -0
- package/dist/component/usageObservations/mutations.d.ts.map +1 -0
- package/dist/component/usageObservations/mutations.js +39 -0
- package/dist/component/usageObservations/mutations.js.map +1 -0
- package/dist/component/usageObservations/queries.d.ts +78 -0
- package/dist/component/usageObservations/queries.d.ts.map +1 -0
- package/dist/component/usageObservations/queries.js +131 -0
- package/dist/component/usageObservations/queries.js.map +1 -0
- package/dist/component/usageObservations/schema.d.ts +72 -0
- package/dist/component/usageObservations/schema.d.ts.map +1 -0
- package/dist/component/usageObservations/schema.js +29 -0
- package/dist/component/usageObservations/schema.js.map +1 -0
- package/docs/README.md +5 -1
- package/docs/metering-setup.md +3 -1
- package/docs/usage-delivery.md +143 -0
- package/docs/usage-observations.md +152 -0
- package/package.json +4 -2
- package/src/client/index.ts +34 -0
- package/src/client/usage/client.ts +5 -2
- package/src/client/usage/deliveryClient.ts +130 -0
- package/src/client/usage/deliveryProtocol.ts +26 -0
- package/src/client/usage/deliveryProvider.test.ts +207 -0
- package/src/client/usage/deliveryProvider.ts +155 -0
- package/src/client/usage/index.ts +31 -0
- package/src/client/usage/invoiceObservation.test.ts +195 -0
- package/src/client/usage/invoiceObservation.ts +203 -0
- package/src/client/usage/observationClient.ts +233 -0
- package/src/client/usage/observationTypes.ts +101 -0
- package/src/client/usage/providerObservation.test.ts +207 -0
- package/src/client/usage/providerObservation.ts +124 -0
- package/src/client/usage/reconciliation.test.ts +117 -0
- package/src/client/usage/reconciliation.ts +86 -0
- package/src/client/usage/summary.test.ts +222 -0
- package/src/client/usage/summary.ts +188 -0
- package/src/client/webhooks/handler.ts +2 -1
- package/src/client/webhooks/meterErrors.test.ts +164 -0
- package/src/client/webhooks/meterErrors.ts +118 -0
- package/src/client/webhooks/v2.ts +5 -0
- package/src/component/_generated/api.ts +22 -0
- package/src/component/_generated/component.ts +437 -0
- package/src/component/schema.ts +12 -0
- package/src/component/usage/schema.ts +11 -2
- package/src/component/usageDelivery/bridge.ts +56 -0
- package/src/component/usageDelivery/concurrency.test.ts +62 -0
- package/src/component/usageDelivery/fields.ts +60 -0
- package/src/component/usageDelivery/helpers.ts +77 -0
- package/src/component/usageDelivery/mutations.ts +210 -0
- package/src/component/usageDelivery/queries.ts +25 -0
- package/src/component/usageDelivery/schema.ts +33 -0
- package/src/component/usageDelivery/steps.ts +209 -0
- package/src/component/usageDelivery/workflow.test.ts +402 -0
- package/src/component/usageDelivery/workflow.ts +36 -0
- package/src/component/usageObservations/helpers.ts +95 -0
- package/src/component/usageObservations/history.test.ts +87 -0
- package/src/component/usageObservations/history.ts +39 -0
- package/src/component/usageObservations/mutations.ts +39 -0
- package/src/component/usageObservations/queries.test.ts +162 -0
- package/src/component/usageObservations/queries.ts +145 -0
- package/src/component/usageObservations/schema.ts +40 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { makeFunctionReference } from "convex/server";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import { schema, modules } from "../../test.js";
|
|
5
|
+
const summary = makeFunctionReference<"query">(
|
|
6
|
+
"usageObservations/queries:summary",
|
|
7
|
+
),
|
|
8
|
+
record = makeFunctionReference<"mutation">("usage/mutations:record");
|
|
9
|
+
it("bounds local summaries and never includes another scope", async () => {
|
|
10
|
+
const t = convexTest(schema, modules),
|
|
11
|
+
now = Math.floor(Date.now() / 1000),
|
|
12
|
+
period = { start: now - 5, end: now + 100 };
|
|
13
|
+
const scope = {
|
|
14
|
+
stripeAccountId: "acct_platform",
|
|
15
|
+
customerAccount: "acct_buyer",
|
|
16
|
+
meterId: "mtr_calls",
|
|
17
|
+
livemode: false,
|
|
18
|
+
};
|
|
19
|
+
for (const sourceEventId of ["a", "b"])
|
|
20
|
+
await t.mutation(record, {
|
|
21
|
+
inputJson: JSON.stringify({
|
|
22
|
+
scope,
|
|
23
|
+
binding: {
|
|
24
|
+
stripeCustomerId: "cus_buyer",
|
|
25
|
+
stripePriceId: "price_calls",
|
|
26
|
+
eventName: "calls",
|
|
27
|
+
plan: {
|
|
28
|
+
key: "calls",
|
|
29
|
+
version: "v1",
|
|
30
|
+
model: "pay_as_you_go",
|
|
31
|
+
unit: { name: "call", precision: 0 },
|
|
32
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
sourceEventId,
|
|
36
|
+
quantity: "1",
|
|
37
|
+
occurredAt: now,
|
|
38
|
+
}),
|
|
39
|
+
subscriptionJson: JSON.stringify({
|
|
40
|
+
subscriptionId: "sub_calls",
|
|
41
|
+
subscriptionItemId: "si_calls",
|
|
42
|
+
stripePriceId: "price_calls",
|
|
43
|
+
period,
|
|
44
|
+
interval: "month",
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
expect(
|
|
48
|
+
JSON.parse(await t.query(summary, { scope, period, maxRecords: 1 })),
|
|
49
|
+
).toMatchObject({
|
|
50
|
+
complete: false,
|
|
51
|
+
eventCount: 1,
|
|
52
|
+
groups: [
|
|
53
|
+
{
|
|
54
|
+
acceptedQuantity: "1",
|
|
55
|
+
estimate: { status: "unavailable", reason: "local_history_incomplete" },
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
});
|
|
59
|
+
expect(
|
|
60
|
+
JSON.parse(
|
|
61
|
+
await t.query(summary, {
|
|
62
|
+
scope: { ...scope, customerAccount: "acct_other" },
|
|
63
|
+
period,
|
|
64
|
+
maxRecords: 10,
|
|
65
|
+
}),
|
|
66
|
+
),
|
|
67
|
+
).toMatchObject({ complete: true, eventCount: 0, groups: [] });
|
|
68
|
+
await expect(
|
|
69
|
+
t.query(summary, { scope, period, maxRecords: 501 }),
|
|
70
|
+
).rejects.toThrow();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("retains immutable scoped cache bindings and rejects stale projections and raw payloads", async () => {
|
|
74
|
+
const t = convexTest(schema, modules),
|
|
75
|
+
now = Math.floor(Date.now() / 1000),
|
|
76
|
+
period = { start: 600, end: 1200 };
|
|
77
|
+
const source = {
|
|
78
|
+
scope: {
|
|
79
|
+
stripeAccountId: "acct_platform",
|
|
80
|
+
customerAccount: "acct_buyer",
|
|
81
|
+
meterId: "mtr_calls",
|
|
82
|
+
livemode: false,
|
|
83
|
+
},
|
|
84
|
+
binding: {
|
|
85
|
+
stripeCustomerId: "cus_buyer",
|
|
86
|
+
stripePriceId: "price_calls",
|
|
87
|
+
eventName: "calls",
|
|
88
|
+
plan: {
|
|
89
|
+
key: "calls",
|
|
90
|
+
version: "v1",
|
|
91
|
+
model: "pay_as_you_go",
|
|
92
|
+
unit: { name: "call", precision: 0 },
|
|
93
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
subscription: {
|
|
97
|
+
subscriptionId: "sub_calls",
|
|
98
|
+
subscriptionItemId: "si_calls",
|
|
99
|
+
stripePriceId: "price_calls",
|
|
100
|
+
period,
|
|
101
|
+
interval: "month",
|
|
102
|
+
},
|
|
103
|
+
period,
|
|
104
|
+
limit: 100,
|
|
105
|
+
cursor: null,
|
|
106
|
+
};
|
|
107
|
+
const save = makeFunctionReference<"mutation">(
|
|
108
|
+
"usageObservations/mutations:save",
|
|
109
|
+
),
|
|
110
|
+
cached = makeFunctionReference<"query">("usageObservations/queries:cached");
|
|
111
|
+
const observation = {
|
|
112
|
+
status: "available",
|
|
113
|
+
observedAt: now,
|
|
114
|
+
value: {
|
|
115
|
+
period,
|
|
116
|
+
quantity: "1",
|
|
117
|
+
scaledQuantity: "1",
|
|
118
|
+
complete: true,
|
|
119
|
+
nextCursor: null,
|
|
120
|
+
aggregationLagPossible: true,
|
|
121
|
+
stale: false,
|
|
122
|
+
entries: [
|
|
123
|
+
{ id: "mtrusg_one", start: 600, end: 1200, scaledQuantity: "1" },
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
const args = {
|
|
128
|
+
sourceJson: JSON.stringify(source),
|
|
129
|
+
observationJson: JSON.stringify(observation),
|
|
130
|
+
startedAt: Date.now(),
|
|
131
|
+
};
|
|
132
|
+
await t.mutation(save, args);
|
|
133
|
+
await t.mutation(save, {
|
|
134
|
+
...args,
|
|
135
|
+
startedAt: args.startedAt - 1,
|
|
136
|
+
observationJson: JSON.stringify({
|
|
137
|
+
status: "unavailable",
|
|
138
|
+
reason: "provider_unavailable",
|
|
139
|
+
retryable: true,
|
|
140
|
+
}),
|
|
141
|
+
});
|
|
142
|
+
expect(
|
|
143
|
+
JSON.parse(await t.query(cached, { sourceJson: args.sourceJson })),
|
|
144
|
+
).toMatchObject({ status: "available", value: { quantity: "1" } });
|
|
145
|
+
expect(
|
|
146
|
+
await t.query(cached, {
|
|
147
|
+
sourceJson: JSON.stringify({
|
|
148
|
+
...source,
|
|
149
|
+
scope: { ...source.scope, customerAccount: "acct_other" },
|
|
150
|
+
}),
|
|
151
|
+
}),
|
|
152
|
+
).toBeNull();
|
|
153
|
+
await expect(
|
|
154
|
+
t.mutation(save, {
|
|
155
|
+
...args,
|
|
156
|
+
observationJson: JSON.stringify({
|
|
157
|
+
...observation,
|
|
158
|
+
privateMetadata: "private",
|
|
159
|
+
}),
|
|
160
|
+
}),
|
|
161
|
+
).rejects.toThrow("Invalid usage provider observation");
|
|
162
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
paginationOptsValidator,
|
|
3
|
+
paginationResultValidator,
|
|
4
|
+
} from "convex/server";
|
|
5
|
+
import { paginator } from "convex-helpers/server/pagination";
|
|
6
|
+
import schema from "../schema.js";
|
|
7
|
+
import { refFields } from "../usageDelivery/schema.js";
|
|
8
|
+
import { read } from "../usageDelivery/helpers.js";
|
|
9
|
+
import { historyDoc } from "./schema.js";
|
|
10
|
+
import { errorDoc } from "../usageDelivery/fields.js";
|
|
11
|
+
import { source } from "./helpers.js";
|
|
12
|
+
import { query } from "../_generated/server.js";
|
|
13
|
+
import { v } from "convex/values";
|
|
14
|
+
import { scopeValidator } from "../usage/schema.js";
|
|
15
|
+
import { scopeKey, subscriptionSnapshot } from "../usage/helpers.js";
|
|
16
|
+
import { validateUsagePageRequest } from "../../client/usage/boundaries.js";
|
|
17
|
+
import { summarizeUsageRecords } from "../../client/usage/summary.js";
|
|
18
|
+
import type { PreparedUsageRecord } from "../../client/usage/records.js";
|
|
19
|
+
export const summary = query({
|
|
20
|
+
args: {
|
|
21
|
+
scope: scopeValidator,
|
|
22
|
+
period: v.object({ start: v.number(), end: v.number() }),
|
|
23
|
+
maxRecords: v.number(),
|
|
24
|
+
},
|
|
25
|
+
returns: v.string(),
|
|
26
|
+
handler: async (ctx, args) => {
|
|
27
|
+
const key = scopeKey(args.scope);
|
|
28
|
+
validateUsagePageRequest({ period: args.period, limit: 1, cursor: null });
|
|
29
|
+
if (
|
|
30
|
+
!Number.isInteger(args.maxRecords) ||
|
|
31
|
+
args.maxRecords < 1 ||
|
|
32
|
+
args.maxRecords > 500
|
|
33
|
+
)
|
|
34
|
+
throw new Error("Usage summary read budget must be 1–500");
|
|
35
|
+
const rows = await ctx.db
|
|
36
|
+
.query("usageEvents")
|
|
37
|
+
.withIndex("by_scope_time", (q) =>
|
|
38
|
+
q
|
|
39
|
+
.eq("scopeKey", key)
|
|
40
|
+
.gte("occurredAt", args.period.start)
|
|
41
|
+
.lt("occurredAt", args.period.end),
|
|
42
|
+
)
|
|
43
|
+
.take(args.maxRecords + 1);
|
|
44
|
+
const views = await Promise.all(
|
|
45
|
+
rows.slice(0, args.maxRecords).map(async (event) => {
|
|
46
|
+
const [outbox, error] = await Promise.all([
|
|
47
|
+
ctx.db
|
|
48
|
+
.query("usageOutbox")
|
|
49
|
+
.withIndex("by_event", (q) => q.eq("eventId", event._id))
|
|
50
|
+
.unique(),
|
|
51
|
+
ctx.db
|
|
52
|
+
.query("usageDeliveryErrors")
|
|
53
|
+
.withIndex("by_event", (q) => q.eq("eventId", event._id))
|
|
54
|
+
.first(),
|
|
55
|
+
]);
|
|
56
|
+
if (!outbox) throw new Error("Usage outbox invariant violated");
|
|
57
|
+
return {
|
|
58
|
+
record: JSON.parse(event.recordJson) as PreparedUsageRecord,
|
|
59
|
+
subscription: subscriptionSnapshot(event.subscriptionJson),
|
|
60
|
+
delivery: outbox.state,
|
|
61
|
+
hasValidationError: error !== null,
|
|
62
|
+
};
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
return JSON.stringify(
|
|
66
|
+
summarizeUsageRecords(views, {
|
|
67
|
+
period: args.period,
|
|
68
|
+
observedAt: Math.floor(Date.now() / 1000),
|
|
69
|
+
complete: rows.length <= args.maxRecords,
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
export const cached = query({
|
|
76
|
+
args: { sourceJson: v.string() },
|
|
77
|
+
returns: v.union(v.string(), v.null()),
|
|
78
|
+
handler: async (ctx, args) => {
|
|
79
|
+
const binding = await source(args.sourceJson);
|
|
80
|
+
const row = await ctx.db
|
|
81
|
+
.query("usageProviderObservations")
|
|
82
|
+
.withIndex("by_key", (q) => q.eq("key", binding.key))
|
|
83
|
+
.unique();
|
|
84
|
+
return row &&
|
|
85
|
+
row.scopeKey === binding.scopeKey &&
|
|
86
|
+
row.sourceJson === binding.sourceJson
|
|
87
|
+
? row.observationJson
|
|
88
|
+
: null;
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
function validate(numItems: number, cursor: string | null) {
|
|
93
|
+
validateUsagePageRequest({
|
|
94
|
+
period: { start: 0, end: 1 },
|
|
95
|
+
limit: numItems,
|
|
96
|
+
cursor,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const paginated = paginationResultValidator(historyDoc);
|
|
100
|
+
export const deliveryHistory = query({
|
|
101
|
+
args: { ...refFields, paginationOpts: paginationOptsValidator },
|
|
102
|
+
returns: v.object({ ...paginated.fields, legacyHistoryMissing: v.boolean() }),
|
|
103
|
+
handler: async (ctx, args) => {
|
|
104
|
+
const { outbox } = await read(ctx, args);
|
|
105
|
+
validate(args.paginationOpts.numItems, args.paginationOpts.cursor);
|
|
106
|
+
const [page, first] = await Promise.all([
|
|
107
|
+
paginator(ctx.db, schema)
|
|
108
|
+
.query("usageDeliveryHistory")
|
|
109
|
+
.withIndex("by_event_sequence", (q) => q.eq("eventId", args.eventId))
|
|
110
|
+
.order("asc")
|
|
111
|
+
.paginate({
|
|
112
|
+
...args.paginationOpts,
|
|
113
|
+
maximumRowsRead: 200,
|
|
114
|
+
maximumBytesRead: 1000000,
|
|
115
|
+
}),
|
|
116
|
+
ctx.db
|
|
117
|
+
.query("usageDeliveryHistory")
|
|
118
|
+
.withIndex("by_event_identity", (q) =>
|
|
119
|
+
q.eq("eventId", args.eventId).eq("identity", "started:1"),
|
|
120
|
+
)
|
|
121
|
+
.unique(),
|
|
122
|
+
]);
|
|
123
|
+
return {
|
|
124
|
+
...page,
|
|
125
|
+
legacyHistoryMissing: outbox.attempts > 0 && first === null,
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
export const validationHistory = query({
|
|
130
|
+
args: { ...refFields, paginationOpts: paginationOptsValidator },
|
|
131
|
+
returns: paginationResultValidator(errorDoc),
|
|
132
|
+
handler: async (ctx, args) => {
|
|
133
|
+
await read(ctx, args);
|
|
134
|
+
validate(args.paginationOpts.numItems, args.paginationOpts.cursor);
|
|
135
|
+
return paginator(ctx.db, schema)
|
|
136
|
+
.query("usageDeliveryErrors")
|
|
137
|
+
.withIndex("by_event", (q) => q.eq("eventId", args.eventId))
|
|
138
|
+
.order("desc")
|
|
139
|
+
.paginate({
|
|
140
|
+
...args.paginationOpts,
|
|
141
|
+
maximumRowsRead: 200,
|
|
142
|
+
maximumBytesRead: 1000000,
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
export const observationsTable = defineTable({
|
|
4
|
+
key: v.string(),
|
|
5
|
+
scopeKey: v.string(),
|
|
6
|
+
sourceJson: v.string(),
|
|
7
|
+
observationJson: v.string(),
|
|
8
|
+
startedAt: v.number(),
|
|
9
|
+
}).index("by_key", ["key"]);
|
|
10
|
+
|
|
11
|
+
export const historyPhase = v.union(
|
|
12
|
+
v.literal("started"),
|
|
13
|
+
v.literal("accepted"),
|
|
14
|
+
v.literal("rate_limited"),
|
|
15
|
+
v.literal("unavailable"),
|
|
16
|
+
v.literal("unknown"),
|
|
17
|
+
v.literal("rejected"),
|
|
18
|
+
v.literal("held"),
|
|
19
|
+
v.literal("resumed"),
|
|
20
|
+
v.literal("window_expired"),
|
|
21
|
+
v.literal("retry_exhausted"),
|
|
22
|
+
);
|
|
23
|
+
export const historyFields = {
|
|
24
|
+
scopeKey: v.string(),
|
|
25
|
+
eventId: v.id("usageEvents"),
|
|
26
|
+
sequence: v.number(),
|
|
27
|
+
attempt: v.number(),
|
|
28
|
+
phase: historyPhase,
|
|
29
|
+
identity: v.string(),
|
|
30
|
+
recordedAt: v.number(),
|
|
31
|
+
created: v.optional(v.number()),
|
|
32
|
+
};
|
|
33
|
+
export const historyDoc = v.object({
|
|
34
|
+
_id: v.id("usageDeliveryHistory"),
|
|
35
|
+
_creationTime: v.number(),
|
|
36
|
+
...historyFields,
|
|
37
|
+
});
|
|
38
|
+
export const historyTable = defineTable(historyFields)
|
|
39
|
+
.index("by_event_sequence", ["eventId", "sequence"])
|
|
40
|
+
.index("by_event_identity", ["eventId", "identity"]);
|