@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,66 @@
|
|
|
1
|
+
import { anyApi, type FunctionHandle } from "convex/server";
|
|
2
|
+
import { internalAction } from "../_generated/server.js";
|
|
3
|
+
import { refFields, phase, receipt } from "./schema.js";
|
|
4
|
+
import type { Infer } from "convex/values";
|
|
5
|
+
import { same } from "./helpers.js";
|
|
6
|
+
const unknown = {
|
|
7
|
+
status: "unknown",
|
|
8
|
+
reason: "provider_outcome_unknown",
|
|
9
|
+
} as const;
|
|
10
|
+
export const invoke = internalAction({
|
|
11
|
+
args: { ...refFields, phase },
|
|
12
|
+
returns: receipt,
|
|
13
|
+
handler: async (ctx, args): Promise<Infer<typeof receipt>> => {
|
|
14
|
+
try {
|
|
15
|
+
const { phase: _phase, ...ref } = args;
|
|
16
|
+
const op = await ctx.runQuery(anyApi.recoveryWorkflow.queries.saved, ref);
|
|
17
|
+
if (
|
|
18
|
+
!(await ctx.runMutation(
|
|
19
|
+
anyApi.recoveryWorkflow.steps.beginProvider,
|
|
20
|
+
args,
|
|
21
|
+
))
|
|
22
|
+
)
|
|
23
|
+
return { status: "canceled" };
|
|
24
|
+
const request = {
|
|
25
|
+
...args,
|
|
26
|
+
providerApiVersion: op.providerApiVersion,
|
|
27
|
+
definitionVersion: op.definitionVersion,
|
|
28
|
+
};
|
|
29
|
+
const result: unknown = await ctx.runAction(
|
|
30
|
+
op.provider as FunctionHandle<"action">,
|
|
31
|
+
request,
|
|
32
|
+
);
|
|
33
|
+
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
34
|
+
return unknown;
|
|
35
|
+
const r = result as Record<string, unknown>;
|
|
36
|
+
const allowed = [
|
|
37
|
+
...Object.keys(request),
|
|
38
|
+
"sourceId",
|
|
39
|
+
"status",
|
|
40
|
+
"outcome",
|
|
41
|
+
"reason",
|
|
42
|
+
];
|
|
43
|
+
if (
|
|
44
|
+
Object.keys(r).some((k) => !allowed.includes(k)) ||
|
|
45
|
+
!same(
|
|
46
|
+
Object.fromEntries(Object.keys(request).map((k) => [k, r[k]])),
|
|
47
|
+
request,
|
|
48
|
+
) ||
|
|
49
|
+
r.sourceId !== op.sourceId
|
|
50
|
+
)
|
|
51
|
+
return unknown;
|
|
52
|
+
if (
|
|
53
|
+
r.status === "ready" &&
|
|
54
|
+
(r.outcome === "collect" || r.outcome === "release") &&
|
|
55
|
+
(op.sourceKind !== "settlement" || r.outcome === "collect")
|
|
56
|
+
)
|
|
57
|
+
return { status: "ready", outcome: r.outcome };
|
|
58
|
+
if (r.status === "not_ready" && r.reason === "provider_pending")
|
|
59
|
+
return { status: "not_ready", reason: "provider_pending" };
|
|
60
|
+
return unknown;
|
|
61
|
+
} catch {
|
|
62
|
+
// Never allow an SDK exception, response object, or host payload into Workflow history.
|
|
63
|
+
return unknown;
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
2
|
+
import type { QueryCtx, MutationCtx } from "../_generated/server.js";
|
|
3
|
+
import type { Infer } from "convex/values";
|
|
4
|
+
import { v } from "convex/values";
|
|
5
|
+
import { refFields } from "./schema.js";
|
|
6
|
+
const _ref = v.object(refFields);
|
|
7
|
+
export type OperationRef = Infer<typeof _ref>;
|
|
8
|
+
export const identityConflict = {
|
|
9
|
+
status: "conflict",
|
|
10
|
+
reason: "identity_changed",
|
|
11
|
+
retryable: false,
|
|
12
|
+
} as const;
|
|
13
|
+
export function reference(op: Doc<"recoveryWorkflowOperations">): OperationRef {
|
|
14
|
+
return {
|
|
15
|
+
accountId: op.accountId,
|
|
16
|
+
scopeKey: op.scopeKey,
|
|
17
|
+
currency: op.currency,
|
|
18
|
+
groupKey: op.groupKey,
|
|
19
|
+
operationId: op._id,
|
|
20
|
+
version: op.version,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function status(op: Doc<"recoveryWorkflowOperations">) {
|
|
24
|
+
return {
|
|
25
|
+
...reference(op),
|
|
26
|
+
sourceKind: op.sourceKind,
|
|
27
|
+
sourceId: op.sourceId,
|
|
28
|
+
status: op.status,
|
|
29
|
+
cancelRequested: op.cancelRequested,
|
|
30
|
+
...(op.outcome ? { outcome: op.outcome } : {}),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export async function read(ctx: QueryCtx | MutationCtx, args: OperationRef) {
|
|
34
|
+
const op = await ctx.db.get("recoveryWorkflowOperations", args.operationId);
|
|
35
|
+
if (
|
|
36
|
+
!op ||
|
|
37
|
+
Object.entries(reference(op)).some(
|
|
38
|
+
([k, value]) => value !== args[k as keyof OperationRef],
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
throw new Error("Recovery operation identity does not match.");
|
|
42
|
+
return op;
|
|
43
|
+
}
|
|
44
|
+
/** Stable object ordering without admitting arbitrary durable caller payloads. */
|
|
45
|
+
export function same(a: unknown, b: unknown): boolean {
|
|
46
|
+
const canonical = (x: unknown): unknown =>
|
|
47
|
+
Array.isArray(x)
|
|
48
|
+
? x.map(canonical)
|
|
49
|
+
: x && typeof x === "object"
|
|
50
|
+
? Object.fromEntries(
|
|
51
|
+
Object.entries(x)
|
|
52
|
+
.filter(([, v]) => v !== undefined)
|
|
53
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
54
|
+
.map(([k, v]) => [k, canonical(v)]),
|
|
55
|
+
)
|
|
56
|
+
: x;
|
|
57
|
+
return JSON.stringify(canonical(a)) === JSON.stringify(canonical(b));
|
|
58
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
+
import {
|
|
4
|
+
anyApi,
|
|
5
|
+
componentsGeneric,
|
|
6
|
+
createFunctionHandle,
|
|
7
|
+
defineSchema,
|
|
8
|
+
internalActionGeneric,
|
|
9
|
+
internalMutationGeneric,
|
|
10
|
+
internalQueryGeneric,
|
|
11
|
+
} from "convex/server";
|
|
12
|
+
import { beforeEach, afterEach, expect, it, vi } from "vitest";
|
|
13
|
+
import { v } from "convex/values";
|
|
14
|
+
import { modules, schema } from "../../test.js";
|
|
15
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
16
|
+
const nested = componentsGeneric();
|
|
17
|
+
const scope = {
|
|
18
|
+
accountId: "acct_a",
|
|
19
|
+
scopeKey: "seller",
|
|
20
|
+
currency: "usd",
|
|
21
|
+
groupKey: "journal",
|
|
22
|
+
};
|
|
23
|
+
const host = {
|
|
24
|
+
provider: internalActionGeneric({
|
|
25
|
+
args: {
|
|
26
|
+
...Object.fromEntries(Object.keys(scope).map((k) => [k, v.string()])),
|
|
27
|
+
operationId: v.string(),
|
|
28
|
+
version: v.number(),
|
|
29
|
+
providerApiVersion: v.string(),
|
|
30
|
+
definitionVersion: v.number(),
|
|
31
|
+
phase: v.string(),
|
|
32
|
+
},
|
|
33
|
+
returns: v.any(),
|
|
34
|
+
handler: async () => {
|
|
35
|
+
throw new Error("sk_test_PRIVATE accidental provider body");
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
start: internalMutationGeneric({
|
|
39
|
+
args: {},
|
|
40
|
+
returns: v.any(),
|
|
41
|
+
handler: async (ctx) =>
|
|
42
|
+
ctx.runMutation(nested.betterStripe.recoveryWorkflow.mutations.start, {
|
|
43
|
+
...scope,
|
|
44
|
+
requestKey: "journal",
|
|
45
|
+
provider: await createFunctionHandle(anyApi.host.provider),
|
|
46
|
+
providerApiVersion: "2026-09-30.endive",
|
|
47
|
+
definitionVersion: 1,
|
|
48
|
+
releaseUncollectible: false,
|
|
49
|
+
maxAttempts: 3,
|
|
50
|
+
retryDelayMs: 1,
|
|
51
|
+
}),
|
|
52
|
+
}),
|
|
53
|
+
};
|
|
54
|
+
beforeEach(() => vi.useFakeTimers());
|
|
55
|
+
afterEach(() => vi.useRealTimers());
|
|
56
|
+
it("journals only a closed sanitized outcome from a throwing host action", async () => {
|
|
57
|
+
const t = convexTest(defineSchema({}), {
|
|
58
|
+
"./_generated/api.ts": async () => ({}),
|
|
59
|
+
"./host.ts": async () => host,
|
|
60
|
+
});
|
|
61
|
+
t.registerComponent("betterStripe", schema, {
|
|
62
|
+
...modules,
|
|
63
|
+
"./component/fixture.ts": async () => ({
|
|
64
|
+
history: internalQueryGeneric({
|
|
65
|
+
args: {},
|
|
66
|
+
returns: v.any(),
|
|
67
|
+
handler: async (ctx) => {
|
|
68
|
+
const op = (await ctx.db
|
|
69
|
+
.query("recoveryWorkflowOperations")
|
|
70
|
+
.first())!;
|
|
71
|
+
return executionWorkflow.listSteps(ctx, op.workflowId, {
|
|
72
|
+
paginationOpts: { cursor: null, numItems: 100 },
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
78
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
79
|
+
await t.run((ctx) =>
|
|
80
|
+
ctx.runMutation(nested.betterStripe.costRecovery.preparation.prepare, {
|
|
81
|
+
...scope,
|
|
82
|
+
sourceKind: "invoice",
|
|
83
|
+
sourceId: "in_a",
|
|
84
|
+
maxDebit: 100,
|
|
85
|
+
maxCredit: 0,
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
await t.mutation(anyApi.host.start, {});
|
|
89
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
90
|
+
const history = await t.query(nested.betterStripe.fixture.history, {});
|
|
91
|
+
const text = JSON.stringify(history);
|
|
92
|
+
expect(text).not.toContain("sk_test_PRIVATE");
|
|
93
|
+
expect(text).not.toContain("accidental provider body");
|
|
94
|
+
expect(
|
|
95
|
+
history.page.find((step: { name: string }) =>
|
|
96
|
+
step.name.startsWith("provider-"),
|
|
97
|
+
),
|
|
98
|
+
).toMatchObject({
|
|
99
|
+
runResult: {
|
|
100
|
+
kind: "success",
|
|
101
|
+
returnValue: { status: "unknown", reason: "provider_outcome_unknown" },
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { allocationFields } from "../settlement/schema.js";
|
|
2
|
+
import { anyApi, type FunctionReference } from "convex/server";
|
|
3
|
+
import type { WorkflowId } from "@convex-dev/workflow";
|
|
4
|
+
import { mutation } from "../_generated/server.js";
|
|
5
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
6
|
+
import { validateScope } from "../costRecovery/helpers.js";
|
|
7
|
+
import {
|
|
8
|
+
bindingFields,
|
|
9
|
+
startFields,
|
|
10
|
+
startResult,
|
|
11
|
+
refFields,
|
|
12
|
+
safeStatus,
|
|
13
|
+
} from "./schema.js";
|
|
14
|
+
import { identityConflict, read, reference, same, status } from "./helpers.js";
|
|
15
|
+
const workflow = anyApi.recoveryWorkflow.workflow.run as FunctionReference<
|
|
16
|
+
"mutation",
|
|
17
|
+
"internal"
|
|
18
|
+
>;
|
|
19
|
+
export const start = mutation({
|
|
20
|
+
args: startFields,
|
|
21
|
+
returns: startResult,
|
|
22
|
+
handler: async (ctx, args) => {
|
|
23
|
+
validateScope(args);
|
|
24
|
+
if (
|
|
25
|
+
!args.requestKey ||
|
|
26
|
+
!args.provider ||
|
|
27
|
+
!/^\d{4}-\d{2}-\d{2}\.[a-z][a-z0-9_.-]*$/.test(args.providerApiVersion) ||
|
|
28
|
+
args.definitionVersion !== 1 ||
|
|
29
|
+
!Number.isSafeInteger(args.maxAttempts) ||
|
|
30
|
+
args.maxAttempts < 1 ||
|
|
31
|
+
args.maxAttempts > 20 ||
|
|
32
|
+
!Number.isSafeInteger(args.retryDelayMs) ||
|
|
33
|
+
args.retryDelayMs < 1 ||
|
|
34
|
+
args.retryDelayMs > 86400000
|
|
35
|
+
)
|
|
36
|
+
throw new Error("Invalid recovery workflow policy.");
|
|
37
|
+
const { requestKey, ...binding } = args;
|
|
38
|
+
const request = await ctx.db
|
|
39
|
+
.query("recoveryWorkflowRequests")
|
|
40
|
+
.withIndex("by_key", (q) => q.eq("requestKey", requestKey))
|
|
41
|
+
.unique();
|
|
42
|
+
if (request) {
|
|
43
|
+
if (!same(request.binding, binding)) return identityConflict;
|
|
44
|
+
return status(
|
|
45
|
+
(await ctx.db.get("recoveryWorkflowOperations", request.operationId))!,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const group = await ctx.db
|
|
49
|
+
.query("recoveryGroups")
|
|
50
|
+
.withIndex("by_key", (q) => q.eq("groupKey", args.groupKey))
|
|
51
|
+
.unique();
|
|
52
|
+
if (
|
|
53
|
+
!group ||
|
|
54
|
+
group.accountId !== args.accountId ||
|
|
55
|
+
group.scopeKey !== args.scopeKey ||
|
|
56
|
+
group.currency !== args.currency
|
|
57
|
+
)
|
|
58
|
+
return identityConflict;
|
|
59
|
+
const existing = await ctx.db
|
|
60
|
+
.query("recoveryWorkflowOperations")
|
|
61
|
+
.withIndex("by_group", (q) => q.eq("groupKey", args.groupKey))
|
|
62
|
+
.unique();
|
|
63
|
+
if (existing) {
|
|
64
|
+
const saved = Object.fromEntries(
|
|
65
|
+
Object.keys(bindingFields).map((k) => [
|
|
66
|
+
k,
|
|
67
|
+
existing[k as keyof typeof existing],
|
|
68
|
+
]),
|
|
69
|
+
);
|
|
70
|
+
if (!same(saved, binding)) return identityConflict;
|
|
71
|
+
await ctx.db.insert("recoveryWorkflowRequests", {
|
|
72
|
+
requestKey,
|
|
73
|
+
binding,
|
|
74
|
+
operationId: existing._id,
|
|
75
|
+
});
|
|
76
|
+
return status(existing);
|
|
77
|
+
}
|
|
78
|
+
if (group.status !== "sealed")
|
|
79
|
+
return {
|
|
80
|
+
status: "not_ready",
|
|
81
|
+
reason: "history_incomplete",
|
|
82
|
+
retryable: true,
|
|
83
|
+
} as const;
|
|
84
|
+
if (group.sourceKind === "settlement") {
|
|
85
|
+
const a = args.allocation;
|
|
86
|
+
if (
|
|
87
|
+
!a ||
|
|
88
|
+
a.sourceChargeId !== group.sourceId ||
|
|
89
|
+
a.currency !== group.currency ||
|
|
90
|
+
a.gross !== group.sourceGross ||
|
|
91
|
+
a.processingFee !== group.sourceProcessingFee ||
|
|
92
|
+
(a.adjustmentAmount ?? 0) !== group.amount
|
|
93
|
+
)
|
|
94
|
+
return identityConflict;
|
|
95
|
+
const priorOperation = await ctx.db
|
|
96
|
+
.query("recoveryWorkflowOperations")
|
|
97
|
+
.withIndex("by_source", (q) =>
|
|
98
|
+
q.eq("sourceKind", "settlement").eq("sourceId", group.sourceId),
|
|
99
|
+
)
|
|
100
|
+
.first();
|
|
101
|
+
if (priorOperation) return identityConflict;
|
|
102
|
+
const prior = await ctx.db
|
|
103
|
+
.query("settlementAllocations")
|
|
104
|
+
.withIndex("by_source_charge", (q) =>
|
|
105
|
+
q.eq("sourceChargeId", a.sourceChargeId),
|
|
106
|
+
)
|
|
107
|
+
.first();
|
|
108
|
+
if (
|
|
109
|
+
prior &&
|
|
110
|
+
!same(
|
|
111
|
+
Object.fromEntries(
|
|
112
|
+
Object.keys(allocationFields).map((k) => [
|
|
113
|
+
k,
|
|
114
|
+
prior[k as keyof typeof prior],
|
|
115
|
+
]),
|
|
116
|
+
),
|
|
117
|
+
a,
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
return identityConflict;
|
|
121
|
+
await ctx.runMutation(
|
|
122
|
+
anyApi.settlement.mutations.reserveSettlementAllocation,
|
|
123
|
+
a,
|
|
124
|
+
);
|
|
125
|
+
} else if (args.allocation) return identityConflict;
|
|
126
|
+
const operationId = await ctx.db.insert("recoveryWorkflowOperations", {
|
|
127
|
+
...binding,
|
|
128
|
+
sourceKind: group.sourceKind,
|
|
129
|
+
sourceId: group.sourceId,
|
|
130
|
+
version: 1,
|
|
131
|
+
status: "queued",
|
|
132
|
+
cancelRequested: false,
|
|
133
|
+
providerStarted: false,
|
|
134
|
+
resumeQueued: false,
|
|
135
|
+
});
|
|
136
|
+
const op = (await ctx.db.get("recoveryWorkflowOperations", operationId))!;
|
|
137
|
+
await ctx.db.insert("recoveryWorkflowRequests", {
|
|
138
|
+
requestKey,
|
|
139
|
+
binding,
|
|
140
|
+
operationId,
|
|
141
|
+
});
|
|
142
|
+
const workflowId = await executionWorkflow.start(
|
|
143
|
+
ctx,
|
|
144
|
+
workflow,
|
|
145
|
+
{ ...reference(op), initialPhase: "execute" },
|
|
146
|
+
{ startAsync: true },
|
|
147
|
+
);
|
|
148
|
+
await ctx.db.patch("recoveryWorkflowOperations", operationId, {
|
|
149
|
+
workflowId,
|
|
150
|
+
});
|
|
151
|
+
return status(op);
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
export const requestCancel = mutation({
|
|
155
|
+
args: refFields,
|
|
156
|
+
returns: safeStatus,
|
|
157
|
+
handler: async (ctx, args) => {
|
|
158
|
+
const op = await read(ctx, args);
|
|
159
|
+
if (op.status === "completed" || op.status === "canceled")
|
|
160
|
+
return status(op);
|
|
161
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
162
|
+
cancelRequested: true,
|
|
163
|
+
});
|
|
164
|
+
return status((await ctx.db.get("recoveryWorkflowOperations", op._id))!);
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
export const resume = mutation({
|
|
168
|
+
args: refFields,
|
|
169
|
+
returns: safeStatus,
|
|
170
|
+
handler: async (ctx, args) => {
|
|
171
|
+
const op = await read(ctx, args);
|
|
172
|
+
const workflowStatus = await executionWorkflow.status(
|
|
173
|
+
ctx,
|
|
174
|
+
op.workflowId as WorkflowId,
|
|
175
|
+
);
|
|
176
|
+
if (workflowStatus.type === "failed") {
|
|
177
|
+
const last = (
|
|
178
|
+
await executionWorkflow.listSteps(ctx, op.workflowId as WorkflowId, {
|
|
179
|
+
order: "desc",
|
|
180
|
+
paginationOpts: { cursor: null, numItems: 1 },
|
|
181
|
+
})
|
|
182
|
+
).page[0];
|
|
183
|
+
if (
|
|
184
|
+
last &&
|
|
185
|
+
["receipt", "finish", "completion"].includes(last.name) &&
|
|
186
|
+
last.runResult?.kind === "failed"
|
|
187
|
+
)
|
|
188
|
+
await executionWorkflow.restart(ctx, op.workflowId as WorkflowId, {
|
|
189
|
+
from: last.stepNumber,
|
|
190
|
+
startAsync: true,
|
|
191
|
+
});
|
|
192
|
+
else {
|
|
193
|
+
// A bounded/exhausted or unjournaled provider segment must never
|
|
194
|
+
// replay an execute phase. Keep the same operation and money keys.
|
|
195
|
+
const workflowId = await executionWorkflow.start(
|
|
196
|
+
ctx,
|
|
197
|
+
workflow,
|
|
198
|
+
{ ...reference(op), initialPhase: "reconcile" },
|
|
199
|
+
{ startAsync: true },
|
|
200
|
+
);
|
|
201
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
202
|
+
workflowId,
|
|
203
|
+
resumeQueued: false,
|
|
204
|
+
status: op.outcome ? "running" : "queued",
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return status((await ctx.db.get("recoveryWorkflowOperations", op._id))!);
|
|
208
|
+
}
|
|
209
|
+
if (
|
|
210
|
+
(op.status === "unknown" || op.status === "waiting") &&
|
|
211
|
+
!op.resumeQueued
|
|
212
|
+
) {
|
|
213
|
+
await executionWorkflow.sendEvent(ctx, {
|
|
214
|
+
workflowId: op.workflowId as WorkflowId,
|
|
215
|
+
name: "reconcile",
|
|
216
|
+
value: null,
|
|
217
|
+
});
|
|
218
|
+
await ctx.db.patch("recoveryWorkflowOperations", op._id, {
|
|
219
|
+
resumeQueued: true,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return status((await ctx.db.get("recoveryWorkflowOperations", op._id))!);
|
|
223
|
+
},
|
|
224
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { query, internalQuery } from "../_generated/server.js";
|
|
2
|
+
import { refFields, safeStatus, operationDoc } from "./schema.js";
|
|
3
|
+
import { read, status } from "./helpers.js";
|
|
4
|
+
export const get = query({
|
|
5
|
+
args: refFields,
|
|
6
|
+
returns: safeStatus,
|
|
7
|
+
handler: async (ctx, args) => status(await read(ctx, args)),
|
|
8
|
+
});
|
|
9
|
+
/** Authorized host action reads the pinned public inputs; credentials are never stored here. */
|
|
10
|
+
export const snapshot = query({
|
|
11
|
+
args: refFields,
|
|
12
|
+
returns: operationDoc,
|
|
13
|
+
handler: read,
|
|
14
|
+
});
|
|
15
|
+
export const saved = internalQuery({
|
|
16
|
+
args: refFields,
|
|
17
|
+
returns: operationDoc,
|
|
18
|
+
handler: read,
|
|
19
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { scopeFields } from "../costRecovery/schema.js";
|
|
4
|
+
import { allocationFields } from "../settlement/schema.js";
|
|
5
|
+
export const bindingFields = {
|
|
6
|
+
...scopeFields,
|
|
7
|
+
groupKey: v.string(),
|
|
8
|
+
provider: v.string(),
|
|
9
|
+
completion: v.optional(v.string()),
|
|
10
|
+
providerApiVersion: v.string(),
|
|
11
|
+
definitionVersion: v.number(),
|
|
12
|
+
releaseUncollectible: v.boolean(),
|
|
13
|
+
maxAttempts: v.number(),
|
|
14
|
+
retryDelayMs: v.number(),
|
|
15
|
+
allocation: v.optional(v.object(allocationFields)),
|
|
16
|
+
};
|
|
17
|
+
export const startFields = { ...bindingFields, requestKey: v.string() };
|
|
18
|
+
export const refFields = {
|
|
19
|
+
...scopeFields,
|
|
20
|
+
groupKey: v.string(),
|
|
21
|
+
operationId: v.id("recoveryWorkflowOperations"),
|
|
22
|
+
version: v.number(),
|
|
23
|
+
};
|
|
24
|
+
export const phase = v.union(v.literal("execute"), v.literal("reconcile"));
|
|
25
|
+
export const operationFields = {
|
|
26
|
+
...bindingFields,
|
|
27
|
+
version: v.number(),
|
|
28
|
+
sourceKind: v.union(v.literal("settlement"), v.literal("invoice")),
|
|
29
|
+
sourceId: v.string(),
|
|
30
|
+
workflowId: v.optional(v.string()),
|
|
31
|
+
status: v.union(
|
|
32
|
+
v.literal("queued"),
|
|
33
|
+
v.literal("running"),
|
|
34
|
+
v.literal("waiting"),
|
|
35
|
+
v.literal("unknown"),
|
|
36
|
+
v.literal("completed"),
|
|
37
|
+
v.literal("canceled"),
|
|
38
|
+
),
|
|
39
|
+
cancelRequested: v.boolean(),
|
|
40
|
+
providerStarted: v.boolean(),
|
|
41
|
+
activePhase: v.optional(phase),
|
|
42
|
+
resumeQueued: v.boolean(),
|
|
43
|
+
outcome: v.optional(v.union(v.literal("collect"), v.literal("release"))),
|
|
44
|
+
};
|
|
45
|
+
export const operationsTable = defineTable(operationFields)
|
|
46
|
+
.index("by_group", ["groupKey"])
|
|
47
|
+
.index("by_source", ["sourceKind", "sourceId"]);
|
|
48
|
+
export const requestsTable = defineTable({
|
|
49
|
+
requestKey: v.string(),
|
|
50
|
+
binding: v.object(bindingFields),
|
|
51
|
+
operationId: v.id("recoveryWorkflowOperations"),
|
|
52
|
+
}).index("by_key", ["requestKey"]);
|
|
53
|
+
export const operationDoc = v.object({
|
|
54
|
+
...operationFields,
|
|
55
|
+
_id: v.id("recoveryWorkflowOperations"),
|
|
56
|
+
_creationTime: v.number(),
|
|
57
|
+
});
|
|
58
|
+
export const statusFields = {
|
|
59
|
+
...refFields,
|
|
60
|
+
sourceKind: v.union(v.literal("settlement"), v.literal("invoice")),
|
|
61
|
+
sourceId: v.string(),
|
|
62
|
+
status: operationFields.status,
|
|
63
|
+
cancelRequested: v.boolean(),
|
|
64
|
+
outcome: operationFields.outcome,
|
|
65
|
+
};
|
|
66
|
+
export const safeStatus = v.object(statusFields);
|
|
67
|
+
export const conflict = v.object({
|
|
68
|
+
status: v.literal("conflict"),
|
|
69
|
+
reason: v.literal("identity_changed"),
|
|
70
|
+
retryable: v.literal(false),
|
|
71
|
+
});
|
|
72
|
+
export const unavailable = v.object({
|
|
73
|
+
status: v.literal("not_ready"),
|
|
74
|
+
reason: v.literal("history_incomplete"),
|
|
75
|
+
retryable: v.literal(true),
|
|
76
|
+
});
|
|
77
|
+
export const startResult = v.union(safeStatus, conflict, unavailable);
|
|
78
|
+
export const receipt = v.union(
|
|
79
|
+
v.object({
|
|
80
|
+
status: v.literal("ready"),
|
|
81
|
+
outcome: v.union(v.literal("collect"), v.literal("release")),
|
|
82
|
+
}),
|
|
83
|
+
v.object({
|
|
84
|
+
status: v.literal("not_ready"),
|
|
85
|
+
reason: v.literal("provider_pending"),
|
|
86
|
+
}),
|
|
87
|
+
v.object({
|
|
88
|
+
status: v.literal("unknown"),
|
|
89
|
+
reason: v.literal("provider_outcome_unknown"),
|
|
90
|
+
}),
|
|
91
|
+
v.object({ status: v.literal("canceled") }),
|
|
92
|
+
);
|