diff --git a/src/libs/actions/IOU/RejectMoneyRequest.ts b/src/libs/actions/IOU/RejectMoneyRequest.ts index f46a421393a..d41e75fab2b 100644 --- a/src/libs/actions/IOU/RejectMoneyRequest.ts +++ b/src/libs/actions/IOU/RejectMoneyRequest.ts @@ -72,6 +72,11 @@ type RejectMoneyRequestData = { urlToNavigateBack: Route | undefined; }; +type RejectMoneyRequestOptions = { + sharedRejectedToReportID?: string; + existingRejectedReport?: OnyxEntry; +}; + function dismissRejectUseExplanation() { const parameters: SetNameValuePairParams = { name: ONYXKEYS.NVP_DISMISSED_REJECT_USE_EXPLANATION, @@ -108,12 +113,13 @@ function prepareRejectMoneyRequestData( currentUserAccountIDParam: number, currentUserLogin: string, betas: OnyxEntry, - options?: {sharedRejectedToReportID?: string}, + options?: RejectMoneyRequestOptions, shouldUseBulkAction?: boolean, ): RejectMoneyRequestData | undefined { const allTransactions = getAllTransactions(); const allReports = getAllReports(); const allTransactionViolations = getAllTransactionViolations(); + const sharedRejectOptions = options; const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; const transactionAmount = getAmount(transaction); @@ -348,17 +354,26 @@ function prepareRejectMoneyRequestData( // 1. Update report total // 2. Remove expense from report // 3. Add to existing draft report or create new one - const existingOpenReport = Object.values(allReports ?? {}).find( - (r) => - r?.reportID !== reportID && - r?.chatReportID === report.chatReportID && - r?.type === CONST.REPORT.TYPE.EXPENSE && - isOpenReport(r) && - r?.ownerAccountID === report.ownerAccountID, - ); + const existingOpenReport = + options?.existingRejectedReport ?? + Object.values(allReports ?? {}).find( + (r) => + r?.reportID !== reportID && + r?.chatReportID === report.chatReportID && + r?.type === CONST.REPORT.TYPE.EXPENSE && + isOpenReport(r) && + r?.ownerAccountID === report.ownerAccountID, + ); if (existingOpenReport) { - movedToReport = existingOpenReport; + const originalRejectedReportTotal = existingOpenReport?.total ?? 0; + movedToReport = { + ...existingOpenReport, + total: originalRejectedReportTotal - transactionAmount, + }; + if (sharedRejectOptions) { + sharedRejectOptions.existingRejectedReport = movedToReport; + } rejectedToReportID = existingOpenReport.reportID; const [, , iouAction] = buildOptimisticMoneyRequestEntities({ @@ -380,10 +395,7 @@ function prepareRejectMoneyRequestData( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, - value: { - ...movedToReport, - total: (movedToReport?.total ?? 0) - transactionAmount, - }, + value: movedToReport, }, { onyxMethod: Onyx.METHOD.MERGE, @@ -420,7 +432,7 @@ function prepareRejectMoneyRequestData( onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, value: { - total: movedToReport?.total ?? 0, + total: originalRejectedReportTotal, pendingFields: {total: null}, }, }, @@ -473,6 +485,9 @@ function prepareRejectMoneyRequestData( expenseMovedReportActionID = movedTransactionAction.reportActionID; expenseCreatedReportActionID = createdActionForExpenseReport.reportActionID; newExpenseReport.parentReportActionID = reportPreviewAction.reportActionID; + if (sharedRejectOptions) { + sharedRejectOptions.existingRejectedReport = newExpenseReport; + } optimisticData.push( { onyxMethod: Onyx.METHOD.MERGE, @@ -886,7 +901,7 @@ function rejectMoneyRequest( currentUserAccountIDParam: number, currentUserLogin: string, betas: OnyxEntry, - options?: {sharedRejectedToReportID?: string}, + options?: RejectMoneyRequestOptions, ): Route | undefined { const data = prepareRejectMoneyRequestData(transactionID, reportID, comment, policy, currentUserAccountIDParam, currentUserLogin, betas, options); if (!data) { diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index dfb748fe457..f9e241a0d05 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -1088,9 +1088,9 @@ function rejectMoneyRequestsOnSearch( rejectMoneyRequestInBulk(reportID, comment, policy, selectedTransactionIDs, currentUserAccountIDParam, currentUserLogin, betas, hash); } else { // Share a single destination ID across all rejections from the same source report - const sharedRejectedToReportID = generateReportID(); + const sharedRejectOptions = {sharedRejectedToReportID: generateReportID()}; for (const transactionID of selectedTransactionIDs) { - rejectMoneyRequest(transactionID, reportID, comment, policy, currentUserAccountIDParam, currentUserLogin, betas, {sharedRejectedToReportID}); + rejectMoneyRequest(transactionID, reportID, comment, policy, currentUserAccountIDParam, currentUserLogin, betas, sharedRejectOptions); } } if (isSingleReport && areAllExpensesSelected && !isPolicyDelayedSubmissionEnabled) {