Skip to content
Merged
29 changes: 1 addition & 28 deletions src/components/BookTravelButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {isEmailPublicDomain} from '@libs/LoginUtils';
import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute';
import Navigation from '@libs/Navigation/Navigation';
import {openTravelDotLink} from '@libs/openTravelDotLink';
import {areTravelPersonalDetailsMissing} from '@libs/PersonalDetailsUtils';
import {getActivePolicies, getAdminsPrivateEmailDomains, hasAcceptedTravelTerms, isPaidGroupPolicy, isWorkspaceProvisionedForTravel} from '@libs/PolicyUtils';
import {getSearchParamFromPath} from '@libs/Url';

Expand All @@ -30,7 +29,7 @@ import type {ReactElement} from 'react';

import {emailSelector} from '@selectors/Session';
import {Str} from 'expensify-common';
import React, {useEffect, useRef, useState} from 'react';
import React, {useEffect, useState} from 'react';

import Button from './ButtonComposed';
import DotIndicatorMessage from './DotIndicatorMessage';
Expand Down Expand Up @@ -85,14 +84,10 @@ function BookTravelButton({
const {isBetaEnabled} = usePermissions();
const {showConfirmModal} = useConfirmModal();
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS);
const {login: currentUserLogin} = useCurrentUserPersonalDetails();
const activePolicies = getActivePolicies(policies, currentUserLogin);
const groupPaidPolicies = activePolicies.filter((activePolicy) => activePolicy.type !== CONST.POLICY.TYPE.PERSONAL && isPaidGroupPolicy(activePolicy));

// Ref to track if we should auto-resume the booking flow after returning from the missing-personal-details page
const shouldResumeBookingRef = useRef(false);

const navigateToPublicDomainError = () => {
const dynamicSuffix = getPolicyDynamicSuffix(DYNAMIC_ROUTES.TRAVEL_PUBLIC_DOMAIN_ERROR, activePolicyID);
Navigation.navigate(createDynamicRoute(dynamicSuffix));
Expand Down Expand Up @@ -138,16 +133,6 @@ function BookTravelButton({

const isPolicyProvisioned = isWorkspaceProvisionedForTravel(policy?.travelSettings);
const hasPolicyAcceptedTravelTerms = hasAcceptedTravelTerms(policy, travelSettings);
const willUseEnablementStepper = !hasPolicyAcceptedTravelTerms && (isPolicyProvisioned || isBetaEnabled(CONST.BETAS.IS_TRAVEL_VERIFIED));

// The enablement stepper collects a missing legal name as one of its own steps, so this pre-check (and its
// resume-after-filling behavior) only still applies to the outcomes that never reach the stepper: opening
// TravelDot on an already-enabled workspace, and the legacy request-access path below.
if (!willUseEnablementStepper && areTravelPersonalDetailsMissing(privatePersonalDetails)) {
shouldResumeBookingRef.current = true;
Navigation.navigate(ROUTES.WORKSPACE_TRAVEL_MISSING_PERSONAL_DETAILS.getRoute(policy?.id ?? String(CONST.DEFAULT_NUMBER_ID)));
return;
}

const adminDomains = getAdminsPrivateEmailDomains(policy);
if (adminDomains.length === 0) {
Expand Down Expand Up @@ -214,18 +199,6 @@ function BookTravelButton({
Navigation.navigate(enableTravelRoute);
};

// Auto-resume the booking flow after returning from the missing-personal-details page: when the user saves
// their legal name and navigates back, privatePersonalDetails updates and this effect re-triggers bookATrip()
useEffect(() => {
if (!shouldResumeBookingRef.current || areTravelPersonalDetailsMissing(privatePersonalDetails)) {
return;
}

shouldResumeBookingRef.current = false;
bookATrip();
// eslint-disable-next-line react-hooks/exhaustive-deps -- we only want to trigger this effect when privatePersonalDetails changes
}, [privatePersonalDetails]);

return (
<>
{!shouldRenderErrorMessageBelowButton && !!errorMessage && (
Expand Down
7 changes: 5 additions & 2 deletions src/components/Navigation/QuickCreationActionsBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import getCreateReportRoute, {getReportsRootRoute, navigateToCreateReportWorkspa
import Navigation from '@libs/Navigation/Navigation';
import {openTravelDotLink} from '@libs/openTravelDotLink';
import Permissions from '@libs/Permissions';
import {getDefaultChatEnabledPolicySelection, hasAcceptedTravelTerms, isPaidGroupPolicy} from '@libs/PolicyUtils';
import {getDefaultChatEnabledPolicySelection, hasAcceptedTravelTerms, isPaidGroupPolicy, isPolicyAccessible, isWorkspaceProvisionedForTravel} from '@libs/PolicyUtils';
import {generateReportID, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';

Expand Down Expand Up @@ -77,7 +77,10 @@ function QuickCreationActionsBar() {

const shouldShowEmptyReportConfirmationForDefaultChatEnabledPolicy = useShouldShowEmptyReportConfirmation(defaultChatEnabledPolicyID);

const travelEnabledPolicy = useMemo(() => Object.values(allPolicies ?? {}).find((policy) => !!policy?.isTravelEnabled), [allPolicies]);
const travelEnabledPolicy = useMemo(
() => Object.values(allPolicies ?? {}).find((policy) => isWorkspaceProvisionedForTravel(policy?.travelSettings) && isPolicyAccessible(policy, email ?? '')),
[allPolicies, email],
);

const shouldShowBookTravel = !!travelEnabledPolicy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import interceptAnonymousUser from '@libs/interceptAnonymousUser';
import Navigation from '@libs/Navigation/Navigation';
import {openTravelDotLink, shouldOpenTravelDotLinkWeb} from '@libs/openTravelDotLink';
import Permissions from '@libs/Permissions';
import {hasAcceptedTravelTerms, isPaidGroupPolicy} from '@libs/PolicyUtils';
import {hasAcceptedTravelTerms, isPaidGroupPolicy, isWorkspaceProvisionedForTravel} from '@libs/PolicyUtils';

import FABFocusableMenuItem from '@pages/inbox/sidebar/FABPopoverContent/FABFocusableMenuItem';

Expand All @@ -32,7 +32,7 @@ function TravelMenuItem() {
const [allBetas] = useOnyx(ONYXKEYS.BETAS);
const isBlockedFromSpotnanaTravel = Permissions.isBetaEnabled(CONST.BETAS.PREVENT_SPOTNANA_TRAVEL, allBetas);
const primaryContactMethod = primaryLogin ?? sessionEmail ?? '';
const isVisible = !!activePolicy?.isTravelEnabled;
const isVisible = isWorkspaceProvisionedForTravel(activePolicy?.travelSettings);

const isTravelEnabled =
!isBlockedFromSpotnanaTravel &&
Expand Down
14 changes: 12 additions & 2 deletions tests/ui/components/QuickCreationActionsBarTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('QuickCreationActionsBar - empty report confirmation', () => {
describe('QuickCreationActionsBar - travel', () => {
const TRAVEL_POLICY_ID = 'policy-travel-456';

const seedTravelWorkspaces = async (defaultPolicyID: string) => {
const seedTravelWorkspaces = async (defaultPolicyID: string, isTravelWorkspaceProvisioned = true) => {
await act(async () => {
await Onyx.merge(ONYXKEYS.SESSION, {accountID: CURRENT_USER_ACCOUNT_ID, email: CURRENT_USER_EMAIL});
await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: CURRENT_USER_EMAIL});
Expand All @@ -167,7 +167,9 @@ describe('QuickCreationActionsBar - travel', () => {
owner: CURRENT_USER_EMAIL,
outputCurrency: CONST.CURRENCY.USD,
isTravelEnabled: true,
travelSettings: {spotnanaCompanyID: 'spotnana-company-uuid', associatedTravelDomainAccountID: 'spotnana-entity-uuid', hasAcceptedTerms: true},
travelSettings: isTravelWorkspaceProvisioned
? {spotnanaCompanyID: 'spotnana-company-uuid', associatedTravelDomainAccountID: 'spotnana-entity-uuid', hasAcceptedTerms: true}
: undefined,
});
await Onyx.set(ONYXKEYS.NVP_ACTIVE_POLICY_ID, defaultPolicyID);
});
Expand Down Expand Up @@ -208,4 +210,12 @@ describe('QuickCreationActionsBar - travel', () => {

expect(openTravelDotLink).toHaveBeenCalledWith(TRAVEL_POLICY_ID);
});

it('does not show travel for a workspace that is not provisioned and has a stale enabled flag', async () => {
await seedTravelWorkspaces(TRAVEL_POLICY_ID, false);
renderComponent();
await waitForBatchedUpdatesWithAct();

expect(screen.queryByText(translateLocal('workspace.common.travel'))).toBeNull();
});
});
20 changes: 18 additions & 2 deletions tests/unit/components/BookTravelButtonTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ describe('BookTravelButton', () => {
expect(mockShowConfirmModal).not.toHaveBeenCalled();
});

it('does not collect a missing legal name outside the travel enablement flow', async () => {
await seedWorkspaces(travelEnabledPolicy, POLICY_ID);
await act(async () => {
await Onyx.set(ONYXKEYS.PRIVATE_PERSONAL_DETAILS, {});
await waitForBatchedUpdatesWithAct();
});
renderBookTravelButton();
await waitForBatchedUpdatesWithAct();

fireEvent.press(screen.getByText('Book a trip'));
await waitForBatchedUpdatesWithAct();

expect(openTravelDotLink).toHaveBeenCalledWith(POLICY_ID);
expect(Navigation.navigate).not.toHaveBeenCalledWith(expect.stringContaining('missing-personal-details'));
});

it('asks the user to switch defaults when the default workspace accepted travel terms but has travel switched off', async () => {
await seedWorkspaces(travelEnabledPolicy, DEFAULT_POLICY_ID, {...travelEnabledPolicy, id: DEFAULT_POLICY_ID, isTravelEnabled: false});
renderBookTravelButton();
Expand All @@ -237,7 +253,7 @@ describe('BookTravelButton', () => {
});

describe('when the user has a personal-email login', () => {
it('shows the public-domain error before the missing legal-name step even when legal details are missing', async () => {
it('shows the public-domain error even when legal details are missing', async () => {
// Given a user logged in with a public-domain email and no legal name set yet
await act(async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, provisionedPolicy);
Expand All @@ -251,7 +267,7 @@ describe('BookTravelButton', () => {
fireEvent.press(screen.getByText('Book a trip'));
await waitForBatchedUpdatesWithAct();

// Then they are routed to the public-domain error, not the missing legal-name page
// Then they are routed to the public-domain error without entering the workspace legal-name page
expect(Navigation.navigate).toHaveBeenCalledWith(expect.stringContaining('public-domain-error'));
expect(Navigation.navigate).not.toHaveBeenCalledWith(expect.stringContaining('missing-personal-details'));
});
Expand Down
Loading