diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 00b8753a405f..a4b2e4796859 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1081,10 +1081,10 @@ function getTransactionReportName(reportAction) { * Get money request message for an IOU report * * @param {Object} report - * @param {Object} reportAction + * @param {Object} [reportAction={}] * @returns {String} */ -function getReportPreviewMessage(report, reportAction) { +function getReportPreviewMessage(report, reportAction = {}) { const reportActionMessage = lodashGet(reportAction, 'message[0].html', ''); if (_.isEmpty(report) || !report.reportID) { @@ -1561,26 +1561,27 @@ function buildOptimisticIOUReportAction(type, amount, currency, comment, partici }; } -function buildOptimisticReportPreview(reportID, iouReportID, payeeAccountID) { +function buildOptimisticReportPreview(chatReport, iouReport) { + const message = getReportPreviewMessage(iouReport); return { reportActionID: NumberUtils.rand64(), - reportID, - created: DateUtils.getDBTime(), + reportID: chatReport.reportID, actionName: CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - accountID: payeeAccountID, + originalMessage: { + linkedReportID: iouReport.reportID, + }, message: [ { - html: '', - text: '', + html: message, + text: message, isEdited: false, type: CONST.REPORT.MESSAGE.TYPE.COMMENT, }, ], - originalMessage: { - linkedReportID: iouReportID, - }, - actorAccountID: currentUserAccountID, + created: DateUtils.getDBTime(), + accountID: iouReport.managerID || 0, + actorAccountID: iouReport.managerID || 0, }; } diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index e3bd124743b5..0612fce7b4be 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -89,7 +89,6 @@ function buildOnyxDataForMoneyRequest( reportPreviewAction, isNewChatReport, isNewIOUReport, - isNewReportPreviewAction, ) { const optimisticData = [ { @@ -124,9 +123,7 @@ function buildOnyxDataForMoneyRequest( key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, value: { ...(isNewChatReport ? {[chatCreatedAction.reportActionID]: chatCreatedAction} : {}), - [reportPreviewAction.reportActionID]: { - ...(isNewReportPreviewAction ? reportPreviewAction : {created: DateUtils.getDBTime()}), - }, + [reportPreviewAction.reportActionID]: reportPreviewAction, }, }, { @@ -189,13 +186,9 @@ function buildOnyxDataForMoneyRequest( }, } : {}), - ...(isNewReportPreviewAction - ? { - [reportPreviewAction.reportActionID]: { - pendingAction: null, - }, - } - : {}), + [reportPreviewAction.reportActionID]: { + pendingAction: null, + }, }, }, { @@ -387,11 +380,14 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part }, }; - let isNewReportPreviewAction = false; let reportPreviewAction = isNewIOUReport ? null : ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID); - if (!reportPreviewAction) { - isNewReportPreviewAction = true; - reportPreviewAction = ReportUtils.buildOptimisticReportPreview(chatReport.reportID, iouReport.reportID); + if (reportPreviewAction) { + reportPreviewAction.created = DateUtils.getDBTime(); + const message = ReportUtils.getReportPreviewMessage(iouReport, reportPreviewAction); + reportPreviewAction.message[0].html = message; + reportPreviewAction.message[0].text = message; + } else { + reportPreviewAction = ReportUtils.buildOptimisticReportPreview(chatReport, iouReport); } // STEP 5: Build Onyx Data @@ -406,7 +402,6 @@ function requestMoney(report, amount, currency, payeeEmail, payeeAccountID, part reportPreviewAction, isNewChatReport, isNewIOUReport, - isNewReportPreviewAction, ); // STEP 6: Make the request @@ -649,11 +644,14 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco }, }; - let isNewOneOnOneReportPreviewAction = false; let oneOnOneReportPreviewAction = ReportActionsUtils.getReportPreviewAction(oneOnOneChatReport.reportID, oneOnOneIOUReport.reportID); - if (!oneOnOneReportPreviewAction) { - isNewOneOnOneReportPreviewAction = true; - oneOnOneReportPreviewAction = ReportUtils.buildOptimisticReportPreview(oneOnOneChatReport.reportID, oneOnOneIOUReport.reportID); + if (oneOnOneReportPreviewAction) { + oneOnOneReportPreviewAction.created = DateUtils.getDBTime(); + const message = ReportUtils.getReportPreviewMessage(oneOnOneIOUReport, oneOnOneReportPreviewAction); + oneOnOneReportPreviewAction.message[0].html = message; + oneOnOneReportPreviewAction.message[0].text = message; + } else { + oneOnOneReportPreviewAction = ReportUtils.buildOptimisticReportPreview(oneOnOneChatReport, oneOnOneIOUReport); } // STEP 5: Build Onyx Data @@ -668,7 +666,6 @@ function createSplitsAndOnyxData(participants, currentUserLogin, currentUserAcco oneOnOneReportPreviewAction, isNewOneOnOneChatReport, isNewOneOnOneIOUReport, - isNewOneOnOneReportPreviewAction, ); const splitData = { @@ -944,6 +941,8 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType true, ); + const reportPreviewAction = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticIOUReport); + // First, add data that will be used in all cases const optimisticChatReportData = { onyxMethod: Onyx.METHOD.MERGE, @@ -951,7 +950,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType value: { ...chatReport, lastReadTime: DateUtils.getDBTime(), - lastVisibleActionCreated: optimisticIOUReportAction.created, + lastVisibleActionCreated: reportPreviewAction.created, }, }; const optimisticIOUReportData = { @@ -963,7 +962,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType lastMessageHtml: optimisticIOUReportAction.message[0].html, }, }; - const optimisticReportActionsData = { + const optimisticIOUReportActionsData = { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticIOUReport.reportID}`, value: { @@ -973,6 +972,13 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType }, }, }; + const optimisticChatReportActionsData = { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [reportPreviewAction.reportActionID]: reportPreviewAction, + }, + }; const successData = [ { @@ -989,6 +995,15 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType key: `${ONYXKEYS.COLLECTION.TRANSACTION}${optimisticTransaction.transactionID}`, value: {pendingAction: null}, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [reportPreviewAction.reportActionID]: { + pendingAction: null, + }, + }, + }, ]; const failureData = [ @@ -1008,7 +1023,6 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType // Change the method to set for new reports because it doesn't exist yet, is faster, // and we need the data to be available when we navigate to the chat page optimisticChatReportData.onyxMethod = Onyx.METHOD.SET; - optimisticReportActionsData.onyxMethod = Onyx.METHOD.SET; optimisticIOUReportData.onyxMethod = Onyx.METHOD.SET; // Set and clear pending fields on the chat report @@ -1053,8 +1067,8 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType }, }; - // Add an optimistic created action to the optimistic reportActions data - optimisticReportActionsData.value[optimisticCreatedAction.reportActionID] = optimisticCreatedAction; + // Add an optimistic created action to the optimistic chat reportActions data + optimisticChatReportActionsData.value[optimisticCreatedAction.reportActionID] = optimisticCreatedAction; } else { failureData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -1067,7 +1081,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType }); } - const optimisticData = [optimisticChatReportData, optimisticIOUReportData, optimisticReportActionsData, optimisticTransactionData]; + const optimisticData = [optimisticChatReportData, optimisticIOUReportData, optimisticChatReportActionsData, optimisticIOUReportActionsData, optimisticTransactionData]; if (!_.isEmpty(optimisticPersonalDetailListData)) { optimisticData.push(optimisticPersonalDetailListData); } @@ -1081,6 +1095,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType transactionID: optimisticTransaction.transactionID, newIOUReportDetails, createdReportActionID: isNewChat ? optimisticCreatedAction.reportActionID : 0, + reportPreviewReportActionID: reportPreviewAction.reportActionID, }, optimisticData, successData, @@ -1115,6 +1130,12 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho login: recipient.login, }; + const optimisticReportPreviewAction = ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID); + optimisticReportPreviewAction.created = DateUtils.getDBTime(); + const message = ReportUtils.getReportPreviewMessage(iouReport, optimisticReportPreviewAction); + optimisticReportPreviewAction.message[0].html = message; + optimisticReportPreviewAction.message[0].text = message; + const optimisticData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1139,6 +1160,13 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticReportPreviewAction.reportActionID]: optimisticReportPreviewAction, + }, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, @@ -1196,6 +1224,15 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticReportPreviewAction.reportActionID]: { + created: optimisticReportPreviewAction.created, + }, + }, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.TRANSACTION}${optimisticTransaction.transactionID}`,