@@ -8,7 +8,8 @@ import Text from '@components/Text';
88import useHasTeam2025Pricing from '@hooks/useHasTeam2025Pricing' ;
99import useLocalize from '@hooks/useLocalize' ;
1010import useThemeStyles from '@hooks/useThemeStyles' ;
11- import { getOwnedPaidPolicies } from '@libs/PolicyUtils' ;
11+ import { upgradeToCorporate } from '@libs/actions/Policy/Policy' ;
12+ import { canModifyPlan , getOwnedPaidPolicies } from '@libs/PolicyUtils' ;
1213import Navigation from '@navigation/Navigation' ;
1314import { getCurrentUserAccountID } from '@userActions/Report' ;
1415import CONST from '@src/CONST' ;
@@ -39,11 +40,19 @@ function SubscriptionPlanCardActionButton({subscriptionPlan, isFromComparisonMod
3940 const { translate} = useLocalize ( ) ;
4041 const hasTeam2025Pricing = useHasTeam2025Pricing ( ) ;
4142 const currentUserAccountID = getCurrentUserAccountID ( ) ;
42- const [ policies ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY ) ;
43- const [ privateSubscription ] = useOnyx ( ONYXKEYS . NVP_PRIVATE_SUBSCRIPTION ) ;
43+ const [ policies ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY , { canBeMissing : false } ) ;
44+ const [ privateSubscription ] = useOnyx ( ONYXKEYS . NVP_PRIVATE_SUBSCRIPTION , { canBeMissing : false } ) ;
4445 const isAnnual = privateSubscription ?. type === CONST . SUBSCRIPTION . TYPE . ANNUAL ;
4546 const ownerPolicies = useMemo ( ( ) => getOwnedPaidPolicies ( policies , currentUserAccountID ) , [ policies , currentUserAccountID ] ) ;
4647
48+ const [ canPerformUpgrade , policy ] = useMemo ( ( ) => {
49+ const firstPolicy = ownerPolicies . at ( 0 ) ;
50+ if ( ! firstPolicy || ownerPolicies . length > 1 ) {
51+ return [ false , undefined ] ;
52+ }
53+ return [ canModifyPlan ( firstPolicy . id ) , firstPolicy ] ;
54+ } , [ ownerPolicies ] ) ;
55+
4756 const handlePlanPress = ( planType : PersonalPolicyTypeExcludedProps ) => {
4857 closeComparisonModal ?.( ) ;
4958
@@ -52,15 +61,18 @@ function SubscriptionPlanCardActionButton({subscriptionPlan, isFromComparisonMod
5261 return ;
5362 }
5463
55- const ownerPolicy = ownerPolicies . length === 1 ? ownerPolicies . at ( 0 ) ?. id : undefined ;
56-
5764 if ( planType === CONST . POLICY . TYPE . TEAM ) {
58- Navigation . navigate ( ROUTES . WORKSPACE_DOWNGRADE . getRoute ( ownerPolicy , Navigation . getActiveRoute ( ) ) ) ;
65+ Navigation . navigate ( ROUTES . WORKSPACE_DOWNGRADE . getRoute ( policy ?. id , Navigation . getActiveRoute ( ) ) ) ;
5966 return ;
6067 }
6168
6269 if ( planType === CONST . POLICY . TYPE . CORPORATE ) {
63- Navigation . navigate ( ROUTES . WORKSPACE_UPGRADE . getRoute ( ownerPolicy , undefined , Navigation . getActiveRoute ( ) ) ) ;
70+ if ( canPerformUpgrade && ! ! policy ?. id ) {
71+ upgradeToCorporate ( policy . id ) ;
72+ closeComparisonModal ?.( ) ;
73+ return ;
74+ }
75+ Navigation . navigate ( ROUTES . WORKSPACE_UPGRADE . getRoute ( policy ?. id , undefined , Navigation . getActiveRoute ( ) ) ) ;
6476 }
6577 } ;
6678
0 commit comments