Skip to content

Commit

Permalink
Merge pull request #1785 from Expensify/joe-persist-iou-report
Browse files Browse the repository at this point in the history
Persist IOU data in Onyx
  • Loading branch information
Jag96 authored Mar 19, 2021
2 parents 3e78ca7 + d8b9eb5 commit 4bc3221
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ const CONST = {
},
REPORT: {
MAXIMUM_PARTICIPANTS: 8,
REPORT_ACTIONS_LIMIT: 50,
ACTIONS: {
LIMIT: 50,
TYPE: {
IOU: 'IOU',
},
},
},
MODAL: {
MODAL_TYPE: {
Expand Down
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';
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
102 changes: 100 additions & 2 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,102 @@ function getSimplifiedReportObject(report) {
};
}

/**
* 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} report
* @param {Object[]} report.reportActionList
* @param {Number} report.reportID
*/
function updateIOUReportData(report) {
const reportActionList = report.reportActionList || [];
const containsIOUAction = _.any(reportActionList,
reportAction => reportAction.action === CONST.REPORT.ACTIONS.TYPE.IOU);

// If there aren't any IOU actions, we don't need to fetch any additional data
if (!containsIOUAction) {
return;
}

// If we don't have one participant (other than the current user), this is not an IOU
const participants = getParticipantEmailsFromReport(report);
if (participants.length !== 1) {
Log.alert('[Report] Report with IOU action has more than 2 participants', true, {
reportID: report.reportID,
participants,
});
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;
API.GetIOUReport({
debtorEmail: participants[0],
}).then((response) => {
iouReportID = response.reportID || 0;
if (response.jsonCode !== 200) {
throw new Error(response.message);
} else if (iouReportID === 0) {
throw new Error('GetIOUReport returned a reportID of 0, not fetching IOU report data');
}

return API.Get({
returnValueList: 'reportStuff',
reportIDList: iouReportID,
shouldLoadOptionalKeys: true,
includePinnedReports: true,
});
}).then((response) => {
if (response.jsonCode !== 200) {
throw new Error(response.message);
}

const iouReportData = response.reports[iouReportID];
if (!iouReportData) {
throw new Error(`No iouReportData found for reportID ${iouReportID}`);
}

Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReportID}`,
getSimplifiedIOUReport(iouReportData));
}).catch((error) => {
console.debug(`[Report] Failed to populate IOU Collection: ${error.message}`);
});
}

/**
* Fetches chat reports when provided a list of
* chat report IDs
Expand All @@ -178,7 +274,9 @@ function fetchChatReportsByIDs(chatList) {
const simplifiedReports = {};
_.each(fetchedReports, (report) => {
const key = `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`;
simplifiedReports[key] = getSimplifiedReportObject(report);
const simplifiedReport = getSimplifiedReportObject(report);
simplifiedReports[key] = simplifiedReport;
updateIOUReportData(report);
});

// We use mergeCollection such that it updates ONYXKEYS.COLLECTION.REPORT in one go.
Expand Down Expand Up @@ -518,7 +616,7 @@ function fetchActions(reportID, offset) {
return API.Report_GetHistory({
reportID,
reportActionsOffset,
reportActionsLimit: CONST.REPORT.REPORT_ACTIONS_LIMIT,
reportActionsLimit: CONST.REPORT.ACTIONS.LIMIT,
})
.then((data) => {
// We must remove all optimistic actions so there will not be any stuck comments. At this point, we should
Expand Down
4 changes: 2 additions & 2 deletions src/pages/home/report/ReportActionsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ class ReportActionsView extends React.Component {
}

this.setState({isLoadingMoreChats: true}, () => {
// Retrieve the next REPORT_ACTIONS_LIMIT sized page of comments, unless we're near the beginning, in which
// Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments, unless we're near the beginning, in which
// case just get everything starting from 0.
const offset = Math.max(minSequenceNumber - CONST.REPORT.REPORT_ACTIONS_LIMIT, 0);
const offset = Math.max(minSequenceNumber - CONST.REPORT.ACTIONS.LIMIT, 0);
fetchActions(this.props.reportID, offset)
.then(() => this.setState({isLoadingMoreChats: false}));
});
Expand Down

0 comments on commit 4bc3221

Please sign in to comment.