From 8fd59230d688be659b26d75d594083aa4d645679 Mon Sep 17 00:00:00 2001 From: kushalshah-stripe <109370037+kushalshah-stripe@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:21:03 -0800 Subject: [PATCH] Update furever to reduce API calls (#204) * Update furever to reduce API calls * formatting --- app/api/account_session/route.ts | 26 ++++++++------------------ app/components/SubscriptionsBanner.tsx | 2 +- lib/auth.ts | 18 ++---------------- lib/utils.ts | 8 ++++++-- types/next-auth.d.ts | 12 ++++++++++-- 5 files changed, 27 insertions(+), 39 deletions(-) diff --git a/app/api/account_session/route.ts b/app/api/account_session/route.ts index b24c4691..f02c310b 100644 --- a/app/api/account_session/route.ts +++ b/app/api/account_session/route.ts @@ -13,6 +13,7 @@ export async function POST(req: NextRequest) { let stripeAccountId = session?.user?.stripeAccount?.id; + let demoAccount = undefined; if (demoOnboarding !== undefined) { const accountId: string = (() => { switch (locale) { @@ -37,14 +38,7 @@ export async function POST(req: NextRequest) { const demoOnboardingAccount = await stripe.v2.core.accounts.retrieve( accountId, { - include: [ - 'configuration.customer', - 'configuration.merchant', - 'configuration.recipient', - 'defaults', - 'identity', - 'requirements', - ], + include: ['defaults', 'identity'], } ); if (demoOnboardingAccount) { @@ -52,6 +46,7 @@ export async function POST(req: NextRequest) { `Using demo onboarding account: ${demoOnboardingAccount.id}` ); stripeAccountId = demoOnboardingAccount.id; + demoAccount = demoOnboardingAccount; } else { console.log('No demo onboarding account found'); } @@ -66,16 +61,11 @@ export async function POST(req: NextRequest) { ); } - const account = await stripe.v2.core.accounts.retrieve(stripeAccountId, { - include: [ - 'configuration.customer', - 'configuration.merchant', - 'configuration.recipient', - 'defaults', - 'identity', - 'requirements', - ], - }); + const account = + demoAccount ?? + (await stripe.v2.core.accounts.retrieve(stripeAccountId, { + include: ['defaults', 'identity'], + })); const isCustom = account?.dashboard === 'none' && diff --git a/app/components/SubscriptionsBanner.tsx b/app/components/SubscriptionsBanner.tsx index de7fd703..d2e2292e 100644 --- a/app/components/SubscriptionsBanner.tsx +++ b/app/components/SubscriptionsBanner.tsx @@ -26,7 +26,7 @@ export const SubscriptionsBanner = () => { fetchSubscription().then(({subscriptions}) => { setShowBanner(subscriptions.length === 0); }); - }, []); + }); return ( diff --git a/lib/auth.ts b/lib/auth.ts index 03a2f1a2..549cdcbb 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -65,14 +65,7 @@ export const authOptions: AuthOptions = { stripeAccount = await stripe.v2.core.accounts.retrieve( salon.stripeAccountId, { - include: [ - 'configuration.customer', - 'configuration.merchant', - 'configuration.recipient', - 'defaults', - 'identity', - 'requirements', - ], + include: ['defaults', 'identity'], } ); } catch (err) { @@ -167,14 +160,7 @@ export const authOptions: AuthOptions = { const stripeAccount = await stripe.v2.core.accounts.retrieve( stripeAccountId, { - include: [ - 'configuration.customer', - 'configuration.merchant', - 'configuration.recipient', - 'defaults', - 'identity', - 'requirements', - ], + include: ['defaults', 'identity'], } ); if (stripeAccount?.contact_email) { diff --git a/lib/utils.ts b/lib/utils.ts index a5c3e090..b3a6a311 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -37,11 +37,15 @@ export function resolveCountryParam( return country.toLowerCase() as Stripe.V2.Core.AccountCreateParams.Identity.Country; } -export function accountDetailsSubmitted(account?: Stripe.V2.Core.Account) { +export function accountDetailsSubmitted( + account?: Pick +) { return !!account?.identity?.attestations?.terms_of_service?.account; } -export function defaultCurrency(account?: Stripe.V2.Core.Account) { +export function defaultCurrency( + account?: Pick +) { if (!account) { return 'usd'; } diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts index 3bb08d74..a315db82 100644 --- a/types/next-auth.d.ts +++ b/types/next-auth.d.ts @@ -7,8 +7,16 @@ declare module 'next-auth' { */ interface Session { user: { - /** The user's Stripe account. */ - stripeAccount: Stripe.V2.Core.Account; + /** The user's Stripe account. + * We are intentionally not using the `Stripe.V2.Core.Account` type here so we can limit + * the amount of data we need to fetch each time we get the session. Eventually we want + * all components and pages to retrieve the user's Stripe account on their own and only store + * the `id` in the session. + */ + stripeAccount: Pick< + Stripe.V2.Core.Account, + 'id' | 'identity' | 'defaults' + >; businessName?: string | null; password?: string | null; setup?: boolean;