Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition on stripe subscription #9629

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ const testCases = [
{ loc: AppPath.PlanRequired, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.InviteTeam, res: AppPath.InviteTeam },
{ loc: AppPath.PlanRequired, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath },

{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: undefined, onboardingStatus: OnboardingStatus.PlanRequired, res: AppPath.PlanRequired },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Canceled, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Unpaid, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.PastDue, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: undefined, onboardingStatus: OnboardingStatus.PlanRequired, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Canceled, onboardingStatus: OnboardingStatus.Completed, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Unpaid, onboardingStatus: OnboardingStatus.Completed, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.PastDue, onboardingStatus: OnboardingStatus.Completed, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: false, subscriptionStatus: undefined, onboardingStatus: undefined, res: AppPath.SignInUp },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.WorkspaceActivation, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.ProfileCreation, res: AppPath.CreateProfile },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.SyncEmail, res: AppPath.SyncEmails },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.InviteTeam, res: AppPath.InviteTeam },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.ProfileCreation, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.SyncEmail, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.InviteTeam, res: undefined },
{ loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.Completed, res: undefined },

{ loc: AppPath.Index, isLoggedIn: true, subscriptionStatus: undefined, onboardingStatus: OnboardingStatus.PlanRequired, res: AppPath.PlanRequired },
{ loc: AppPath.Index, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Canceled, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const usePageChangeEffectNavigateLocation = () => {
isMatchingLocation(AppPath.Invite) ||
isMatchingLocation(AppPath.ResetPassword);

const isMatchingOpenSignedInRoute = isMatchingLocation(
martmull marked this conversation as resolved.
Show resolved Hide resolved
AppPath.PlanRequiredSuccess,
);

const isMatchingOngoingUserCreationRoute =
isMatchingOpenRoute ||
isMatchingLocation(AppPath.SignInUp) ||
Expand All @@ -29,13 +33,16 @@ export const usePageChangeEffectNavigateLocation = () => {
isMatchingLocation(AppPath.CreateProfile) ||
isMatchingLocation(AppPath.SyncEmails) ||
isMatchingLocation(AppPath.InviteTeam) ||
isMatchingLocation(AppPath.PlanRequired) ||
isMatchingLocation(AppPath.PlanRequiredSuccess);
isMatchingLocation(AppPath.PlanRequired);

if (isMatchingOpenRoute) {
return;
}

if (isLoggedIn && isMatchingOpenSignedInRoute) {
return;
martmull marked this conversation as resolved.
Show resolved Hide resolved
}

if (!isLoggedIn && !isMatchingOngoingUserCreationRoute) {
return AppPath.SignInUp;
}
Expand Down
28 changes: 25 additions & 3 deletions packages/twenty-front/src/pages/onboarding/PaymentSuccess.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import {
AnimatedEaseIn,
IconCheck,
isDefined,
MainButton,
RGBA,
UndecoratedLink,
Expand All @@ -12,8 +13,12 @@ import {
import { SubTitle } from '@/auth/components/SubTitle';
import { Title } from '@/auth/components/Title';
import { currentUserState } from '@/auth/states/currentUserState';
import {
OnboardingStatus,
useGetCurrentUserLazyQuery,
} from '~/generated/graphql';
import { useSubscriptionStatus } from '@/workspace/hooks/useSubscriptionStatus';
import { AppPath } from '@/types/AppPath';
import { OnboardingStatus } from '~/generated/graphql';

const StyledCheckContainer = styled.div`
align-items: center;
Expand All @@ -35,9 +40,26 @@ const StyledButtonContainer = styled.div`
export const PaymentSuccess = () => {
const theme = useTheme();
const currentUser = useRecoilValue(currentUserState);
const subscriptionStatus = useSubscriptionStatus();
const [getCurrentUser] = useGetCurrentUserLazyQuery();
const setCurrentUser = useSetRecoilState(currentUserState);
const color =
theme.name === 'light' ? theme.grayScale.gray90 : theme.grayScale.gray10;

const checkSubscriptionStatus = async () => {
if (!isDefined(subscriptionStatus)) {
const result = await getCurrentUser({ fetchPolicy: 'network-only' });
const currentUser = result.data?.currentUser;
if (isDefined(currentUser)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make a deep equal here (make it a useRecoilCallback and check the currentUser in the snapshot, this will avoid unnecessary re-renders)

setCurrentUser(currentUser);
}
}
};

const onClick = async () => {
martmull marked this conversation as resolved.
Show resolved Hide resolved
await checkSubscriptionStatus();
};

if (currentUser?.onboardingStatus === OnboardingStatus.Completed) {
return <></>;
}
Expand All @@ -52,7 +74,7 @@ export const PaymentSuccess = () => {
<Title>All set!</Title>
<SubTitle>Your account has been activated.</SubTitle>
<StyledButtonContainer>
<UndecoratedLink to={AppPath.CreateWorkspace}>
<UndecoratedLink onClick={onClick} to={AppPath.CreateWorkspace}>
<MainButton title="Start" width={200} />
</UndecoratedLink>
</StyledButtonContainer>
Expand Down
Loading