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

Sync newly returned report objects from commands #3544

Merged
merged 18 commits into from
Jul 8, 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
36 changes: 19 additions & 17 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CONST from '../../CONST';
import ONYXKEYS from '../../ONYXKEYS';
import ROUTES from '../../ROUTES';
import * as API from '../API';
import {getSimplifiedIOUReport, fetchChatReportsByIDs, fetchIOUReportByIDAndUpdateChatReport} from './Report';
import {getSimplifiedIOUReport, syncChatAndIOUReports} from './Report';
import Navigation from '../Navigation/Navigation';

/**
Expand Down Expand Up @@ -144,14 +144,15 @@ function rejectTransaction({
if (response.jsonCode !== 200) {
throw new Error(`${response.code} ${response.message}`);
}
fetchChatReportsByIDs([chatReportID]);

// If an iouReport is open (has an IOU, but is not yet paid) then we sync the chatReport's 'iouReportID'
// field in Onyx, simplifying IOU data retrieval and reducing necessary API calls when displaying IOU
// components. If we didn't sync the reportIDs, the transaction would still be shown to users as rejectable
// The iouReport being fetched here must be open, because only an open iouReoport can be paid. Therefore,
// we should also sync the chatReport after fetching the iouReport.
fetchIOUReportByIDAndUpdateChatReport(reportID, chatReportID);

// Save the updated chat and iou reports sent back in the response
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
// NOTE: since the API doesn't handle syncing chat reports with IOU reports,
// we also need to set the iouReportID and hasOutstandingIOU fields of the chatReport in Onyx manually
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
// If we didn't sync the reportIDs, the paid IOU would still be shown to users as unpaid. The
// iouReport being fetched here must be open, because only an open iouReport can be paid.
const chatReportStuff = response.reports[chatReportID];
const iouReportStuff = response.reports[reportID];
syncChatAndIOUReports(chatReportStuff, iouReportStuff);
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
})
.catch(error => console.error(`Error rejecting transaction: ${error}`));
}
Expand Down Expand Up @@ -204,14 +205,15 @@ function payIOUReport({
if (response.jsonCode !== 200) {
throw new Error(response.message);
}
fetchChatReportsByIDs([chatReportID]);

// If an iouReport is open (has an IOU, but is not yet paid) then we sync the chatReport's 'iouReportID'
// field in Onyx, simplifying IOU data retrieval and reducing necessary API calls when displaying IOU
// components. If we didn't sync the reportIDs, the paid IOU would still be shown to users as unpaid. The
// iouReport being fetched here must be open, because only an open iouReoport can be paid.
// Therefore, we should also sync the chatReport after fetching the iouReport.
fetchIOUReportByIDAndUpdateChatReport(reportID, chatReportID);

// Save the updated chat and iou reports sent back in the response
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
// NOTE: since the API doesn't handle syncing chat reports with IOU reports,
// we also need to set the iouReportID and hasOutstandingIOU fields of the chatReport in Onyx manually
// If we didn't sync the reportIDs, the paid IOU would still be shown to users as unpaid. The
// iouReport being fetched here must be open, because only an open iouReport can be paid.
const chatReportStuff = response.reports[chatReportID];
const iouReportStuff = response.reports[reportID];
syncChatAndIOUReports(chatReportStuff, iouReportStuff);
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved

// Once we have successfully paid the IOU we will transfer the user to their platform of choice if they have
// selected something other than a manual settlement or Expensify Wallet e.g. Venmo or PayPal.me
Expand Down
24 changes: 24 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,28 @@ function saveReportActionDraft(reportID, reportActionID, draftMessage) {
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}_${reportActionID}`, draftMessage);
}

/**
* Syncs up a chat report and an IOU report in Onyx after an IOU transaction has been made
* and they've both been updated in the back-end.
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
* @param {Object} chatReportStuff
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
* @param {Object} iouReportStuff
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
*/
function syncChatAndIOUReports(chatReportStuff, iouReportStuff) {
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
const iouReportData = {};
const chatReportData = {};
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
const chatReportKey = `${ONYXKEYS.COLLECTION.REPORT}${chatReportStuff.reportID}`;
const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReportStuff.reportID}`;

chatReportData[chatReportKey] = getSimplifiedReportObject(chatReportStuff);
chatReportData[chatReportKey].iouReportID = iouReportStuff.reportID;
Julesssss marked this conversation as resolved.
Show resolved Hide resolved
chatReportData[chatReportKey].hasOutstandingIOU = iouReportStuff.stateNum
=== CONST.REPORT.STATE_NUM.PROCESSING && iouReportStuff.total !== 0;
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
iouReportData[iouReportKey] = getSimplifiedIOUReport(iouReportStuff, chatReportStuff.reportID);

Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT_IOUS, iouReportData);
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, chatReportData);
}

export {
fetchAllReports,
fetchActions,
Expand All @@ -1287,4 +1309,6 @@ export {
saveReportActionDraft,
deleteReportComment,
getSimplifiedIOUReport,
getSimplifiedReportObject,
syncChatAndIOUReports,
};