Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Removing the transaction's ID from other duplicate violation data when deleting one of the duplicate expenses. #55801

Merged
merged 11 commits into from
Feb 10, 2025
59 changes: 51 additions & 8 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ import {
isPerDiemRequest as isPerDiemRequestTransactionUtils,
isReceiptBeingScanned as isReceiptBeingScannedTransactionUtils,
isScanRequest as isScanRequestTransactionUtils,
removeSettledAndApprovedTransactions,
shouldShowBrokenConnectionViolation,
} from '@libs/TransactionUtils';
import ViolationsUtils from '@libs/Violations/ViolationsUtils';
Expand Down Expand Up @@ -6706,6 +6707,56 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony
value: null,
});

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: transaction ?? null,
},
];

if (transactionViolations) {
removeSettledAndApprovedTransactions(
transactionViolations.filter((violation) => violation?.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION).flatMap((violation) => violation?.data?.duplicates ?? []),
).forEach((duplicateID) => {
const duplicateTransactionsViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${duplicateID}`];
if (!duplicateTransactionsViolations) {
return;
}

const duplicateViolation = duplicateTransactionsViolations.find((violation) => violation.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION);
if (!duplicateViolation?.data?.duplicates) {
return;
}

const duplicateTransactionIDs = duplicateViolation.data.duplicates.filter((duplicateTransactionID) => duplicateTransactionID !== transactionID);

const optimisticViolations: OnyxTypes.TransactionViolations = duplicateTransactionsViolations.filter((violation) => violation.name !== CONST.VIOLATIONS.DUPLICATED_TRANSACTION);

if (duplicateTransactionIDs.length > 0) {
optimisticViolations.push({
...duplicateViolation,
data: {
...duplicateViolation.data,
duplicates: duplicateTransactionIDs,
},
});
}

optimisticData.push({
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${duplicateID}`,
value: optimisticViolations.length > 0 ? optimisticViolations : null,
});

failureData.push({
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${duplicateID}`,
value: duplicateTransactionsViolations,
});
});
}

if (shouldDeleteTransactionThread) {
optimisticData.push(
// Use merge instead of set to avoid deleting the report too quickly, which could cause a brief "not found" page to appear.
Expand Down Expand Up @@ -6851,14 +6902,6 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony
});
}

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: transaction ?? null,
},
];

failureData.push({
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`,
Expand Down