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 8 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
3 changes: 3 additions & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const CONST = {
REPORT: {
MAXIMUM_PARTICIPANTS: 8,
REPORT_ACTIONS_LIMIT: 50,
REPORT_ACTION_TYPE: {
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
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';
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
99 changes: 98 additions & 1 deletion src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,101 @@ 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) {
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
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.REPORT_ACTION_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) {
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
Log.alert('[Report] Report with IOU action has more than 2 participants', true, {
reportID: report.reportID,
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;
API.GetIOUReport({
debtorEmail: participants[0],
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
}).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 @@ -184,7 +279,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