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 14 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
32 changes: 17 additions & 15 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 @@ -135,14 +135,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
// NOTE: since the API doesn't handle syncing chat reports with IOU reports,
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
// 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}`))
.finally(() => {
Expand Down Expand Up @@ -201,14 +202,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
// 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

// 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
26 changes: 25 additions & 1 deletion src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ function deleteReportComment(reportID, reportAction) {
const sequenceNumber = reportAction.sequenceNumber;
const reportActionsToMerge = {};
const oldMessage = {...reportAction.message};
reportActionsToMerge[reportAction.sequenceNumber] = {
reportActionsToMerge[sequenceNumber] = {
...reportAction,
message: [
{
Expand Down Expand Up @@ -1269,6 +1269,29 @@ 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
* @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);
iouReportData[iouReportKey] = getSimplifiedIOUReport(iouReportStuff, chatReportStuff.reportID);

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

/**
* Updates a user's notification preferences for a chat room
*
Expand Down Expand Up @@ -1302,4 +1325,5 @@ export {
saveReportActionDraft,
deleteReportComment,
getSimplifiedIOUReport,
syncChatAndIOUReports,
};