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
24 changes: 3 additions & 21 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,9 @@ function rejectTransaction({
throw new Error(`${response.code} ${response.message}`);
}

// 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
// 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);
const chatReport = response.reports[chatReportID];
const iouReport = response.reports[reportID];
syncChatAndIOUReports(chatReport, iouReport);
})
.catch(error => console.error(`Error rejecting transaction: ${error}`))
.finally(() => {
Expand Down Expand Up @@ -214,22 +209,9 @@ function payIOUReport({
throw new Error(response.message);
}

// 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
// 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);

// 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
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.PAYPAL_ME) {
Linking.openURL(buildPayPalPaymentUrl(amount, submitterPayPalMeAddress, currency));
} else if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.VENMO) {
Linking.openURL(buildVenmoPaymentURL(amount, submitterPhoneNumber));
}
Copy link
Contributor

@Julesssss Julesssss Jul 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this supposed to be removed? Am I missing something, or was this a mistake? Wouldn't this break Venmo/Paypal?

})
.catch((error) => {
console.error(`Error Paying iouReport: ${error}`);
Expand Down
45 changes: 28 additions & 17 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -1271,25 +1271,36 @@ function saveReportActionDraft(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.
* by setting the iouReportID and hasOutstandingIOU for the chat report.
* Even though both reports are updated in the back-end, the API doesn't handle syncing their reportIDs.
* If we didn't sync these 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.
*
* @param {Object} chatReportStuff
* @param {Object} iouReportStuff
* @param {Object} chatReport
* @param {Object} iouReport
*/
function syncChatAndIOUReports(chatReportStuff, iouReportStuff) {
const iouReportData = {};
const chatReportData = {};
const chatReportKey = `${ONYXKEYS.COLLECTION.REPORT}${chatReportStuff.reportID}`;
const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReportStuff.reportID}`;

chatReportData[chatReportKey] = getSimplifiedReportObject(chatReportStuff);
chatReportData[chatReportKey].iouReportID = iouReportStuff.reportID;
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);
function syncChatAndIOUReports(chatReport, iouReport) {
// Return early in case there's a back-end issue preventing the IOU command from returning the report objects.
if (!chatReport || !iouReport) {
return;
}

const simplifiedIouReport = {};
const simplifiedReport = {};
const chatReportKey = `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`;
const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReport.reportID}`;

// We don't want to sync an iou report that's already been reimbursed with its chat report.
if (!iouReport.stateNum === CONST.REPORT.STATE_NUM.SUBMITTED) {
simplifiedReport[chatReportKey].iouReportID = iouReport.reportID;
}
simplifiedReport[chatReportKey] = getSimplifiedReportObject(chatReport);
simplifiedReport[chatReportKey].hasOutstandingIOU = iouReport.stateNum
=== (CONST.REPORT.STATE_NUM.PROCESSING && iouReport.total !== 0);
simplifiedIouReport[iouReportKey] = getSimplifiedIOUReport(iouReport, chatReport.reportID);

Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT_IOUS, simplifiedIouReport);
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, simplifiedReport);
}

/**
Expand Down