@fantastic.dev/better-stripe 0.39.0 → 0.41.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 +24 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +442 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +221 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +442 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +221 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/costRecovery-invoices.d.ts +2 -2
- package/dist/client/costRecovery-invoices.d.ts.map +1 -1
- package/dist/client/costRecovery-invoices.js +7 -6
- package/dist/client/costRecovery-invoices.js.map +1 -1
- package/dist/client/costRecovery.d.ts +5 -2
- package/dist/client/costRecovery.d.ts.map +1 -1
- package/dist/client/costRecovery.js +3 -1
- package/dist/client/costRecovery.js.map +1 -1
- package/dist/client/index.d.ts +3 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/paymentSource/client.d.ts +21 -0
- package/dist/client/paymentSource/client.d.ts.map +1 -0
- package/dist/client/paymentSource/client.js +8 -0
- package/dist/client/paymentSource/client.js.map +1 -0
- package/dist/client/paymentSource/contracts.d.ts +30 -0
- package/dist/client/paymentSource/contracts.d.ts.map +1 -0
- package/dist/client/paymentSource/contracts.js +16 -0
- package/dist/client/paymentSource/contracts.js.map +1 -0
- package/dist/client/paymentSource/economics.d.ts +12 -0
- package/dist/client/paymentSource/economics.d.ts.map +1 -0
- package/dist/client/paymentSource/economics.js +13 -0
- package/dist/client/paymentSource/economics.js.map +1 -0
- package/dist/client/paymentSource/funding.d.ts +10 -0
- package/dist/client/paymentSource/funding.d.ts.map +1 -0
- package/dist/client/paymentSource/funding.js +12 -0
- package/dist/client/paymentSource/funding.js.map +1 -0
- package/dist/client/paymentSource/history.d.ts +13 -0
- package/dist/client/paymentSource/history.d.ts.map +1 -0
- package/dist/client/paymentSource/history.js +41 -0
- package/dist/client/paymentSource/history.js.map +1 -0
- package/dist/client/paymentSource/resolver.d.ts +6 -0
- package/dist/client/paymentSource/resolver.d.ts.map +1 -0
- package/dist/client/paymentSource/resolver.js +222 -0
- package/dist/client/paymentSource/resolver.js.map +1 -0
- package/dist/client/recoveryWorkflow/client.d.ts +14 -0
- package/dist/client/recoveryWorkflow/client.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/client.js +34 -0
- package/dist/client/recoveryWorkflow/client.js.map +1 -0
- package/dist/client/recoveryWorkflow/contracts.d.ts +63 -0
- package/dist/client/recoveryWorkflow/contracts.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/contracts.js +11 -0
- package/dist/client/recoveryWorkflow/contracts.js.map +1 -0
- package/dist/client/recoveryWorkflow/provider.d.ts +10 -0
- package/dist/client/recoveryWorkflow/provider.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/provider.js +80 -0
- package/dist/client/recoveryWorkflow/provider.js.map +1 -0
- package/dist/client/recoveryWorkflow/settlement.d.ts +6 -0
- package/dist/client/recoveryWorkflow/settlement.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/settlement.js +43 -0
- package/dist/client/recoveryWorkflow/settlement.js.map +1 -0
- package/dist/client/settlement/verifiedSale.d.ts.map +1 -1
- package/dist/client/settlement/verifiedSale.js +4 -6
- package/dist/client/settlement/verifiedSale.js.map +1 -1
- package/dist/component/_generated/api.d.ts +12 -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 +164 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/triggers.d.ts +194 -0
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/bridge.d.ts +21 -0
- package/dist/component/recoveryWorkflow/bridge.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/bridge.js +52 -0
- package/dist/component/recoveryWorkflow/bridge.js.map +1 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts +83 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/helpers.js +48 -0
- package/dist/component/recoveryWorkflow/helpers.js.map +1 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts +91 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/mutations.js +177 -0
- package/dist/component/recoveryWorkflow/mutations.js.map +1 -0
- package/dist/component/recoveryWorkflow/queries.d.ts +118 -0
- package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/queries.js +20 -0
- package/dist/component/recoveryWorkflow/queries.js.map +1 -0
- package/dist/component/recoveryWorkflow/schema.d.ts +653 -0
- package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/schema.js +81 -0
- package/dist/component/recoveryWorkflow/schema.js.map +1 -0
- package/dist/component/recoveryWorkflow/steps.d.ts +70 -0
- package/dist/component/recoveryWorkflow/steps.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/steps.js +94 -0
- package/dist/component/recoveryWorkflow/steps.js.map +1 -0
- package/dist/component/recoveryWorkflow/workflow.d.ts +22 -0
- package/dist/component/recoveryWorkflow/workflow.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/workflow.js +56 -0
- package/dist/component/recoveryWorkflow/workflow.js.map +1 -0
- package/dist/component/schema.d.ts +221 -0
- package/dist/component/schema.js +3 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/settlement.md +48 -0
- package/package.json +1 -1
- package/src/client/costRecovery-invoices.ts +9 -7
- package/src/client/costRecovery.ts +8 -3
- package/src/client/index.ts +6 -2
- package/src/client/paymentSource/client.ts +13 -0
- package/src/client/paymentSource/contracts.ts +33 -0
- package/src/client/paymentSource/economics.ts +20 -0
- package/src/client/paymentSource/funding.ts +23 -0
- package/src/client/paymentSource/history.ts +48 -0
- package/src/client/paymentSource/resolver.test.ts +582 -0
- package/src/client/paymentSource/resolver.ts +350 -0
- package/src/client/recoveryWorkflow/client.test.ts +37 -0
- package/src/client/recoveryWorkflow/client.ts +76 -0
- package/src/client/recoveryWorkflow/contracts.ts +72 -0
- package/src/client/recoveryWorkflow/provider.test.ts +424 -0
- package/src/client/recoveryWorkflow/provider.ts +119 -0
- package/src/client/recoveryWorkflow/settlement.ts +67 -0
- package/src/client/settlement/verifiedSale.ts +4 -10
- package/src/component/_generated/api.ts +12 -0
- package/src/component/_generated/component.ts +222 -0
- package/src/component/recoveryWorkflow/bridge.test.ts +481 -0
- package/src/component/recoveryWorkflow/bridge.ts +66 -0
- package/src/component/recoveryWorkflow/helpers.ts +58 -0
- package/src/component/recoveryWorkflow/journal.test.ts +104 -0
- package/src/component/recoveryWorkflow/mutations.ts +224 -0
- package/src/component/recoveryWorkflow/queries.ts +19 -0
- package/src/component/recoveryWorkflow/schema.ts +92 -0
- package/src/component/recoveryWorkflow/state.test.ts +191 -0
- package/src/component/recoveryWorkflow/steps.ts +97 -0
- package/src/component/recoveryWorkflow/workflow.ts +73 -0
- package/src/component/schema.ts +6 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
+
import { makeFunctionReference } from "convex/server";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import { modules, schema } from "../../test.js";
|
|
6
|
+
const start = makeFunctionReference<"mutation">(
|
|
7
|
+
"recoveryWorkflow/mutations:start",
|
|
8
|
+
);
|
|
9
|
+
const scope = {
|
|
10
|
+
accountId: "acct_a",
|
|
11
|
+
scopeKey: "seller",
|
|
12
|
+
currency: "usd",
|
|
13
|
+
groupKey: "group",
|
|
14
|
+
};
|
|
15
|
+
async function fixture() {
|
|
16
|
+
const t = convexTest(schema, modules);
|
|
17
|
+
workflowTest.register(t, "workflow");
|
|
18
|
+
await t.run(async (ctx) => {
|
|
19
|
+
await ctx.db.insert("recoveryGroups", {
|
|
20
|
+
...scope,
|
|
21
|
+
sourceKind: "invoice",
|
|
22
|
+
sourceId: "in_a",
|
|
23
|
+
maxDebit: 100,
|
|
24
|
+
maxCredit: 0,
|
|
25
|
+
amount: 0,
|
|
26
|
+
pendingOperations: 0,
|
|
27
|
+
status: "sealed",
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
return t;
|
|
31
|
+
}
|
|
32
|
+
const args = {
|
|
33
|
+
...scope,
|
|
34
|
+
requestKey: "request",
|
|
35
|
+
provider: "function://host/provider",
|
|
36
|
+
providerApiVersion: "2026-09-30.endive",
|
|
37
|
+
definitionVersion: 1,
|
|
38
|
+
releaseUncollectible: false,
|
|
39
|
+
maxAttempts: 3,
|
|
40
|
+
retryDelayMs: 1000,
|
|
41
|
+
};
|
|
42
|
+
it("binds duplicate requests to one operation and rejects changed policy", async () => {
|
|
43
|
+
const t = await fixture();
|
|
44
|
+
const first = await t.mutation(start, args);
|
|
45
|
+
expect(await t.mutation(start, args)).toEqual(first);
|
|
46
|
+
expect(
|
|
47
|
+
await t.mutation(start, { ...args, releaseUncollectible: true }),
|
|
48
|
+
).toEqual({
|
|
49
|
+
status: "conflict",
|
|
50
|
+
reason: "identity_changed",
|
|
51
|
+
retryable: false,
|
|
52
|
+
});
|
|
53
|
+
expect(
|
|
54
|
+
await t.run((ctx) => ctx.db.query("recoveryWorkflowOperations").collect()),
|
|
55
|
+
).toHaveLength(1);
|
|
56
|
+
});
|
|
57
|
+
it("does not expose operations through another authorized scope", async () => {
|
|
58
|
+
const t = await fixture();
|
|
59
|
+
await t.mutation(start, args);
|
|
60
|
+
expect(
|
|
61
|
+
await t.mutation(start, {
|
|
62
|
+
...args,
|
|
63
|
+
requestKey: "other",
|
|
64
|
+
accountId: "acct_b",
|
|
65
|
+
}),
|
|
66
|
+
).toEqual({
|
|
67
|
+
status: "conflict",
|
|
68
|
+
reason: "identity_changed",
|
|
69
|
+
retryable: false,
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
it("rejects a second group that would reuse the same settlement adjustment", async () => {
|
|
73
|
+
const t = await fixture();
|
|
74
|
+
const allocation = {
|
|
75
|
+
sourceChargeId: "ch_a",
|
|
76
|
+
currency: "usd",
|
|
77
|
+
gross: 100,
|
|
78
|
+
processingFee: 10,
|
|
79
|
+
platformNet: 90,
|
|
80
|
+
adjustmentAmount: 0,
|
|
81
|
+
transfers: [],
|
|
82
|
+
};
|
|
83
|
+
await t.run(async (ctx) => {
|
|
84
|
+
for (const groupKey of ["settle_a", "settle_b"])
|
|
85
|
+
await ctx.db.insert("recoveryGroups", {
|
|
86
|
+
...scope,
|
|
87
|
+
groupKey,
|
|
88
|
+
sourceKind: "settlement",
|
|
89
|
+
sourceId: "ch_a",
|
|
90
|
+
sourceGross: 100,
|
|
91
|
+
sourceProcessingFee: 10,
|
|
92
|
+
maxDebit: 100,
|
|
93
|
+
maxCredit: 0,
|
|
94
|
+
amount: 0,
|
|
95
|
+
pendingOperations: 0,
|
|
96
|
+
status: "sealed",
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
await t.mutation(start, { ...args, groupKey: "settle_a", allocation });
|
|
100
|
+
expect(
|
|
101
|
+
await t.mutation(start, {
|
|
102
|
+
...args,
|
|
103
|
+
groupKey: "settle_b",
|
|
104
|
+
requestKey: "second",
|
|
105
|
+
allocation,
|
|
106
|
+
}),
|
|
107
|
+
).toEqual({
|
|
108
|
+
status: "conflict",
|
|
109
|
+
reason: "identity_changed",
|
|
110
|
+
retryable: false,
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
it("compares every existing allocation field even when optional requested fields are absent", async () => {
|
|
114
|
+
const t = await fixture();
|
|
115
|
+
const allocation = {
|
|
116
|
+
sourceChargeId: "ch_a",
|
|
117
|
+
currency: "usd",
|
|
118
|
+
gross: 100,
|
|
119
|
+
processingFee: 10,
|
|
120
|
+
platformNet: 90,
|
|
121
|
+
transfers: [],
|
|
122
|
+
};
|
|
123
|
+
await t.run(async (ctx) => {
|
|
124
|
+
await ctx.db.insert("recoveryGroups", {
|
|
125
|
+
...scope,
|
|
126
|
+
groupKey: "settle",
|
|
127
|
+
sourceKind: "settlement",
|
|
128
|
+
sourceId: "ch_a",
|
|
129
|
+
sourceGross: 100,
|
|
130
|
+
sourceProcessingFee: 10,
|
|
131
|
+
maxDebit: 100,
|
|
132
|
+
maxCredit: 0,
|
|
133
|
+
amount: 0,
|
|
134
|
+
pendingOperations: 0,
|
|
135
|
+
status: "sealed",
|
|
136
|
+
});
|
|
137
|
+
await ctx.db.insert("settlementAllocations", {
|
|
138
|
+
...allocation,
|
|
139
|
+
paymentId: "original_payment",
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
expect(
|
|
143
|
+
await t.mutation(start, { ...args, groupKey: "settle", allocation }),
|
|
144
|
+
).toEqual({
|
|
145
|
+
status: "conflict",
|
|
146
|
+
reason: "identity_changed",
|
|
147
|
+
retryable: false,
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
it("concurrent aliases join one immutable group operation", async () => {
|
|
151
|
+
const t = await fixture();
|
|
152
|
+
const outcomes = await Promise.all([
|
|
153
|
+
t.mutation(start, args),
|
|
154
|
+
t.mutation(start, { ...args, requestKey: "alias" }),
|
|
155
|
+
]);
|
|
156
|
+
expect(outcomes[0].operationId).toBe(outcomes[1].operationId);
|
|
157
|
+
expect(
|
|
158
|
+
await t.run((ctx) => ctx.db.query("recoveryWorkflowOperations").collect()),
|
|
159
|
+
).toHaveLength(1);
|
|
160
|
+
expect(
|
|
161
|
+
await t.run((ctx) => ctx.db.query("recoveryWorkflowRequests").collect()),
|
|
162
|
+
).toHaveLength(2);
|
|
163
|
+
});
|
|
164
|
+
it("admits execution only after preparation has sealed the group", async () => {
|
|
165
|
+
const t = await fixture();
|
|
166
|
+
await t.run(async (ctx) => {
|
|
167
|
+
const group = (await ctx.db.query("recoveryGroups").first())!;
|
|
168
|
+
await ctx.db.patch(group._id, { status: "open" });
|
|
169
|
+
});
|
|
170
|
+
expect(await t.mutation(start, args)).toEqual({
|
|
171
|
+
status: "not_ready",
|
|
172
|
+
reason: "history_incomplete",
|
|
173
|
+
retryable: true,
|
|
174
|
+
});
|
|
175
|
+
expect(
|
|
176
|
+
await t.run((ctx) => ctx.db.query("recoveryWorkflowRequests").collect()),
|
|
177
|
+
).toHaveLength(0);
|
|
178
|
+
});
|
|
179
|
+
it.each([
|
|
180
|
+
{ maxAttempts: 0 },
|
|
181
|
+
{ maxAttempts: 21 },
|
|
182
|
+
{ retryDelayMs: 0 },
|
|
183
|
+
{ retryDelayMs: 86400001 },
|
|
184
|
+
{ providerApiVersion: "sk_test_not_a_version" },
|
|
185
|
+
{ definitionVersion: 2 },
|
|
186
|
+
])("rejects invalid durable execution policy %j", async (patch) => {
|
|
187
|
+
const t = await fixture();
|
|
188
|
+
await expect(t.mutation(start, { ...args, ...patch })).rejects.toThrow(
|
|
189
|
+
"Invalid recovery workflow policy",
|
|
190
|
+
);
|
|
191
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type FunctionHandle } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { internalMutation } from "../_generated/server.js";
|
|
4
|
+
import { finishGroup } from "../costRecovery/completion.js";
|
|
5
|
+
import { refFields, phase, receipt, safeStatus } from "./schema.js";
|
|
6
|
+
import { read, status } from "./helpers.js";
|
|
7
|
+
export const beginProvider = internalMutation({
|
|
8
|
+
args: { ...refFields, phase },
|
|
9
|
+
returns: v.boolean(),
|
|
10
|
+
handler: async (ctx, args) => {
|
|
11
|
+
const op = await read(ctx, args);
|
|
12
|
+
if (op.status === "completed" || op.status === "canceled") return false;
|
|
13
|
+
if (op.cancelRequested && !op.providerStarted) {
|
|
14
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
15
|
+
status: "canceled",
|
|
16
|
+
});
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
if (args.phase === "execute" && op.providerStarted)
|
|
20
|
+
throw new Error("Recovery provider execution has already started.");
|
|
21
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
22
|
+
status: "running",
|
|
23
|
+
providerStarted: true,
|
|
24
|
+
activePhase: args.phase,
|
|
25
|
+
resumeQueued: false,
|
|
26
|
+
});
|
|
27
|
+
return true;
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
export const saveReceipt = internalMutation({
|
|
31
|
+
args: { ...refFields, result: receipt },
|
|
32
|
+
returns: safeStatus,
|
|
33
|
+
handler: async (ctx, args) => {
|
|
34
|
+
const op = await read(ctx, args);
|
|
35
|
+
if (args.result.status === "ready") {
|
|
36
|
+
if (op.sourceKind === "settlement" && args.result.outcome !== "collect")
|
|
37
|
+
throw new Error("Settlement recovery cannot release after execution.");
|
|
38
|
+
if (op.outcome && op.outcome !== args.result.outcome)
|
|
39
|
+
throw new Error("Recovery receipt outcome cannot change.");
|
|
40
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
41
|
+
outcome: args.result.outcome,
|
|
42
|
+
status: "running",
|
|
43
|
+
});
|
|
44
|
+
} else if (args.result.status !== "canceled")
|
|
45
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
46
|
+
status:
|
|
47
|
+
args.result.status === "unknown" || op.cancelRequested
|
|
48
|
+
? "unknown"
|
|
49
|
+
: "waiting",
|
|
50
|
+
});
|
|
51
|
+
return status((await ctx.db.get("recoveryWorkflowOperations", op._id))!);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
export const finish = internalMutation({
|
|
55
|
+
args: refFields,
|
|
56
|
+
returns: v.boolean(),
|
|
57
|
+
handler: async (ctx, args) => {
|
|
58
|
+
const op = await read(ctx, args);
|
|
59
|
+
if (!op.outcome) throw new Error("Verified recovery receipt is required.");
|
|
60
|
+
return (
|
|
61
|
+
await finishGroup(ctx, { groupKey: op.groupKey, outcome: op.outcome })
|
|
62
|
+
).isDone;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
export const complete = internalMutation({
|
|
66
|
+
args: refFields,
|
|
67
|
+
returns: safeStatus,
|
|
68
|
+
handler: async (ctx, args) => {
|
|
69
|
+
const op = await read(ctx, args);
|
|
70
|
+
if (op.status === "completed") return status(op);
|
|
71
|
+
const group = await ctx.db
|
|
72
|
+
.query("recoveryGroups")
|
|
73
|
+
.withIndex("by_key", (q) => q.eq("groupKey", op.groupKey))
|
|
74
|
+
.unique();
|
|
75
|
+
if (
|
|
76
|
+
!op.outcome ||
|
|
77
|
+
group?.status !== (op.outcome === "collect" ? "collected" : "released")
|
|
78
|
+
)
|
|
79
|
+
throw new Error("Recovery collection is incomplete.");
|
|
80
|
+
const completed = { ...status(op), status: "completed" as const };
|
|
81
|
+
// This callback and terminal status commit atomically; callback must be DB-only.
|
|
82
|
+
if (op.completion) {
|
|
83
|
+
try {
|
|
84
|
+
await ctx.runMutation(
|
|
85
|
+
op.completion as FunctionHandle<"mutation">,
|
|
86
|
+
completed,
|
|
87
|
+
);
|
|
88
|
+
} catch {
|
|
89
|
+
throw new Error("Recovery completion callback failed.");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
93
|
+
status: "completed",
|
|
94
|
+
});
|
|
95
|
+
return completed;
|
|
96
|
+
},
|
|
97
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { financialRetryDecision } from "../../client/financialOutcomes/retry.js";
|
|
2
|
+
import { anyApi } from "convex/server";
|
|
3
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
4
|
+
import { refFields, safeStatus, phase as phaseValidator } from "./schema.js";
|
|
5
|
+
import type { Infer } from "convex/values";
|
|
6
|
+
export const run = executionWorkflow
|
|
7
|
+
.define({
|
|
8
|
+
args: { ...refFields, initialPhase: phaseValidator },
|
|
9
|
+
returns: safeStatus,
|
|
10
|
+
})
|
|
11
|
+
.handler(async (step, input): Promise<Infer<typeof safeStatus>> => {
|
|
12
|
+
const { initialPhase, ...args } = input;
|
|
13
|
+
const saved = await step.runQuery(
|
|
14
|
+
anyApi.recoveryWorkflow.queries.saved,
|
|
15
|
+
args,
|
|
16
|
+
);
|
|
17
|
+
const finish = async (): Promise<Infer<typeof safeStatus>> => {
|
|
18
|
+
for (let batch = 0; batch < 20; batch++) {
|
|
19
|
+
if (
|
|
20
|
+
await step.runMutation(anyApi.recoveryWorkflow.steps.finish, args, {
|
|
21
|
+
name: "finish",
|
|
22
|
+
})
|
|
23
|
+
)
|
|
24
|
+
return step.runMutation(
|
|
25
|
+
anyApi.recoveryWorkflow.steps.complete,
|
|
26
|
+
args,
|
|
27
|
+
{ name: "completion" },
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
throw new Error("Recovery finish batch budget exhausted.");
|
|
31
|
+
};
|
|
32
|
+
if (saved.outcome) return finish();
|
|
33
|
+
let phase: "execute" | "reconcile" = initialPhase;
|
|
34
|
+
let attempts = 0;
|
|
35
|
+
for (let turn = 0; turn < 20; turn++) {
|
|
36
|
+
const result = await step.runAction(
|
|
37
|
+
anyApi.recoveryWorkflow.bridge.invoke,
|
|
38
|
+
{ ...args, phase },
|
|
39
|
+
{ name: `provider-${phase}-${turn}`, retry: false },
|
|
40
|
+
);
|
|
41
|
+
const status = await step.runMutation(
|
|
42
|
+
anyApi.recoveryWorkflow.steps.saveReceipt,
|
|
43
|
+
{ ...args, result },
|
|
44
|
+
{ name: "receipt" },
|
|
45
|
+
);
|
|
46
|
+
if (result.status === "canceled") return status;
|
|
47
|
+
if (result.status === "ready") return finish();
|
|
48
|
+
phase = "reconcile";
|
|
49
|
+
const retry = financialRetryDecision(
|
|
50
|
+
result.status === "not_ready"
|
|
51
|
+
? { status: "not_ready", reason: "provider_pending", retryable: true }
|
|
52
|
+
: {
|
|
53
|
+
status: "unknown",
|
|
54
|
+
reason: "provider_outcome_unknown",
|
|
55
|
+
retryable: false,
|
|
56
|
+
},
|
|
57
|
+
attempts,
|
|
58
|
+
{
|
|
59
|
+
maxRetries: saved.maxAttempts - 1,
|
|
60
|
+
initialDelayMs: saved.retryDelayMs,
|
|
61
|
+
maxDelayMs: saved.retryDelayMs,
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
if (!status.cancelRequested && retry.status === "retry") {
|
|
65
|
+
attempts = retry.nextAttempt;
|
|
66
|
+
await step.sleep(retry.delayMs);
|
|
67
|
+
} else {
|
|
68
|
+
await step.awaitEvent({ name: "reconcile" });
|
|
69
|
+
attempts = 0;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
throw new Error("Recovery reconciliation budget exhausted.");
|
|
73
|
+
});
|
package/src/component/schema.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
operationsTable as recoveryWorkflowOperations,
|
|
3
|
+
requestsTable as recoveryWorkflowRequests,
|
|
4
|
+
} from "./recoveryWorkflow/schema.js";
|
|
1
5
|
import {
|
|
2
6
|
preparationsTable as balanceFundingPreparations,
|
|
3
7
|
operationsTable as balanceFundingOperations,
|
|
@@ -83,6 +87,8 @@ import { pricesTable, productsTable } from "./products/schema";
|
|
|
83
87
|
import { webhookEventsTable } from "./webhooks/schema";
|
|
84
88
|
|
|
85
89
|
export default defineSchema({
|
|
90
|
+
recoveryWorkflowOperations,
|
|
91
|
+
recoveryWorkflowRequests,
|
|
86
92
|
balanceFundingPreparations,
|
|
87
93
|
balanceFundingOperations,
|
|
88
94
|
balanceFundingRequests,
|