|
1 | 1 | import type {OnyxCollection} from 'react-native-onyx'; |
2 | 2 | import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig'; |
3 | 3 | import ONYXKEYS from '@src/ONYXKEYS'; |
4 | | -import type {Transaction} from '@src/types/onyx'; |
| 4 | +import type {Transaction, TransactionViolation} from '@src/types/onyx'; |
5 | 5 |
|
6 | 6 | let previousTransactions: OnyxCollection<Transaction> = {}; |
| 7 | +let previousViolations: OnyxCollection<TransactionViolation[]> = {}; |
7 | 8 |
|
8 | 9 | export default createOnyxDerivedValueConfig({ |
9 | 10 | key: ONYXKEYS.DERIVED.REPORT_TRANSACTIONS_AND_VIOLATIONS, |
@@ -57,15 +58,22 @@ export default createOnyxDerivedValueConfig({ |
57 | 58 |
|
58 | 59 | const transactionID = transaction.transactionID; |
59 | 60 | const transactionViolations = violations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; |
| 61 | + const violationKey = `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`; |
| 62 | + const previousTransactionViolations = previousViolations?.[violationKey]; |
60 | 63 |
|
| 64 | + // If violations exist and have length > 0, add them to the structure |
61 | 65 | if (transactionViolations && transactionViolations.length > 0) { |
62 | | - reportTransactionsAndViolations[reportID].violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`] = transactionViolations; |
| 66 | + reportTransactionsAndViolations[reportID].violations[violationKey] = transactionViolations; |
| 67 | + } else if (previousTransactionViolations && previousTransactionViolations.length > 0) { |
| 68 | + // If violations were removed (previous had violations but current doesn't), remove them from the structure |
| 69 | + delete reportTransactionsAndViolations[reportID].violations[violationKey]; |
63 | 70 | } |
64 | 71 |
|
65 | 72 | reportTransactionsAndViolations[reportID].transactions[transactionKey] = transaction; |
66 | 73 | } |
67 | 74 |
|
68 | 75 | previousTransactions = transactions; |
| 76 | + previousViolations = violations; |
69 | 77 |
|
70 | 78 | return reportTransactionsAndViolations; |
71 | 79 | }, |
|
0 commit comments