From 2a481433f859f2a7e280967593fc4b5c66ca8313 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Thu, 15 Feb 2024 20:17:09 +0700 Subject: [PATCH] fix incorrect amount for the case create or delete a request with foreign currency --- src/libs/IOUUtils.ts | 13 +++++++++++-- src/libs/actions/IOU.ts | 17 ++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/libs/IOUUtils.ts b/src/libs/IOUUtils.ts index 55317c9c050c..1aafe09c53fe 100644 --- a/src/libs/IOUUtils.ts +++ b/src/libs/IOUUtils.ts @@ -53,9 +53,18 @@ function calculateAmount(numberOfParticipants: number, total: number, currency: * If user1 requests $17 from user2, then we have: {ownerAccountID: user1, managerID: user2, total: $7 (still a positive amount, but now owed to user1)} * * @param isDeleting - whether the user is deleting the request + * @param isUpdating - whether the user is updating the request */ -function updateIOUOwnerAndTotal>(iouReport: TReport, actorAccountID: number, amount: number, currency: string, isDeleting = false): TReport { - if (!iouReport?.currency) { +function updateIOUOwnerAndTotal>( + iouReport: TReport, + actorAccountID: number, + amount: number, + currency: string, + isDeleting = false, + isUpdating = false, +): TReport { + // For the update case, we have calculated the diff amount in the calculateDiffAmount function so there is no need to compare currencies here + if ((currency !== iouReport?.currency && !isUpdating) || !iouReport) { return iouReport; } diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 6be6d347e223..fb61bf3565ab 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1138,7 +1138,7 @@ function getUpdateMoneyRequestParams( updatedMoneyRequestReport.total -= diff; } else { updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) + ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false, true) : {}; } updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); @@ -1739,7 +1739,7 @@ function createSplitsAndOnyxData( oneOnOneIOUReport.total -= splitAmount; } } else { - oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, currentUserAccountID, splitAmount, currency); + oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, currentUserAccountID, splitAmount, currency, false); } // STEP 3: Build optimistic transaction @@ -2363,7 +2363,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA oneOnOneIOUReport.total -= splitAmount; } } else { - oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, sessionAccountID, splitAmount, currency ?? ''); + oneOnOneIOUReport = IOUUtils.updateIOUOwnerAndTotal(oneOnOneIOUReport, sessionAccountID, splitAmount, currency ?? '', false); } const oneOnOneTransaction = TransactionUtils.buildOptimisticTransaction( @@ -2810,23 +2810,18 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor // STEP 4: Update the iouReport and reportPreview with new totals and messages if it wasn't deleted let updatedIOUReport: OnyxTypes.Report | null; + const currency = TransactionUtils.getCurrency(transaction); const updatedReportPreviewAction: OnyxTypes.ReportAction | EmptyObject = {...reportPreviewAction}; updatedReportPreviewAction.pendingAction = shouldDeleteIOUReport ? CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE : CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE; if (iouReport && ReportUtils.isExpenseReport(iouReport)) { updatedIOUReport = {...iouReport}; - if (typeof updatedIOUReport.total === 'number') { + if (typeof updatedIOUReport.total === 'number' && currency === iouReport?.currency) { // Because of the Expense reports are stored as negative values, we add the total from the amount updatedIOUReport.total += TransactionUtils.getAmount(transaction, true); } } else { - updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal( - iouReport, - reportAction.actorAccountID ?? -1, - TransactionUtils.getAmount(transaction, false), - TransactionUtils.getCurrency(transaction), - true, - ); + updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, reportAction.actorAccountID ?? -1, TransactionUtils.getAmount(transaction, false), currency, true); } if (updatedIOUReport) {