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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"test:debug": "TZ=utc NODE_OPTIONS='--inspect-brk --experimental-vm-modules' jest --runInBand",
"perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure",
"typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc",
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=292 --cache --cache-location=node_modules/.cache/eslint",
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=291 --cache --cache-location=node_modules/.cache/eslint",
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh",
"lint-watch": "npx eslint-watch --watch --changed",
"shellcheck": "./scripts/shellCheck.sh",
Expand Down
2 changes: 1 addition & 1 deletion src/components/EReceipt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function EReceipt({transactionID, transactionItem, isThumbnail = false}: EReceip
const formattedAmount = convertToDisplayString(transactionAmount, transactionCurrency);
const currency = getCurrencySymbol(transactionCurrency ?? '');
const amount = currency ? formattedAmount.replace(currency, '') : formattedAmount;
const cardDescription = getCompanyCardDescription(transactionCardName, transactionCardID, cardList) ?? (transactionCardID ? getCardDescription(transactionCardID) : '');
const cardDescription = getCompanyCardDescription(transactionCardName, transactionCardID, cardList) ?? (transactionCardID ? getCardDescription(cardList?.[transactionCardID]) : '');

const secondaryBgcolorStyle = secondaryColor ? StyleUtils.getBackgroundColorStyle(secondaryColor) : undefined;
const primaryTextColorStyle = primaryColor ? StyleUtils.getColorStyle(primaryColor) : undefined;
Expand Down
5 changes: 2 additions & 3 deletions src/components/Search/SearchAutocompleteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ function SearchAutocompleteList(
.filter(
(card) =>
(card.bank.toLowerCase().includes(autocompleteValue.toLowerCase()) || card.lastFourPAN?.includes(autocompleteValue)) &&
!alreadyAutocompletedKeys.includes(getCardDescription(card.cardID).toLowerCase()),
!alreadyAutocompletedKeys.includes(getCardDescription(card).toLowerCase()),
)
.sort()
.slice(0, 10);

return filteredCards.map((card) => ({
filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.CARD_ID,
text: getCardDescription(card.cardID, allCards),
text: getCardDescription(card),
autocompleteID: card.cardID.toString(),
mapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID,
}));
Expand Down Expand Up @@ -462,7 +462,6 @@ function SearchAutocompleteList(
expenseTypes,
feedAutoCompleteList,
cardAutocompleteList,
allCards,
booleanTypes,
workspaceList,
]);
Expand Down
35 changes: 7 additions & 28 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import * as Illustrations from '@src/components/Icon/Illustrations';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import type {OnyxValues} from '@src/ONYXKEYS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {BankAccountList, Card, CardFeeds, CardList, CompanyCardFeed, CurrencyList, ExpensifyCardSettings, PersonalDetailsList, Policy, WorkspaceCardsList} from '@src/types/onyx';
import type {FilteredCardList} from '@src/types/onyx/Card';
Expand All @@ -21,20 +20,8 @@
import {getDisplayNameOrDefault} from './PersonalDetailsUtils';
import StringUtils from './StringUtils';

let allCards: OnyxValues[typeof ONYXKEYS.CARD_LIST] = {};
Onyx.connect({
key: ONYXKEYS.CARD_LIST,
callback: (val) => {
if (!val || Object.keys(val).length === 0) {
return;
}

allCards = val;
},
});

let allWorkspaceCards: OnyxCollection<WorkspaceCardsList> = {};
Onyx.connect({

Check warning on line 24 in src/libs/CardUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function

Check warning on line 24 in src/libs/CardUtils.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -50,29 +37,21 @@
}

/**
* @param cardID
* @param card
* @returns boolean
*/
function isExpensifyCard(cardID?: number) {
if (!cardID) {
return false;
}
const card = allCards[cardID];
function isExpensifyCard(card?: Card) {
Comment thread
mountiny marked this conversation as resolved.
if (!card) {
return false;
}
return card.bank === CONST.EXPENSIFY_CARD.BANK;
}

/**
* @param cardID
* @param card
* @returns string in format %<bank> - <lastFourPAN || Not Activated>%.
*/
function getCardDescription(cardID?: number, cards: CardList = allCards) {
if (!cardID) {
return '';
}
const card = cards[cardID];
function getCardDescription(card?: Card) {
if (!card) {
return '';
}
Expand All @@ -90,7 +69,7 @@
* @returns company card name
*/
function getCompanyCardDescription(transactionCardName?: string, cardID?: number, cards?: CardList) {
Comment thread
mountiny marked this conversation as resolved.
if (!cardID || isExpensifyCard(cardID) || !cards?.[cardID]) {
if (!cardID || !cards?.[cardID] || isExpensifyCard(cards[cardID])) {
return transactionCardName;
}
const card = cards[cardID];
Expand All @@ -110,9 +89,9 @@
return card?.state === CONST.EXPENSIFY_CARD.STATE.CLOSED;
}

function mergeCardListWithWorkspaceFeeds(workspaceFeeds: Record<string, WorkspaceCardsList | undefined>, cardList = allCards, shouldExcludeCardHiddenFromSearch = false) {
function mergeCardListWithWorkspaceFeeds(workspaceFeeds: Record<string, WorkspaceCardsList | undefined>, cardList: CardList | undefined, shouldExcludeCardHiddenFromSearch = false) {
Comment thread
shubham1206agra marked this conversation as resolved.
const feedCards: CardList = {};
Object.values(cardList).forEach((card) => {
Object.values(cardList ?? {}).forEach((card) => {
if (!isCard(card) || (shouldExcludeCardHiddenFromSearch && isCardHiddenFromSearch(card))) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ function getFilterDisplayValue(
if (Number.isNaN(cardID)) {
return filterValue;
}
return getCardDescription(cardID, cardList) || filterValue;
return getCardDescription(cardList?.[cardID]) || filterValue;
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) {
return getReportName(reports?.[`${ONYXKEYS.COLLECTION.REPORT}${filterValue}`]) || filterValue;
Expand Down Expand Up @@ -822,7 +822,7 @@ function buildUserReadableQueryString(
if (Number.isNaN(cardID)) {
acc.push({operator: filter.operator, value: cardID});
} else {
acc.push({operator: filter.operator, value: getCardDescription(cardID, cardList) || cardID});
acc.push({operator: filter.operator, value: getCardDescription(cardList?.[cardID]) || cardID});
}
}
return acc;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Search/AdvancedSearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function getFilterCardDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>,

const cardNames = Object.values(cards)
.filter((card) => cardIdsFilter.includes(card.cardID.toString()) && !feedFilter.includes(createCardFeedKey(card.fundID, card.bank)))
.map((card) => getCardDescription(card.cardID, cards));
.map((card) => getCardDescription(card));

const feedNames = Object.keys(cardFeedNamesWithType)
.filter((workspaceCardFeedKey) => {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/Wallet/PaymentMethodList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ function PaymentMethodList({
const assignedCards = Object.values(isLoadingCardList ? {} : (cardList ?? {}))
// Filter by active cards associated with a domain
.filter((card) => !!card.domainName && CONST.EXPENSIFY_CARD.ACTIVE_STATES.includes(card.state ?? 0));
const assignedCardsSorted = lodashSortBy(assignedCards, (card) => !isExpensifyCard(card.cardID));
const assignedCardsSorted = lodashSortBy(assignedCards, (card) => !isExpensifyCard(card));

const assignedCardsGrouped: PaymentMethodItem[] = [];
assignedCardsSorted.forEach((card) => {
const isDisabled = card.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || !!card.errors;
const icon = getCardFeedIcon(card.bank as CompanyCardFeed, illustrations);

if (!isExpensifyCard(card.cardID)) {
if (!isExpensifyCard(card)) {
const pressHandler = onPress as CardPressHandler;
const lastFourPAN = lastFourNumbersFromCardName(card.cardName);
const plaidUrl = getPlaidInstitutionIconUrl(card.bank);
Expand Down
116 changes: 115 additions & 1 deletion tests/unit/CardUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {
formatCardExpiration,
getBankCardDetailsImage,
getBankName,
getCardDescription,
getCardFeedIcon,
getCardsByCardholderName,
getCompanyCardDescription,
getCompanyFeeds,
getCustomOrFormattedFeedName,
getFeedType,
Expand All @@ -21,12 +23,13 @@ import {
getYearFromExpirationDateString,
hasIssuedExpensifyCard,
isCustomFeed as isCustomFeedCardUtils,
isExpensifyCard,
isExpensifyCardFullySetUp,
lastFourNumbersFromCardName,
maskCardNumber,
sortCardsByCardholderName,
} from '@src/libs/CardUtils';
import type {CardFeeds, CardList, CompanyCardFeed, ExpensifyCardSettings, PersonalDetailsList, Policy, WorkspaceCardsList} from '@src/types/onyx';
import type {Card, CardFeeds, CardList, CompanyCardFeed, ExpensifyCardSettings, PersonalDetailsList, Policy, WorkspaceCardsList} from '@src/types/onyx';
import type {CompanyCardFeedWithNumber} from '@src/types/onyx/CardFeeds';
import {localeCompare} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
Expand Down Expand Up @@ -1024,4 +1027,115 @@ describe('CardUtils', () => {
expect(sortedCards.at(0)?.cardID).toBe(1);
});
});

describe('getCardDescription', () => {
it('should return the correct card description for company card', () => {
const card: Card = {
accountID: 18439984,
bank: CONST.COMPANY_CARD.FEED_BANK_NAME.VISA,
cardID: 21310091,
cardName: '480801XXXXXX2554',
domainName: 'expensify-policy41314f4dc5ce25af.exfy',
fraud: 'none',
lastFourPAN: '2554',
lastUpdated: '',
lastScrape: '2024-11-27 11:00:53',
scrapeMinDate: '2024-10-17',
state: 3,
};
const description = getCardDescription(card);
expect(description).toBe('Visa - 2554');
});

it('should return the correct card description for Expensify card', () => {
const card: Card = {
accountID: 18439984,
bank: CONST.EXPENSIFY_CARD.BANK,
cardID: 21570657,
cardName: 'CREDIT CARD...5644',
domainName: 'expensify-policy17f617b9fe23d2f1.exfy',
fraud: 'none',
lastFourPAN: '',
lastScrape: '',
lastUpdated: '',
state: 2,
};
const description = getCardDescription(card);
expect(description).toBe('Expensify Card');
});
});

describe('isExpensifyCard', () => {
it('should return true for Expensify Card', () => {
const card: Card = {
accountID: 18439984,
bank: CONST.EXPENSIFY_CARD.BANK,
cardID: 21570657,
cardName: 'CREDIT CARD...5644',
domainName: 'expensify-policy17f617b9fe23d2f1.exfy',
fraud: 'none',
lastFourPAN: '',
lastScrape: '',
lastUpdated: '',
state: 2,
};
expect(isExpensifyCard(card)).toBe(true);
});

it('should return false for non-Expensify Card', () => {
const card: Card = {
accountID: 18439984,
bank: CONST.COMPANY_CARD.FEED_BANK_NAME.VISA,
cardID: 21310091,
cardName: '480801XXXXXX2554',
domainName: 'expensify-policy41314f4dc5ce25af.exfy',
fraud: 'none',
lastFourPAN: '2554',
lastUpdated: '',
lastScrape: '2024-11-27 11:00:53',
scrapeMinDate: '2024-10-17',
state: 3,
};
expect(isExpensifyCard(card)).toBe(false);
});
});

describe('getCompanyCardDescription', () => {
const cardList: CardList = {
'21310091': {
accountID: 18439984,
bank: CONST.COMPANY_CARD.FEED_BANK_NAME.VISA,
cardID: 21310091,
cardName: '480801XXXXXX2554',
domainName: 'expensify-policy41314f4dc5ce25af.exfy',
fraud: 'none',
lastFourPAN: '2554',
lastUpdated: '',
lastScrape: '2024-11-27 11:00:53',
scrapeMinDate: '2024-10-17',
state: 3,
},
'21570657': {
accountID: 18439984,
bank: CONST.EXPENSIFY_CARD.BANK,
cardID: 21570657,
cardName: 'CREDIT CARD...5644',
domainName: 'expensify-policy17f617b9fe23d2f1.exfy',
fraud: 'none',
lastFourPAN: '',
lastScrape: '',
lastUpdated: '',
state: 2,
},
};
it('should return the correct description for a company card', () => {
const description = getCompanyCardDescription('Test', 21310091, cardList);
expect(description).toBe('480801XXXXXX2554');
});

it('should return the correct description for an Expensify card', () => {
const description = getCompanyCardDescription('Test', 21570657, cardList);
expect(description).toBe('Test');
});
});
});
Loading