Skip to content

Commit

Permalink
Add fixes regarding 2663603239 review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKorytko committed Mar 6, 2025
1 parent 11bd523 commit e7fab49
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/components/Search/SearchAutocompleteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import usePolicy from '@hooks/usePolicy';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {searchInServer} from '@libs/actions/Report';
import {getCardFeedKey, getCardFeedNamesWithType} from '@libs/CardFeedUtils';
import {getCardDescription, isCard, isCardHiddenFromSearch, mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import {getCardFeedKey, getCardFeedNamesWithType} from '@libs/FeedUtils';
import {combineOrderingOfReportsAndPersonalDetails, getSearchOptions, getValidOptions} from '@libs/OptionsListUtils';
import type {Options, SearchOption} from '@libs/OptionsListUtils';
import Performance from '@libs/Performance';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {navigateToAndOpenReport} from '@libs/actions/Report';
import {clearAllFilters} from '@libs/actions/Search';
import {getCardFeedNamesWithType} from '@libs/CardFeedUtils';
import {mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import {getCardFeedNamesWithType} from '@libs/FeedUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getAllTaxRates} from '@libs/PolicyUtils';
import type {OptionData} from '@libs/ReportUtils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import {clearAllFilters} from '@libs/actions/Search';
import {getCardFeedNamesWithType} from '@libs/CardFeedUtils';
import {mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import {getCardFeedNamesWithType} from '@libs/FeedUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getAllTaxRates} from '@libs/PolicyUtils';
import {buildSearchQueryJSON, buildUserReadableQueryString, isCannedSearchQuery} from '@libs/SearchQueryUtils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {OnyxCollection} from 'react-native-onyx';
import type {SearchAutocompleteQueryRange, SearchFilterKey} from '@components/Search/types';
import type {CardFeedNamesWithType} from '@libs/FeedUtils';
import type {CardFeedNamesWithType} from '@libs/CardFeedUtils';
import {parse} from '@libs/SearchParser/autocompleteParser';
import {getFilterDisplayValue} from '@libs/SearchQueryUtils';
import CONST from '@src/CONST';
Expand Down
41 changes: 38 additions & 3 deletions src/libs/FeedUtils.ts → src/libs/CardFeedUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Card, CardList, CompanyCardFeed, PersonalDetailsList, WorkspaceCardsList} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {DomainFeedData} from './CardUtils';
import {generateDomainFeedData, getBankName, getCardFeedIcon, isCard, isCardClosed, isCardHiddenFromSearch} from './CardUtils';
import {getBankName, getCardFeedIcon, isCard, isCardClosed, isCardHiddenFromSearch} from './CardUtils';
import {getDescriptionForPolicyDomainCard, getPolicy} from './PolicyUtils';
import type {OptionData} from './ReportUtils';

type CardFilterItem = Partial<OptionData> & AdditionalCardProps & {isCardFeed?: boolean; correspondingCards?: string[]; cardFeedKey: string};
type DomainFeedData = {bank: string; domainName: string; correspondingCardIDs: string[]; fundID?: string};
type ItemsGroupedBySelection = {selected: CardFilterItem[]; unselected: CardFilterItem[]};
type CardFeedNamesWithType = Record<string, {name: string; type: 'domain' | 'workspace'}>;
type CardFeedData = {cardName: string; bank: string; label?: string; type: 'domain' | 'workspace'};
Expand Down Expand Up @@ -121,6 +121,31 @@ function buildCardsData(
return {selected: selectedCardItems, unselected: unselectedCardItems};
}

/**
* @param cardList - The list of cards to process. Can be undefined.
* @returns a record where keys are domain names and values contain domain feed data.
*/
function generateDomainFeedData(cardList: CardList | undefined): Record<string, DomainFeedData> {
return Object.values(cardList ?? {}).reduce((domainFeedData, currentCard) => {
// Cards in cardList can also be domain cards, we use them to compute domain feed
if (!currentCard.domainName.match(CONST.REGEX.EXPENSIFY_POLICY_DOMAIN_NAME) && !isCardHiddenFromSearch(currentCard)) {
if (domainFeedData[currentCard.domainName]) {
domainFeedData[currentCard.domainName].correspondingCardIDs.push(currentCard.cardID.toString());
} else {
// if the cards belongs to the same domain, every card of it should have the same fundID
// eslint-disable-next-line no-param-reassign
domainFeedData[currentCard.domainName] = {
fundID: currentCard.fundID,
domainName: currentCard.domainName,
bank: currentCard.bank,
correspondingCardIDs: [currentCard.cardID.toString()],
};
}
}
return domainFeedData;
}, {} as Record<string, DomainFeedData>);
}

function getWorkspaceCardFeedData(cardFeed: WorkspaceCardsList | undefined, repeatingBanks: string[], translate: LocaleContextProps['translate']): CardFeedData | undefined {
const cardFeedArray = Object.values(cardFeed ?? {});
const representativeCard = cardFeedArray.find((cardFeedItem) => isCard(cardFeedItem));
Expand Down Expand Up @@ -341,4 +366,14 @@ const generateSelectedCards = (
};

export type {CardFilterItem, ItemsGroupedBySelection, CardFeedNamesWithType};
export {buildCardsData, getCardFeedNamesWithType, buildCardFeedsData, generateSelectedCards, getSelectedCardsFromFeeds, createCardFeedKey, getCardFeedKey, getWorkspaceCardFeedKey};
export {
buildCardsData,
getCardFeedNamesWithType,
buildCardFeedsData,
generateSelectedCards,
getSelectedCardsFromFeeds,
createCardFeedKey,
getCardFeedKey,
getWorkspaceCardFeedKey,
generateDomainFeedData,
};
31 changes: 0 additions & 31 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,6 @@ function getCardDescription(cardID?: number, cards: CardList = allCards) {
return cardDescriptor ? `${humanReadableBankName} - ${cardDescriptor}` : `${humanReadableBankName}`;
}

type DomainFeedData = {bank: string; domainName: string; correspondingCardIDs: string[]; fundID?: string};

/**
* @param cardList - The list of cards to process. Can be undefined.
* @returns a record where keys are domain names and values contain domain feed data.
*/

function generateDomainFeedData(cardList: CardList | undefined): Record<string, DomainFeedData> {
return Object.values(cardList ?? {}).reduce((domainFeedData, currentCard) => {
// Cards in cardList can also be domain cards, we use them to compute domain feed
if (!currentCard.domainName.match(CONST.REGEX.EXPENSIFY_POLICY_DOMAIN_NAME) && !isCardHiddenFromSearch(currentCard)) {
if (domainFeedData[currentCard.domainName]) {
domainFeedData[currentCard.domainName].correspondingCardIDs.push(currentCard.cardID.toString());
} else {
// if the cards belongs to the same domain, every card of it should have the same fundID
// eslint-disable-next-line no-param-reassign
domainFeedData[currentCard.domainName] = {
fundID: currentCard.fundID,
domainName: currentCard.domainName,
bank: currentCard.bank,
correspondingCardIDs: [currentCard.cardID.toString()],
};
}
}
return domainFeedData;
}, {} as Record<string, DomainFeedData>);
}

function isCard(item: Card | Record<string, string>): item is Card {
return typeof item === 'object' && 'cardID' in item && !!item.cardID && 'bank' in item && !!item.bank;
}
Expand Down Expand Up @@ -616,7 +588,4 @@ export {
hasIssuedExpensifyCard,
hasCardListObject,
isExpensifyCardFullySetUp,
generateDomainFeedData,
};

export type {DomainFeedData};
4 changes: 2 additions & 2 deletions src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type {SearchAdvancedFiltersForm} from '@src/types/form';
import FILTER_KEYS, {DATE_FILTER_KEYS} from '@src/types/form/SearchAdvancedFiltersForm';
import type * as OnyxTypes from '@src/types/onyx';
import type {SearchDataTypes} from '@src/types/onyx/SearchResults';
import type {CardFeedNamesWithType} from './CardFeedUtils';
import {getWorkspaceCardFeedKey} from './CardFeedUtils';
import {getCardDescription} from './CardUtils';
import {convertToBackendAmount, convertToFrontendAmountAsInteger} from './CurrencyUtils';
import type {CardFeedNamesWithType} from './FeedUtils';
import {getWorkspaceCardFeedKey} from './FeedUtils';
import localeCompare from './LocaleCompare';
import Log from './Log';
import {validateAmount} from './MoneyRequestUtils';
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 @@ -16,9 +16,9 @@ import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
import useWaitForNavigation from '@hooks/useWaitForNavigation';
import {clearAllFilters, saveSearch} from '@libs/actions/Search';
import {createCardFeedKey, getCardFeedKey, getCardFeedNamesWithType, getWorkspaceCardFeedKey} from '@libs/CardFeedUtils';
import {getCardDescription, mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import {convertToDisplayStringWithoutCurrency} from '@libs/CurrencyUtils';
import {createCardFeedKey, getCardFeedKey, getCardFeedNamesWithType, getWorkspaceCardFeedKey} from '@libs/FeedUtils';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import {createDisplayName} from '@libs/PersonalDetailsUtils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import useLocalize from '@hooks/useLocalize';
import useThemeIllustrations from '@hooks/useThemeIllustrations';
import useThemeStyles from '@hooks/useThemeStyles';
import {openSearchFiltersCardPage, updateAdvancedFilters} from '@libs/actions/Search';
import {generateDomainFeedData} from '@libs/CardUtils';
import type {CardFilterItem} from '@libs/FeedUtils';
import {buildCardFeedsData, buildCardsData, generateSelectedCards, getSelectedCardsFromFeeds} from '@libs/FeedUtils';
import type {CardFilterItem} from '@libs/CardFeedUtils';
import {buildCardFeedsData, buildCardsData, generateDomainFeedData, generateSelectedCards, getSelectedCardsFromFeeds} from '@libs/CardFeedUtils';
import Navigation from '@navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Search/SearchTypeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import useNetwork from '@hooks/useNetwork';
import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
import {clearAllFilters} from '@libs/actions/Search';
import {getCardFeedNamesWithType} from '@libs/CardFeedUtils';
import {mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import {getCardFeedNamesWithType} from '@libs/FeedUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getAllTaxRates} from '@libs/PolicyUtils';
import {buildSearchQueryJSON, buildUserReadableQueryString, isCannedSearchQuery} from '@libs/SearchQueryUtils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getCardFeedNamesWithType, getSelectedCardsFromFeeds} from '@libs/FeedUtils';
import {getCardFeedNamesWithType, getSelectedCardsFromFeeds} from '@libs/CardFeedUtils';
import {translateLocal} from '@libs/Localize';
import type {WorkspaceCardsList} from '@src/types/onyx';

Expand Down Expand Up @@ -56,7 +56,7 @@ const fakeWorkspace: Record<string, WorkspaceCardsList> = {
};
/* eslint-enable @typescript-eslint/naming-convention */

describe('Feed Utils', () => {
describe('Card Feed Utils', () => {
it('returns display name of workspace & domain cards', () => {
const cardFeedNamesWithType = getCardFeedNamesWithType({workspaceCardFeeds: fakeWorkspace, userCardList: {}, translate: translateLocal});
expect(Object.keys(cardFeedNamesWithType).length).toBe(2);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Search/buildCardFilterDataTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* eslint-disable @typescript-eslint/naming-convention */
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import {buildCardFeedsData, buildCardsData} from '@libs/FeedUtils';
import {buildCardFeedsData, buildCardsData} from '@libs/CardFeedUtils';
// eslint-disable-next-line no-restricted-syntax
import * as PolicyUtils from '@libs/PolicyUtils';
import type IllustrationsType from '@styles/theme/illustrations/types';
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Search/buildSubstitutionsMapTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
// we need "dirty" object key names in these tests
import type {OnyxCollection} from 'react-native-onyx';
import type {CardFeedNamesWithType} from '@libs/FeedUtils';
import type {CardFeedNamesWithType} from '@libs/CardFeedUtils';
import {buildSubstitutionsMap} from '@src/components/Search/SearchRouter/buildSubstitutionsMap';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
Expand Down

0 comments on commit e7fab49

Please sign in to comment.