diff --git a/src/CONST.ts b/src/CONST.ts index 51e41a435b235..a81af16113455 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -6700,6 +6700,12 @@ const CONST = { ERROR_PERMISSION_DENIED: 'permissionDenied', }, }, + LAST_PAYMENT_METHOD: { + LAST_USED: 'lastUsed', + IOU: 'Iou', + EXPENSE: 'Expense', + INVOICE: 'Invoice', + }, SKIPPABLE_COLLECTION_MEMBER_IDS: [String(DEFAULT_NUMBER_ID), '-1', 'undefined', 'null', 'NaN'] as string[], SETUP_SPECIALIST_LOGIN: 'Setup Specialist', } as const; diff --git a/src/components/Search/SearchPageHeader/SearchPageHeader.tsx b/src/components/Search/SearchPageHeader/SearchPageHeader.tsx index b25467563f776..a3b814650a236 100644 --- a/src/components/Search/SearchPageHeader/SearchPageHeader.tsx +++ b/src/components/Search/SearchPageHeader/SearchPageHeader.tsx @@ -23,6 +23,7 @@ import { approveMoneyRequestOnSearch, deleteMoneyRequestOnSearch, exportSearchItemsToCSV, + getLastPolicyPaymentMethod, payMoneyRequestOnSearch, unholdMoneyRequestOnSearch, updateAdvancedFilters, @@ -149,9 +150,12 @@ function SearchPageHeader({queryJSON, searchName, searchRouterListVisible, hideS !isOffline && !isAnyTransactionOnHold && (selectedReports.length - ? selectedReports.every((report) => report.action === CONST.SEARCH.ACTION_TYPES.PAY && report.policyID && lastPaymentMethods[report.policyID]) + ? selectedReports.every((report) => report.action === CONST.SEARCH.ACTION_TYPES.PAY && report.policyID && getLastPolicyPaymentMethod(report.policyID, lastPaymentMethods)) : selectedTransactionsKeys.every( - (id) => selectedTransactions[id].action === CONST.SEARCH.ACTION_TYPES.PAY && selectedTransactions[id].policyID && lastPaymentMethods[selectedTransactions[id].policyID], + (id) => + selectedTransactions[id].action === CONST.SEARCH.ACTION_TYPES.PAY && + selectedTransactions[id].policyID && + getLastPolicyPaymentMethod(selectedTransactions[id].policyID, lastPaymentMethods), )); if (shouldShowPayOption) { @@ -172,7 +176,7 @@ function SearchPageHeader({queryJSON, searchName, searchRouterListVisible, hideS for (const item of items) { const policyID = item.policyID; - const lastPolicyPaymentMethod = policyID ? lastPaymentMethods?.[policyID] : null; + const lastPolicyPaymentMethod = getLastPolicyPaymentMethod(policyID, lastPaymentMethods); if (!lastPolicyPaymentMethod) { Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: item.reportID, backTo: activeRoute})); @@ -189,11 +193,15 @@ function SearchPageHeader({queryJSON, searchName, searchRouterListVisible, hideS const paymentData = ( selectedReports.length - ? selectedReports.map((report) => ({reportID: report.reportID, amount: report.total, paymentType: lastPaymentMethods[`${report.policyID}`]})) + ? selectedReports.map((report) => ({ + reportID: report.reportID, + amount: report.total, + paymentType: getLastPolicyPaymentMethod(report.policyID, lastPaymentMethods), + })) : Object.values(selectedTransactions).map((transaction) => ({ reportID: transaction.reportID, amount: transaction.amount, - paymentType: lastPaymentMethods[transaction.policyID], + paymentType: getLastPolicyPaymentMethod(transaction.policyID, lastPaymentMethods), })) ) as PaymentData[]; diff --git a/src/components/SettlementButton/index.tsx b/src/components/SettlementButton/index.tsx index 667826d29f8b7..bca6765d099f9 100644 --- a/src/components/SettlementButton/index.tsx +++ b/src/components/SettlementButton/index.tsx @@ -21,6 +21,7 @@ import {approveMoneyRequest, savePreferredPaymentMethod as savePreferredPaymentM import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; +import type {LastPaymentMethodType} from '@src/types/onyx'; import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; @@ -74,7 +75,14 @@ function SettlementButton({ const policyEmployeeAccountIDs = policyID ? getPolicyEmployeeAccountIDs(policyID) : []; const reportBelongsToWorkspace = policyID ? doesReportBelongToWorkspace(chatReport, policyEmployeeAccountIDs, policyID) : false; const policyIDKey = reportBelongsToWorkspace ? policyID : CONST.POLICY.ID_FAKE; - const [lastPaymentMethod = '-1', lastPaymentMethodResult] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {selector: (paymentMethod) => paymentMethod?.[policyIDKey]}); + const [lastPaymentMethod, lastPaymentMethodResult] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + selector: (paymentMethod) => { + if (typeof paymentMethod?.[policyIDKey] === 'string') { + return paymentMethod?.[policyIDKey]; + } + return (paymentMethod?.[policyIDKey] as LastPaymentMethodType)?.lastUsed; + }, + }); const isLoadingLastPaymentMethod = isLoadingOnyxValue(lastPaymentMethodResult); const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); @@ -221,7 +229,7 @@ function SettlementButton({ }; const savePreferredPaymentMethod = (id: string, value: PaymentMethodType) => { - savePreferredPaymentMethodIOU(id, value); + savePreferredPaymentMethodIOU(id, value, undefined); }; return ( diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 6a8b4d43daa6e..acf9a2e395908 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -9544,8 +9544,8 @@ function navigateToStartStepIfScanFileCannotBeRead( } /** Save the preferred payment method for a policy */ -function savePreferredPaymentMethod(policyID: string, paymentMethod: PaymentMethodType) { - Onyx.merge(`${ONYXKEYS.NVP_LAST_PAYMENT_METHOD}`, {[policyID]: paymentMethod}); +function savePreferredPaymentMethod(policyID: string, paymentMethod: PaymentMethodType, type: ValueOf | undefined) { + Onyx.merge(`${ONYXKEYS.NVP_LAST_PAYMENT_METHOD}`, {[policyID]: type ? {[type]: paymentMethod, [CONST.LAST_PAYMENT_METHOD.LAST_USED]: paymentMethod} : paymentMethod}); } /** Get report policy id of IOU request */ diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index 39cb9bc63e34a..ef1ddb998f63c 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -19,7 +19,7 @@ import playSound, {SOUNDS} from '@libs/Sound'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import FILTER_KEYS from '@src/types/form/SearchAdvancedFiltersForm'; -import type {LastPaymentMethod, SearchResults} from '@src/types/onyx'; +import type {LastPaymentMethod, LastPaymentMethodType, SearchResults} from '@src/types/onyx'; import type {SearchPolicy, SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults'; import {openReport} from './Report'; @@ -77,8 +77,22 @@ function handleActionButtonPress(hash: number, item: TransactionListItemType | R } } +function getLastPolicyPaymentMethod(policyID: string | undefined, lastPaymentMethods: OnyxEntry) { + if (!policyID) { + return null; + } + let lastPolicyPaymentMethod = null; + if (typeof lastPaymentMethods?.[policyID] === 'string') { + lastPolicyPaymentMethod = lastPaymentMethods?.[policyID] as ValueOf; + } else { + lastPolicyPaymentMethod = (lastPaymentMethods?.[policyID] as LastPaymentMethodType)?.lastUsed as ValueOf; + } + + return lastPolicyPaymentMethod; +} + function getPayActionCallback(hash: number, item: TransactionListItemType | ReportListItemType, goToItem: () => void) { - const lastPolicyPaymentMethod = item.policyID ? (lastPaymentMethod?.[item.policyID] as ValueOf) : null; + const lastPolicyPaymentMethod = getLastPolicyPaymentMethod(item.policyID, lastPaymentMethod); if (!lastPolicyPaymentMethod) { goToItem(); @@ -421,4 +435,5 @@ export { handleActionButtonPress, submitMoneyRequestOnSearch, openSearchFiltersCardPage, + getLastPolicyPaymentMethod, }; diff --git a/src/types/onyx/LastPaymentMethod.ts b/src/types/onyx/LastPaymentMethod.ts index ea0c644fc7304..62805e86af4b0 100644 --- a/src/types/onyx/LastPaymentMethod.ts +++ b/src/types/onyx/LastPaymentMethod.ts @@ -1,4 +1,18 @@ +/** + * The new lastPaymentMethod object + */ +type LastPaymentMethodType = { + /** The default last payment method */ + lastUsed: string; + /** The lastPaymentMethod of an IOU */ + Iou: string; + /** The lastPaymentMethod of an Expense */ + Expense: string; + /** The lastPaymentMethod of an Invoice */ + Invoice: string; +}; + /** Record of last payment methods, indexed by policy id */ -type LastPaymentMethod = Record; +type LastPaymentMethod = Record; -export default LastPaymentMethod; +export type {LastPaymentMethodType, LastPaymentMethod}; diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index 569504437fb20..f5a9acb5f3ea4 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -35,7 +35,7 @@ import type InvitedEmailsToAccountIDs from './InvitedEmailsToAccountIDs'; import type IOU from './IOU'; import type JoinablePolicies from './JoinablePolicies'; import type LastExportMethod from './LastExportMethod'; -import type LastPaymentMethod from './LastPaymentMethod'; +import type {LastPaymentMethod, LastPaymentMethodType} from './LastPaymentMethod'; import type LastSelectedDistanceRates from './LastSelectedDistanceRates'; import type Locale from './Locale'; import type {LoginList} from './Login'; @@ -250,4 +250,5 @@ export type { JoinablePolicies, DismissedProductTraining, TravelProvisioning, + LastPaymentMethodType, };