diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index a865b78a9bbf..4fc850c83631 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -393,7 +393,7 @@ function getOriginalAmount(transaction: Transaction): number { /** * Verify if the transaction is expecting the distance to be calculated on the server */ -function isFetchingWaypointsFromServer(transaction: OnyxEntry): boolean { +function isFetchingWaypointsFromServer(transaction: OnyxInputOrEntry): boolean { return !!transaction?.pendingFields?.waypoints; } @@ -753,7 +753,9 @@ function calculateAmountForUpdatedWaypointOrRate( policy: OnyxInputOrEntry, isFromExpenseReport: boolean, ) { - if (isEmptyObject(transactionChanges?.routes?.route0?.geometry) && isEmptyObject(transactionChanges.customUnitRateID)) { + const hasModifiedRouteWithPendingWaypoints = !isEmptyObject(transactionChanges.waypoints) && isEmptyObject(transactionChanges?.routes?.route0?.geometry); + const hasModifiedRateWithPendingWaypoints = !!transactionChanges?.customUnitRateID && isFetchingWaypointsFromServer(transaction); + if (hasModifiedRouteWithPendingWaypoints || hasModifiedRateWithPendingWaypoints) { return { amount: CONST.IOU.DEFAULT_AMOUNT, modifiedAmount: CONST.IOU.DEFAULT_AMOUNT, diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 9cc844849c89..e84a14836efd 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2524,11 +2524,13 @@ function getUpdateMoneyRequestParams( } // Step 3: Build the modified expense report actions - // We don't create a modified report action if we're updating the waypoints, - // since there isn't actually any optimistic data we can create for them and the report action is created on the server - // with the response from the MapBox API + // We don't create a modified report action if: + // - we're updating the waypoints + // - we're updating the distance rate while the waypoints are still pending + // In these cases, there isn't a valid optimistic mileage data we can use, + // and the report action is created on the server with the distance-related response from the MapBox API const updatedReportAction = ReportUtils.buildOptimisticModifiedExpenseReportAction(transactionThread, transaction, transactionChanges, isFromExpenseReport, policy); - if (!hasPendingWaypoints) { + if (!hasPendingWaypoints && !(hasModifiedDistanceRate && TransactionUtils.isFetchingWaypointsFromServer(transaction))) { params.reportActionID = updatedReportAction.reportActionID; optimisticData.push({ @@ -2806,7 +2808,7 @@ function getUpdateTrackExpenseParams( if (transaction && updatedTransaction && (hasPendingWaypoints || hasModifiedDistanceRate)) { updatedTransaction = { ...updatedTransaction, - ...TransactionUtils.calculateAmountForUpdatedWaypointOrRate(transaction, transactionChanges, policy, ReportUtils.isExpenseReport(transactionThread)), + ...TransactionUtils.calculateAmountForUpdatedWaypointOrRate(transaction, transactionChanges, policy, false), }; // Delete the draft transaction when editing waypoints when the server responds successfully and there are no errors @@ -2830,11 +2832,13 @@ function getUpdateTrackExpenseParams( } // Step 3: Build the modified expense report actions - // We don't create a modified report action if we're updating the waypoints, - // since there isn't actually any optimistic data we can create for them and the report action is created on the server - // with the response from the MapBox API + // We don't create a modified report action if: + // - we're updating the waypoints + // - we're updating the distance rate while the waypoints are still pending + // In these cases, there isn't a valid optimistic mileage data we can use, + // and the report action is created on the server with the distance-related response from the MapBox API const updatedReportAction = ReportUtils.buildOptimisticModifiedExpenseReportAction(transactionThread, transaction, transactionChanges, false, policy); - if (!hasPendingWaypoints) { + if (!hasPendingWaypoints && !(hasModifiedDistanceRate && TransactionUtils.isFetchingWaypointsFromServer(transaction))) { params.reportActionID = updatedReportAction.reportActionID; optimisticData.push({ @@ -3144,9 +3148,9 @@ function updateMoneyRequestDistanceRate( }; const allReports = ReportConnection.getAllReports(); const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] ?? null; - + const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null; let data: UpdateMoneyRequestData; - if (ReportUtils.isTrackExpenseReport(transactionThreadReport)) { + if (ReportUtils.isTrackExpenseReport(transactionThreadReport) && ReportUtils.isSelfDM(parentReport)) { data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, true, policy); } else { data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true);