Skip to content
21 changes: 12 additions & 9 deletions src/hooks/useExpenseActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
62 changes: 38 additions & 24 deletions src/libs/actions/IOU/DeleteMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
getOutstandingChildRequest,
getPersonalDetailsForAccountID,
getReimbursableTotal,
getReportTransactions,
getUnheldReimbursableTotal,
hasOutstandingChildRequest,
isExpenseReport,
Expand Down Expand Up @@ -94,7 +93,7 @@ type PrepareToCleanUpMoneyRequestParams = {
reportAction: OnyxTypes.ReportAction | undefined;
transactionThreadReport: OnyxEntry<OnyxTypes.Report>;
iouReport: OnyxEntry<OnyxTypes.Report>;
iouReportTransactions?: OnyxTypes.Transaction[];
iouReportTransactions: OnyxTypes.Transaction[];
chatReport: OnyxEntry<OnyxTypes.Report>;
isChatReportArchived: boolean | undefined;
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'];
Expand All @@ -112,7 +111,7 @@ function prepareToCleanUpMoneyRequest({
reportAction,
transactionThreadReport,
iouReport,
iouReportTransactions = [],
iouReportTransactions,
chatReport,
isChatReportArchived,
getCurrencyDecimals,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -335,25 +334,37 @@ function prepareToCleanUpMoneyRequest({
};
}

type GetNavigationUrlOnMoneyRequestDeleteParams = {
transactionID: string | undefined;
reportAction: OnyxTypes.ReportAction | undefined;
transactionThreadReport: OnyxEntry<OnyxTypes.Report>;
iouReport: OnyxEntry<OnyxTypes.Report>;
iouReportTransactions: OnyxTypes.Transaction[];
chatReport: OnyxEntry<OnyxTypes.Report>;
isChatReportArchived: boolean | undefined;
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'];
currentUserAccountID: number;
isSingleTransactionView?: boolean;
chatReportActions?: OnyxEntry<OnyxTypes.ReportActions>;
};

/**
* 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<OnyxTypes.Report>,
iouReport: OnyxEntry<OnyxTypes.Report>,
chatReport: OnyxEntry<OnyxTypes.Report>,
isChatReportArchived: boolean | undefined,
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'],
currentUserAccountID: number,
function getNavigationUrlOnMoneyRequestDelete({
transactionID,
reportAction,
transactionThreadReport,
iouReport,
iouReportTransactions,
chatReport,
isChatReportArchived,
getCurrencyDecimals,
currentUserAccountID,
isSingleTransactionView = false,
chatReportActions?: OnyxEntry<OnyxTypes.ReportActions>,
): Route | undefined {
chatReportActions,
}: GetNavigationUrlOnMoneyRequestDeleteParams): Route | undefined {
if (!transactionID) {
return undefined;
}
Expand All @@ -362,6 +373,7 @@ function getNavigationUrlOnMoneyRequestDelete(
reportAction,
transactionThreadReport,
iouReport,
iouReportTransactions,
chatReport,
isChatReportArchived,
getCurrencyDecimals,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
57 changes: 35 additions & 22 deletions src/libs/actions/IOU/TrackExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3140,26 +3140,37 @@ function trackExpense(
return {iouReport, reportPreviewAction, transactionID: transaction?.transactionID};
}

type GetNavigationUrlAfterTrackExpenseDeleteParams = {
chatReportID: string | undefined;
chatReport: OnyxEntry<OnyxTypes.Report> | undefined;
transactionID: string | undefined;
reportAction: OnyxTypes.ReportAction;
iouReport: OnyxEntry<OnyxTypes.Report>;
iouReportTransactions: OnyxTypes.Transaction[];
chatIOUReport: OnyxEntry<OnyxTypes.Report>;
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<OnyxTypes.Report> | undefined,
transactionID: string | undefined,
reportAction: OnyxTypes.ReportAction,
iouReport: OnyxEntry<OnyxTypes.Report>,
chatIOUReport: OnyxEntry<OnyxTypes.Report>,
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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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}`];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function useReportDetailsDeleteModal(reportID: string, caseID: CaseID, parentRep
moneyRequestReport,
iouTransactionID,
iouReport,
iouReportTransactions = [],
chatIOUReport,
isChatIOUReportArchived,
isSingleTransactionView,
Expand Down Expand Up @@ -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,
});
}
}

Expand Down
Loading
Loading