|
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, |
@@ -56,16 +57,25 @@ export default createOnyxDerivedValueConfig({ |
56 | 57 | } |
57 | 58 |
|
58 | 59 | const transactionID = transaction.transactionID; |
59 | | - const transactionViolations = violations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; |
| 60 | + const violationKey = `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`; |
| 61 | + const transactionViolations = violations?.[violationKey]; |
| 62 | + const previousTransactionViolations = previousViolations?.[violationKey]; |
60 | 63 |
|
| 64 | + const violationInSourceValues = transactionViolationsUpdates?.[violationKey]; |
| 65 | + |
| 66 | + // If violations exist and have length > 0, add them to the structure |
61 | 67 | if (transactionViolations && transactionViolations.length > 0) { |
62 | | - reportTransactionsAndViolations[reportID].violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`] = transactionViolations; |
| 68 | + reportTransactionsAndViolations[reportID].violations[violationKey] = transactionViolations; |
| 69 | + } else if (violationInSourceValues === undefined || (previousTransactionViolations && previousTransactionViolations.length > 0)) { |
| 70 | + // If violations were removed (previous had violations but current doesn't) or explicitly set to undefined, remove them from the structure |
| 71 | + delete reportTransactionsAndViolations[reportID].violations[violationKey]; |
63 | 72 | } |
64 | 73 |
|
65 | 74 | reportTransactionsAndViolations[reportID].transactions[transactionKey] = transaction; |
66 | 75 | } |
67 | 76 |
|
68 | 77 | previousTransactions = transactions; |
| 78 | + previousViolations = violations; |
69 | 79 |
|
70 | 80 | return reportTransactionsAndViolations; |
71 | 81 | }, |
|
0 commit comments