@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
|
@@ -6420,6 +6420,228 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
6420
6420
|
>;
|
|
6421
6421
|
};
|
|
6422
6422
|
};
|
|
6423
|
+
recoveryWorkflow: {
|
|
6424
|
+
mutations: {
|
|
6425
|
+
requestCancel: FunctionReference<
|
|
6426
|
+
"mutation",
|
|
6427
|
+
"internal",
|
|
6428
|
+
{
|
|
6429
|
+
accountId: string;
|
|
6430
|
+
currency: string;
|
|
6431
|
+
groupKey: string;
|
|
6432
|
+
operationId: string;
|
|
6433
|
+
scopeKey: string;
|
|
6434
|
+
version: number;
|
|
6435
|
+
},
|
|
6436
|
+
{
|
|
6437
|
+
accountId: string;
|
|
6438
|
+
cancelRequested: boolean;
|
|
6439
|
+
currency: string;
|
|
6440
|
+
groupKey: string;
|
|
6441
|
+
operationId: string;
|
|
6442
|
+
outcome?: "collect" | "release";
|
|
6443
|
+
scopeKey: string;
|
|
6444
|
+
sourceId: string;
|
|
6445
|
+
sourceKind: "settlement" | "invoice";
|
|
6446
|
+
status:
|
|
6447
|
+
| "queued"
|
|
6448
|
+
| "running"
|
|
6449
|
+
| "waiting"
|
|
6450
|
+
| "unknown"
|
|
6451
|
+
| "completed"
|
|
6452
|
+
| "canceled";
|
|
6453
|
+
version: number;
|
|
6454
|
+
},
|
|
6455
|
+
Name
|
|
6456
|
+
>;
|
|
6457
|
+
resume: FunctionReference<
|
|
6458
|
+
"mutation",
|
|
6459
|
+
"internal",
|
|
6460
|
+
{
|
|
6461
|
+
accountId: string;
|
|
6462
|
+
currency: string;
|
|
6463
|
+
groupKey: string;
|
|
6464
|
+
operationId: string;
|
|
6465
|
+
scopeKey: string;
|
|
6466
|
+
version: number;
|
|
6467
|
+
},
|
|
6468
|
+
{
|
|
6469
|
+
accountId: string;
|
|
6470
|
+
cancelRequested: boolean;
|
|
6471
|
+
currency: string;
|
|
6472
|
+
groupKey: string;
|
|
6473
|
+
operationId: string;
|
|
6474
|
+
outcome?: "collect" | "release";
|
|
6475
|
+
scopeKey: string;
|
|
6476
|
+
sourceId: string;
|
|
6477
|
+
sourceKind: "settlement" | "invoice";
|
|
6478
|
+
status:
|
|
6479
|
+
| "queued"
|
|
6480
|
+
| "running"
|
|
6481
|
+
| "waiting"
|
|
6482
|
+
| "unknown"
|
|
6483
|
+
| "completed"
|
|
6484
|
+
| "canceled";
|
|
6485
|
+
version: number;
|
|
6486
|
+
},
|
|
6487
|
+
Name
|
|
6488
|
+
>;
|
|
6489
|
+
start: FunctionReference<
|
|
6490
|
+
"mutation",
|
|
6491
|
+
"internal",
|
|
6492
|
+
{
|
|
6493
|
+
accountId: string;
|
|
6494
|
+
allocation?: {
|
|
6495
|
+
adjustmentAmount?: number;
|
|
6496
|
+
currency: string;
|
|
6497
|
+
gross: number;
|
|
6498
|
+
paymentId?: string;
|
|
6499
|
+
paymentIntentId?: string;
|
|
6500
|
+
platformNet: number;
|
|
6501
|
+
processingFee: number;
|
|
6502
|
+
sourceChargeId: string;
|
|
6503
|
+
sourceInvoiceId?: string;
|
|
6504
|
+
transfers: Array<{
|
|
6505
|
+
amount: number;
|
|
6506
|
+
destinationAccountId: string;
|
|
6507
|
+
role: "store" | "affiliate" | "other";
|
|
6508
|
+
}>;
|
|
6509
|
+
};
|
|
6510
|
+
completion?: string;
|
|
6511
|
+
currency: string;
|
|
6512
|
+
definitionVersion: number;
|
|
6513
|
+
groupKey: string;
|
|
6514
|
+
maxAttempts: number;
|
|
6515
|
+
provider: string;
|
|
6516
|
+
providerApiVersion: string;
|
|
6517
|
+
releaseUncollectible: boolean;
|
|
6518
|
+
requestKey: string;
|
|
6519
|
+
retryDelayMs: number;
|
|
6520
|
+
scopeKey: string;
|
|
6521
|
+
},
|
|
6522
|
+
| {
|
|
6523
|
+
accountId: string;
|
|
6524
|
+
cancelRequested: boolean;
|
|
6525
|
+
currency: string;
|
|
6526
|
+
groupKey: string;
|
|
6527
|
+
operationId: string;
|
|
6528
|
+
outcome?: "collect" | "release";
|
|
6529
|
+
scopeKey: string;
|
|
6530
|
+
sourceId: string;
|
|
6531
|
+
sourceKind: "settlement" | "invoice";
|
|
6532
|
+
status:
|
|
6533
|
+
| "queued"
|
|
6534
|
+
| "running"
|
|
6535
|
+
| "waiting"
|
|
6536
|
+
| "unknown"
|
|
6537
|
+
| "completed"
|
|
6538
|
+
| "canceled";
|
|
6539
|
+
version: number;
|
|
6540
|
+
}
|
|
6541
|
+
| { reason: "identity_changed"; retryable: false; status: "conflict" }
|
|
6542
|
+
| {
|
|
6543
|
+
reason: "history_incomplete";
|
|
6544
|
+
retryable: true;
|
|
6545
|
+
status: "not_ready";
|
|
6546
|
+
},
|
|
6547
|
+
Name
|
|
6548
|
+
>;
|
|
6549
|
+
};
|
|
6550
|
+
queries: {
|
|
6551
|
+
get: FunctionReference<
|
|
6552
|
+
"query",
|
|
6553
|
+
"internal",
|
|
6554
|
+
{
|
|
6555
|
+
accountId: string;
|
|
6556
|
+
currency: string;
|
|
6557
|
+
groupKey: string;
|
|
6558
|
+
operationId: string;
|
|
6559
|
+
scopeKey: string;
|
|
6560
|
+
version: number;
|
|
6561
|
+
},
|
|
6562
|
+
{
|
|
6563
|
+
accountId: string;
|
|
6564
|
+
cancelRequested: boolean;
|
|
6565
|
+
currency: string;
|
|
6566
|
+
groupKey: string;
|
|
6567
|
+
operationId: string;
|
|
6568
|
+
outcome?: "collect" | "release";
|
|
6569
|
+
scopeKey: string;
|
|
6570
|
+
sourceId: string;
|
|
6571
|
+
sourceKind: "settlement" | "invoice";
|
|
6572
|
+
status:
|
|
6573
|
+
| "queued"
|
|
6574
|
+
| "running"
|
|
6575
|
+
| "waiting"
|
|
6576
|
+
| "unknown"
|
|
6577
|
+
| "completed"
|
|
6578
|
+
| "canceled";
|
|
6579
|
+
version: number;
|
|
6580
|
+
},
|
|
6581
|
+
Name
|
|
6582
|
+
>;
|
|
6583
|
+
snapshot: FunctionReference<
|
|
6584
|
+
"query",
|
|
6585
|
+
"internal",
|
|
6586
|
+
{
|
|
6587
|
+
accountId: string;
|
|
6588
|
+
currency: string;
|
|
6589
|
+
groupKey: string;
|
|
6590
|
+
operationId: string;
|
|
6591
|
+
scopeKey: string;
|
|
6592
|
+
version: number;
|
|
6593
|
+
},
|
|
6594
|
+
{
|
|
6595
|
+
_creationTime: number;
|
|
6596
|
+
_id: string;
|
|
6597
|
+
accountId: string;
|
|
6598
|
+
activePhase?: "execute" | "reconcile";
|
|
6599
|
+
allocation?: {
|
|
6600
|
+
adjustmentAmount?: number;
|
|
6601
|
+
currency: string;
|
|
6602
|
+
gross: number;
|
|
6603
|
+
paymentId?: string;
|
|
6604
|
+
paymentIntentId?: string;
|
|
6605
|
+
platformNet: number;
|
|
6606
|
+
processingFee: number;
|
|
6607
|
+
sourceChargeId: string;
|
|
6608
|
+
sourceInvoiceId?: string;
|
|
6609
|
+
transfers: Array<{
|
|
6610
|
+
amount: number;
|
|
6611
|
+
destinationAccountId: string;
|
|
6612
|
+
role: "store" | "affiliate" | "other";
|
|
6613
|
+
}>;
|
|
6614
|
+
};
|
|
6615
|
+
cancelRequested: boolean;
|
|
6616
|
+
completion?: string;
|
|
6617
|
+
currency: string;
|
|
6618
|
+
definitionVersion: number;
|
|
6619
|
+
groupKey: string;
|
|
6620
|
+
maxAttempts: number;
|
|
6621
|
+
outcome?: "collect" | "release";
|
|
6622
|
+
provider: string;
|
|
6623
|
+
providerApiVersion: string;
|
|
6624
|
+
providerStarted: boolean;
|
|
6625
|
+
releaseUncollectible: boolean;
|
|
6626
|
+
resumeQueued: boolean;
|
|
6627
|
+
retryDelayMs: number;
|
|
6628
|
+
scopeKey: string;
|
|
6629
|
+
sourceId: string;
|
|
6630
|
+
sourceKind: "settlement" | "invoice";
|
|
6631
|
+
status:
|
|
6632
|
+
| "queued"
|
|
6633
|
+
| "running"
|
|
6634
|
+
| "waiting"
|
|
6635
|
+
| "unknown"
|
|
6636
|
+
| "completed"
|
|
6637
|
+
| "canceled";
|
|
6638
|
+
version: number;
|
|
6639
|
+
workflowId?: string;
|
|
6640
|
+
},
|
|
6641
|
+
Name
|
|
6642
|
+
>;
|
|
6643
|
+
};
|
|
6644
|
+
};
|
|
6423
6645
|
refundHistory: {
|
|
6424
6646
|
mutations: {
|
|
6425
6647
|
normalizeRefundCommandBindings: FunctionReference<
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CostRecoveryClient,
|
|
3
|
+
type RecoveryStartOptions,
|
|
4
|
+
} from "../../client/costRecovery.js";
|
|
5
|
+
import type { Component } from "../../client/helpers.js";
|
|
6
|
+
import { invoke } from "./bridge.js";
|
|
7
|
+
import { convexTest } from "convex-test";
|
|
8
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
9
|
+
import {
|
|
10
|
+
anyApi,
|
|
11
|
+
componentsGeneric,
|
|
12
|
+
defineSchema,
|
|
13
|
+
defineTable,
|
|
14
|
+
internalActionGeneric,
|
|
15
|
+
internalMutationGeneric,
|
|
16
|
+
} from "convex/server";
|
|
17
|
+
import { v } from "convex/values";
|
|
18
|
+
import { beforeEach, afterEach, expect, it, vi } from "vitest";
|
|
19
|
+
import { modules, schema } from "../../test.js";
|
|
20
|
+
const nested = componentsGeneric();
|
|
21
|
+
const ref = (op: { operationId: string; version: number }) => ({
|
|
22
|
+
...scope,
|
|
23
|
+
operationId: op.operationId,
|
|
24
|
+
version: op.version,
|
|
25
|
+
});
|
|
26
|
+
const scope = {
|
|
27
|
+
accountId: "acct_a",
|
|
28
|
+
scopeKey: "seller",
|
|
29
|
+
currency: "usd",
|
|
30
|
+
groupKey: "group",
|
|
31
|
+
};
|
|
32
|
+
let calls: string[];
|
|
33
|
+
let result: unknown;
|
|
34
|
+
let providerGate: Promise<void> | null;
|
|
35
|
+
let callbackFailures = 0;
|
|
36
|
+
let loseProviderJournal = false;
|
|
37
|
+
const host = {
|
|
38
|
+
completed: internalMutationGeneric({
|
|
39
|
+
args: {
|
|
40
|
+
...Object.fromEntries(Object.keys(scope).map((k) => [k, v.string()])),
|
|
41
|
+
operationId: v.string(),
|
|
42
|
+
version: v.number(),
|
|
43
|
+
sourceKind: v.string(),
|
|
44
|
+
sourceId: v.string(),
|
|
45
|
+
status: v.string(),
|
|
46
|
+
cancelRequested: v.boolean(),
|
|
47
|
+
outcome: v.string(),
|
|
48
|
+
},
|
|
49
|
+
returns: v.null(),
|
|
50
|
+
handler: async (ctx, args) => {
|
|
51
|
+
if (callbackFailures-- > 0)
|
|
52
|
+
throw new Error("private callback database detail");
|
|
53
|
+
await ctx.db.insert("completions", {
|
|
54
|
+
operationId: args.operationId,
|
|
55
|
+
outcome: args.outcome,
|
|
56
|
+
});
|
|
57
|
+
return null;
|
|
58
|
+
},
|
|
59
|
+
}),
|
|
60
|
+
provider: internalActionGeneric({
|
|
61
|
+
args: {
|
|
62
|
+
...Object.fromEntries(Object.keys(scope).map((k) => [k, v.string()])),
|
|
63
|
+
operationId: v.string(),
|
|
64
|
+
version: v.number(),
|
|
65
|
+
providerApiVersion: v.string(),
|
|
66
|
+
definitionVersion: v.number(),
|
|
67
|
+
phase: v.string(),
|
|
68
|
+
},
|
|
69
|
+
returns: v.any(),
|
|
70
|
+
handler: async (_ctx, args) => {
|
|
71
|
+
calls.push(args.phase);
|
|
72
|
+
if (providerGate) await providerGate;
|
|
73
|
+
if (result instanceof Error) throw result;
|
|
74
|
+
return { ...args, sourceId: "in_a", ...(result as object) };
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
start: internalMutationGeneric({
|
|
78
|
+
args: {},
|
|
79
|
+
returns: v.any(),
|
|
80
|
+
handler: async (ctx) =>
|
|
81
|
+
new CostRecoveryClient(
|
|
82
|
+
nested.betterStripe as unknown as Component,
|
|
83
|
+
).startGroup(ctx, {
|
|
84
|
+
...scope,
|
|
85
|
+
requestKey: "request",
|
|
86
|
+
provider: anyApi.host.provider as RecoveryStartOptions["provider"],
|
|
87
|
+
completion: anyApi.host.completed as RecoveryStartOptions["completion"],
|
|
88
|
+
providerApiVersion: "2026-09-30.endive",
|
|
89
|
+
definitionVersion: 1,
|
|
90
|
+
releaseUncollectible: false,
|
|
91
|
+
maxAttempts: 2,
|
|
92
|
+
retryDelayMs: 1,
|
|
93
|
+
}),
|
|
94
|
+
}),
|
|
95
|
+
};
|
|
96
|
+
async function fixture() {
|
|
97
|
+
const t = convexTest(
|
|
98
|
+
defineSchema({
|
|
99
|
+
completions: defineTable({
|
|
100
|
+
operationId: v.string(),
|
|
101
|
+
outcome: v.string(),
|
|
102
|
+
}),
|
|
103
|
+
}),
|
|
104
|
+
{
|
|
105
|
+
"./_generated/api.ts": async () => ({}),
|
|
106
|
+
"./host.ts": async () => host,
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
t.registerComponent("betterStripe", schema, {
|
|
110
|
+
...modules,
|
|
111
|
+
"./component/recoveryWorkflow/bridge.ts": async () => ({
|
|
112
|
+
invoke: internalActionGeneric({
|
|
113
|
+
args: {
|
|
114
|
+
accountId: v.string(),
|
|
115
|
+
scopeKey: v.string(),
|
|
116
|
+
currency: v.string(),
|
|
117
|
+
groupKey: v.string(),
|
|
118
|
+
operationId: v.string(),
|
|
119
|
+
version: v.number(),
|
|
120
|
+
phase: v.union(v.literal("execute"), v.literal("reconcile")),
|
|
121
|
+
},
|
|
122
|
+
returns: v.any(),
|
|
123
|
+
handler: async (ctx, args) => {
|
|
124
|
+
const result = await (
|
|
125
|
+
invoke as unknown as {
|
|
126
|
+
_handler: (ctx: unknown, args: unknown) => Promise<unknown>;
|
|
127
|
+
}
|
|
128
|
+
)._handler(ctx, args);
|
|
129
|
+
if (loseProviderJournal) {
|
|
130
|
+
loseProviderJournal = false;
|
|
131
|
+
throw new Error("Provider journal unavailable");
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
}),
|
|
137
|
+
"./component/fixture.ts": async () => ({
|
|
138
|
+
largeGroup: internalMutationGeneric({
|
|
139
|
+
args: {},
|
|
140
|
+
handler: async (ctx) => {
|
|
141
|
+
const group = (await ctx.db.query("recoveryGroups").first())!;
|
|
142
|
+
for (let i = 0; i < 201; i++) {
|
|
143
|
+
const claimKey = `claim:${i}`;
|
|
144
|
+
const claimId = await ctx.db.insert("recoveryClaims", {
|
|
145
|
+
accountId: scope.accountId,
|
|
146
|
+
scopeKey: scope.scopeKey,
|
|
147
|
+
currency: scope.currency,
|
|
148
|
+
claimKey,
|
|
149
|
+
sourceChargeId: `ch_${i}`,
|
|
150
|
+
sourceGross: 100,
|
|
151
|
+
sourceProcessingFee: 1,
|
|
152
|
+
amountDue: 1,
|
|
153
|
+
revision: 1,
|
|
154
|
+
collectedAmount: 0,
|
|
155
|
+
reservedAmount: 1,
|
|
156
|
+
outstanding: 0,
|
|
157
|
+
sign: "zero",
|
|
158
|
+
});
|
|
159
|
+
await ctx.db.insert("recoveryReservations", {
|
|
160
|
+
operationKey: `reservation:${i}`,
|
|
161
|
+
groupKey: scope.groupKey,
|
|
162
|
+
amount: 1,
|
|
163
|
+
limit: 1,
|
|
164
|
+
allocations: [{ claimId, claimKey, amount: 1 }],
|
|
165
|
+
status: "reserved",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
await ctx.db.patch(group._id, {
|
|
169
|
+
maxDebit: 1000,
|
|
170
|
+
amount: 201,
|
|
171
|
+
pendingOperations: 201,
|
|
172
|
+
});
|
|
173
|
+
const balance = (await ctx.db.query("recoveryBalances").first())!;
|
|
174
|
+
await ctx.db.patch(balance._id, {
|
|
175
|
+
amountDue: 201,
|
|
176
|
+
reservedAmount: 201,
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
breakBalance: internalMutationGeneric({
|
|
181
|
+
args: {},
|
|
182
|
+
handler: async (ctx) => {
|
|
183
|
+
const balance = await ctx.db.query("recoveryBalances").first();
|
|
184
|
+
await ctx.db.patch(balance!._id, { amountDue: 1 });
|
|
185
|
+
},
|
|
186
|
+
}),
|
|
187
|
+
repairBalance: internalMutationGeneric({
|
|
188
|
+
args: {},
|
|
189
|
+
handler: async (ctx) => {
|
|
190
|
+
const balance = await ctx.db.query("recoveryBalances").first();
|
|
191
|
+
await ctx.db.patch(balance!._id, { amountDue: 0 });
|
|
192
|
+
},
|
|
193
|
+
}),
|
|
194
|
+
}),
|
|
195
|
+
});
|
|
196
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
197
|
+
await t.run(async (ctx) =>
|
|
198
|
+
ctx.runMutation(nested.betterStripe.costRecovery.preparation.prepare, {
|
|
199
|
+
...scope,
|
|
200
|
+
sourceKind: "invoice",
|
|
201
|
+
sourceId: "in_a",
|
|
202
|
+
maxDebit: 100,
|
|
203
|
+
maxCredit: 0,
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
return t;
|
|
207
|
+
}
|
|
208
|
+
beforeEach(() => {
|
|
209
|
+
vi.useFakeTimers();
|
|
210
|
+
calls = [];
|
|
211
|
+
providerGate = null;
|
|
212
|
+
callbackFailures = 0;
|
|
213
|
+
loseProviderJournal = false;
|
|
214
|
+
result = { status: "ready", outcome: "collect" };
|
|
215
|
+
});
|
|
216
|
+
afterEach(() => vi.useRealTimers());
|
|
217
|
+
it("runs the real nested Workflow once and finishes recovery", async () => {
|
|
218
|
+
const t = await fixture();
|
|
219
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
220
|
+
expect(await t.mutation(anyApi.host.start, {})).toEqual(op);
|
|
221
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
222
|
+
const saved = await t.query(
|
|
223
|
+
nested.betterStripe.recoveryWorkflow.queries.get,
|
|
224
|
+
ref(op),
|
|
225
|
+
);
|
|
226
|
+
expect(saved.status).toBe("completed");
|
|
227
|
+
expect(calls).toEqual(["execute"]);
|
|
228
|
+
});
|
|
229
|
+
it("sanitizes provider exceptions and reconciles without replaying execute", async () => {
|
|
230
|
+
const t = await fixture();
|
|
231
|
+
result = new Error("sk_test_PRIVATE provider response");
|
|
232
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
233
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
234
|
+
expect(
|
|
235
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
236
|
+
.status,
|
|
237
|
+
).toBe("unknown");
|
|
238
|
+
result = { status: "ready", outcome: "collect", secret: "sk_test_PRIVATE" };
|
|
239
|
+
await t.mutation(
|
|
240
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
241
|
+
ref(op),
|
|
242
|
+
);
|
|
243
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
244
|
+
expect(calls).toEqual(["execute", "reconcile"]);
|
|
245
|
+
expect(
|
|
246
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
247
|
+
.status,
|
|
248
|
+
).toBe("unknown");
|
|
249
|
+
});
|
|
250
|
+
it("bounds automatic readiness observations and deduplicates explicit resume", async () => {
|
|
251
|
+
const t = await fixture();
|
|
252
|
+
result = { status: "not_ready", reason: "provider_pending" };
|
|
253
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
254
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
255
|
+
expect(calls).toEqual(["execute", "reconcile"]);
|
|
256
|
+
await t.mutation(
|
|
257
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
258
|
+
ref(op),
|
|
259
|
+
);
|
|
260
|
+
await t.mutation(
|
|
261
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
262
|
+
ref(op),
|
|
263
|
+
);
|
|
264
|
+
result = { status: "ready", outcome: "collect" };
|
|
265
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
266
|
+
expect(calls).toEqual(["execute", "reconcile", "reconcile"]);
|
|
267
|
+
expect(
|
|
268
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
269
|
+
.status,
|
|
270
|
+
).toBe("completed");
|
|
271
|
+
});
|
|
272
|
+
it("cancels before provider dispatch without releasing the reservation", async () => {
|
|
273
|
+
const t = await fixture();
|
|
274
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
275
|
+
await t.mutation(
|
|
276
|
+
nested.betterStripe.recoveryWorkflow.mutations.requestCancel,
|
|
277
|
+
ref(op),
|
|
278
|
+
);
|
|
279
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
280
|
+
expect(calls).toEqual([]);
|
|
281
|
+
expect(
|
|
282
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
283
|
+
.status,
|
|
284
|
+
).toBe("canceled");
|
|
285
|
+
expect(
|
|
286
|
+
(
|
|
287
|
+
await t.query(nested.betterStripe.costRecovery.queries.group, {
|
|
288
|
+
groupKey: scope.groupKey,
|
|
289
|
+
})
|
|
290
|
+
).status,
|
|
291
|
+
).toBe("sealed");
|
|
292
|
+
});
|
|
293
|
+
it("restarts a failed database completion without another provider call", async () => {
|
|
294
|
+
const t = await fixture();
|
|
295
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
296
|
+
// A missing balance will be created by finish; an invalid reserved total
|
|
297
|
+
// instead forces the transactional finish to fail after the provider receipt.
|
|
298
|
+
await t.run(async (ctx) =>
|
|
299
|
+
ctx.runMutation(nested.betterStripe.fixture.breakBalance, {}),
|
|
300
|
+
);
|
|
301
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
302
|
+
expect(calls).toEqual(["execute"]);
|
|
303
|
+
await t.run(async (ctx) =>
|
|
304
|
+
ctx.runMutation(nested.betterStripe.fixture.repairBalance, {}),
|
|
305
|
+
);
|
|
306
|
+
await t.mutation(
|
|
307
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
308
|
+
ref(op),
|
|
309
|
+
);
|
|
310
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
311
|
+
expect(calls).toEqual(["execute"]);
|
|
312
|
+
expect(
|
|
313
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
314
|
+
.status,
|
|
315
|
+
).toBe("completed");
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("collects a verified in-flight effect even after cancellation is requested", async () => {
|
|
319
|
+
let release!: () => void;
|
|
320
|
+
providerGate = new Promise<void>((resolve) => {
|
|
321
|
+
release = resolve;
|
|
322
|
+
});
|
|
323
|
+
const t = await fixture();
|
|
324
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
325
|
+
const drain = t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
326
|
+
await vi.waitFor(() => expect(calls).toHaveLength(1));
|
|
327
|
+
await t.mutation(
|
|
328
|
+
nested.betterStripe.recoveryWorkflow.mutations.requestCancel,
|
|
329
|
+
ref(op),
|
|
330
|
+
);
|
|
331
|
+
release();
|
|
332
|
+
await drain;
|
|
333
|
+
expect(
|
|
334
|
+
await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)),
|
|
335
|
+
).toMatchObject({
|
|
336
|
+
status: "completed",
|
|
337
|
+
cancelRequested: true,
|
|
338
|
+
outcome: "collect",
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
it("never releases an unknown in-flight effect after cancellation", async () => {
|
|
342
|
+
let release!: () => void;
|
|
343
|
+
providerGate = new Promise<void>((resolve) => {
|
|
344
|
+
release = resolve;
|
|
345
|
+
});
|
|
346
|
+
result = new Error("provider outcome lost");
|
|
347
|
+
const t = await fixture();
|
|
348
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
349
|
+
const drain = t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
350
|
+
await vi.waitFor(() => expect(calls).toHaveLength(1));
|
|
351
|
+
await t.mutation(
|
|
352
|
+
nested.betterStripe.recoveryWorkflow.mutations.requestCancel,
|
|
353
|
+
ref(op),
|
|
354
|
+
);
|
|
355
|
+
release();
|
|
356
|
+
await drain;
|
|
357
|
+
expect(
|
|
358
|
+
await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)),
|
|
359
|
+
).toMatchObject({ status: "unknown", cancelRequested: true });
|
|
360
|
+
expect(
|
|
361
|
+
(
|
|
362
|
+
await t.query(nested.betterStripe.costRecovery.queries.group, {
|
|
363
|
+
groupKey: scope.groupKey,
|
|
364
|
+
})
|
|
365
|
+
).status,
|
|
366
|
+
).toBe("sealed");
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("restarts a failed host completion atomically while retaining the provider receipt", async () => {
|
|
370
|
+
const t = await fixture();
|
|
371
|
+
callbackFailures = 1;
|
|
372
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
373
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
374
|
+
expect(calls).toEqual(["execute"]);
|
|
375
|
+
expect(await t.run((ctx) => ctx.db.query("completions").collect())).toEqual(
|
|
376
|
+
[],
|
|
377
|
+
);
|
|
378
|
+
await t.mutation(
|
|
379
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
380
|
+
ref(op),
|
|
381
|
+
);
|
|
382
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
383
|
+
expect(calls).toEqual(["execute"]);
|
|
384
|
+
expect(
|
|
385
|
+
await t.run((ctx) => ctx.db.query("completions").collect()),
|
|
386
|
+
).toMatchObject([{ operationId: op.operationId, outcome: "collect" }]);
|
|
387
|
+
expect(
|
|
388
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
389
|
+
.status,
|
|
390
|
+
).toBe("completed");
|
|
391
|
+
await t.mutation(
|
|
392
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
393
|
+
ref(op),
|
|
394
|
+
);
|
|
395
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
396
|
+
expect(
|
|
397
|
+
await t.run((ctx) => ctx.db.query("completions").collect()),
|
|
398
|
+
).toHaveLength(1);
|
|
399
|
+
});
|
|
400
|
+
it("continues an exhausted segment with reconciliation only and the original operation", async () => {
|
|
401
|
+
const t = await fixture();
|
|
402
|
+
result = new Error("provider outcome unknown");
|
|
403
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
404
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
405
|
+
for (let i = 0; i < 20; i++) {
|
|
406
|
+
await t.mutation(
|
|
407
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
408
|
+
ref(op),
|
|
409
|
+
);
|
|
410
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
411
|
+
}
|
|
412
|
+
result = { status: "ready", outcome: "collect" };
|
|
413
|
+
await t.mutation(
|
|
414
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
415
|
+
ref(op),
|
|
416
|
+
);
|
|
417
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
418
|
+
expect(calls.filter((p) => p === "execute")).toHaveLength(1);
|
|
419
|
+
expect(
|
|
420
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
421
|
+
.status,
|
|
422
|
+
).toBe("completed");
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("recovers an unjournaled provider success using a new reconcile-only segment", async () => {
|
|
426
|
+
const t = await fixture();
|
|
427
|
+
loseProviderJournal = true;
|
|
428
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
429
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
430
|
+
expect(calls).toEqual(["execute"]);
|
|
431
|
+
await t.mutation(
|
|
432
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
433
|
+
ref(op),
|
|
434
|
+
);
|
|
435
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
436
|
+
expect(calls).toEqual(["execute", "reconcile"]);
|
|
437
|
+
expect(
|
|
438
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
439
|
+
.status,
|
|
440
|
+
).toBe("completed");
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("continues bounded ledger completion from the persisted receipt without another provider observation", async () => {
|
|
444
|
+
const t = await fixture();
|
|
445
|
+
await t.run((ctx) =>
|
|
446
|
+
ctx.runMutation(nested.betterStripe.fixture.largeGroup, {}),
|
|
447
|
+
);
|
|
448
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
449
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
450
|
+
expect(calls).toEqual(["execute"]);
|
|
451
|
+
expect(
|
|
452
|
+
(
|
|
453
|
+
await t.query(nested.betterStripe.costRecovery.queries.group, {
|
|
454
|
+
groupKey: scope.groupKey,
|
|
455
|
+
})
|
|
456
|
+
).pendingOperations,
|
|
457
|
+
).toBe(1);
|
|
458
|
+
result = new Error("provider currently unavailable");
|
|
459
|
+
await t.mutation(
|
|
460
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
461
|
+
ref(op),
|
|
462
|
+
);
|
|
463
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
464
|
+
expect(calls).toEqual(["execute"]);
|
|
465
|
+
expect(
|
|
466
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
467
|
+
.status,
|
|
468
|
+
).toBe("completed");
|
|
469
|
+
expect(
|
|
470
|
+
await t.query(nested.betterStripe.costRecovery.queries.balance, {
|
|
471
|
+
accountId: scope.accountId,
|
|
472
|
+
scopeKey: scope.scopeKey,
|
|
473
|
+
currency: scope.currency,
|
|
474
|
+
}),
|
|
475
|
+
).toMatchObject({
|
|
476
|
+
amountDue: 201,
|
|
477
|
+
collectedAmount: 201,
|
|
478
|
+
reservedAmount: 0,
|
|
479
|
+
outstanding: 0,
|
|
480
|
+
});
|
|
481
|
+
});
|