Skip to content

Commit

Permalink
Merge pull request #55102 from Tony-MK/fix/54471
Browse files Browse the repository at this point in the history
fix: reverting the amount and currency after error clearance.
  • Loading branch information
tylerkaraszewski authored Jan 22, 2025
2 parents c8b112e + 85693a0 commit e60725e
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 151 deletions.
3 changes: 2 additions & 1 deletion src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import AnimatedEmptyStateBackground from '@pages/home/report/AnimatedEmptyStateB
import {cleanUpMoneyRequest, updateMoneyRequestBillable} from '@userActions/IOU';
import {navigateToConciergeChatAndDeleteReport} from '@userActions/Report';
import {clearAllRelatedReportActionErrors} from '@userActions/ReportActions';
import {clearError} from '@userActions/Transaction';
import {clearError, getLastModifiedExpense, revert} from '@userActions/Transaction';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -510,6 +510,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
clearAllRelatedReportActionErrors(report.reportID, parentReportAction);
return;
}
revert(transaction?.transactionID ?? linkedTransactionID, getLastModifiedExpense(report?.reportID));
clearError(transaction.transactionID);
clearAllRelatedReportActionErrors(report.reportID, parentReportAction);
}}
Expand Down
20 changes: 20 additions & 0 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as TransactionUtils from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetails, RecentWaypoint, ReportAction, ReportActions, ReviewDuplicates, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx';
import type {OriginalMessageModifiedExpense} from '@src/types/onyx/OriginalMessage';
import type {OnyxData} from '@src/types/onyx/Request';
import type {WaypointCollection} from '@src/types/onyx/Transaction';
import type TransactionState from '@src/types/utils/TransactionStateType';
Expand Down Expand Up @@ -476,6 +477,23 @@ function clearError(transactionID: string) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {errors: null, errorFields: {route: null, waypoints: null, routes: null}});
}

function getLastModifiedExpense(reportID?: string): OriginalMessageModifiedExpense | undefined {
const modifiedExpenseActions = Object.values(ReportActionsUtils.getAllReportActions(reportID)).filter(ReportActionsUtils.isModifiedExpenseAction);
modifiedExpenseActions.sort((a, b) => Number(a.reportActionID) - Number(b.reportActionID));
return ReportActionsUtils.getOriginalMessage(modifiedExpenseActions.at(-1));
}

function revert(transactionID?: string, originalMessage?: OriginalMessageModifiedExpense | undefined) {
const transaction = TransactionUtils.getTransaction(transactionID);

if (transaction && originalMessage?.oldAmount && originalMessage.oldCurrency && 'amount' in originalMessage && 'currency' in originalMessage) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
modifiedAmount: transaction?.amount && transaction?.amount < 0 ? -Math.abs(originalMessage.oldAmount) : originalMessage.oldAmount,
modifiedCurrency: originalMessage.oldCurrency,
});
}
}

function markAsCash(transactionID: string | undefined, transactionThreadReportID: string | undefined) {
if (!transactionID || !transactionThreadReportID) {
return;
Expand Down Expand Up @@ -571,4 +589,6 @@ export {
sanitizeRecentWaypoints,
getAllTransactionViolationsLength,
getAllTransactions,
getLastModifiedExpense,
revert,
};
Loading

0 comments on commit e60725e

Please sign in to comment.