Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist IOU data in Onyx #1785

Merged
merged 9 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_',
},
};
13 changes: 12 additions & 1 deletion src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,6 @@ function SetNameValuePair(parameters) {
}

/**
*
* @param {Object} parameters
* @param {String[]} data
* @returns {Promise}
Expand All @@ -621,6 +620,17 @@ function Mobile_GetConstants(parameters) {
return Network.post(commandName, finalParameters);
}

/**
* @param {Object} parameters
* @param {String} parameters.debtorEmail
* @returns {Promise}
*/
function GetIOUReport(parameters) {
const commandName = 'GetIOUReport';
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
requireParameters(['debtorEmail'], parameters, commandName);
return Network.post(commandName, parameters);
}

export {
getAuthToken,
Authenticate,
Expand All @@ -630,6 +640,7 @@ export {
DeleteLogin,
Get,
GetAccountStatus,
GetIOUReport,
GetRequestCountryCode,
Graphite_Timer,
Log,
Expand Down
80 changes: 80 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,84 @@ function fetchChatReports() {
});
}

/**
* Get a simplified version of an IOU report
*
* @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
* @param {Number} reportData.reportID
* @returns {Object}
*/
function getSimplifiedIOUReport(reportData) {
const transactions = _.map(reportData.transactionList, transaction => ({
transactionID: transaction.transactionID,
amount: transaction.amount,
currency: transaction.currency,
created: transaction.created,
comment: transaction.comment,
}));

return {
reportID: reportData.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 {Object[]} reportHistory
*/
function updateIOUReportData(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();
Jag96 marked this conversation as resolved.
Show resolved Hide resolved

// If we have more than one participant, this is not an IOU
if (otherParticipants.length > 1) {
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// Since the Chat and the IOU are different reports with different reportIDs, and GetIOUReport only returns the
// IOU's reportID, keep track of the IOU's reportID so we can use it to get the IOUReport data via `GetReportStuff`
let iouReportID = 0;
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
API.GetIOUReport({
debtorEmail: otherParticipants[0],
}).then((response) => {
iouReportID = response.reportID;

return API.Get({
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
returnValueList: 'reportStuff',
reportIDList: iouReportID,
shouldLoadOptionalKeys: true,
includePinnedReports: true,
});
}).then((response) => {
const iouReportData = response.reports[iouReportID];
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReportID}`,
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
getSimplifiedIOUReport(iouReportData));
});
}

/**
* Get the actions of a report
*
Expand Down Expand Up @@ -529,6 +607,8 @@ function fetchActions(reportID, offset) {
.max()
.value();

updateIOUReportData(indexedData);

Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, indexedData);
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {maxSequenceNumber});
});
Expand Down