@fantastic.dev/better-stripe 0.48.0 → 0.48.1

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/subscriptions.d.ts +2 -3
  4. package/dist/client/billing/subscriptions.d.ts.map +1 -1
  5. package/dist/client/billing/subscriptions.js.map +1 -1
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
  8. package/dist/client/core/accounts.d.ts +2 -3
  9. package/dist/client/core/accounts.d.ts.map +1 -1
  10. package/dist/client/core/accounts.js.map +1 -1
  11. package/dist/client/index.d.ts +3 -6
  12. package/dist/client/index.d.ts.map +1 -1
  13. package/dist/client/index.js.map +1 -1
  14. package/dist/client/originalIdReadOptions.d.ts +10 -0
  15. package/dist/client/originalIdReadOptions.d.ts.map +1 -0
  16. package/dist/client/originalIdReadOptions.js +2 -0
  17. package/dist/client/originalIdReadOptions.js.map +1 -0
  18. package/dist/client/publicTypes.d.ts +1 -0
  19. package/dist/client/publicTypes.d.ts.map +1 -1
  20. package/dist/component/_generated/component.d.ts +2 -0
  21. package/dist/component/_generated/component.d.ts.map +1 -1
  22. package/dist/component/balanceFunding/mutations.d.ts +16 -16
  23. package/dist/component/billing/queries.d.ts +1 -0
  24. package/dist/component/billing/queries.d.ts.map +1 -1
  25. package/dist/component/billing/queries.js +10 -4
  26. package/dist/component/billing/queries.js.map +1 -1
  27. package/dist/component/core/queries.d.ts +1 -0
  28. package/dist/component/core/queries.d.ts.map +1 -1
  29. package/dist/component/core/queries.js +7 -4
  30. package/dist/component/core/queries.js.map +1 -1
  31. package/dist/component/refundHistory/queries.d.ts +1 -1
  32. package/docs/personal-account-mapping.md +30 -0
  33. package/package.json +1 -1
  34. package/src/client/billing/subscriptions.ts +2 -1
  35. package/src/client/core/accounts.ts +2 -1
  36. package/src/client/index.ts +6 -2
  37. package/src/client/originalIdReadOptions.test.ts +63 -0
  38. package/src/client/originalIdReadOptions.ts +10 -0
  39. package/src/client/publicTypes.ts +5 -0
  40. package/src/component/_generated/component.ts +2 -2
  41. package/src/component/billing/queries.ts +9 -4
  42. package/src/component/core/original-id-unique.test.ts +209 -0
  43. package/src/component/core/queries.ts +6 -4
@@ -0,0 +1,209 @@
1
+ // @vitest-environment edge-runtime
2
+ import { convexTest } from "convex-test";
3
+ import { expect, it } from "vitest";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+
7
+ async function fixture(duplicate = false) {
8
+ const t = convexTest(schema, modules);
9
+ const ids = await t.run(async (ctx) => {
10
+ const account = {
11
+ stripeAccountId: "acct_original_history",
12
+ userId: "original_buyer",
13
+ onboardingStatus: "complete" as const,
14
+ };
15
+ const subscription = {
16
+ stripeSubscriptionId: "sub_original_history",
17
+ userId: "original_buyer",
18
+ accountId: account.stripeAccountId,
19
+ status: "canceled" as const,
20
+ isTrialing: false,
21
+ cancelAtPeriodEnd: false,
22
+ };
23
+ const accountId = await ctx.db.insert("accounts", account);
24
+ const subscriptionId = await ctx.db.insert("subscriptions", subscription);
25
+ if (duplicate) {
26
+ await ctx.db.insert("accounts", {
27
+ ...account,
28
+ userId: "foreign_buyer",
29
+ });
30
+ await ctx.db.insert("subscriptions", {
31
+ ...subscription,
32
+ userId: "foreign_buyer",
33
+ status: "active",
34
+ });
35
+ }
36
+ return { accountId, subscriptionId };
37
+ });
38
+ return { t, ...ids };
39
+ }
40
+
41
+ it("retains existing unique original account and historical subscription reads", async () => {
42
+ const f = await fixture();
43
+ expect(
44
+ await f.t.query(api.core.queries.getAccountByStripeId, {
45
+ stripeAccountId: "acct_original_history",
46
+ }),
47
+ ).toMatchObject({ _id: f.accountId, userId: "original_buyer" });
48
+ expect(
49
+ await f.t.query(api.billing.queries.getSubscriptionByStripeId, {
50
+ stripeSubscriptionId: "sub_original_history",
51
+ }),
52
+ ).toMatchObject({ _id: f.subscriptionId, status: "canceled" });
53
+ });
54
+
55
+ it("preserves legacy first-row defaults when duplicate original provider IDs exist", async () => {
56
+ const f = await fixture(true);
57
+ expect(
58
+ await f.t.query(api.core.queries.getAccountByStripeId, {
59
+ stripeAccountId: "acct_original_history",
60
+ }),
61
+ ).toMatchObject({ _id: f.accountId });
62
+ expect(
63
+ await f.t.query(api.billing.queries.getSubscriptionByStripeId, {
64
+ stripeSubscriptionId: "sub_original_history",
65
+ }),
66
+ ).toMatchObject({ _id: f.subscriptionId });
67
+ });
68
+
69
+ it("returns null instead of selecting the first duplicate original Stripe account ID when uniqueness is required", async () => {
70
+ const f = await fixture(true);
71
+ const args = {
72
+ stripeAccountId: "acct_original_history",
73
+ requireUnique: true,
74
+ };
75
+ await expect(
76
+ f.t.query(api.core.queries.getAccountByStripeId, args),
77
+ ).resolves.toBeNull();
78
+ });
79
+
80
+ it("returns null instead of selecting the first duplicate original Stripe subscription ID when uniqueness is required", async () => {
81
+ const f = await fixture(true);
82
+ const args = {
83
+ stripeSubscriptionId: "sub_original_history",
84
+ requireUnique: true,
85
+ };
86
+ await expect(
87
+ f.t.query(api.billing.queries.getSubscriptionByStripeId, args),
88
+ ).resolves.toBeNull();
89
+ });
90
+
91
+ it("returns null for absent original IDs when uniqueness is required", async () => {
92
+ const f = await fixture();
93
+ expect(
94
+ await f.t.query(api.core.queries.getAccountByStripeId, {
95
+ stripeAccountId: "acct_missing",
96
+ requireUnique: true,
97
+ }),
98
+ ).toBeNull();
99
+ expect(
100
+ await f.t.query(api.billing.queries.getSubscriptionByStripeId, {
101
+ stripeSubscriptionId: "sub_missing",
102
+ requireUnique: true,
103
+ }),
104
+ ).toBeNull();
105
+ });
106
+
107
+ it("returns the unchanged sole original documents when uniqueness is required", async () => {
108
+ const f = await fixture();
109
+ const account = await f.t.query(api.core.queries.getAccount, {
110
+ accountId: f.accountId,
111
+ });
112
+ const subscription = await f.t.query(api.billing.queries.getSubscription, {
113
+ subscriptionId: f.subscriptionId,
114
+ });
115
+ expect(
116
+ await f.t.query(api.core.queries.getAccountByStripeId, {
117
+ stripeAccountId: "acct_original_history",
118
+ requireUnique: true,
119
+ }),
120
+ ).toEqual(account);
121
+ expect(
122
+ await f.t.query(api.billing.queries.getSubscriptionByStripeId, {
123
+ stripeSubscriptionId: "sub_original_history",
124
+ requireUnique: true,
125
+ }),
126
+ ).toEqual(subscription);
127
+ });
128
+
129
+ it("preserves explicit false legacy first-row behavior for duplicate IDs", async () => {
130
+ const f = await fixture(true);
131
+ expect(
132
+ await f.t.query(api.core.queries.getAccountByStripeId, {
133
+ stripeAccountId: "acct_original_history",
134
+ requireUnique: false,
135
+ }),
136
+ ).toMatchObject({ _id: f.accountId });
137
+ expect(
138
+ await f.t.query(api.billing.queries.getSubscriptionByStripeId, {
139
+ stripeSubscriptionId: "sub_original_history",
140
+ requireUnique: false,
141
+ }),
142
+ ).toMatchObject({ _id: f.subscriptionId });
143
+ });
144
+
145
+ it("holds global duplicates even when only one belongs to the original buyer or organization", async () => {
146
+ const f = await fixture();
147
+ await f.t.run(async (ctx) => {
148
+ await ctx.db.insert("accounts", {
149
+ stripeAccountId: "acct_original_history",
150
+ orgId: "foreign_org",
151
+ userId: "foreign_org_buyer",
152
+ onboardingStatus: "pending",
153
+ });
154
+ await ctx.db.insert("subscriptions", {
155
+ stripeSubscriptionId: "sub_original_history",
156
+ orgId: "foreign_org",
157
+ userId: "foreign_org_buyer",
158
+ accountId: "acct_foreign",
159
+ status: "incomplete",
160
+ isTrialing: false,
161
+ cancelAtPeriodEnd: false,
162
+ });
163
+ });
164
+ expect(
165
+ await f.t.query(api.core.queries.getAccountByStripeId, {
166
+ stripeAccountId: "acct_original_history",
167
+ requireUnique: true,
168
+ }),
169
+ ).toBeNull();
170
+ expect(
171
+ await f.t.query(api.billing.queries.getSubscriptionByStripeId, {
172
+ stripeSubscriptionId: "sub_original_history",
173
+ requireUnique: true,
174
+ }),
175
+ ).toBeNull();
176
+ });
177
+
178
+ it("retains a unique closed original account instead of substituting its current replacement", async () => {
179
+ const f = await fixture();
180
+ await f.t.run(async (ctx) => {
181
+ await ctx.db.insert("accountClosures", {
182
+ stripeAccountId: "acct_original_history",
183
+ userId: "original_buyer",
184
+ appliedConfigurations: [],
185
+ state: "closed",
186
+ closedAt: 1,
187
+ });
188
+ await ctx.db.insert("accounts", {
189
+ stripeAccountId: "acct_replacement",
190
+ userId: "original_buyer",
191
+ onboardingStatus: "complete",
192
+ });
193
+ });
194
+ expect(
195
+ await f.t.query(api.core.queries.getAccountByStripeId, {
196
+ stripeAccountId: "acct_original_history",
197
+ requireUnique: true,
198
+ }),
199
+ ).toMatchObject({
200
+ _id: f.accountId,
201
+ stripeAccountId: "acct_original_history",
202
+ });
203
+ expect(
204
+ await f.t.query(api.billing.queries.getSubscriptionByStripeId, {
205
+ stripeSubscriptionId: "sub_original_history",
206
+ requireUnique: true,
207
+ }),
208
+ ).toMatchObject({ _id: f.subscriptionId, status: "canceled" });
209
+ });
@@ -20,15 +20,17 @@ export const getAccount = query({
20
20
  });
21
21
 
22
22
  export const getAccountByStripeId = query({
23
- args: { stripeAccountId: v.string() },
23
+ args: { stripeAccountId: v.string(), requireUnique: v.optional(v.boolean()) },
24
24
  returns: v.union(accountDocValidator, v.null()),
25
25
  handler: async (ctx, args) => {
26
- return await ctx.db
26
+ const rows = ctx.db
27
27
  .query("accounts")
28
28
  .withIndex("by_stripe_account_id", (q) =>
29
29
  q.eq("stripeAccountId", args.stripeAccountId),
30
- )
31
- .first();
30
+ );
31
+ if (!args.requireUnique) return await rows.first();
32
+ const matches = await rows.take(2);
33
+ return matches.length === 1 ? (matches[0] ?? null) : null;
32
34
  },
33
35
  });
34
36