Skip to content

Commit

Permalink
Update furever to reduce API calls (#204)
Browse files Browse the repository at this point in the history
* Update furever to reduce API calls

* formatting
  • Loading branch information
kushalshah-stripe authored Dec 3, 2024
1 parent 5f2e12e commit 8fd5923
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 39 deletions.
26 changes: 8 additions & 18 deletions app/api/account_session/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -37,21 +38,15 @@ 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) {
console.log(
`Using demo onboarding account: ${demoOnboardingAccount.id}`
);
stripeAccountId = demoOnboardingAccount.id;
demoAccount = demoOnboardingAccount;
} else {
console.log('No demo onboarding account found');
}
Expand All @@ -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' &&
Expand Down
2 changes: 1 addition & 1 deletion app/components/SubscriptionsBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const SubscriptionsBanner = () => {
fetchSubscription().then(({subscriptions}) => {
setShowBanner(subscriptions.length === 0);
});
}, []);
});

return (
<Banner open={showBanner && !withinBilling} variant="cool_gradient">
Expand Down
18 changes: 2 additions & 16 deletions lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stripe.V2.Core.Account, 'identity'>
) {
return !!account?.identity?.attestations?.terms_of_service?.account;
}

export function defaultCurrency(account?: Stripe.V2.Core.Account) {
export function defaultCurrency(
account?: Pick<Stripe.V2.Core.Account, 'identity' | 'defaults'>
) {
if (!account) {
return 'usd';
}
Expand Down
12 changes: 10 additions & 2 deletions types/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8fd5923

Please sign in to comment.