From 4e0ff21d56b270db25ba73708fa08033febda11b Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Fri, 9 Aug 2024 12:06:39 +0200 Subject: [PATCH 1/3] Do not add optimistic action when the route is pending --- src/libs/TransactionUtils/index.ts | 6 ++++-- src/libs/actions/IOU.ts | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index aceb053a75f7..1e55854c35b6 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 174d6f241b75..c32a1622ac08 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2521,11 +2521,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({ From fe10f735f71266bb9becccba0e1c019fbab64b4b Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Tue, 13 Aug 2024 12:38:16 +0200 Subject: [PATCH 2/3] Handle getUpdateTrackExpenseParams similar to getUpdateMoneyRequestParams --- src/libs/actions/IOU.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 8b9c3dc7bbf4..4105573532e6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2829,11 +2829,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({ From 00a16eaf0f2e829bfb5d66d348b0cc97ae8c8ac4 Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Tue, 13 Aug 2024 13:23:16 +0200 Subject: [PATCH 3/3] Align with other updates: call getUpdateTrackExpenseParams only from self-dm --- src/libs/actions/IOU.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 4105573532e6..f388a3d1d8f3 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2805,7 +2805,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 @@ -3145,9 +3145,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);