From ab38d478ac912b06529bd0e1b1eb5381cbdce8e2 Mon Sep 17 00:00:00 2001 From: Joe Gambino Date: Mon, 15 Mar 2021 18:51:24 -0700 Subject: [PATCH] start persisting IOUReport data in onyx --- src/ONYXKEYS.js | 1 + src/libs/actions/Report.js | 81 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index b9057c941645..feb0ae043567 100644 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -61,5 +61,6 @@ export default { REPORT_ACTIONS: 'reportActions_', REPORT_DRAFT_COMMENT: 'reportDraftComment_', REPORT_USER_IS_TYPING: 'reportUserIsTyping_', + REPORT_IOUS: 'reportIOUs_', }, }; diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 3a746aef0156..b88262803e42 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -495,6 +495,85 @@ function fetchChatReports() { }); } +/** + * Get a simplified version of an IOU report + * + * @param {Number} reportID + * @param {Object} reportData + * @param {Number} reportData.transactionID + * @param {Number} reportData.amount + * @param {String} reportData.currency + * @param {String} reportData.created + * @param {String} reportData.comment + * @param {Object[]} reportData.transactionList + * @param {String} reportData.ownerEmail + * @param {String} reportData.managerEmail + * @returns {Object} + */ +function getSimplifiedIOUReport(reportID, reportData) { + const transactions = _.map(reportData.transactionList, transaction => ({ + transactionID: transaction.transactionID, + amount: transaction.amount, + currency: transaction.currency, + created: transaction.created, + comment: transaction.comment, + })); + + return { + reportID, + ownerEmail: reportData.ownerEmail, + managerEmail: reportData.managerEmail, + currency: reportData.currency, + transactions, + }; +} + +/** + * Fetches the updated data for an IOU Report and updates the IOU collection in ONYX + * + * @param {Number} reportID + * @param {Object[]} reportHistory + */ +function updateIOUReportData(reportID, reportHistory) { + const containsIOUAction = _.any(reportHistory, action => action.actionName === 'IOU'); + + // If there aren't any IOU actions, we don't need to fetch any additional data + if (!containsIOUAction) { + return; + } + + const otherParticipants = _.chain(reportHistory) + .pluck('actorEmail') + .unique() + .without(currentUserEmail) + .value(); + + // If we have more than one participant, this is not an IOU + if (otherParticipants.length > 1) { + return; + } + + // If we have an IOU action, get the IOU reportID + let iouReportID = 0; + API.GetIOUReport({ + debtorEmail: otherParticipants[0], + }).then((response) => { + iouReportID = response.reportID; + + Log.info('[Report] Fetching IOU report data', true, iouReportID); + return API.Get({ + returnValueList: 'reportStuff', + reportIDList: iouReportID, + shouldLoadOptionalKeys: true, + includePinnedReports: true, + }); + }).then((response) => { + const iouReportData = response.reports[iouReportID]; + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReportID}`, + getSimplifiedIOUReport(iouReportID, iouReportData)); + }); +} + /** * Get the actions of a report * @@ -529,6 +608,8 @@ function fetchActions(reportID, offset) { .max() .value(); + updateIOUReportData(reportID, indexedData); + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, indexedData); Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {maxSequenceNumber}); });