Skip to content

Commit

Permalink
start persisting IOUReport data in onyx
Browse files Browse the repository at this point in the history
  • Loading branch information
Jag96 committed Mar 16, 2021
1 parent 9a5b7c4 commit ab38d47
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ export default {
REPORT_ACTIONS: 'reportActions_',
REPORT_DRAFT_COMMENT: 'reportDraftComment_',
REPORT_USER_IS_TYPING: 'reportUserIsTyping_',
REPORT_IOUS: 'reportIOUs_',
},
};
81 changes: 81 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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});
});
Expand Down

0 comments on commit ab38d47

Please sign in to comment.