diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 7aa9a5dcda51..0751999ccb4e 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -242,13 +242,16 @@ function requestMoney(report, amount, currency, recipientEmail, participant, com * @param {String} comment * @param {String} currency * @param {String} locale + * @param {String} existingGroupChatReportID * * @return {Object} */ -function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment, currency, locale) { +function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment, currency, locale, existingGroupChatReportID = '') { const currentUserEmail = OptionsListUtils.addSMSDomainIfPhoneNumber(currentUserLogin); const participantLogins = _.map(participants, participant => OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login).toLowerCase()); - const existingGroupChatReport = ReportUtils.getChatByParticipants(participantLogins); + const existingGroupChatReport = existingGroupChatReportID + ? chatReports[`${ONYXKEYS.COLLECTION.REPORT}${existingGroupChatReportID}`] + : ReportUtils.getChatByParticipants(participantLogins); const groupChatReport = existingGroupChatReport || ReportUtils.buildOptimisticChatReport(participantLogins); const groupCreatedReportAction = existingGroupChatReport ? {} : ReportUtils.buildOptimisticCreatedReportAction(currentUserEmail); const groupChatReportMaxSequenceNumber = lodashGet(groupChatReport, 'maxSequenceNumber', 0); @@ -333,8 +336,8 @@ function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment return; } - // If we only have one participant, the oneOnOneChatReport is the groupChatReport - const existingOneOnOneChatReport = hasMultipleParticipants ? ReportUtils.getChatByParticipants([email]) : groupChatReport; + // If we only have one participant and the request was initiated from the global create menu, i.e. !existingGroupChatReportID, the oneOnOneChatReport is the groupChatReport + const existingOneOnOneChatReport = (!hasMultipleParticipants && !existingGroupChatReportID) ? groupChatReport : ReportUtils.getChatByParticipants([email]); const oneOnOneChatReport = existingOneOnOneChatReport || ReportUtils.buildOptimisticChatReport([email]); let oneOnOneIOUReport; let existingIOUReport = null; @@ -382,77 +385,59 @@ function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment createChat: existingOneOnOneChatReport ? null : CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }; - // If we only have one other participant, we just need to update onyxData for the groupChatReport and add an iouReportAction of type = create - // If we have more participants, we need to push the new oneOnOneChaReport, the create reportAction and iouReportAction of type = create to onyxData - if (!hasMultipleParticipants) { - optimisticData[0].value = oneOnOneChatReport; - optimisticData[1].value = { - ...optimisticData[1].value, - [oneOnOneIOUReportAction.sequenceNumber]: oneOnOneIOUReportAction, - }; - successData[1].value = { - ...successData[1].value, - [oneOnOneIOUReportAction.sequenceNumber]: {pendingAction: null}, - }; - failureData[1].value = { - ...failureData[1].value, - [oneOnOneIOUReportAction.sequenceNumber]: {pendingAction: null}, - }; - } else { - optimisticData.push( - { - onyxMethod: existingOneOnOneChatReport ? CONST.ONYX.METHOD.MERGE : CONST.ONYX.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.reportID}`, - value: oneOnOneChatReport, - }, - { - onyxMethod: existingOneOnOneChatReport ? CONST.ONYX.METHOD.MERGE : CONST.ONYX.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oneOnOneChatReport.reportID}`, - value: { - ...oneOnOneCreatedReportAction, - [oneOnOneIOUReportAction.sequenceNumber]: oneOnOneIOUReportAction, - }, + optimisticData.push( + { + onyxMethod: existingOneOnOneChatReport ? CONST.ONYX.METHOD.MERGE : CONST.ONYX.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.reportID}`, + value: oneOnOneChatReport, + }, + { + onyxMethod: existingOneOnOneChatReport ? CONST.ONYX.METHOD.MERGE : CONST.ONYX.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oneOnOneChatReport.reportID}`, + value: { + ...oneOnOneCreatedReportAction, + [oneOnOneIOUReportAction.sequenceNumber]: oneOnOneIOUReportAction, }, - ); + }, + ); - successData.push( - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.reportID}`, - value: {pendingFields: {createChat: null}}, - }, - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oneOnOneChatReport.reportID}`, - value: { - 0: {pendingAction: null}, - [oneOnOneIOUReportAction.sequenceNumber]: {pendingAction: null}, - }, + successData.push( + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.reportID}`, + value: {pendingFields: {createChat: null}}, + }, + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oneOnOneChatReport.reportID}`, + value: { + 0: {pendingAction: null}, + [oneOnOneIOUReportAction.sequenceNumber]: {pendingAction: null}, }, - ); + }, + ); - failureData.push( - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.reportID}`, - value: { - pendingFields: {createChat: null}, - hasOutstandingIOU: existingOneOnOneChatReport ? existingOneOnOneChatReport.hasOutstandingIOU : false, - iouReportID: existingOneOnOneChatReport ? existingOneOnOneChatReport.iouReportID : null, - maxSequenceNumber: oneOnOneChatReportMaxSequenceNumber, - lastReadSequenceNumber: oneOnOneChatReportMaxSequenceNumber, - }, + failureData.push( + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.reportID}`, + value: { + pendingFields: {createChat: null}, + hasOutstandingIOU: existingOneOnOneChatReport ? existingOneOnOneChatReport.hasOutstandingIOU : false, + iouReportID: existingOneOnOneChatReport ? existingOneOnOneChatReport.iouReportID : null, + maxSequenceNumber: oneOnOneChatReportMaxSequenceNumber, + lastReadSequenceNumber: oneOnOneChatReportMaxSequenceNumber, }, - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oneOnOneChatReport.reportID}`, - value: { - 0: {pendingAction: null}, - [oneOnOneIOUReportAction.sequenceNumber]: {pendingAction: null}, - }, + }, + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${oneOnOneChatReport.reportID}`, + value: { + 0: {pendingAction: null}, + [oneOnOneIOUReportAction.sequenceNumber]: {pendingAction: null}, }, - ); - } + }, + ); // Regardless of the number of participants, we always want to push the iouReport update to onyxData optimisticData.push({ @@ -500,9 +485,10 @@ function createSplitsAndOnyxData(participants, currentUserLogin, amount, comment * @param {String} comment * @param {String} currency * @param {String} locale + * @param {String} existingGroupChatReportID */ -function splitBill(participants, currentUserLogin, amount, comment, currency, locale) { - const {groupData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, amount, comment, currency, locale); +function splitBill(participants, currentUserLogin, amount, comment, currency, locale, existingGroupChatReportID = '') { + const {groupData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, amount, comment, currency, locale, existingGroupChatReportID); API.write('SplitBill', { reportID: groupData.chatReportID, diff --git a/src/pages/iou/IOUModal.js b/src/pages/iou/IOUModal.js index 34033ea98f8a..19e4321b9a0b 100755 --- a/src/pages/iou/IOUModal.js +++ b/src/pages/iou/IOUModal.js @@ -326,6 +326,7 @@ class IOUModal extends Component { this.state.comment, this.props.iou.selectedCurrencyCode, this.props.preferredLocale, + reportID, ); return; }