Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 4 additions & 9 deletions src/libs/TravelInvoicingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, WorkspaceCardsList | undefined> | undefined) {
function getTravelInvoicingCard(cardList: OnyxEntry<CardList>) {
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()) {
Expand All @@ -189,7 +184,7 @@ function getTravelInvoicingCard(cardList: Record<string, WorkspaceCardsList | un
* Checks if user is eligible to see Travel CVV in Wallet.
* Requires: TRAVEL_INVOICING beta AND having a travel card.
*/
function isTravelCVVEligible(isTravelInvoicingBetaEnabled: boolean, cardList: Record<string, WorkspaceCardsList | undefined> | undefined): boolean {
function isTravelCVVEligible(isTravelInvoicingBetaEnabled: boolean, cardList: OnyxEntry<CardList>): boolean {
const hasTravelCard = !!getTravelInvoicingCard(cardList);
return isTravelInvoicingBetaEnabled && hasTravelCard;
}
Expand Down
7 changes: 2 additions & 5 deletions src/pages/settings/Wallet/PaymentMethodList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -478,7 +476,6 @@ function PaymentMethodList({
filterCurrency,
isLoadingCardList,
cardList,
workspaceCardList,
isBetaEnabled,
onPress,
policiesForAssignedCards,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
130 changes: 59 additions & 71 deletions tests/unit/TravelInvoicingUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand Down Expand Up @@ -242,64 +242,58 @@ 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<string, WorkspaceCardsList>;
} 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<string, WorkspaceCardsList>;
} as unknown as CardList;
const result = getTravelInvoicingCard(cardList);
expect(result).toBeDefined();
expect(result?.cardID).toBe(1234);
});

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<string, WorkspaceCardsList>;
} as unknown as CardList;
const result = getTravelInvoicingCard(cardList);
expect(result).toBeDefined();
expect(result?.nameValuePairs?.feedCountry).toBe(CONST.TRAVEL.PROGRAM_TRAVEL_US);
Expand All @@ -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<string, WorkspaceCardsList>;
} as unknown as CardList;

const result = getTravelInvoicingCard(cardList);
expect(result).toBeDefined();
Expand All @@ -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<string, WorkspaceCardsList>;
} 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<string, WorkspaceCardsList>;
} as unknown as CardList;

it('Should return false when beta is false', () => {
const result = isTravelCVVEligible(false, mockTravelCardList);
Expand Down
Loading