diff --git a/src/libs/API/parameters/RejectMoneyRequestParams.ts b/src/libs/API/parameters/RejectMoneyRequestParams.ts index a49c17bac363e..193b78a479dba 100644 --- a/src/libs/API/parameters/RejectMoneyRequestParams.ts +++ b/src/libs/API/parameters/RejectMoneyRequestParams.ts @@ -3,8 +3,12 @@ type RejectMoneyRequestParams = { reportID: string; comment: string; rejectedToReportID?: string; + reportPreviewReportActionID?: string; rejectedActionReportActionID: string; rejectedCommentReportActionID: string; + createdIOUReportActionID?: string; + expenseMovedReportActionID?: string; + expenseCreatedReportActionID?: string; }; export default RejectMoneyRequestParams; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0f7c976896179..d75268e21633e 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -78,7 +78,7 @@ import Parser from '@libs/Parser'; import {getCustomUnitID} from '@libs/PerDiemRequestUtils'; import Performance from '@libs/Performance'; import Permissions from '@libs/Permissions'; -import {getAccountIDsByLogins} from '@libs/PersonalDetailsUtils'; +import {getAccountIDsByLogins, getLoginByAccountID} from '@libs/PersonalDetailsUtils'; import {addSMSDomainIfPhoneNumber} from '@libs/PhoneNumber'; import { getCorrectedAutoReportingFrequency, @@ -12741,6 +12741,7 @@ function rejectMoneyRequest(transactionID: string, reportID: string, comment: st const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; const transactionAmount = getAmount(transaction); const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; + const policyExpenseChat = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.chatReportID}`]; const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]; const isPolicyDelayedSubmissionEnabled = policy ? isDelayedSubmissionEnabled(policy) : false; const isIOU = isIOUReport(report); @@ -12755,10 +12756,15 @@ function rejectMoneyRequest(transactionID: string, reportID: string, comment: st const reportAction = getIOUActionForReportID(reportID, transactionID); const childReportID = reportAction?.childReportID; + const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`]; let movedToReport; let rejectedToReportID; let urlToNavigateBack; + let reportPreviewAction: OnyxTypes.ReportAction | undefined; + let createdIOUReportActionID; + let expenseMovedReportActionID; + let expenseCreatedReportActionID; const hasMultipleExpenses = getReportTransactions(reportID).length > 1; @@ -12770,6 +12776,7 @@ function rejectMoneyRequest(transactionID: string, reportID: string, comment: st const baseTimestamp = DateUtils.getDBTime(); const optimisticRejectReportAction = buildOptimisticRejectReportAction(baseTimestamp); const optimisticRejectReportActionComment = buildOptimisticRejectReportActionComment(comment, DateUtils.addMillisecondsFromDateTime(baseTimestamp, 1)); + let movedTransactionAction; // Build successData and failureData to prevent duplication const successData: OnyxUpdate[] = []; @@ -12938,33 +12945,234 @@ function rejectMoneyRequest(transactionID: string, reportID: string, comment: st if (existingOpenReport) { movedToReport = existingOpenReport; rejectedToReportID = existingOpenReport.reportID; - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, - value: { - ...movedToReport, - total: (movedToReport?.total ?? 0) - transactionAmount, - }, + + const [, , iouAction, ,] = buildOptimisticMoneyRequestEntities({ + iouReport: movedToReport, + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + amount: transactionAmount, + currency: getCurrency(transaction), + comment, + payeeEmail: getLoginByAccountID(report.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID) ?? '', + participants: [{accountID: report?.ownerAccountID}], + transactionID: transaction.transactionID, + existingTransactionThreadReportID: childReportID, + shouldGenerateTransactionThreadReport: false, }); + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, + value: { + ...movedToReport, + total: (movedToReport?.total ?? 0) - transactionAmount, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${rejectedToReportID}`, + value: {[iouAction.reportActionID]: iouAction}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${childReportID}`, + value: { + parentReportActionID: iouAction.reportActionID, + parentReportID: rejectedToReportID, + }, + }, + ); + // Add success data for existing report update - successData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, - value: {pendingFields: {total: null}}, - }); + successData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, + value: {pendingFields: {total: null}}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${rejectedToReportID}`, + value: {[iouAction.reportActionID]: {pendingAction: null}}, + }, + ); - // Add failure data to revert existing report total - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, - value: { - total: movedToReport?.total ?? 0, - pendingFields: {total: null}, + failureData.push( + // Add failure data to revert existing report total + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${movedToReport?.reportID}`, + value: { + total: movedToReport?.total ?? 0, + pendingFields: {total: null}, + }, }, - }); + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${childReportID}`, + value: { + parentReportActionID: transactionThreadReport?.parentReportActionID, + parentReportID: transactionThreadReport?.parentReportID, + }, + }, + ); } else { + // Create optimistic report for the rejected transaction rejectedToReportID = generateReportID(); + const newExpenseReport = buildOptimisticExpenseReport( + report.chatReportID, + report?.policyID, + report?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID, + transactionAmount, + getCurrency(transaction), + transactionAmount, + undefined, + rejectedToReportID, + ); + const [, createdActionForExpenseReport, iouAction, ,] = buildOptimisticMoneyRequestEntities({ + iouReport: newExpenseReport, + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + amount: transactionAmount, + currency: getCurrency(transaction), + comment, + payeeEmail: currentUserEmail, + participants: [{accountID: report?.ownerAccountID}], + transactionID: transaction.transactionID, + existingTransactionThreadReportID: childReportID, + shouldGenerateTransactionThreadReport: false, + }); + + reportPreviewAction = buildOptimisticReportPreview(policyExpenseChat, newExpenseReport, undefined, transaction, undefined); + movedTransactionAction = buildOptimisticMovedTransactionAction(childReportID, newExpenseReport.reportID); + createdIOUReportActionID = iouAction.reportActionID; + expenseMovedReportActionID = movedTransactionAction.reportActionID; + expenseCreatedReportActionID = createdActionForExpenseReport.reportActionID; + newExpenseReport.parentReportActionID = reportPreviewAction.reportActionID; + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat?.reportID}`, + value: { + lastVisibleActionCreated: reportPreviewAction.created, + }, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${rejectedToReportID}`, + value: { + ...newExpenseReport, + pendingFields: {createReport: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}, + }, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${rejectedToReportID}`, + value: { + isOptimisticReport: true, + hasOnceLoadedReportActions: true, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${rejectedToReportID}`, + value: {[createdActionForExpenseReport.reportActionID]: createdActionForExpenseReport, [iouAction.reportActionID]: iouAction}, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${rejectedToReportID}`, + value: { + parentReportID: report?.chatReportID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChat?.reportID}`, + value: { + [reportPreviewAction.reportActionID]: reportPreviewAction, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${childReportID}`, + value: { + parentReportActionID: iouAction.reportActionID, + parentReportID: rejectedToReportID, + }, + }, + ); + successData.push( + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${rejectedToReportID}`, + value: { + pendingFields: null, + }, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${rejectedToReportID}`, + value: { + isOptimisticReport: null, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${rejectedToReportID}`, + value: {[createdActionForExpenseReport.reportActionID]: {pendingAction: null}, [iouAction.reportActionID]: {pendingAction: null}}, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChat?.reportID}`, + value: { + [reportPreviewAction.reportActionID]: {pendingAction: null}, + }, + }, + ); + + failureData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${policyExpenseChat?.reportID}`, + value: { + lastVisibleActionCreated: policyExpenseChat?.lastVisibleActionCreated, + }, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${rejectedToReportID}`, + value: null, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${rejectedToReportID}`, + value: null, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${rejectedToReportID}`, + value: null, + }, + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${rejectedToReportID}`, + value: null, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChat?.reportID}`, + value: { + [reportPreviewAction.reportActionID]: null, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${childReportID}`, + value: { + parentReportActionID: transactionThreadReport?.parentReportActionID, + parentReportID: transactionThreadReport?.parentReportID, + }, + }, + ); } optimisticData.push( { @@ -13069,6 +13277,7 @@ function rejectMoneyRequest(transactionID: string, reportID: string, comment: st value: { [optimisticRejectReportAction.reportActionID]: optimisticRejectReportAction, [optimisticRejectReportActionComment.reportActionID]: optimisticRejectReportActionComment, + ...(movedTransactionAction ? {[movedTransactionAction.reportActionID]: movedTransactionAction} : {}), }, }); @@ -13187,8 +13396,12 @@ function rejectMoneyRequest(transactionID: string, reportID: string, comment: st reportID, comment, rejectedToReportID, + reportPreviewReportActionID: reportPreviewAction?.reportActionID, rejectedActionReportActionID: optimisticRejectReportAction.reportActionID, rejectedCommentReportActionID: optimisticRejectReportActionComment.reportActionID, + createdIOUReportActionID, + expenseMovedReportActionID, + expenseCreatedReportActionID, }; // Make API call