@fantastic.dev/better-stripe 0.33.0 → 0.35.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 +42 -0
- package/README.md +30 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +522 -2
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +261 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +522 -2
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +261 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/refundCommandEvidence.d.ts +14 -0
- package/dist/client/connect/refundCommandEvidence.d.ts.map +1 -0
- package/dist/client/connect/refundCommandEvidence.js +20 -0
- package/dist/client/connect/refundCommandEvidence.js.map +1 -0
- package/dist/client/connect/refundCommandNormalization.d.ts +11 -0
- package/dist/client/connect/refundCommandNormalization.d.ts.map +1 -0
- package/dist/client/connect/refundCommandNormalization.js +12 -0
- package/dist/client/connect/refundCommandNormalization.js.map +1 -0
- package/dist/client/core/accountClosures.d.ts +15 -0
- package/dist/client/core/accountClosures.d.ts.map +1 -0
- package/dist/client/core/accountClosures.js +10 -0
- package/dist/client/core/accountClosures.js.map +1 -0
- package/dist/client/core/accounts.d.ts.map +1 -1
- package/dist/client/core/accounts.js +15 -23
- package/dist/client/core/accounts.js.map +1 -1
- package/dist/client/core/personalAccountMapping.d.ts +8 -0
- package/dist/client/core/personalAccountMapping.d.ts.map +1 -0
- package/dist/client/core/personalAccountMapping.js +11 -0
- package/dist/client/core/personalAccountMapping.js.map +1 -0
- package/dist/client/financialObservations.d.ts +42 -0
- package/dist/client/financialObservations.d.ts.map +1 -0
- package/dist/client/financialObservations.js +58 -0
- package/dist/client/financialObservations.js.map +1 -0
- package/dist/client/financialOwnership.d.ts +52 -0
- package/dist/client/financialOwnership.d.ts.map +1 -0
- package/dist/client/financialOwnership.js +72 -0
- package/dist/client/financialOwnership.js.map +1 -0
- package/dist/client/index.d.ts +57 -4
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +15 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/observationPagination.d.ts +10 -0
- package/dist/client/observationPagination.d.ts.map +1 -0
- package/dist/client/observationPagination.js +12 -0
- package/dist/client/observationPagination.js.map +1 -0
- package/dist/client/publicTypes.d.ts +10 -0
- package/dist/client/publicTypes.d.ts.map +1 -0
- package/dist/client/publicTypes.js +2 -0
- package/dist/client/publicTypes.js.map +1 -0
- package/dist/client/readContracts.d.ts +29 -0
- package/dist/client/readContracts.d.ts.map +1 -1
- package/dist/client/readContracts.js +10 -0
- package/dist/client/readContracts.js.map +1 -1
- package/dist/client/transferObservations.d.ts +59 -0
- package/dist/client/transferObservations.d.ts.map +1 -0
- package/dist/client/transferObservations.js +82 -0
- package/dist/client/transferObservations.js.map +1 -0
- package/dist/component/_generated/component.d.ts +594 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/accountClosure/helpers.d.ts +18 -0
- package/dist/component/accountClosure/helpers.d.ts.map +1 -0
- package/dist/component/accountClosure/helpers.js +18 -0
- package/dist/component/accountClosure/helpers.js.map +1 -0
- package/dist/component/accountClosure/mutations.d.ts +20 -0
- package/dist/component/accountClosure/mutations.d.ts.map +1 -0
- package/dist/component/accountClosure/mutations.js +55 -0
- package/dist/component/accountClosure/mutations.js.map +1 -0
- package/dist/component/accountClosure/queries.d.ts +34 -0
- package/dist/component/accountClosure/queries.d.ts.map +1 -0
- package/dist/component/accountClosure/queries.js +36 -0
- package/dist/component/accountClosure/queries.js.map +1 -0
- package/dist/component/accountClosure/schema.d.ts +46 -0
- package/dist/component/accountClosure/schema.d.ts.map +1 -0
- package/dist/component/accountClosure/schema.js +20 -0
- package/dist/component/accountClosure/schema.js.map +1 -0
- package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
- package/dist/component/affiliateRecovery/mutations.js +4 -1
- package/dist/component/affiliateRecovery/mutations.js.map +1 -1
- package/dist/component/connect/mutations.d.ts.map +1 -1
- package/dist/component/connect/mutations.js +18 -2
- package/dist/component/connect/mutations.js.map +1 -1
- package/dist/component/connect/queries.d.ts +8 -0
- package/dist/component/connect/queries.d.ts.map +1 -1
- package/dist/component/connect/reversalClaims.d.ts.map +1 -1
- package/dist/component/connect/reversalClaims.js +7 -1
- package/dist/component/connect/reversalClaims.js.map +1 -1
- package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
- package/dist/component/connect/reversalReceipts.js +2 -0
- package/dist/component/connect/reversalReceipts.js.map +1 -1
- package/dist/component/connect/schema.d.ts +11 -1
- package/dist/component/connect/schema.d.ts.map +1 -1
- package/dist/component/connect/schema.js +9 -1
- package/dist/component/connect/schema.js.map +1 -1
- package/dist/component/connect/validators.d.ts +9 -1
- package/dist/component/connect/validators.d.ts.map +1 -1
- package/dist/component/connect/validators.js +2 -0
- package/dist/component/connect/validators.js.map +1 -1
- package/dist/component/core/mutations.d.ts.map +1 -1
- package/dist/component/core/mutations.js +5 -0
- package/dist/component/core/mutations.js.map +1 -1
- package/dist/component/core/personalAccountMapping.d.ts +95 -0
- package/dist/component/core/personalAccountMapping.d.ts.map +1 -0
- package/dist/component/core/personalAccountMapping.js +54 -0
- package/dist/component/core/personalAccountMapping.js.map +1 -0
- package/dist/component/core/queries.d.ts +27 -0
- package/dist/component/core/queries.d.ts.map +1 -1
- package/dist/component/core/queries.js +7 -0
- package/dist/component/core/queries.js.map +1 -1
- package/dist/component/earnings/triggers.d.ts +468 -2
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/earnings/triggers.js +7 -1
- package/dist/component/earnings/triggers.js.map +1 -1
- package/dist/component/financialObservations/continuation.d.ts +7 -0
- package/dist/component/financialObservations/continuation.d.ts.map +1 -0
- package/dist/component/financialObservations/continuation.js +131 -0
- package/dist/component/financialObservations/continuation.js.map +1 -0
- package/dist/component/financialObservations/generations.d.ts +6 -0
- package/dist/component/financialObservations/generations.d.ts.map +1 -0
- package/dist/component/financialObservations/generations.js +46 -0
- package/dist/component/financialObservations/generations.js.map +1 -0
- package/dist/component/financialObservations/mutations.d.ts +62 -0
- package/dist/component/financialObservations/mutations.d.ts.map +1 -0
- package/dist/component/financialObservations/mutations.js +69 -0
- package/dist/component/financialObservations/mutations.js.map +1 -0
- package/dist/component/financialObservations/queries.d.ts +307 -0
- package/dist/component/financialObservations/queries.d.ts.map +1 -0
- package/dist/component/financialObservations/queries.js +219 -0
- package/dist/component/financialObservations/queries.js.map +1 -0
- package/dist/component/financialObservations/schema.d.ts +182 -0
- package/dist/component/financialObservations/schema.d.ts.map +1 -0
- package/dist/component/financialObservations/schema.js +53 -0
- package/dist/component/financialObservations/schema.js.map +1 -0
- package/dist/component/financialObservations/settlement.d.ts +7 -0
- package/dist/component/financialObservations/settlement.d.ts.map +1 -0
- package/dist/component/financialObservations/settlement.js +118 -0
- package/dist/component/financialObservations/settlement.js.map +1 -0
- package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
- package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
- package/dist/component/financialObservations/settlementHelpers.js +70 -0
- package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
- package/dist/component/financialObservations/transferBatches.d.ts +12 -0
- package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
- package/dist/component/financialObservations/transferBatches.js +76 -0
- package/dist/component/financialObservations/transferBatches.js.map +1 -0
- package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
- package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
- package/dist/component/financialObservations/transferContinuation.js +35 -0
- package/dist/component/financialObservations/transferContinuation.js.map +1 -0
- package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
- package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
- package/dist/component/financialObservations/transferContinuationState.js +69 -0
- package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
- package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
- package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
- package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
- package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
- package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
- package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
- package/dist/component/financialObservations/transferHelpers.js +37 -0
- package/dist/component/financialObservations/transferHelpers.js.map +1 -0
- package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
- package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
- package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
- package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
- package/dist/component/financialObservations/transferSchema.d.ts +237 -0
- package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
- package/dist/component/financialObservations/transferSchema.js +65 -0
- package/dist/component/financialObservations/transferSchema.js.map +1 -0
- package/dist/component/financialObservations/transfers.d.ts +7 -0
- package/dist/component/financialObservations/transfers.d.ts.map +1 -0
- package/dist/component/financialObservations/transfers.js +79 -0
- package/dist/component/financialObservations/transfers.js.map +1 -0
- package/dist/component/financialOwnership/helpers.d.ts +37 -0
- package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
- package/dist/component/financialOwnership/helpers.js +76 -0
- package/dist/component/financialOwnership/helpers.js.map +1 -0
- package/dist/component/financialOwnership/mutations.d.ts +28 -0
- package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
- package/dist/component/financialOwnership/mutations.js +74 -0
- package/dist/component/financialOwnership/mutations.js.map +1 -0
- package/dist/component/financialOwnership/queries.d.ts +57 -0
- package/dist/component/financialOwnership/queries.d.ts.map +1 -0
- package/dist/component/financialOwnership/queries.js +50 -0
- package/dist/component/financialOwnership/queries.js.map +1 -0
- package/dist/component/financialOwnership/schema.d.ts +140 -0
- package/dist/component/financialOwnership/schema.d.ts.map +1 -0
- package/dist/component/financialOwnership/schema.js +35 -0
- package/dist/component/financialOwnership/schema.js.map +1 -0
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/refundHistory/commandBinding.d.ts +17 -0
- package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
- package/dist/component/refundHistory/commandBinding.js +41 -0
- package/dist/component/refundHistory/commandBinding.js.map +1 -0
- package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
- package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
- package/dist/component/refundHistory/commandEvidence.js +54 -0
- package/dist/component/refundHistory/commandEvidence.js.map +1 -0
- package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
- package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
- package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
- package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
- package/dist/component/refundHistory/commandFields.d.ts +8 -0
- package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
- package/dist/component/refundHistory/commandFields.js +9 -0
- package/dist/component/refundHistory/commandFields.js.map +1 -0
- package/dist/component/refundHistory/mutations.d.ts +8 -0
- package/dist/component/refundHistory/mutations.d.ts.map +1 -0
- package/dist/component/refundHistory/mutations.js +25 -0
- package/dist/component/refundHistory/mutations.js.map +1 -0
- package/dist/component/refundHistory/queries.d.ts +51 -0
- package/dist/component/refundHistory/queries.d.ts.map +1 -1
- package/dist/component/refundHistory/queries.js +14 -0
- package/dist/component/refundHistory/queries.js.map +1 -1
- package/dist/component/schema.d.ts +261 -1
- package/dist/component/schema.js +14 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/settlement/mutations.d.ts.map +1 -1
- package/dist/component/settlement/mutations.js +5 -1
- package/dist/component/settlement/mutations.js.map +1 -1
- package/dist/shared/refundCommandScope.d.ts +10 -0
- package/dist/shared/refundCommandScope.d.ts.map +1 -0
- package/dist/shared/refundCommandScope.js +9 -0
- package/dist/shared/refundCommandScope.js.map +1 -0
- package/docs/financial-observations.md +159 -0
- package/docs/financial-ownership.md +54 -0
- package/docs/personal-account-mapping.md +54 -0
- package/docs/refund-command-evidence.md +49 -0
- package/package.json +6 -2
- package/src/client/connect/refundCommandEvidence.ts +41 -0
- package/src/client/connect/refundCommandNormalization.ts +28 -0
- package/src/client/core/account-close-orchestration.test.ts +131 -0
- package/src/client/core/account-closure.test.ts +317 -0
- package/src/client/core/accountClosures.ts +37 -0
- package/src/client/core/accounts.test.ts +0 -111
- package/src/client/core/accounts.ts +24 -29
- package/src/client/core/personal-account-mapping.test.ts +71 -0
- package/src/client/core/personalAccountMapping.ts +23 -0
- package/src/client/financialObservations.test.ts +109 -0
- package/src/client/financialObservations.ts +106 -0
- package/src/client/financialOwnership.test.ts +80 -0
- package/src/client/financialOwnership.ts +116 -0
- package/src/client/index.ts +38 -35
- package/src/client/observationPagination.ts +11 -0
- package/src/client/publicTypes.ts +72 -0
- package/src/client/readContracts.ts +21 -0
- package/src/client/refund-command-evidence.test.ts +68 -0
- package/src/client/refund-command-normalization.test.ts +61 -0
- package/src/client/transferContinuation.test.ts +94 -0
- package/src/client/transferObservations.test.ts +98 -0
- package/src/client/transferObservations.ts +180 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +716 -0
- package/src/component/accountClosure/helpers.ts +21 -0
- package/src/component/accountClosure/mutations.ts +56 -0
- package/src/component/accountClosure/queries.ts +43 -0
- package/src/component/accountClosure/schema.ts +20 -0
- package/src/component/affiliateRecovery/mutations.ts +10 -1
- package/src/component/connect/mutations.ts +29 -2
- package/src/component/connect/reversalClaims.ts +19 -1
- package/src/component/connect/reversalReceipts.ts +2 -0
- package/src/component/connect/schema.ts +9 -1
- package/src/component/connect/validators.ts +2 -0
- package/src/component/core/mutations.ts +3 -0
- package/src/component/core/personal-account-mapping.test.ts +173 -0
- package/src/component/core/personalAccountMapping.ts +81 -0
- package/src/component/core/queries.ts +11 -0
- package/src/component/core/reset.test.ts +8 -1
- package/src/component/earnings/earnings-totals.test.ts +14 -10
- package/src/component/earnings/triggers.ts +9 -1
- package/src/component/financialObservations/continuation.test.ts +445 -0
- package/src/component/financialObservations/continuation.ts +159 -0
- package/src/component/financialObservations/generations.ts +53 -0
- package/src/component/financialObservations/mutations.ts +75 -0
- package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
- package/src/component/financialObservations/queries.ts +246 -0
- package/src/component/financialObservations/schema.ts +68 -0
- package/src/component/financialObservations/settlement-observations.test.ts +86 -0
- package/src/component/financialObservations/settlement.ts +142 -0
- package/src/component/financialObservations/settlementHelpers.ts +92 -0
- package/src/component/financialObservations/transfer-batches.test.ts +61 -0
- package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
- package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
- package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
- package/src/component/financialObservations/transfer-observations.test.ts +212 -0
- package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
- package/src/component/financialObservations/transfer-writers.test.ts +162 -0
- package/src/component/financialObservations/transferBatches.ts +96 -0
- package/src/component/financialObservations/transferContinuation.ts +53 -0
- package/src/component/financialObservations/transferContinuationState.ts +85 -0
- package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
- package/src/component/financialObservations/transferHelpers.ts +38 -0
- package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
- package/src/component/financialObservations/transferSchema.ts +74 -0
- package/src/component/financialObservations/transfers.ts +99 -0
- package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
- package/src/component/financialOwnership/helpers.ts +98 -0
- package/src/component/financialOwnership/mutations.ts +83 -0
- package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
- package/src/component/financialOwnership/queries.ts +65 -0
- package/src/component/financialOwnership/schema.ts +35 -0
- package/src/component/refundHistory/command-evidence.test.ts +288 -0
- package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
- package/src/component/refundHistory/command-normalization.test.ts +229 -0
- package/src/component/refundHistory/command-provenance.test.ts +59 -0
- package/src/component/refundHistory/commandBinding.ts +54 -0
- package/src/component/refundHistory/commandEvidence.ts +71 -0
- package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
- package/src/component/refundHistory/commandFields.ts +9 -0
- package/src/component/refundHistory/mutations.ts +33 -0
- package/src/component/refundHistory/queries.ts +15 -0
- package/src/component/schema.ts +24 -0
- package/src/component/settlement/mutations.ts +5 -1
- package/src/shared/refundCommandScope.ts +19 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { MutationCtx, QueryCtx } from "../_generated/server.js";
|
|
2
|
+
export function closureByStripeId(ctx: QueryCtx, stripeAccountId: string) {
|
|
3
|
+
return ctx.db
|
|
4
|
+
.query("accountClosures")
|
|
5
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
6
|
+
q.eq("stripeAccountId", stripeAccountId),
|
|
7
|
+
)
|
|
8
|
+
.unique();
|
|
9
|
+
}
|
|
10
|
+
/** A delayed account event cannot revive a closed recipient or change a pinned owner. */
|
|
11
|
+
export async function canUpsertAccount(
|
|
12
|
+
ctx: MutationCtx,
|
|
13
|
+
account: { stripeAccountId: string; userId: string; orgId?: string },
|
|
14
|
+
) {
|
|
15
|
+
const closure = await closureByStripeId(ctx, account.stripeAccountId);
|
|
16
|
+
if (!closure) return true;
|
|
17
|
+
if (closure.state === "closed") return false;
|
|
18
|
+
if (closure.userId !== account.userId || closure.orgId !== account.orgId)
|
|
19
|
+
throw new Error("Account ownership is pinned while closure is unresolved");
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation } from "../_generated/server.js";
|
|
3
|
+
import { closureByStripeId } from "./helpers.js";
|
|
4
|
+
import { closureDoc } from "./schema.js";
|
|
5
|
+
/** Trusted host boundary: authenticate and authorize the actor before invoking close. */
|
|
6
|
+
export const prepare = mutation({
|
|
7
|
+
args: { stripeAccountId: v.string() },
|
|
8
|
+
returns: v.object({ closure: closureDoc, isRetry: v.boolean() }),
|
|
9
|
+
handler: async (ctx, { stripeAccountId }) => {
|
|
10
|
+
const existing = await closureByStripeId(ctx, stripeAccountId);
|
|
11
|
+
if (existing) return { closure: existing, isRetry: true };
|
|
12
|
+
const account = await ctx.db
|
|
13
|
+
.query("accounts")
|
|
14
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
15
|
+
q.eq("stripeAccountId", stripeAccountId),
|
|
16
|
+
)
|
|
17
|
+
.unique();
|
|
18
|
+
const id = await ctx.db.insert("accountClosures", {
|
|
19
|
+
stripeAccountId,
|
|
20
|
+
userId: account?.userId,
|
|
21
|
+
orgId: account?.orgId,
|
|
22
|
+
appliedConfigurations: account?.appliedConfigurations,
|
|
23
|
+
state: "pending",
|
|
24
|
+
});
|
|
25
|
+
const closure = await ctx.db.get("accountClosures", id);
|
|
26
|
+
if (!closure) throw new Error("Closure intent was not persisted");
|
|
27
|
+
return { closure, isRetry: false };
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const complete = mutation({
|
|
32
|
+
args: { stripeAccountId: v.string() },
|
|
33
|
+
returns: v.null(),
|
|
34
|
+
handler: async (ctx, { stripeAccountId }) => {
|
|
35
|
+
const closure = await closureByStripeId(ctx, stripeAccountId);
|
|
36
|
+
if (!closure) throw new Error("Closure intent is required");
|
|
37
|
+
if (closure.state === "closed") return null;
|
|
38
|
+
const account = await ctx.db
|
|
39
|
+
.query("accounts")
|
|
40
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
41
|
+
q.eq("stripeAccountId", stripeAccountId),
|
|
42
|
+
)
|
|
43
|
+
.unique();
|
|
44
|
+
if (
|
|
45
|
+
account &&
|
|
46
|
+
(account.userId !== closure.userId || account.orgId !== closure.orgId)
|
|
47
|
+
)
|
|
48
|
+
throw new Error("Account ownership changed during closure");
|
|
49
|
+
await ctx.db.patch("accountClosures", closure._id, {
|
|
50
|
+
state: "closed",
|
|
51
|
+
closedAt: Date.now(),
|
|
52
|
+
});
|
|
53
|
+
if (account) await ctx.db.delete("accounts", account._id);
|
|
54
|
+
return null;
|
|
55
|
+
},
|
|
56
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
paginationOptsValidator,
|
|
3
|
+
paginationResultValidator,
|
|
4
|
+
} from "convex/server";
|
|
5
|
+
import { paginator } from "convex-helpers/server/pagination";
|
|
6
|
+
import { v } from "convex/values";
|
|
7
|
+
import { query } from "../_generated/server.js";
|
|
8
|
+
import schema from "../schema.js";
|
|
9
|
+
import { closureByStripeId } from "./helpers.js";
|
|
10
|
+
import { closureDoc } from "./schema.js";
|
|
11
|
+
/** This is provenance, not end-user authentication; hosts must compare the authenticated owner. */
|
|
12
|
+
export const getClosedAccountByStripeId = query({
|
|
13
|
+
args: { stripeAccountId: v.string() },
|
|
14
|
+
returns: v.union(closureDoc, v.null()),
|
|
15
|
+
handler: async (ctx, { stripeAccountId }) => {
|
|
16
|
+
const closure = await closureByStripeId(ctx, stripeAccountId);
|
|
17
|
+
return closure?.state === "closed" ? closure : null;
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const listClosedAccountsByUserId = query({
|
|
22
|
+
args: { userId: v.string(), paginationOpts: paginationOptsValidator },
|
|
23
|
+
returns: paginationResultValidator(closureDoc),
|
|
24
|
+
handler: async (ctx, { userId, paginationOpts }) => {
|
|
25
|
+
if (
|
|
26
|
+
!Number.isInteger(paginationOpts.numItems) ||
|
|
27
|
+
paginationOpts.numItems < 1 ||
|
|
28
|
+
paginationOpts.numItems > 100
|
|
29
|
+
)
|
|
30
|
+
throw new Error("Closure history page size must be between 1 and 100");
|
|
31
|
+
return paginator(ctx.db, schema)
|
|
32
|
+
.query("accountClosures")
|
|
33
|
+
.withIndex("by_user_state", (q) =>
|
|
34
|
+
q.eq("userId", userId).eq("orgId", undefined).eq("state", "closed"),
|
|
35
|
+
)
|
|
36
|
+
.order("desc")
|
|
37
|
+
.paginate({
|
|
38
|
+
...paginationOpts,
|
|
39
|
+
maximumRowsRead: 200,
|
|
40
|
+
maximumBytesRead: 1_000_000,
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { appliedConfigurationsValidator } from "../core/validators.js";
|
|
4
|
+
|
|
5
|
+
export const closureFields = {
|
|
6
|
+
stripeAccountId: v.string(),
|
|
7
|
+
userId: v.optional(v.string()),
|
|
8
|
+
orgId: v.optional(v.string()),
|
|
9
|
+
appliedConfigurations: appliedConfigurationsValidator,
|
|
10
|
+
state: v.union(v.literal("pending"), v.literal("closed")),
|
|
11
|
+
closedAt: v.optional(v.number()),
|
|
12
|
+
};
|
|
13
|
+
export const closureDoc = v.object({
|
|
14
|
+
_id: v.id("accountClosures"),
|
|
15
|
+
_creationTime: v.number(),
|
|
16
|
+
...closureFields,
|
|
17
|
+
});
|
|
18
|
+
export const accountClosuresTable = defineTable(closureFields)
|
|
19
|
+
.index("by_stripe_account_id", ["stripeAccountId"])
|
|
20
|
+
.index("by_user_state", ["userId", "orgId", "state"]);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { observeTransfer } from "../financialObservations/transfers.js";
|
|
1
2
|
import { commissionExposureValidator } from "../settlement/clawbackSchema.js";
|
|
2
3
|
import { applyObligationExposure } from "../settlement/clawbacks.js";
|
|
3
4
|
import { v } from "convex/values";
|
|
@@ -169,6 +170,10 @@ export const completeOperation = mutation({
|
|
|
169
170
|
transfer.status,
|
|
170
171
|
),
|
|
171
172
|
);
|
|
173
|
+
await observeTransfer(
|
|
174
|
+
ctx,
|
|
175
|
+
(await ctx.db.get("transfers", transfer._id))!,
|
|
176
|
+
);
|
|
172
177
|
const duplicate = await ctx.db
|
|
173
178
|
.query("affiliateReversalReceipts")
|
|
174
179
|
.withIndex("by_receipt", (q) =>
|
|
@@ -187,7 +192,7 @@ export const completeOperation = mutation({
|
|
|
187
192
|
});
|
|
188
193
|
await updateObligation(ctx, row, { ...patch, recoveredAmount: total });
|
|
189
194
|
} else {
|
|
190
|
-
await ctx.db.insert("transfers", {
|
|
195
|
+
const transferId = await ctx.db.insert("transfers", {
|
|
191
196
|
stripeTransferId: args.receiptId!,
|
|
192
197
|
sourceChargeId: row.sourceChargeId,
|
|
193
198
|
sourceInvoiceId: row.sourceInvoiceId,
|
|
@@ -199,6 +204,10 @@ export const completeOperation = mutation({
|
|
|
199
204
|
reinstatement: true,
|
|
200
205
|
metadata: { bsRecoveryOperation: op._id },
|
|
201
206
|
});
|
|
207
|
+
await observeTransfer(
|
|
208
|
+
ctx,
|
|
209
|
+
(await ctx.db.get("transfers", transferId))!,
|
|
210
|
+
);
|
|
202
211
|
await updateObligation(ctx, row, {
|
|
203
212
|
...patch,
|
|
204
213
|
restoredAmount: (row.restoredAmount ?? 0) + op.amount,
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import { refundCommandBinding } from "../refundHistory/commandBinding.js";
|
|
2
|
+
import {
|
|
3
|
+
deferTransferFacts,
|
|
4
|
+
synchronousTransferFactLimit,
|
|
5
|
+
} from "../financialObservations/transferBatches.js";
|
|
6
|
+
import { observeTransfer } from "../financialObservations/transfers.js";
|
|
7
|
+
|
|
1
8
|
import { syncCommissionTransfer } from "../settlement/clawbacks.js";
|
|
2
9
|
import { recordTransferReversalDefinition } from "./reversalReceipts.js";
|
|
3
10
|
import { claimReversalSlicesDefinition } from "./reversalClaims.js";
|
|
@@ -140,7 +147,11 @@ export const upsertRefund = mutation({
|
|
|
140
147
|
|
|
141
148
|
const accountId =
|
|
142
149
|
args.accountId ?? (await paymentSeller(ctx, args.stripePaymentIntentId));
|
|
143
|
-
const record = {
|
|
150
|
+
const record = {
|
|
151
|
+
...args,
|
|
152
|
+
...(accountId ? { accountId } : {}),
|
|
153
|
+
...refundCommandBinding(existing, args, Date.now()),
|
|
154
|
+
};
|
|
144
155
|
if (existing) {
|
|
145
156
|
await ctx.db.patch("refunds", existing._id, record);
|
|
146
157
|
} else {
|
|
@@ -317,13 +328,21 @@ export const upsertTransfer = mutation({
|
|
|
317
328
|
);
|
|
318
329
|
await syncCommissionTransfer(ctx, { ...existing, ...args, ...derived });
|
|
319
330
|
await ctx.db.patch("transfers", existing._id, { ...args, ...derived });
|
|
331
|
+
await observeTransfer(
|
|
332
|
+
ctx,
|
|
333
|
+
(await ctx.db.get("transfers", existing._id))!,
|
|
334
|
+
);
|
|
320
335
|
} else {
|
|
321
336
|
const derived = deriveReversalState(
|
|
322
337
|
args.amount,
|
|
323
338
|
args.reversedAmount ?? 0,
|
|
324
339
|
args.status,
|
|
325
340
|
);
|
|
326
|
-
await ctx.db.insert("transfers", {
|
|
341
|
+
const transferId = await ctx.db.insert("transfers", {
|
|
342
|
+
...args,
|
|
343
|
+
...derived,
|
|
344
|
+
});
|
|
345
|
+
await observeTransfer(ctx, (await ctx.db.get("transfers", transferId))!);
|
|
327
346
|
}
|
|
328
347
|
|
|
329
348
|
return null;
|
|
@@ -448,8 +467,16 @@ export const releaseReversalClaim = mutation({
|
|
|
448
467
|
await ctx.db.patch("transfers", p.legId, {
|
|
449
468
|
reversalClaimedAmount: p.from,
|
|
450
469
|
});
|
|
470
|
+
if (plan.length <= synchronousTransferFactLimit)
|
|
471
|
+
await observeTransfer(ctx, (await ctx.db.get("transfers", p.legId))!);
|
|
451
472
|
rewound.push({ stripeTransferId: p.stripeTransferId, to: p.from });
|
|
452
473
|
}
|
|
474
|
+
if (plan.length > synchronousTransferFactLimit)
|
|
475
|
+
await deferTransferFacts(
|
|
476
|
+
ctx,
|
|
477
|
+
op,
|
|
478
|
+
plan.map((p) => p.legId),
|
|
479
|
+
);
|
|
453
480
|
await ctx.db.delete("transferReversalOps", op._id);
|
|
454
481
|
|
|
455
482
|
return { released: true, rewound };
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deferTransferFacts,
|
|
3
|
+
synchronousTransferFactLimit,
|
|
4
|
+
} from "../financialObservations/transferBatches.js";
|
|
5
|
+
import { observeTransfer } from "../financialObservations/transfers.js";
|
|
1
6
|
import { v } from "convex/values";
|
|
2
7
|
import type { MutationCtx } from "../_generated/server.js";
|
|
3
8
|
import { computeReversalSlices } from "../lib/reversals.js";
|
|
@@ -203,10 +208,15 @@ export const claimReversalSlicesDefinition = {
|
|
|
203
208
|
await ctx.db.patch("transfers", byId.get(s.stripeTransferId)!._id, {
|
|
204
209
|
reversalClaimedAmount: s.to,
|
|
205
210
|
});
|
|
211
|
+
if (slices.length <= synchronousTransferFactLimit)
|
|
212
|
+
await observeTransfer(
|
|
213
|
+
ctx,
|
|
214
|
+
(await ctx.db.get("transfers", byId.get(s.stripeTransferId)!._id))!,
|
|
215
|
+
);
|
|
206
216
|
}
|
|
207
217
|
// Record the operation even when it claims nothing, so a redelivery is a
|
|
208
218
|
// deterministic no-op instead of a recomputation against newer state.
|
|
209
|
-
await ctx.db.insert("transferReversalOps", {
|
|
219
|
+
const operationId = await ctx.db.insert("transferReversalOps", {
|
|
210
220
|
operationId: args.operationId,
|
|
211
221
|
sourceChargeId: args.sourceChargeId,
|
|
212
222
|
slices,
|
|
@@ -215,6 +225,14 @@ export const claimReversalSlicesDefinition = {
|
|
|
215
225
|
: {}),
|
|
216
226
|
});
|
|
217
227
|
|
|
228
|
+
if (slices.length > synchronousTransferFactLimit)
|
|
229
|
+
await deferTransferFacts(
|
|
230
|
+
ctx,
|
|
231
|
+
(await ctx.db.get("transferReversalOps", operationId))!,
|
|
232
|
+
slices.map((s) => byId.get(s.stripeTransferId)!._id),
|
|
233
|
+
"transfer_frontier_claim",
|
|
234
|
+
);
|
|
235
|
+
|
|
218
236
|
return {
|
|
219
237
|
replay: false,
|
|
220
238
|
slices: slices.map((s) => ({
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { observeTransfer } from "../financialObservations/transfers.js";
|
|
1
2
|
import { updateObligation } from "../affiliateRecovery/accounting.js";
|
|
2
3
|
import { v } from "convex/values";
|
|
3
4
|
import type { Doc } from "../_generated/dataModel.js";
|
|
@@ -131,6 +132,7 @@ export const recordTransferReversalDefinition = {
|
|
|
131
132
|
args.receipt,
|
|
132
133
|
);
|
|
133
134
|
await ctx.db.patch("transfers", transfer._id, derived);
|
|
135
|
+
await observeTransfer(ctx, (await ctx.db.get("transfers", transfer._id))!);
|
|
134
136
|
const obligation = await ctx.db
|
|
135
137
|
.query("affiliateObligations")
|
|
136
138
|
.withIndex("by_transfer", (q) =>
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { refundCommandFields } from "../refundHistory/commandFields.js";
|
|
1
2
|
import { v } from "convex/values";
|
|
2
3
|
import { defineTable } from "convex/server";
|
|
3
4
|
|
|
@@ -48,7 +49,14 @@ export const pendingFeeRefundsTable = defineTable(pendingFeeRefundFields).index(
|
|
|
48
49
|
* Refunds. Compound status indexes follow Plan 002: filtered list queries
|
|
49
50
|
* use a compound index, never filter-after-take.
|
|
50
51
|
*/
|
|
51
|
-
export const refundsTable = defineTable(
|
|
52
|
+
export const refundsTable = defineTable({
|
|
53
|
+
...refundFields,
|
|
54
|
+
...refundCommandFields,
|
|
55
|
+
})
|
|
56
|
+
.index("by_refund_command_key", ["refundCommandKey"])
|
|
57
|
+
.index("by_refund_command_normalization_version", [
|
|
58
|
+
"refundCommandNormalizationVersion",
|
|
59
|
+
])
|
|
52
60
|
.index("by_stripe_refund_id", ["stripeRefundId"])
|
|
53
61
|
.index("by_stripe_payment_intent_id", ["stripePaymentIntentId"])
|
|
54
62
|
.index("by_account_id", ["accountId"])
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { refundCommandFields } from "../refundHistory/commandFields.js";
|
|
1
2
|
import { commissionExposureValidator } from "../settlement/clawbackSchema.js";
|
|
2
3
|
import { type Infer, v } from "convex/values";
|
|
3
4
|
|
|
@@ -154,6 +155,7 @@ export const refundDocValidator = v.object({
|
|
|
154
155
|
_id: v.id("refunds"),
|
|
155
156
|
_creationTime: v.number(),
|
|
156
157
|
...refundFields,
|
|
158
|
+
...refundCommandFields,
|
|
157
159
|
});
|
|
158
160
|
|
|
159
161
|
// =============================================================================
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { canUpsertAccount } from "../accountClosure/helpers.js";
|
|
1
2
|
import { assertPayoutResetSafe } from "./resetSafety.js";
|
|
2
3
|
import { v } from "convex/values";
|
|
3
4
|
|
|
@@ -32,6 +33,7 @@ export const upsertAccount = mutation({
|
|
|
32
33
|
},
|
|
33
34
|
returns: v.null(),
|
|
34
35
|
handler: async (ctx, args) => {
|
|
36
|
+
if (!(await canUpsertAccount(ctx, args))) return null;
|
|
35
37
|
const existing = await ctx.db
|
|
36
38
|
.query("accounts")
|
|
37
39
|
.withIndex("by_stripe_account_id", (q) =>
|
|
@@ -128,6 +130,7 @@ export const upsertAccountInternal = mutation({
|
|
|
128
130
|
},
|
|
129
131
|
returns: v.null(),
|
|
130
132
|
handler: async (ctx, args) => {
|
|
133
|
+
if (!(await canUpsertAccount(ctx, args))) return null;
|
|
131
134
|
const existing = await ctx.db
|
|
132
135
|
.query("accounts")
|
|
133
136
|
.withIndex("by_stripe_account_id", (q) =>
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { makeFunctionReference } from "convex/server";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { api } from "../_generated/api.js";
|
|
6
|
+
import { modules, schema } from "../../test.js";
|
|
7
|
+
|
|
8
|
+
const read = makeFunctionReference<"query", { userId: string }>(
|
|
9
|
+
"core/queries:getPersonalAccountMapping",
|
|
10
|
+
);
|
|
11
|
+
const account = (
|
|
12
|
+
stripeAccountId = "acct_original",
|
|
13
|
+
userId = "user_original",
|
|
14
|
+
orgId?: string,
|
|
15
|
+
) => ({
|
|
16
|
+
stripeAccountId,
|
|
17
|
+
userId,
|
|
18
|
+
onboardingStatus: "pending" as const,
|
|
19
|
+
...(orgId === undefined ? {} : { orgId }),
|
|
20
|
+
});
|
|
21
|
+
const fixture = () => convexTest(schema, modules);
|
|
22
|
+
|
|
23
|
+
describe("bounded personal account mapping", () => {
|
|
24
|
+
it("returns a unique reciprocal personal account without requiring financial onboarding", async () => {
|
|
25
|
+
const t = fixture();
|
|
26
|
+
const id = await t.run((ctx) => ctx.db.insert("accounts", account()));
|
|
27
|
+
expect(await t.query(read, { userId: "user_original" })).toEqual({
|
|
28
|
+
status: "unique",
|
|
29
|
+
account: expect.objectContaining({
|
|
30
|
+
_id: id,
|
|
31
|
+
stripeAccountId: "acct_original",
|
|
32
|
+
userId: "user_original",
|
|
33
|
+
onboardingStatus: "pending",
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("distinguishes a missing mapping from a guessed current account", async () => {
|
|
39
|
+
expect(await fixture().query(read, { userId: "user_missing" })).toEqual({
|
|
40
|
+
status: "unavailable",
|
|
41
|
+
reason: "missing",
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it.each([
|
|
46
|
+
"same user",
|
|
47
|
+
"same Stripe ID",
|
|
48
|
+
"same exact mapping",
|
|
49
|
+
"mixed organization",
|
|
50
|
+
] as const)(
|
|
51
|
+
"rejects ambiguous %s instead of selecting the first row",
|
|
52
|
+
async (kind) => {
|
|
53
|
+
const t = fixture();
|
|
54
|
+
await t.run(async (ctx) => {
|
|
55
|
+
await ctx.db.insert("accounts", account());
|
|
56
|
+
await ctx.db.insert(
|
|
57
|
+
"accounts",
|
|
58
|
+
kind === "same user"
|
|
59
|
+
? account("acct_replacement")
|
|
60
|
+
: kind === "same Stripe ID"
|
|
61
|
+
? account("acct_original", "user_other")
|
|
62
|
+
: kind === "mixed organization"
|
|
63
|
+
? account("acct_organization", "user_original", "org_one")
|
|
64
|
+
: account(),
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
expect(await t.query(read, { userId: "user_original" })).toEqual({
|
|
68
|
+
status: "unavailable",
|
|
69
|
+
reason: "ambiguous",
|
|
70
|
+
});
|
|
71
|
+
expect(
|
|
72
|
+
(
|
|
73
|
+
await t.query(api.core.queries.getAccountByUserId, {
|
|
74
|
+
userId: "user_original",
|
|
75
|
+
})
|
|
76
|
+
)?.stripeAccountId,
|
|
77
|
+
).toBe("acct_original");
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
it.each(["org_one", ""])(
|
|
82
|
+
"never treats organization scope %j as personal",
|
|
83
|
+
async (orgId) => {
|
|
84
|
+
const t = fixture();
|
|
85
|
+
await t.run((ctx) =>
|
|
86
|
+
ctx.db.insert(
|
|
87
|
+
"accounts",
|
|
88
|
+
account("acct_original", "user_original", orgId),
|
|
89
|
+
),
|
|
90
|
+
);
|
|
91
|
+
expect(await t.query(read, { userId: "user_original" })).toEqual({
|
|
92
|
+
status: "unavailable",
|
|
93
|
+
reason: "organization",
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
it.each(["acct_", "cus_123", "acct_bad space", "acct_" + "x".repeat(129)])(
|
|
99
|
+
"rejects malformed provider account %j",
|
|
100
|
+
async (stripeAccountId) => {
|
|
101
|
+
const t = fixture();
|
|
102
|
+
await t.run((ctx) => ctx.db.insert("accounts", account(stripeAccountId)));
|
|
103
|
+
expect(await t.query(read, { userId: "user_original" })).toEqual({
|
|
104
|
+
status: "unavailable",
|
|
105
|
+
reason: "invalid_account",
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
it.each(["closed", "pending"] as const)(
|
|
111
|
+
"excludes a stale live row with %s closure provenance",
|
|
112
|
+
async (state) => {
|
|
113
|
+
const t = fixture();
|
|
114
|
+
await t.run(async (ctx) => {
|
|
115
|
+
await ctx.db.insert("accounts", account());
|
|
116
|
+
await ctx.db.insert("accountClosures", {
|
|
117
|
+
stripeAccountId: "acct_original",
|
|
118
|
+
userId: "user_original",
|
|
119
|
+
state,
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
expect(await t.query(read, { userId: "user_original" })).toEqual({
|
|
123
|
+
status: "unavailable",
|
|
124
|
+
reason: state === "closed" ? "closed" : "closure_unresolved",
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
it("rejects conflicting closure records rather than choosing one", async () => {
|
|
130
|
+
const t = fixture();
|
|
131
|
+
await t.run(async (ctx) => {
|
|
132
|
+
await ctx.db.insert("accounts", account());
|
|
133
|
+
for (const state of ["pending", "closed"] as const)
|
|
134
|
+
await ctx.db.insert("accountClosures", {
|
|
135
|
+
stripeAccountId: "acct_original",
|
|
136
|
+
userId: "user_original",
|
|
137
|
+
state,
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
expect(await t.query(read, { userId: "user_original" })).toEqual({
|
|
141
|
+
status: "unavailable",
|
|
142
|
+
reason: "ambiguous",
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it.each([
|
|
147
|
+
"",
|
|
148
|
+
" ",
|
|
149
|
+
" user_original",
|
|
150
|
+
"user_original ",
|
|
151
|
+
"user\noriginal",
|
|
152
|
+
"x".repeat(257),
|
|
153
|
+
])("rejects malformed user selector %j", async (userId) => {
|
|
154
|
+
await expect(fixture().query(read, { userId })).rejects.toThrow(
|
|
155
|
+
"Invalid personal account user selector",
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("does not leak another user's mapping or mutate account state", async () => {
|
|
160
|
+
const t = fixture();
|
|
161
|
+
const before = await t.run(async (ctx) => {
|
|
162
|
+
await ctx.db.insert("accounts", account());
|
|
163
|
+
return ctx.db.query("accounts").take(2);
|
|
164
|
+
});
|
|
165
|
+
expect(await t.query(read, { userId: "user_other" })).toEqual({
|
|
166
|
+
status: "unavailable",
|
|
167
|
+
reason: "missing",
|
|
168
|
+
});
|
|
169
|
+
expect(await t.run((ctx) => ctx.db.query("accounts").take(2))).toEqual(
|
|
170
|
+
before,
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type Infer, v } from "convex/values";
|
|
2
|
+
|
|
3
|
+
import type { QueryCtx } from "../_generated/server.js";
|
|
4
|
+
import { accountDocValidator } from "./validators.js";
|
|
5
|
+
|
|
6
|
+
export const personalAccountMappingValidator = v.union(
|
|
7
|
+
v.object({ status: v.literal("unique"), account: accountDocValidator }),
|
|
8
|
+
v.object({
|
|
9
|
+
status: v.literal("unavailable"),
|
|
10
|
+
reason: v.union(
|
|
11
|
+
v.literal("missing"),
|
|
12
|
+
v.literal("ambiguous"),
|
|
13
|
+
v.literal("organization"),
|
|
14
|
+
v.literal("invalid_account"),
|
|
15
|
+
v.literal("closure_unresolved"),
|
|
16
|
+
v.literal("closed"),
|
|
17
|
+
),
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
export type PersonalAccountMapping = Infer<
|
|
21
|
+
typeof personalAccountMappingValidator
|
|
22
|
+
>;
|
|
23
|
+
|
|
24
|
+
/** Opaque host identity is an exact selector, never an authentication credential. */
|
|
25
|
+
export function assertPersonalAccountUserId(userId: string) {
|
|
26
|
+
if (
|
|
27
|
+
typeof userId !== "string" ||
|
|
28
|
+
!userId.length ||
|
|
29
|
+
userId.length > 256 ||
|
|
30
|
+
userId.trim() !== userId ||
|
|
31
|
+
/[\u0000-\u001f\u007f]/.test(userId)
|
|
32
|
+
)
|
|
33
|
+
throw new Error("Invalid personal account user selector");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Reusable in query or mutation transactions; no guessed first/newest account or provider writes. */
|
|
37
|
+
export async function resolvePersonalAccountMapping(
|
|
38
|
+
ctx: QueryCtx,
|
|
39
|
+
userId: string,
|
|
40
|
+
): Promise<PersonalAccountMapping> {
|
|
41
|
+
assertPersonalAccountUserId(userId);
|
|
42
|
+
const candidates = await ctx.db
|
|
43
|
+
.query("accounts")
|
|
44
|
+
.withIndex("by_user_id", (q) => q.eq("userId", userId))
|
|
45
|
+
.take(2);
|
|
46
|
+
if (!candidates.length) return { status: "unavailable", reason: "missing" };
|
|
47
|
+
if (candidates.length !== 1)
|
|
48
|
+
return { status: "unavailable", reason: "ambiguous" };
|
|
49
|
+
const account = candidates[0]!;
|
|
50
|
+
if (account.orgId !== undefined)
|
|
51
|
+
return { status: "unavailable", reason: "organization" };
|
|
52
|
+
if (
|
|
53
|
+
account.userId !== userId ||
|
|
54
|
+
account.stripeAccountId.length > 128 ||
|
|
55
|
+
!/^acct_[A-Za-z0-9_]+$/.test(account.stripeAccountId)
|
|
56
|
+
)
|
|
57
|
+
return { status: "unavailable", reason: "invalid_account" };
|
|
58
|
+
const reciprocal = await ctx.db
|
|
59
|
+
.query("accounts")
|
|
60
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
61
|
+
q.eq("stripeAccountId", account.stripeAccountId),
|
|
62
|
+
)
|
|
63
|
+
.take(2);
|
|
64
|
+
if (reciprocal.length !== 1 || reciprocal[0]?._id !== account._id)
|
|
65
|
+
return { status: "unavailable", reason: "ambiguous" };
|
|
66
|
+
const closures = await ctx.db
|
|
67
|
+
.query("accountClosures")
|
|
68
|
+
.withIndex("by_stripe_account_id", (q) =>
|
|
69
|
+
q.eq("stripeAccountId", account.stripeAccountId),
|
|
70
|
+
)
|
|
71
|
+
.take(2);
|
|
72
|
+
if (closures.length > 1)
|
|
73
|
+
return { status: "unavailable", reason: "ambiguous" };
|
|
74
|
+
const closure = closures[0];
|
|
75
|
+
if (closure)
|
|
76
|
+
return {
|
|
77
|
+
status: "unavailable",
|
|
78
|
+
reason: closure.state === "closed" ? "closed" : "closure_unresolved",
|
|
79
|
+
};
|
|
80
|
+
return { status: "unique", account };
|
|
81
|
+
}
|
|
@@ -2,6 +2,10 @@ import { v } from "convex/values";
|
|
|
2
2
|
|
|
3
3
|
import { query } from "../_generated/server";
|
|
4
4
|
import { accountDocValidator, onboardingStatusValidator } from "./validators";
|
|
5
|
+
import {
|
|
6
|
+
personalAccountMappingValidator,
|
|
7
|
+
resolvePersonalAccountMapping,
|
|
8
|
+
} from "./personalAccountMapping.js";
|
|
5
9
|
|
|
6
10
|
// =============================================================================
|
|
7
11
|
// ACCOUNT QUERIES
|
|
@@ -98,3 +102,10 @@ export const getStripeMode = query({
|
|
|
98
102
|
return secretKey?.startsWith("sk_test_") ? "test" : "live";
|
|
99
103
|
},
|
|
100
104
|
});
|
|
105
|
+
|
|
106
|
+
/** Unique personal mapping evidence only; the host still authenticates its caller. */
|
|
107
|
+
export const getPersonalAccountMapping = query({
|
|
108
|
+
args: { userId: v.string() },
|
|
109
|
+
returns: personalAccountMappingValidator,
|
|
110
|
+
handler: (ctx, { userId }) => resolvePersonalAccountMapping(ctx, userId),
|
|
111
|
+
});
|
|
@@ -108,10 +108,17 @@ describe("core — clearAllTables", () => {
|
|
|
108
108
|
eventType: "invoice.paid",
|
|
109
109
|
});
|
|
110
110
|
|
|
111
|
+
const seededCount = await t.run(async (ctx) => {
|
|
112
|
+
let count = 0;
|
|
113
|
+
for (const tableName of Object.keys(schema.tables) as TableNames[])
|
|
114
|
+
count += (await ctx.db.query(tableName).collect()).length;
|
|
115
|
+
return count;
|
|
116
|
+
});
|
|
117
|
+
expect(seededCount).toBeGreaterThan(0);
|
|
111
118
|
const result = await t.mutation(api.core.mutations.clearAllTables, {});
|
|
112
119
|
const tableCount = Object.keys(schema.tables).length;
|
|
113
120
|
|
|
114
|
-
expect(result.cleared).toBe(
|
|
121
|
+
expect(result.cleared).toBe(seededCount);
|
|
115
122
|
expect(result.tables).toHaveLength(tableCount);
|
|
116
123
|
|
|
117
124
|
for (const tableName of Object.keys(schema.tables) as TableNames[]) {
|