diff --git a/src/hooks/useExpenseActions.ts b/src/hooks/useExpenseActions.ts index 122378e36897..fff21132d629 100644 --- a/src/hooks/useExpenseActions.ts +++ b/src/hooks/useExpenseActions.ts @@ -79,6 +79,7 @@ import {useGetPersonalDetailsByLogin} from './usePersonalDetailByLogin'; import {usePersonalDetailsByIDs} from './usePersonalDetails'; import usePersonalPolicy from './usePersonalPolicy'; import useReportIsArchived from './useReportIsArchived'; +import useReportTransactions from './useReportTransactions'; import useRestrictedActionPolicyID from './useRestrictedActionPolicyID'; import useSplitEffectivePolicy from './useSplitEffectivePolicy'; import useStrictPolicyRules from './useStrictPolicyRules'; @@ -145,6 +146,7 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(iouTransactionID)}`); const [originalTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transaction?.comment?.originalTransactionID)}`); const {iouReport, chatReport: chatIOUReport, isChatIOUReportArchived} = useGetIOUReportFromReportAction(requestParentReportAction); + const iouReportTransactions = useReportTransactions(iouReport?.reportID); const [chatIOUReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(iouReport?.chatReportID)}`); // Global collections @@ -605,18 +607,19 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic deleteTransactions([transaction.transactionID], duplicateTransactions, duplicateTransactionViolations, currentSearchHash, false); return; } - const goBackRoute = getNavigationUrlOnMoneyRequestDelete( - transaction.transactionID, - requestParentReportAction, + const goBackRoute = getNavigationUrlOnMoneyRequestDelete({ + transactionID: transaction.transactionID, + reportAction: requestParentReportAction, transactionThreadReport, iouReport, - chatIOUReport, - isChatIOUReportArchived, + iouReportTransactions, + chatReport: chatIOUReport, + isChatReportArchived: isChatIOUReportArchived, getCurrencyDecimals, - accountID, - false, - chatIOUReportActions, - ); + currentUserAccountID: accountID, + isSingleTransactionView: false, + chatReportActions: chatIOUReportActions, + }); const deleteNavigateBackUrl = goBackRoute ?? backTo ?? Navigation.getActiveRoute(); setDeleteTransactionNavigateBackUrl(deleteNavigateBackUrl); // The wide RHP close animation was changed, and because of that the expense was deleted right after diff --git a/src/libs/actions/IOU/DeleteMoneyRequest.ts b/src/libs/actions/IOU/DeleteMoneyRequest.ts index f53e8968005f..8b26fa1883f0 100644 --- a/src/libs/actions/IOU/DeleteMoneyRequest.ts +++ b/src/libs/actions/IOU/DeleteMoneyRequest.ts @@ -14,7 +14,6 @@ import { getOutstandingChildRequest, getPersonalDetailsForAccountID, getReimbursableTotal, - getReportTransactions, getUnheldReimbursableTotal, hasOutstandingChildRequest, isExpenseReport, @@ -94,7 +93,7 @@ type PrepareToCleanUpMoneyRequestParams = { reportAction: OnyxTypes.ReportAction | undefined; transactionThreadReport: OnyxEntry; iouReport: OnyxEntry; - iouReportTransactions?: OnyxTypes.Transaction[]; + iouReportTransactions: OnyxTypes.Transaction[]; chatReport: OnyxEntry; isChatReportArchived: boolean | undefined; getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals']; @@ -112,7 +111,7 @@ function prepareToCleanUpMoneyRequest({ reportAction, transactionThreadReport, iouReport, - iouReportTransactions = [], + iouReportTransactions, chatReport, isChatReportArchived, getCurrencyDecimals, @@ -169,7 +168,7 @@ function prepareToCleanUpMoneyRequest({ canUserPerformWriteAction = !!canUserPerformWriteActionReportUtils(chatReport, isChatReportArchived); } // If we are deleting the last transaction on a report, then delete the report too - const shouldDeleteIOUReport = getReportTransactions(iouReportID).filter((trans) => !transactionIDsPendingDeletion?.includes(trans.transactionID)).length === 1; + const shouldDeleteIOUReport = iouReportTransactions.filter((trans) => !transactionIDsPendingDeletion?.includes(trans.transactionID)).length === 1; const iouReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`]; if (shouldDeleteIOUReport) { @@ -335,25 +334,37 @@ function prepareToCleanUpMoneyRequest({ }; } +type GetNavigationUrlOnMoneyRequestDeleteParams = { + transactionID: string | undefined; + reportAction: OnyxTypes.ReportAction | undefined; + transactionThreadReport: OnyxEntry; + iouReport: OnyxEntry; + iouReportTransactions: OnyxTypes.Transaction[]; + chatReport: OnyxEntry; + isChatReportArchived: boolean | undefined; + getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals']; + currentUserAccountID: number; + isSingleTransactionView?: boolean; + chatReportActions?: OnyxEntry; +}; + /** * Calculate the URL to navigate to after a money request deletion - * @param transactionID - The ID of the money request being deleted - * @param reportAction - The report action associated with the money request - * @param isSingleTransactionView - whether we are in the transaction thread report * @returns The URL to navigate to */ -function getNavigationUrlOnMoneyRequestDelete( - transactionID: string | undefined, - reportAction: OnyxTypes.ReportAction | undefined, - transactionThreadReport: OnyxEntry, - iouReport: OnyxEntry, - chatReport: OnyxEntry, - isChatReportArchived: boolean | undefined, - getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'], - currentUserAccountID: number, +function getNavigationUrlOnMoneyRequestDelete({ + transactionID, + reportAction, + transactionThreadReport, + iouReport, + iouReportTransactions, + chatReport, + isChatReportArchived, + getCurrencyDecimals, + currentUserAccountID, isSingleTransactionView = false, - chatReportActions?: OnyxEntry, -): Route | undefined { + chatReportActions, +}: GetNavigationUrlOnMoneyRequestDeleteParams): Route | undefined { if (!transactionID) { return undefined; } @@ -362,6 +373,7 @@ function getNavigationUrlOnMoneyRequestDelete( reportAction, transactionThreadReport, iouReport, + iouReportTransactions, chatReport, isChatReportArchived, getCurrencyDecimals, @@ -451,18 +463,19 @@ function cleanUpMoneyRequest({ chatReportActions, }); - const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete( + const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete({ transactionID, reportAction, transactionThreadReport, iouReport, + iouReportTransactions, chatReport, - isChatIOUReportArchived, + isChatReportArchived: isChatIOUReportArchived, getCurrencyDecimals, currentUserAccountID, isSingleTransactionView, chatReportActions, - ); + }); // build Onyx data // Onyx operations to delete the transaction, update the IOU report action and chat report action @@ -874,18 +887,19 @@ function deleteMoneyRequest({ chatReportActions, }); - const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete( + const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete({ transactionID, reportAction, transactionThreadReport, iouReport, + iouReportTransactions, chatReport, - isChatIOUReportArchived, + isChatReportArchived: isChatIOUReportArchived, getCurrencyDecimals, currentUserAccountID, isSingleTransactionView, chatReportActions, - ); + }); // STEP 2: Build Onyx data // The logic mostly resembles the cleanUpMoneyRequest function diff --git a/src/libs/actions/IOU/TrackExpense.ts b/src/libs/actions/IOU/TrackExpense.ts index 13b177dad0de..541d8c9ca273 100644 --- a/src/libs/actions/IOU/TrackExpense.ts +++ b/src/libs/actions/IOU/TrackExpense.ts @@ -3140,26 +3140,37 @@ function trackExpense( return {iouReport, reportPreviewAction, transactionID: transaction?.transactionID}; } +type GetNavigationUrlAfterTrackExpenseDeleteParams = { + chatReportID: string | undefined; + chatReport: OnyxEntry | undefined; + transactionID: string | undefined; + reportAction: OnyxTypes.ReportAction; + iouReport: OnyxEntry; + iouReportTransactions: OnyxTypes.Transaction[]; + chatIOUReport: OnyxEntry; + isChatReportArchived: boolean | undefined; + getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals']; + isSingleTransactionView: boolean | undefined; + currentUserAccountID: number; +}; + /** * Calculate the URL to navigate to after a track expense deletion - * @param chatReportID - The ID of the chat report containing the track expense - * @param transactionID - The ID of the track expense being deleted - * @param reportAction - The report action associated with the track expense - * @param isSingleTransactionView - Whether we're in single transaction view * @returns The URL to navigate to */ -function getNavigationUrlAfterTrackExpenseDelete( - chatReportID: string | undefined, - chatReport: OnyxEntry | undefined, - transactionID: string | undefined, - reportAction: OnyxTypes.ReportAction, - iouReport: OnyxEntry, - chatIOUReport: OnyxEntry, - isChatReportArchived: boolean | undefined, - getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'], - isSingleTransactionView: boolean | undefined, - currentUserAccountID: number, -): Route | undefined { +function getNavigationUrlAfterTrackExpenseDelete({ + chatReportID, + chatReport, + transactionID, + reportAction, + iouReport, + iouReportTransactions, + chatIOUReport, + isChatReportArchived, + getCurrencyDecimals, + isSingleTransactionView, + currentUserAccountID, +}: GetNavigationUrlAfterTrackExpenseDeleteParams): Route | undefined { if (!chatReportID || !transactionID) { return undefined; } @@ -3168,17 +3179,18 @@ function getNavigationUrlAfterTrackExpenseDelete( if (!isSelfDM(chatReport)) { const allReports = getAllReports(); const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`]; - return getNavigationUrlOnMoneyRequestDelete( + return getNavigationUrlOnMoneyRequestDelete({ transactionID, reportAction, transactionThreadReport, iouReport, - chatIOUReport, + iouReportTransactions, + chatReport: chatIOUReport, isChatReportArchived, getCurrencyDecimals, currentUserAccountID, isSingleTransactionView, - ); + }); } // Only navigate if in single transaction view and the thread will be deleted @@ -3216,18 +3228,19 @@ function deleteTrackExpense({ return; } - const urlToNavigateBack = getNavigationUrlAfterTrackExpenseDelete( + const urlToNavigateBack = getNavigationUrlAfterTrackExpenseDelete({ chatReportID, chatReport, transactionID, reportAction, iouReport, + iouReportTransactions, chatIOUReport, - isChatIOUReportArchived, + isChatReportArchived: isChatIOUReportArchived, getCurrencyDecimals, isSingleTransactionView, currentUserAccountID, - ); + }); const allReports = getAllReports(); const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`]; diff --git a/src/pages/DynamicReportDetailsPage/hooks/useReportDetailsDeleteModal.ts b/src/pages/DynamicReportDetailsPage/hooks/useReportDetailsDeleteModal.ts index 962e31216d15..2315487c1653 100644 --- a/src/pages/DynamicReportDetailsPage/hooks/useReportDetailsDeleteModal.ts +++ b/src/pages/DynamicReportDetailsPage/hooks/useReportDetailsDeleteModal.ts @@ -49,6 +49,7 @@ function useReportDetailsDeleteModal(reportID: string, caseID: CaseID, parentRep moneyRequestReport, iouTransactionID, iouReport, + iouReportTransactions = [], chatIOUReport, isChatIOUReportArchived, isSingleTransactionView, @@ -87,31 +88,33 @@ function useReportDetailsDeleteModal(reportID: string, caseID: CaseID, parentRep const isTrackExpense = isTrackExpenseAction(requestParentReportAction); if (isTrackExpense) { - urlToNavigateBack = getNavigationUrlAfterTrackExpenseDelete( - moneyRequestReport?.reportID, - moneyRequestReport, - iouTransactionID, - requestParentReportAction, + urlToNavigateBack = getNavigationUrlAfterTrackExpenseDelete({ + chatReportID: moneyRequestReport?.reportID, + chatReport: moneyRequestReport, + transactionID: iouTransactionID, + reportAction: requestParentReportAction, iouReport, + iouReportTransactions, chatIOUReport, - isChatIOUReportArchived, + isChatReportArchived: isChatIOUReportArchived, getCurrencyDecimals, isSingleTransactionView, currentUserAccountID, - ); + }); } else { - urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete( - iouTransactionID, - requestParentReportAction, - requestParentReportActionChildReport, + urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete({ + transactionID: iouTransactionID, + reportAction: requestParentReportAction, + transactionThreadReport: requestParentReportActionChildReport, iouReport, - chatIOUReport, - isChatIOUReportArchived, + iouReportTransactions, + chatReport: chatIOUReport, + isChatReportArchived: isChatIOUReportArchived, getCurrencyDecimals, currentUserAccountID, isSingleTransactionView, - chatIOUReportActions, - ); + chatReportActions: chatIOUReportActions, + }); } } diff --git a/tests/actions/IOUTest/DeleteMoneyRequestTest.ts b/tests/actions/IOUTest/DeleteMoneyRequestTest.ts index 8663948afba4..bc7cd88d04f1 100644 --- a/tests/actions/IOUTest/DeleteMoneyRequestTest.ts +++ b/tests/actions/IOUTest/DeleteMoneyRequestTest.ts @@ -91,6 +91,12 @@ const RORY_PERSONAL_DETAILS_BY_LOGINS: PersonalDetailsByLogin = { }; const CARLOS_ACCOUNT_ID = 1; +/** Reads every transaction on the report from Onyx, so a test passes deleteMoneyRequest the same list the app would. */ +async function getReportTransactionsFromOnyx(reportID: string | undefined): Promise { + const allTransactions = await getOnyxValue(ONYXKEYS.COLLECTION.TRANSACTION); + return Object.values(allTransactions ?? {}).filter((transaction): transaction is Transaction => !!transaction && transaction.reportID === reportID); +} + OnyxUpdateManager(); describe('actions/IOU/DeleteMoneyRequest', () => { @@ -550,7 +556,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => { transactions: {}, violations: {}, iouReport, - iouReportTransactions: transaction ? [transaction] : [], + iouReportTransactions: await getReportTransactionsFromOnyx(iouReport?.reportID), chatReport, transactionThreadReport: thread, allTransactionViolationsParam: {}, @@ -652,7 +658,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => { reportID: iouReport.reportID, transactionThreadReport: thread, iouReport, - iouReportTransactions: [transaction], + iouReportTransactions: await getReportTransactionsFromOnyx(iouReport.reportID), chatReport, isChatIOUReportArchived: false, originalReportID: iouReport.reportID, @@ -1360,7 +1366,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => { transactions: {}, violations: {}, iouReport, - iouReportTransactions: transaction ? [transaction] : [], + iouReportTransactions: await getReportTransactionsFromOnyx(iouReport?.reportID), chatReport, transactionThreadReport: thread, isChatIOUReportArchived: false, @@ -1483,7 +1489,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => { transactions: {}, violations: {}, iouReport, - iouReportTransactions: transaction ? [transaction] : [], + iouReportTransactions: await getReportTransactionsFromOnyx(iouReport?.reportID), chatReport, transactionThreadReport: thread, isSingleTransactionView: true, @@ -1731,7 +1737,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => { transactions: {}, violations: {}, iouReport, - iouReportTransactions: transaction ? [transaction] : [], + iouReportTransactions: await getReportTransactionsFromOnyx(iouReport?.reportID), chatReport, transactionThreadReport: thread, allTransactionViolationsParam: {}, @@ -3217,7 +3223,18 @@ describe('actions/IOU/DeleteMoneyRequest', () => { const getLastVisibleActionSpy = jest.spyOn(require('@libs/ReportActionsUtils'), 'getLastVisibleAction'); // When the navigation URL is calculated for the current user - getNavigationUrlOnMoneyRequestDelete(DELETED_TRANSACTION_ID, deletedIOUAction, undefined, iouReport, chatReport, false, getCurrencyDecimalsLocal, CURRENT_USER_ACCOUNT_ID, false); + getNavigationUrlOnMoneyRequestDelete({ + transactionID: DELETED_TRANSACTION_ID, + reportAction: deletedIOUAction, + transactionThreadReport: undefined, + iouReport, + iouReportTransactions: await getReportTransactionsFromOnyx(IOU_REPORT_ID), + chatReport, + isChatReportArchived: false, + getCurrencyDecimals: getCurrencyDecimalsLocal, + currentUserAccountID: CURRENT_USER_ACCOUNT_ID, + isSingleTransactionView: false, + }); // Then the last visible action is resolved with that user's whisper visibility instead of the session fallback expect(getLastVisibleActionSpy).toHaveBeenCalledWith(IOU_REPORT_ID, expect.anything(), expect.anything(), undefined, undefined, CURRENT_USER_ACCOUNT_ID); diff --git a/tests/ui/DynamicReportDetailsPageTest.tsx b/tests/ui/DynamicReportDetailsPageTest.tsx index c40c8dd38207..b30a02b1f7b6 100644 --- a/tests/ui/DynamicReportDetailsPageTest.tsx +++ b/tests/ui/DynamicReportDetailsPageTest.tsx @@ -334,6 +334,8 @@ describe('DynamicReportDetailsPage', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, transactionThreadReport); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${invoiceReportID}`, {[iouActionID]: iouAction}); await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction); + // The page reads the invoice's transactions from the derived value, which this test doesn't compute, so seed it. + await Onyx.merge(ONYXKEYS.DERIVED.REPORT_TRANSACTIONS_AND_VIOLATIONS, {[invoiceReportID]: {transactions: {[transactionID]: transaction}, violations: {}}}); await Onyx.set(ONYXKEYS.IS_LOADING_REPORT_DATA, false); await Onyx.merge(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${invoiceReportID}`, {isLoadingInitialReportActions: false}); });