Skip to content

Commit

Permalink
Merge pull request #47145 from paultsimura/fix/46895-rate-blank-action
Browse files Browse the repository at this point in the history
fix: Do not add optimistic modifiedExpense action when the route is pending
  • Loading branch information
marcochavezf authored Aug 16, 2024
2 parents d123de5 + 00a16ea commit bc69605
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transaction>): boolean {
function isFetchingWaypointsFromServer(transaction: OnyxInputOrEntry<Transaction>): boolean {
return !!transaction?.pendingFields?.waypoints;
}

Expand Down Expand Up @@ -753,7 +753,9 @@ function calculateAmountForUpdatedWaypointOrRate(
policy: OnyxInputOrEntry<Policy>,
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,
Expand Down
26 changes: 15 additions & 11 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand All @@ -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({
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bc69605

Please sign in to comment.