Skip to content

Commit

Permalink
fix incorrect amount for the case create or delete a request with for…
Browse files Browse the repository at this point in the history
…eign currency
  • Loading branch information
dukenv0307 committed Feb 15, 2024
1 parent 8796fae commit 2a48143
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
13 changes: 11 additions & 2 deletions src/libs/IOUUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TReport extends OnyxEntry<Report>>(iouReport: TReport, actorAccountID: number, amount: number, currency: string, isDeleting = false): TReport {
if (!iouReport?.currency) {
function updateIOUOwnerAndTotal<TReport extends OnyxEntry<Report>>(
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;
}

Expand Down
17 changes: 6 additions & 11 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2a48143

Please sign in to comment.