Skip to content

Commit ba108ec

Browse files
committed
delete derived value when report ID changed
1 parent 7bfe6b4 commit ba108ec

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/libs/actions/OnyxDerived/configs/reportTransactionsAndViolations.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import type {OnyxCollection} from 'react-native-onyx';
12
import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig';
23
import ONYXKEYS from '@src/ONYXKEYS';
4+
import type {Transaction} from '@src/types/onyx';
5+
6+
let previousTransactions: OnyxCollection<Transaction> = {};
37

48
export default createOnyxDerivedValueConfig({
59
key: ONYXKEYS.DERIVED.REPORT_TRANSACTIONS_AND_VIOLATIONS,
@@ -24,6 +28,19 @@ export default createOnyxDerivedValueConfig({
2428
for (const transactionKey of transactionsToProcess) {
2529
const transaction = transactions[transactionKey];
2630
const reportID = transaction?.reportID;
31+
32+
// If the reportID of the transaction has changed (e.g. the transaction was split into multiple reports), we need to delete the transaction from the previous reportID and the violations from the previous reportID
33+
const previousTransaction = previousTransactions?.[transactionKey];
34+
const previousReportID = previousTransaction?.reportID;
35+
36+
if (previousReportID && previousReportID !== reportID && reportTransactionsAndViolations[previousReportID]) {
37+
delete reportTransactionsAndViolations[previousReportID].transactions[transactionKey];
38+
const transactionID = previousTransaction?.transactionID;
39+
if (transactionID) {
40+
delete reportTransactionsAndViolations[previousReportID].violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`];
41+
}
42+
}
43+
2744
if (!reportID) {
2845
// eslint-disable-next-line no-continue
2946
continue;
@@ -46,6 +63,8 @@ export default createOnyxDerivedValueConfig({
4663
reportTransactionsAndViolations[reportID].transactions[transactionKey] = transaction;
4764
}
4865

66+
previousTransactions = transactions;
67+
4968
return reportTransactionsAndViolations;
5069
},
5170
});

0 commit comments

Comments
 (0)