diff --git a/src/libs/TravelInvoicingUtils.ts b/src/libs/TravelInvoicingUtils.ts index 33ba5b192a69..2ea48200e086 100644 --- a/src/libs/TravelInvoicingUtils.ts +++ b/src/libs/TravelInvoicingUtils.ts @@ -2,7 +2,7 @@ import type {OnyxEntry} from 'react-native-onyx'; import type {LocalizedTranslate} from '@components/LocaleContextProvider'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {BankAccountList, Card, WorkspaceCardsList} from '@src/types/onyx'; +import type {BankAccountList, Card, CardList} from '@src/types/onyx'; import type {ExpensifyCardSettingsBase} from '@src/types/onyx/ExpensifyCardSettings'; import addEncryptedAuthTokenToURL from './addEncryptedAuthTokenToURL'; import {getLastFourDigits} from './BankAccountUtils'; @@ -165,17 +165,12 @@ function downloadTravelInvoiceStatementPDF( * Gets the user's Travel Invoicing card from the card list. * Returns the first card with feedCountry set to PROGRAM_TRAVEL_US. */ -function getTravelInvoicingCard(cardList: Record | undefined) { +function getTravelInvoicingCard(cardList: OnyxEntry) { if (!cardList) { return undefined; } - // Flatten all WorkspaceCardsList into a single array of Cards - // Filter out cardList entries (which are string values) to only get actual Card objects - const allCards = Object.values(cardList) - .filter((workspaceCards): workspaceCards is WorkspaceCardsList => !!workspaceCards) - .flatMap((workspaceCards) => Object.values(workspaceCards)) - .filter((card): card is Card => typeof card !== 'string' && typeof card?.cardID === 'number'); + const allCards = Object.values(cardList).filter((card): card is Card => !!card && typeof card?.cardID === 'number'); const travelCard = allCards.find((card) => card.nameValuePairs?.feedCountry === CONST.TRAVEL.PROGRAM_TRAVEL_US); // If no travel card is found and testing is enabled, return the first available card if (!travelCard && isTravelCVVTestingEnabled()) { @@ -189,7 +184,7 @@ function getTravelInvoicingCard(cardList: Record | undefined): boolean { +function isTravelCVVEligible(isTravelInvoicingBetaEnabled: boolean, cardList: OnyxEntry): boolean { const hasTravelCard = !!getTravelInvoicingCard(cardList); return isTravelInvoicingBetaEnabled && hasTravelCard; } diff --git a/src/pages/settings/Wallet/PaymentMethodList.tsx b/src/pages/settings/Wallet/PaymentMethodList.tsx index 9211014702e4..28a0b680ac30 100644 --- a/src/pages/settings/Wallet/PaymentMethodList.tsx +++ b/src/pages/settings/Wallet/PaymentMethodList.tsx @@ -175,8 +175,6 @@ function PaymentMethodList({ const expensifyIcons = useMemoizedLazyExpensifyIcons(['Plus', 'ThreeDots', 'LuggageWithLines'] as const); const illustrations = useThemeIllustrations(); const companyCardFeedIcons = useCompanyCardFeedIcons(); - const [workspaceCardList] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST); - const [isUserValidated] = useOnyx(ONYXKEYS.ACCOUNT, { selector: isUserValidatedSelector, }); @@ -382,8 +380,8 @@ function PaymentMethodList({ } const travelCardGrouped: PaymentMethodItem[] = []; - const travelCard = getTravelInvoicingCard(workspaceCardList); - if (isTravelCVVEligible(isBetaEnabled(CONST.BETAS.TRAVEL_INVOICING), workspaceCardList) && travelCard) { + const travelCard = getTravelInvoicingCard(cardList); + if (isTravelCVVEligible(isBetaEnabled(CONST.BETAS.TRAVEL_INVOICING), cardList) && travelCard) { travelCardGrouped.push({ title: translate('walletPage.travelCVV.title'), description: translate('walletPage.travelCVV.subtitle'), @@ -478,7 +476,6 @@ function PaymentMethodList({ filterCurrency, isLoadingCardList, cardList, - workspaceCardList, isBetaEnabled, onPress, policiesForAssignedCards, diff --git a/src/pages/settings/Wallet/TravelCVVPage/TravelCVVPage.tsx b/src/pages/settings/Wallet/TravelCVVPage/TravelCVVPage.tsx index 43c30f644b6e..827052a7a290 100644 --- a/src/pages/settings/Wallet/TravelCVVPage/TravelCVVPage.tsx +++ b/src/pages/settings/Wallet/TravelCVVPage/TravelCVVPage.tsx @@ -36,7 +36,7 @@ function TravelCVVPage() { const [account] = useOnyx(ONYXKEYS.ACCOUNT); const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS); - const [cardList] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST); + const [cardList] = useOnyx(ONYXKEYS.CARD_LIST); const {isAccountLocked} = useLockedAccountState(); const {showLockedAccountModal} = useLockedAccountActions(); diff --git a/src/pages/settings/Wallet/TravelCVVPage/TravelCVVVerifyAccountPage.tsx b/src/pages/settings/Wallet/TravelCVVPage/TravelCVVVerifyAccountPage.tsx index 12bdaf1d0e74..695551c73a1c 100644 --- a/src/pages/settings/Wallet/TravelCVVPage/TravelCVVVerifyAccountPage.tsx +++ b/src/pages/settings/Wallet/TravelCVVPage/TravelCVVVerifyAccountPage.tsx @@ -19,7 +19,7 @@ import {useTravelCVVActions, useTravelCVVState} from './TravelCVVContextProvider function TravelCVVVerifyAccountPage() { const {translate} = useLocalize(); const [account] = useOnyx(ONYXKEYS.ACCOUNT); - const [cardList] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST); + const [cardList] = useOnyx(ONYXKEYS.CARD_LIST); const {isLoading, validateError} = useTravelCVVState(); const {setCvv, setIsLoading, setValidateError} = useTravelCVVActions(); diff --git a/src/pages/settings/Wallet/TravelCVVPage/WalletTravelCVVSection.tsx b/src/pages/settings/Wallet/TravelCVVPage/WalletTravelCVVSection.tsx index 98da7bb153f6..97e4868e3b80 100644 --- a/src/pages/settings/Wallet/TravelCVVPage/WalletTravelCVVSection.tsx +++ b/src/pages/settings/Wallet/TravelCVVPage/WalletTravelCVVSection.tsx @@ -23,7 +23,7 @@ function WalletTravelCVVSection() { const icons = useMemoizedLazyExpensifyIcons(['LuggageWithLines']); const {isBetaEnabled} = usePermissions(); - const [cardList] = useOnyx(ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST); + const [cardList] = useOnyx(ONYXKEYS.CARD_LIST); const travelCard = getTravelInvoicingCard(cardList); diff --git a/tests/unit/TravelInvoicingUtilsTest.ts b/tests/unit/TravelInvoicingUtilsTest.ts index b2668035b159..4042cd9c35ae 100644 --- a/tests/unit/TravelInvoicingUtilsTest.ts +++ b/tests/unit/TravelInvoicingUtilsTest.ts @@ -12,7 +12,7 @@ import { hasTravelInvoicingSettlementAccount, isTravelCVVEligible, } from '@src/libs/TravelInvoicingUtils'; -import type {BankAccountList, WorkspaceCardsList} from '@src/types/onyx'; +import type {BankAccountList, CardList} from '@src/types/onyx'; import type {ExpensifyCardSettingsBase} from '@src/types/onyx/ExpensifyCardSettings'; jest.mock('@src/libs/Environment/Environment', () => ({ @@ -242,36 +242,32 @@ describe('TravelInvoicingUtils', () => { it('Should return undefined when no travel card exists', () => { const cardList = { - workspaceCards: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '1234': { - cardID: 1234, - state: 3, - nameValuePairs: { - isVirtual: true, - feedCountry: 'OTHER_COUNTRY', - }, + // eslint-disable-next-line @typescript-eslint/naming-convention + '1234': { + cardID: 1234, + state: 3, + nameValuePairs: { + isVirtual: true, + feedCountry: 'OTHER_COUNTRY', }, }, - } as unknown as Record; + } as unknown as CardList; const result = getTravelInvoicingCard(cardList); expect(result).toBeUndefined(); }); it('Should return the travel card when feedCountry is PROGRAM_TRAVEL_US', () => { const cardList = { - workspaceCards: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '1234': { - cardID: 1234, - state: 3, - nameValuePairs: { - isVirtual: true, - feedCountry: CONST.TRAVEL.PROGRAM_TRAVEL_US, - }, + // eslint-disable-next-line @typescript-eslint/naming-convention + '1234': { + cardID: 1234, + state: 3, + nameValuePairs: { + isVirtual: true, + feedCountry: CONST.TRAVEL.PROGRAM_TRAVEL_US, }, }, - } as unknown as Record; + } as unknown as CardList; const result = getTravelInvoicingCard(cardList); expect(result).toBeDefined(); expect(result?.cardID).toBe(1234); @@ -279,27 +275,25 @@ describe('TravelInvoicingUtils', () => { it('Should return first travel card when multiple cards exist', () => { const cardList = { - workspaceCards: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '1111': { - cardID: 1111, - state: 3, - nameValuePairs: { - isVirtual: true, - feedCountry: 'OTHER_COUNTRY', - }, + // eslint-disable-next-line @typescript-eslint/naming-convention + '1111': { + cardID: 1111, + state: 3, + nameValuePairs: { + isVirtual: true, + feedCountry: 'OTHER_COUNTRY', }, - // eslint-disable-next-line @typescript-eslint/naming-convention - '2222': { - cardID: 2222, - state: 3, - nameValuePairs: { - isVirtual: true, - feedCountry: CONST.TRAVEL.PROGRAM_TRAVEL_US, - }, + }, + // eslint-disable-next-line @typescript-eslint/naming-convention + '2222': { + cardID: 2222, + state: 3, + nameValuePairs: { + isVirtual: true, + feedCountry: CONST.TRAVEL.PROGRAM_TRAVEL_US, }, }, - } as unknown as Record; + } as unknown as CardList; const result = getTravelInvoicingCard(cardList); expect(result).toBeDefined(); expect(result?.nameValuePairs?.feedCountry).toBe(CONST.TRAVEL.PROGRAM_TRAVEL_US); @@ -308,19 +302,17 @@ describe('TravelInvoicingUtils', () => { isDevelopmentSpy.mockReturnValue(true); const cardList = { - workspaceCards: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '9999': { - cardID: 9999, - state: 3, - bank: 'Expensify Card', - nameValuePairs: { - isVirtual: true, - feedCountry: 'OTHER_COUNTRY', - }, + // eslint-disable-next-line @typescript-eslint/naming-convention + '9999': { + cardID: 9999, + state: 3, + bank: 'Expensify Card', + nameValuePairs: { + isVirtual: true, + feedCountry: 'OTHER_COUNTRY', }, }, - } as unknown as Record; + } as unknown as CardList; const result = getTravelInvoicingCard(cardList); expect(result).toBeDefined(); @@ -330,33 +322,29 @@ describe('TravelInvoicingUtils', () => { describe('isTravelCVVEligible', () => { const mockTravelCardList = { - workspaceCards: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '1234': { - cardID: 1234, - state: 3, - nameValuePairs: { - isVirtual: true, - feedCountry: CONST.TRAVEL.PROGRAM_TRAVEL_US, - }, + // eslint-disable-next-line @typescript-eslint/naming-convention + '1234': { + cardID: 1234, + state: 3, + nameValuePairs: { + isVirtual: true, + feedCountry: CONST.TRAVEL.PROGRAM_TRAVEL_US, }, }, - } as unknown as Record; + } as unknown as CardList; const mockNonTravelCardList = { - workspaceCards: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '5678': { - cardID: 5678, - state: 3, - bank: 'Expensify Card', - nameValuePairs: { - isVirtual: true, - feedCountry: 'OTHER_COUNTRY', - }, + // eslint-disable-next-line @typescript-eslint/naming-convention + '5678': { + cardID: 5678, + state: 3, + bank: 'Expensify Card', + nameValuePairs: { + isVirtual: true, + feedCountry: 'OTHER_COUNTRY', }, }, - } as unknown as Record; + } as unknown as CardList; it('Should return false when beta is false', () => { const result = isTravelCVVEligible(false, mockTravelCardList);