|
| 1 | +import type {OnyxCollection} from 'react-native-onyx'; |
| 2 | +import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig'; |
| 3 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 4 | +import type {Transaction} from '@src/types/onyx'; |
| 5 | + |
| 6 | +let previousTransactions: OnyxCollection<Transaction> = {}; |
| 7 | + |
| 8 | +export default createOnyxDerivedValueConfig({ |
| 9 | + key: ONYXKEYS.DERIVED.REPORT_TRANSACTIONS_AND_VIOLATIONS, |
| 10 | + dependencies: [ONYXKEYS.COLLECTION.TRANSACTION, ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS], |
| 11 | + compute: ([transactions, violations], {sourceValues, currentValue}) => { |
| 12 | + if (!transactions) { |
| 13 | + return {}; |
| 14 | + } |
| 15 | + |
| 16 | + // If there is a source value for transactions or transaction violations, we need to process only the transactions that have been updated or added |
| 17 | + // If not, we need to process all transactions |
| 18 | + const transactionsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.TRANSACTION]; |
| 19 | + const transactionViolationsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS]; |
| 20 | + let transactionsToProcess = Object.keys(transactions); |
| 21 | + if (transactionsUpdates) { |
| 22 | + transactionsToProcess = Object.keys(transactionsUpdates); |
| 23 | + } else if (transactionViolationsUpdates) { |
| 24 | + transactionsToProcess = Object.keys(transactionViolationsUpdates).map((transactionViolation) => |
| 25 | + transactionViolation.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ONYXKEYS.COLLECTION.TRANSACTION), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + const reportTransactionsAndViolations = currentValue ?? {}; |
| 30 | + for (const transactionKey of transactionsToProcess) { |
| 31 | + const transaction = transactions[transactionKey]; |
| 32 | + const reportID = transaction?.reportID; |
| 33 | + |
| 34 | + // 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 |
| 35 | + const previousTransaction = previousTransactions?.[transactionKey]; |
| 36 | + const previousReportID = previousTransaction?.reportID; |
| 37 | + |
| 38 | + if (previousReportID && previousReportID !== reportID && reportTransactionsAndViolations[previousReportID]) { |
| 39 | + delete reportTransactionsAndViolations[previousReportID].transactions[transactionKey]; |
| 40 | + const transactionID = previousTransaction?.transactionID; |
| 41 | + if (transactionID) { |
| 42 | + delete reportTransactionsAndViolations[previousReportID].violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (!reportID) { |
| 47 | + // eslint-disable-next-line no-continue |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (!reportTransactionsAndViolations[reportID]) { |
| 52 | + reportTransactionsAndViolations[reportID] = { |
| 53 | + transactions: {}, |
| 54 | + violations: {}, |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + const transactionID = transaction.transactionID; |
| 59 | + const transactionViolations = violations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; |
| 60 | + |
| 61 | + if (transactionViolations && transactionViolations.length > 0) { |
| 62 | + reportTransactionsAndViolations[reportID].violations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`] = transactionViolations; |
| 63 | + } |
| 64 | + |
| 65 | + reportTransactionsAndViolations[reportID].transactions[transactionKey] = transaction; |
| 66 | + } |
| 67 | + |
| 68 | + previousTransactions = transactions; |
| 69 | + |
| 70 | + return reportTransactionsAndViolations; |
| 71 | + }, |
| 72 | +}); |
0 commit comments