diff --git a/src/hooks/useGetExpensifyCardFromReportAction.ts b/src/hooks/useGetExpensifyCardFromReportAction.ts index b0abf78078de..165e57335564 100644 --- a/src/hooks/useGetExpensifyCardFromReportAction.ts +++ b/src/hooks/useGetExpensifyCardFromReportAction.ts @@ -1,11 +1,7 @@ import {useCardList, useWorkspaceCardList} from '@components/OnyxListItemProvider'; -import {isPolicyAdmin} from '@libs/PolicyUtils'; -import {getOriginalMessage, isCardIssuedAction} from '@libs/ReportActionsUtils'; -import {getTravelBillingFeedID} from '@libs/TravelBillingUtils'; +import {getExpensifyCardFromReportAction} from '@libs/ReportAlternateTextUtils'; -import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import type {Card, ReportAction} from '@src/types/onyx'; import usePolicy from './usePolicy'; @@ -14,21 +10,8 @@ function useGetExpensifyCardFromReportAction({reportAction, policyID}: {reportAc const allUserCards = useCardList(); const allExpensifyCards = useWorkspaceCardList(); const policy = usePolicy(policyID); - const workspaceAccountID = policy?.policyAccountID ?? CONST.DEFAULT_NUMBER_ID; - const cardIssuedActionOriginalMessage = isCardIssuedAction(reportAction) ? getOriginalMessage(reportAction) : undefined; - const cardID = cardIssuedActionOriginalMessage?.cardID ?? CONST.DEFAULT_NUMBER_ID; - if (!isPolicyAdmin(policy)) { - return allUserCards?.[cardID]; - } - - // Issued Expensify Cards live on one of two Onyx keys: regular cards on the 2-segment key, - // Travel Billing cards on the `_TRAVEL_US` variant. Check both. - return ( - allExpensifyCards?.[`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`]?.[cardID] ?? - allExpensifyCards?.[`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${getTravelBillingFeedID(workspaceAccountID)}`]?.[cardID] ?? - allUserCards?.[cardID] - ); + return getExpensifyCardFromReportAction({reportAction, policy, cardList: allUserCards, workspaceCardList: allExpensifyCards}); } export default useGetExpensifyCardFromReportAction; diff --git a/src/libs/ReportAlternateTextUtils.ts b/src/libs/ReportAlternateTextUtils.ts index 79bcc3b488cb..b195658eca28 100644 --- a/src/libs/ReportAlternateTextUtils.ts +++ b/src/libs/ReportAlternateTextUtils.ts @@ -6,6 +6,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type { Card, + CardList, PersonalDetails, PersonalDetailsList, Policy, @@ -18,6 +19,7 @@ import type { Rule, Transaction, VisibleReportActionsDerivedValue, + WorkspaceCardsList, } from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; @@ -36,7 +38,7 @@ import createDynamicRoute from './Navigation/helpers/dynamicRoutesUtils/createDy import {getIsOffline} from './NetworkState'; import Parser from './Parser'; import {getLoginByAccountID, getPersonalDetailsByID, getPersonalDetailsForAccountIDs, getPersonalDetailsListByIDs, temporaryGetDisplayNameOrDefault} from './PersonalDetailsUtils'; -import {getCleanedTagName, hasDynamicExternalWorkflow} from './PolicyUtils'; +import {getCleanedTagName, hasDynamicExternalWorkflow, isPolicyAdmin} from './PolicyUtils'; import { getActionableCard3DSTransactionApprovalMessage, getActionableCardFraudAlertResolutionMessage, @@ -245,6 +247,7 @@ import {getAddExpensifyCardRuleMessage, getRemoveExpensifyCardRuleMessage, getUp import StringUtils from './StringUtils'; import {getTaskCreatedMessage, getTaskReportActionMessage} from './TaskUtils'; import {getAmount as getTransactionAmount, getCurrency as getTransactionCurrency, getDescription, isScanning} from './TransactionUtils'; +import {getTravelBillingFeedID} from './TravelBillingUtils'; let allReports: OnyxCollection; // connectWithoutView is justified: this is module-level, non-render preview computation shared by LHN and Search; @@ -1603,9 +1606,38 @@ function getReportAlternateText({ return alternateText; } +function getExpensifyCardFromReportAction({ + reportAction, + policy, + cardList, + workspaceCardList, +}: { + reportAction: OnyxEntry; + policy: OnyxEntry; + cardList: OnyxEntry; + workspaceCardList: OnyxCollection; +}): Card | undefined { + const workspaceAccountID = policy?.policyAccountID ?? CONST.DEFAULT_NUMBER_ID; + + const cardIssuedActionOriginalMessage = isCardIssuedAction(reportAction) ? getOriginalMessage(reportAction) : undefined; + const cardID = cardIssuedActionOriginalMessage?.cardID ?? CONST.DEFAULT_NUMBER_ID; + if (!isPolicyAdmin(policy)) { + return cardList?.[cardID]; + } + + // Issued Expensify Cards live on one of two Onyx keys: regular cards on the 2-segment key, + // Travel Billing cards on the `_TRAVEL_US` variant. Check both. + return ( + workspaceCardList?.[`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`]?.[cardID] ?? + workspaceCardList?.[`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${getTravelBillingFeedID(workspaceAccountID)}`]?.[cardID] ?? + cardList?.[cardID] + ); +} + export { // eslint-disable-next-line @typescript-eslint/no-deprecated deprecatedCachedOneTransactionThreadReportIDs, + getExpensifyCardFromReportAction, getLastActorDisplayName, getLastActorDisplayNameFromLastVisibleActions, getLastMessageTextForReport,