@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,402 @@
|
|
|
1
|
+
import { BetterStripe } from "../../client/index.js";
|
|
2
|
+
import type { Component } from "../../client/helpers.js";
|
|
3
|
+
import type { UsageDeliveryStartOptions } from "../../client/usage/deliveryClient.js";
|
|
4
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
5
|
+
import * as steps from "./steps.js";
|
|
6
|
+
import { request } from "./schema.js";
|
|
7
|
+
import { outcome as outcomeValidator } from "./fields.js";
|
|
8
|
+
import { convexTest } from "convex-test";
|
|
9
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
10
|
+
import {
|
|
11
|
+
anyApi,
|
|
12
|
+
componentsGeneric,
|
|
13
|
+
internalQueryGeneric,
|
|
14
|
+
defineSchema,
|
|
15
|
+
internalActionGeneric,
|
|
16
|
+
internalMutationGeneric,
|
|
17
|
+
} from "convex/server";
|
|
18
|
+
import { v } from "convex/values";
|
|
19
|
+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
|
20
|
+
import { schema, modules } from "../../test.js";
|
|
21
|
+
const nested = componentsGeneric();
|
|
22
|
+
const scope = {
|
|
23
|
+
stripeAccountId: "acct_platform",
|
|
24
|
+
customerAccount: "acct_buyer",
|
|
25
|
+
meterId: "mtr_calls",
|
|
26
|
+
livemode: false,
|
|
27
|
+
};
|
|
28
|
+
let calls = 0;
|
|
29
|
+
let saveFailures = 0;
|
|
30
|
+
let providerThrows = false;
|
|
31
|
+
let outcomes: Array<Record<string, unknown>> = [];
|
|
32
|
+
const host = {
|
|
33
|
+
start: internalMutationGeneric({
|
|
34
|
+
args: { source: v.string() },
|
|
35
|
+
returns: v.any(),
|
|
36
|
+
handler: async (ctx, { source }) => {
|
|
37
|
+
const now = Math.floor(Date.now() / 1000);
|
|
38
|
+
const event = await ctx.runMutation(
|
|
39
|
+
nested.betterStripe.usage.mutations.record,
|
|
40
|
+
{
|
|
41
|
+
inputJson: JSON.stringify({
|
|
42
|
+
scope,
|
|
43
|
+
binding: {
|
|
44
|
+
stripeCustomerId: "cus_buyer",
|
|
45
|
+
stripePriceId: "price_calls",
|
|
46
|
+
eventName: "calls",
|
|
47
|
+
plan: {
|
|
48
|
+
key: "calls",
|
|
49
|
+
version: "v1",
|
|
50
|
+
model: "pay_as_you_go",
|
|
51
|
+
unit: { name: "call", precision: 0 },
|
|
52
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
sourceEventId: source,
|
|
56
|
+
quantity: "2",
|
|
57
|
+
occurredAt: now,
|
|
58
|
+
}),
|
|
59
|
+
subscriptionJson: JSON.stringify({
|
|
60
|
+
subscriptionId: "sub_calls",
|
|
61
|
+
subscriptionItemId: "si_calls",
|
|
62
|
+
stripePriceId: "price_calls",
|
|
63
|
+
period: { start: now - 3600, end: now + 86400 },
|
|
64
|
+
interval: "month",
|
|
65
|
+
}),
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
return new BetterStripe(
|
|
69
|
+
nested.betterStripe as unknown as Component,
|
|
70
|
+
).usage.startDelivery(ctx, {
|
|
71
|
+
scope,
|
|
72
|
+
eventId: event.eventId,
|
|
73
|
+
provider: anyApi.host.provider as UsageDeliveryStartOptions["provider"],
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
provider: internalActionGeneric({
|
|
78
|
+
args: {
|
|
79
|
+
scope: v.any(),
|
|
80
|
+
eventId: v.string(),
|
|
81
|
+
attempt: v.number(),
|
|
82
|
+
definitionVersion: v.number(),
|
|
83
|
+
apiVersion: v.string(),
|
|
84
|
+
identifier: v.string(),
|
|
85
|
+
},
|
|
86
|
+
returns: v.any(),
|
|
87
|
+
handler: async (_ctx, args) => {
|
|
88
|
+
calls++;
|
|
89
|
+
if (providerThrows)
|
|
90
|
+
throw new Error("sk_test_PRIVATE private provider body");
|
|
91
|
+
const outcome = outcomes.shift() ?? {
|
|
92
|
+
status: "accepted",
|
|
93
|
+
created: Math.floor(Date.now() / 1000),
|
|
94
|
+
};
|
|
95
|
+
return { ...args, outcome };
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
async function fixture() {
|
|
100
|
+
const t = convexTest(defineSchema({}), {
|
|
101
|
+
"./_generated/api.ts": async () => ({}),
|
|
102
|
+
"./host.ts": async () => host,
|
|
103
|
+
});
|
|
104
|
+
t.registerComponent("betterStripe", schema, {
|
|
105
|
+
...modules,
|
|
106
|
+
"./component/usageDelivery/steps.ts": async () => ({
|
|
107
|
+
...steps,
|
|
108
|
+
save: internalMutationGeneric({
|
|
109
|
+
args: { ...request, outcome: outcomeValidator },
|
|
110
|
+
returns: v.any(),
|
|
111
|
+
handler: async (ctx, args) => {
|
|
112
|
+
if (saveFailures-- > 0)
|
|
113
|
+
throw new Error("Usage receipt projection failed");
|
|
114
|
+
return (
|
|
115
|
+
steps.save as unknown as {
|
|
116
|
+
_handler: (ctx: unknown, args: unknown) => Promise<unknown>;
|
|
117
|
+
}
|
|
118
|
+
)._handler(ctx, args);
|
|
119
|
+
},
|
|
120
|
+
}),
|
|
121
|
+
}),
|
|
122
|
+
"./component/fixture.ts": async () => ({
|
|
123
|
+
history: internalQueryGeneric({
|
|
124
|
+
args: {},
|
|
125
|
+
returns: v.any(),
|
|
126
|
+
handler: async (ctx) => {
|
|
127
|
+
const op = (await ctx.db.query("usageOutbox").first())!;
|
|
128
|
+
return executionWorkflow.listSteps(ctx, op.workflowId!, {
|
|
129
|
+
paginationOpts: { cursor: null, numItems: 100 },
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
}),
|
|
134
|
+
});
|
|
135
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
136
|
+
return t;
|
|
137
|
+
}
|
|
138
|
+
beforeEach(() => {
|
|
139
|
+
vi.useFakeTimers();
|
|
140
|
+
calls = 0;
|
|
141
|
+
outcomes = [];
|
|
142
|
+
saveFailures = 0;
|
|
143
|
+
providerThrows = false;
|
|
144
|
+
});
|
|
145
|
+
afterEach(() => vi.useRealTimers());
|
|
146
|
+
it("dispatches duplicate requests once through the existing nested Workflow", async () => {
|
|
147
|
+
const t = await fixture();
|
|
148
|
+
const first = await t.mutation(anyApi.host.start, { source: "one" });
|
|
149
|
+
const replay = await t.mutation(anyApi.host.start, { source: "one" });
|
|
150
|
+
expect(replay.eventId).toBe(first.eventId);
|
|
151
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
152
|
+
expect(calls).toBe(1);
|
|
153
|
+
expect(
|
|
154
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
155
|
+
scope,
|
|
156
|
+
eventId: first.eventId,
|
|
157
|
+
}),
|
|
158
|
+
).toMatchObject({ state: "provider_accepted", attempts: 1 });
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("backs off known rate limits without losing partial-batch progress", async () => {
|
|
162
|
+
const t = await fixture();
|
|
163
|
+
outcomes = [
|
|
164
|
+
{ status: "rate_limited" },
|
|
165
|
+
{ status: "accepted", created: Math.floor(Date.now() / 1000) },
|
|
166
|
+
];
|
|
167
|
+
const a = await t.mutation(anyApi.host.start, { source: "a" });
|
|
168
|
+
const b = await t.mutation(anyApi.host.start, { source: "b" });
|
|
169
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
170
|
+
expect(calls).toBe(3);
|
|
171
|
+
for (const op of [a, b])
|
|
172
|
+
expect(
|
|
173
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
174
|
+
scope,
|
|
175
|
+
eventId: op.eventId,
|
|
176
|
+
}),
|
|
177
|
+
).toMatchObject({ state: "provider_accepted" });
|
|
178
|
+
});
|
|
179
|
+
it("holds unknown effects and refuses blind resume even inside the dedupe window", async () => {
|
|
180
|
+
const t = await fixture();
|
|
181
|
+
outcomes = [{ status: "unknown" }];
|
|
182
|
+
const op = await t.mutation(anyApi.host.start, { source: "unknown" });
|
|
183
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
184
|
+
expect(
|
|
185
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
186
|
+
scope,
|
|
187
|
+
eventId: op.eventId,
|
|
188
|
+
}),
|
|
189
|
+
).toMatchObject({ state: "unknown", reason: "provider_outcome_unknown" });
|
|
190
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
191
|
+
scope,
|
|
192
|
+
eventId: op.eventId,
|
|
193
|
+
});
|
|
194
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
195
|
+
expect(calls).toBe(1);
|
|
196
|
+
});
|
|
197
|
+
it("rejects foreign scope controls and preserves acceptance when held", async () => {
|
|
198
|
+
const t = await fixture();
|
|
199
|
+
const op = await t.mutation(anyApi.host.start, { source: "held" });
|
|
200
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.hold, {
|
|
201
|
+
scope,
|
|
202
|
+
eventId: op.eventId,
|
|
203
|
+
});
|
|
204
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
205
|
+
expect(calls).toBe(0);
|
|
206
|
+
expect(
|
|
207
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
208
|
+
scope,
|
|
209
|
+
eventId: op.eventId,
|
|
210
|
+
}),
|
|
211
|
+
).toMatchObject({ state: "held", held: true });
|
|
212
|
+
await expect(
|
|
213
|
+
t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
214
|
+
scope: { ...scope, customerAccount: "acct_foreign" },
|
|
215
|
+
eventId: op.eventId,
|
|
216
|
+
}),
|
|
217
|
+
).rejects.toThrow();
|
|
218
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
219
|
+
scope,
|
|
220
|
+
eventId: op.eventId,
|
|
221
|
+
});
|
|
222
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
223
|
+
expect(calls).toBe(1);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("retains transport acceptance alongside scoped asynchronous validation errors", async () => {
|
|
227
|
+
const t = await fixture();
|
|
228
|
+
const op = await t.mutation(anyApi.host.start, { source: "async" });
|
|
229
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
230
|
+
const receipt = await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
231
|
+
scope,
|
|
232
|
+
eventId: op.eventId,
|
|
233
|
+
});
|
|
234
|
+
const report = {
|
|
235
|
+
stripeAccountId: scope.stripeAccountId,
|
|
236
|
+
livemode: false,
|
|
237
|
+
providerEventId: "evt_error",
|
|
238
|
+
meterId: scope.meterId,
|
|
239
|
+
samples: [
|
|
240
|
+
{
|
|
241
|
+
identifier: receipt.providerIdentifier,
|
|
242
|
+
code: "meter_event_customer_not_found",
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
truncated: false,
|
|
246
|
+
};
|
|
247
|
+
await t.mutation(
|
|
248
|
+
nested.betterStripe.usageDelivery.mutations.recordErrorReport,
|
|
249
|
+
report,
|
|
250
|
+
);
|
|
251
|
+
await t.mutation(
|
|
252
|
+
nested.betterStripe.usageDelivery.mutations.recordErrorReport,
|
|
253
|
+
report,
|
|
254
|
+
);
|
|
255
|
+
const final = await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
256
|
+
scope,
|
|
257
|
+
eventId: op.eventId,
|
|
258
|
+
});
|
|
259
|
+
expect(final).toMatchObject({
|
|
260
|
+
state: "provider_accepted",
|
|
261
|
+
validationErrors: [
|
|
262
|
+
{ code: "meter_event_customer_not_found", providerEventId: "evt_error" },
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
expect(final.validationErrors).toHaveLength(1);
|
|
266
|
+
expect(
|
|
267
|
+
await t.mutation(
|
|
268
|
+
nested.betterStripe.usageDelivery.mutations.recordErrorReport,
|
|
269
|
+
{
|
|
270
|
+
...report,
|
|
271
|
+
stripeAccountId: "acct_foreign",
|
|
272
|
+
providerEventId: "evt_foreign",
|
|
273
|
+
},
|
|
274
|
+
),
|
|
275
|
+
).toMatchObject({ matched: 0, unattributed: 1 });
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("restarts a failed receipt projection without repeating the provider step", async () => {
|
|
279
|
+
const t = await fixture();
|
|
280
|
+
saveFailures = 1;
|
|
281
|
+
const op = await t.mutation(anyApi.host.start, { source: "projection" });
|
|
282
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
283
|
+
expect(calls).toBe(1);
|
|
284
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
285
|
+
scope,
|
|
286
|
+
eventId: op.eventId,
|
|
287
|
+
});
|
|
288
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
289
|
+
expect(calls).toBe(1);
|
|
290
|
+
expect(
|
|
291
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
292
|
+
scope,
|
|
293
|
+
eventId: op.eventId,
|
|
294
|
+
}),
|
|
295
|
+
).toMatchObject({ state: "provider_accepted" });
|
|
296
|
+
});
|
|
297
|
+
it("holds provider exceptions without journaling private data", async () => {
|
|
298
|
+
const t = await fixture();
|
|
299
|
+
providerThrows = true;
|
|
300
|
+
const op = await t.mutation(anyApi.host.start, { source: "private" });
|
|
301
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
302
|
+
expect(
|
|
303
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
304
|
+
scope,
|
|
305
|
+
eventId: op.eventId,
|
|
306
|
+
}),
|
|
307
|
+
).toMatchObject({ state: "unknown" });
|
|
308
|
+
const journal = JSON.stringify(
|
|
309
|
+
await t.query(nested.betterStripe.fixture.history, {}),
|
|
310
|
+
);
|
|
311
|
+
expect(journal).not.toContain("sk_test_PRIVATE");
|
|
312
|
+
expect(journal).not.toContain("private provider body");
|
|
313
|
+
});
|
|
314
|
+
it("stops retryable failures after three sends and supports explicit credential recovery", async () => {
|
|
315
|
+
const t = await fixture();
|
|
316
|
+
outcomes = [{ status: "unavailable" }];
|
|
317
|
+
const op = await t.mutation(anyApi.host.start, { source: "unavailable" });
|
|
318
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
319
|
+
expect(calls).toBe(1);
|
|
320
|
+
expect(
|
|
321
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
322
|
+
scope,
|
|
323
|
+
eventId: op.eventId,
|
|
324
|
+
}),
|
|
325
|
+
).toMatchObject({ state: "held", reason: "provider_unavailable" });
|
|
326
|
+
outcomes = [
|
|
327
|
+
{ status: "rate_limited" },
|
|
328
|
+
{ status: "rate_limited" },
|
|
329
|
+
{ status: "rate_limited" },
|
|
330
|
+
];
|
|
331
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
332
|
+
scope,
|
|
333
|
+
eventId: op.eventId,
|
|
334
|
+
});
|
|
335
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
336
|
+
expect(calls).toBe(4);
|
|
337
|
+
expect(
|
|
338
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
339
|
+
scope,
|
|
340
|
+
eventId: op.eventId,
|
|
341
|
+
}),
|
|
342
|
+
).toMatchObject({ state: "held", reason: "retry_exhausted" });
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("holds an expired deduplication window without another provider call", async () => {
|
|
346
|
+
const t = await fixture();
|
|
347
|
+
outcomes = [{ status: "unavailable" }];
|
|
348
|
+
const op = await t.mutation(anyApi.host.start, { source: "expired" });
|
|
349
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
350
|
+
vi.setSystemTime(Date.now() + 24 * 3600 * 1000);
|
|
351
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
352
|
+
scope,
|
|
353
|
+
eventId: op.eventId,
|
|
354
|
+
});
|
|
355
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
356
|
+
expect(calls).toBe(1);
|
|
357
|
+
expect(
|
|
358
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
359
|
+
scope,
|
|
360
|
+
eventId: op.eventId,
|
|
361
|
+
}),
|
|
362
|
+
).toMatchObject({ state: "unknown", reason: "delivery_window_expired" });
|
|
363
|
+
});
|
|
364
|
+
it("holds an event whose native timestamp window elapsed before first dispatch", async () => {
|
|
365
|
+
const t = await fixture();
|
|
366
|
+
const op = await t.mutation(anyApi.host.start, { source: "late" });
|
|
367
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.hold, {
|
|
368
|
+
scope,
|
|
369
|
+
eventId: op.eventId,
|
|
370
|
+
});
|
|
371
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
372
|
+
vi.setSystemTime(Date.now() + 36 * 86400 * 1000);
|
|
373
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
374
|
+
scope,
|
|
375
|
+
eventId: op.eventId,
|
|
376
|
+
});
|
|
377
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
378
|
+
expect(calls).toBe(0);
|
|
379
|
+
expect(
|
|
380
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
381
|
+
scope,
|
|
382
|
+
eventId: op.eventId,
|
|
383
|
+
}),
|
|
384
|
+
).toMatchObject({ state: "held", reason: "delivery_window_expired" });
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("clears an administrative hold on a completed event without another send", async () => {
|
|
388
|
+
const t = await fixture();
|
|
389
|
+
const op = await t.mutation(anyApi.host.start, { source: "completed-hold" });
|
|
390
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
391
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.hold, {
|
|
392
|
+
scope,
|
|
393
|
+
eventId: op.eventId,
|
|
394
|
+
});
|
|
395
|
+
expect(
|
|
396
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
397
|
+
scope,
|
|
398
|
+
eventId: op.eventId,
|
|
399
|
+
}),
|
|
400
|
+
).toMatchObject({ state: "provider_accepted", held: false });
|
|
401
|
+
expect(calls).toBe(1);
|
|
402
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { anyApi } from "convex/server";
|
|
2
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
3
|
+
import { refFields } from "./schema.js";
|
|
4
|
+
import { v } from "convex/values";
|
|
5
|
+
export const run = executionWorkflow
|
|
6
|
+
.define({ args: refFields, returns: v.null() })
|
|
7
|
+
.handler(async (step, args): Promise<null> => {
|
|
8
|
+
let sends = 0;
|
|
9
|
+
for (let turn = 0; turn < 40; turn++) {
|
|
10
|
+
const claimed = await step.runMutation(
|
|
11
|
+
anyApi.usageDelivery.steps.claim,
|
|
12
|
+
args,
|
|
13
|
+
{ name: `claim-${turn}` },
|
|
14
|
+
);
|
|
15
|
+
if (claimed.kind === "done") return null;
|
|
16
|
+
if (claimed.kind === "wait") {
|
|
17
|
+
await step.sleep(claimed.delayMs);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const outcome = await step.runAction(
|
|
21
|
+
anyApi.usageDelivery.bridge.invoke,
|
|
22
|
+
claimed.request,
|
|
23
|
+
{ name: `provider-${turn}`, retry: false },
|
|
24
|
+
);
|
|
25
|
+
const saved = await step.runMutation(
|
|
26
|
+
anyApi.usageDelivery.steps.save,
|
|
27
|
+
{ ...claimed.request, outcome },
|
|
28
|
+
{ name: "save-receipt" },
|
|
29
|
+
);
|
|
30
|
+
sends++;
|
|
31
|
+
if (saved.state !== "queued") return null;
|
|
32
|
+
if (sends >= 3) break;
|
|
33
|
+
}
|
|
34
|
+
await step.runMutation(anyApi.usageDelivery.steps.exhaust, args);
|
|
35
|
+
return null;
|
|
36
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { canonicalFingerprint } from "../operationEvidence/canonical.js";
|
|
3
|
+
import {
|
|
4
|
+
observationOptions,
|
|
5
|
+
type UsageObservationOptions,
|
|
6
|
+
} from "../../client/usage/observationTypes.js";
|
|
7
|
+
import { scopeKey } from "../usage/helpers.js";
|
|
8
|
+
import { decimalFromScaled } from "../../client/usage/summary.js";
|
|
9
|
+
const integer = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER);
|
|
10
|
+
const period = z.strictObject({ start: integer, end: integer });
|
|
11
|
+
const scaled = z.string().regex(/^\d{1,30}$/),
|
|
12
|
+
quantity = z.string().regex(/^\d{1,30}(\.\d{1,6})?$/);
|
|
13
|
+
const value = z.strictObject({
|
|
14
|
+
period,
|
|
15
|
+
quantity,
|
|
16
|
+
scaledQuantity: scaled,
|
|
17
|
+
complete: z.boolean(),
|
|
18
|
+
nextCursor: z.string().max(2048).nullable(),
|
|
19
|
+
aggregationLagPossible: z.literal(true),
|
|
20
|
+
stale: z.literal(false),
|
|
21
|
+
entries: z
|
|
22
|
+
.array(
|
|
23
|
+
z.strictObject({
|
|
24
|
+
id: z.string().min(1).max(2048),
|
|
25
|
+
start: integer,
|
|
26
|
+
end: integer,
|
|
27
|
+
scaledQuantity: scaled,
|
|
28
|
+
}),
|
|
29
|
+
)
|
|
30
|
+
.max(100),
|
|
31
|
+
});
|
|
32
|
+
const availability = z.union([
|
|
33
|
+
z.strictObject({
|
|
34
|
+
status: z.literal("available"),
|
|
35
|
+
observedAt: integer,
|
|
36
|
+
value,
|
|
37
|
+
}),
|
|
38
|
+
z.strictObject({
|
|
39
|
+
status: z.enum(["unavailable", "unsupported"]),
|
|
40
|
+
reason: z.enum([
|
|
41
|
+
"minute_alignment_required",
|
|
42
|
+
"provider_binding_mismatch",
|
|
43
|
+
"unsupported_scope",
|
|
44
|
+
"aggregation_pending",
|
|
45
|
+
"provider_history_incomplete",
|
|
46
|
+
"provider_precision_unsupported",
|
|
47
|
+
"provider_unavailable",
|
|
48
|
+
]),
|
|
49
|
+
retryable: z.boolean(),
|
|
50
|
+
}),
|
|
51
|
+
]);
|
|
52
|
+
export async function source(json: string) {
|
|
53
|
+
if (new TextEncoder().encode(json).length > 8192)
|
|
54
|
+
throw new Error("Usage observation source exceeds limit");
|
|
55
|
+
const opts = observationOptions(JSON.parse(json) as UsageObservationOptions);
|
|
56
|
+
const sourceJson = JSON.stringify(opts);
|
|
57
|
+
return {
|
|
58
|
+
opts,
|
|
59
|
+
sourceJson,
|
|
60
|
+
scopeKey: scopeKey(opts.scope),
|
|
61
|
+
key: await canonicalFingerprint(opts),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function observation(
|
|
65
|
+
json: string,
|
|
66
|
+
opts: ReturnType<typeof observationOptions>,
|
|
67
|
+
) {
|
|
68
|
+
if (new TextEncoder().encode(json).length > 65536)
|
|
69
|
+
throw new Error("Invalid usage provider observation");
|
|
70
|
+
const parsed = availability.safeParse(JSON.parse(json));
|
|
71
|
+
if (!parsed.success) throw new Error("Invalid usage provider observation");
|
|
72
|
+
const result = parsed.data;
|
|
73
|
+
if (result.status === "available") {
|
|
74
|
+
const v = result.value;
|
|
75
|
+
if (
|
|
76
|
+
result.observedAt > Date.now() / 1000 + 300 ||
|
|
77
|
+
JSON.stringify(v.period) !== JSON.stringify(opts.period) ||
|
|
78
|
+
v.quantity !==
|
|
79
|
+
decimalFromScaled(
|
|
80
|
+
BigInt(v.scaledQuantity),
|
|
81
|
+
opts.binding.plan.unit.precision,
|
|
82
|
+
) ||
|
|
83
|
+
v.entries.reduce((sum, e) => sum + BigInt(e.scaledQuantity), 0n) !==
|
|
84
|
+
BigInt(v.scaledQuantity) ||
|
|
85
|
+
v.entries.some(
|
|
86
|
+
(e) =>
|
|
87
|
+
e.start < opts.period.start ||
|
|
88
|
+
e.end > opts.period.end ||
|
|
89
|
+
e.end <= e.start,
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
throw new Error("Invalid usage provider observation");
|
|
93
|
+
}
|
|
94
|
+
return JSON.stringify(result);
|
|
95
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
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 ref = (name: string) => makeFunctionReference<"mutation">(name);
|
|
6
|
+
it("retains paged delivery attempts and outcomes without admitting a foreign scope", async () => {
|
|
7
|
+
const t = convexTest(schema, modules),
|
|
8
|
+
now = Math.floor(Date.now() / 1000);
|
|
9
|
+
const scope = {
|
|
10
|
+
stripeAccountId: "acct_platform",
|
|
11
|
+
customerAccount: "acct_buyer",
|
|
12
|
+
meterId: "mtr_calls",
|
|
13
|
+
livemode: false,
|
|
14
|
+
};
|
|
15
|
+
const receipt = await t.mutation(ref("usage/mutations:record"), {
|
|
16
|
+
inputJson: JSON.stringify({
|
|
17
|
+
scope,
|
|
18
|
+
binding: {
|
|
19
|
+
stripeCustomerId: "cus_buyer",
|
|
20
|
+
stripePriceId: "price_calls",
|
|
21
|
+
eventName: "calls",
|
|
22
|
+
plan: {
|
|
23
|
+
key: "calls",
|
|
24
|
+
version: "v1",
|
|
25
|
+
model: "pay_as_you_go",
|
|
26
|
+
unit: { name: "call", precision: 0 },
|
|
27
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
sourceEventId: "one",
|
|
31
|
+
quantity: "1",
|
|
32
|
+
occurredAt: now,
|
|
33
|
+
}),
|
|
34
|
+
subscriptionJson: JSON.stringify({
|
|
35
|
+
subscriptionId: "sub_calls",
|
|
36
|
+
subscriptionItemId: "si_calls",
|
|
37
|
+
stripePriceId: "price_calls",
|
|
38
|
+
period: { start: now - 1, end: now + 86400 },
|
|
39
|
+
interval: "month",
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
await t.run(async (ctx) => {
|
|
43
|
+
const row = (await ctx.db.query("usageOutbox").first())!;
|
|
44
|
+
await ctx.db.patch("usageOutbox", row._id, { definitionVersion: 1 });
|
|
45
|
+
});
|
|
46
|
+
const claimed = await t.mutation(ref("usageDelivery/steps:claim"), {
|
|
47
|
+
scope,
|
|
48
|
+
eventId: receipt.eventId,
|
|
49
|
+
});
|
|
50
|
+
await t.mutation(ref("usageDelivery/steps:save"), {
|
|
51
|
+
...claimed.request,
|
|
52
|
+
outcome: { status: "accepted", created: now },
|
|
53
|
+
});
|
|
54
|
+
const history = makeFunctionReference<"query">(
|
|
55
|
+
"usageObservations/queries:deliveryHistory",
|
|
56
|
+
);
|
|
57
|
+
const args = {
|
|
58
|
+
scope,
|
|
59
|
+
eventId: receipt.eventId,
|
|
60
|
+
paginationOpts: { cursor: null, numItems: 1 },
|
|
61
|
+
};
|
|
62
|
+
const first = await t.query(history, args);
|
|
63
|
+
expect(first).toMatchObject({
|
|
64
|
+
page: [{ phase: "started", attempt: 1, sequence: 1 }],
|
|
65
|
+
isDone: false,
|
|
66
|
+
legacyHistoryMissing: false,
|
|
67
|
+
});
|
|
68
|
+
const second = await t.query(history, {
|
|
69
|
+
...args,
|
|
70
|
+
paginationOpts: { cursor: first.continueCursor, numItems: 1 },
|
|
71
|
+
});
|
|
72
|
+
expect(second).toMatchObject({
|
|
73
|
+
page: [{ phase: "accepted", attempt: 1, sequence: 2 }],
|
|
74
|
+
});
|
|
75
|
+
expect(
|
|
76
|
+
await t.query(history, {
|
|
77
|
+
...args,
|
|
78
|
+
paginationOpts: { cursor: second.continueCursor, numItems: 1 },
|
|
79
|
+
}),
|
|
80
|
+
).toMatchObject({ page: [], isDone: true });
|
|
81
|
+
await expect(
|
|
82
|
+
t.query(history, {
|
|
83
|
+
...args,
|
|
84
|
+
scope: { ...scope, customerAccount: "acct_other" },
|
|
85
|
+
}),
|
|
86
|
+
).rejects.toThrow();
|
|
87
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { MutationCtx } from "../_generated/server.js";
|
|
2
|
+
import type { Id } from "../_generated/dataModel.js";
|
|
3
|
+
import type { Infer } from "convex/values";
|
|
4
|
+
import type { historyPhase } from "./schema.js";
|
|
5
|
+
export async function appendDeliveryHistory(
|
|
6
|
+
ctx: MutationCtx,
|
|
7
|
+
eventId: Id<"usageEvents">,
|
|
8
|
+
phase: Infer<typeof historyPhase>,
|
|
9
|
+
attempt: number,
|
|
10
|
+
identity: string,
|
|
11
|
+
created?: number,
|
|
12
|
+
) {
|
|
13
|
+
const prior = await ctx.db
|
|
14
|
+
.query("usageDeliveryHistory")
|
|
15
|
+
.withIndex("by_event_identity", (q) =>
|
|
16
|
+
q.eq("eventId", eventId).eq("identity", identity),
|
|
17
|
+
)
|
|
18
|
+
.unique();
|
|
19
|
+
if (prior) return;
|
|
20
|
+
const outbox = await ctx.db
|
|
21
|
+
.query("usageOutbox")
|
|
22
|
+
.withIndex("by_event", (q) => q.eq("eventId", eventId))
|
|
23
|
+
.unique();
|
|
24
|
+
if (!outbox) throw new Error("Usage history event unavailable");
|
|
25
|
+
const sequence = (outbox.historySequence ?? 0) + 1;
|
|
26
|
+
if (!Number.isSafeInteger(sequence))
|
|
27
|
+
throw new Error("Usage history sequence exhausted");
|
|
28
|
+
await ctx.db.insert("usageDeliveryHistory", {
|
|
29
|
+
eventId,
|
|
30
|
+
scopeKey: outbox.scopeKey,
|
|
31
|
+
sequence,
|
|
32
|
+
attempt,
|
|
33
|
+
phase,
|
|
34
|
+
identity,
|
|
35
|
+
recordedAt: Math.floor(Date.now() / 1000),
|
|
36
|
+
...(created !== undefined ? { created } : {}),
|
|
37
|
+
});
|
|
38
|
+
await ctx.db.patch("usageOutbox", outbox._id, { historySequence: sequence });
|
|
39
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { mutation } from "../_generated/server.js";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { source, observation } from "./helpers.js";
|
|
4
|
+
export const save = mutation({
|
|
5
|
+
args: {
|
|
6
|
+
sourceJson: v.string(),
|
|
7
|
+
observationJson: v.string(),
|
|
8
|
+
startedAt: v.number(),
|
|
9
|
+
},
|
|
10
|
+
returns: v.null(),
|
|
11
|
+
handler: async (ctx, args) => {
|
|
12
|
+
if (
|
|
13
|
+
!Number.isSafeInteger(args.startedAt) ||
|
|
14
|
+
args.startedAt < 0 ||
|
|
15
|
+
args.startedAt > Date.now() + 300000
|
|
16
|
+
)
|
|
17
|
+
throw new Error("Invalid observation start time");
|
|
18
|
+
const binding = await source(args.sourceJson),
|
|
19
|
+
observationJson = observation(args.observationJson, binding.opts);
|
|
20
|
+
const prior = await ctx.db
|
|
21
|
+
.query("usageProviderObservations")
|
|
22
|
+
.withIndex("by_key", (q) => q.eq("key", binding.key))
|
|
23
|
+
.unique();
|
|
24
|
+
if (prior && prior.sourceJson !== binding.sourceJson)
|
|
25
|
+
throw new Error("Usage observation binding conflict");
|
|
26
|
+
if (prior && prior.startedAt > args.startedAt) return null;
|
|
27
|
+
const fields = {
|
|
28
|
+
key: binding.key,
|
|
29
|
+
scopeKey: binding.scopeKey,
|
|
30
|
+
sourceJson: binding.sourceJson,
|
|
31
|
+
observationJson,
|
|
32
|
+
startedAt: args.startedAt,
|
|
33
|
+
};
|
|
34
|
+
if (prior)
|
|
35
|
+
await ctx.db.patch("usageProviderObservations", prior._id, fields);
|
|
36
|
+
else await ctx.db.insert("usageProviderObservations", fields);
|
|
37
|
+
return null;
|
|
38
|
+
},
|
|
39
|
+
});
|