Fix: Improve transaction deduplication logic
Ensures deduplication occurs after combining pasted transactions with existing ones to prevent duplicate entries. -edited src/components/BatchTransactionForm.tsx
This commit is contained in:
@@ -105,28 +105,13 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
|
||||
const handlePaste = (value: string) => {
|
||||
setPastedData(value);
|
||||
const lines = value.trim().split('\n');
|
||||
const transactions: ParsedTransaction[] = [];
|
||||
const seenTransactions = new Set<string>();
|
||||
const pasteTransactionMap = new Map<string, ParsedTransaction>();
|
||||
|
||||
// Pre-populate seenTransactions with existing transactions from jobs
|
||||
eligibleJobs.forEach(job => {
|
||||
job.income.forEach(tx => {
|
||||
const key = createTransactionKeyFromRecord(tx);
|
||||
seenTransactions.add(key);
|
||||
});
|
||||
});
|
||||
|
||||
let duplicates = 0;
|
||||
// STEP 1: First combine all identical transactions within the pasted data
|
||||
lines.forEach((line, index) => {
|
||||
const parsed = parseTransactionLine(line);
|
||||
if (parsed) {
|
||||
const transactionKey = createTransactionKey(parsed);
|
||||
const isDuplicate = seenTransactions.has(transactionKey);
|
||||
|
||||
if (isDuplicate) {
|
||||
duplicates++;
|
||||
}
|
||||
|
||||
// Check if this exact transaction already exists in our paste data
|
||||
if (pasteTransactionMap.has(transactionKey)) {
|
||||
@@ -145,20 +130,37 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
|
||||
buyer: parsed.buyer,
|
||||
location: parsed.location,
|
||||
corporation: parsed.corporation,
|
||||
wallet: parsed.wallet,
|
||||
assignedJobId: !isDuplicate ? findMatchingJob(parsed.itemName) : undefined,
|
||||
isDuplicate
|
||||
wallet: parsed.wallet
|
||||
};
|
||||
pasteTransactionMap.set(transactionKey, newTransaction);
|
||||
|
||||
if (!isDuplicate) {
|
||||
seenTransactions.add(transactionKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Convert map to array for display - each transaction is individual
|
||||
// STEP 2: Now check combined transactions against existing job transactions
|
||||
const existingTransactionKeys = new Set<string>();
|
||||
eligibleJobs.forEach(job => {
|
||||
job.income.forEach(tx => {
|
||||
const key = createTransactionKeyFromRecord(tx);
|
||||
existingTransactionKeys.add(key);
|
||||
});
|
||||
});
|
||||
|
||||
let duplicates = 0;
|
||||
// Update each combined transaction with duplicate status and job assignment
|
||||
pasteTransactionMap.forEach((transaction, key) => {
|
||||
const isDuplicate = existingTransactionKeys.has(key);
|
||||
transaction.isDuplicate = isDuplicate;
|
||||
|
||||
if (isDuplicate) {
|
||||
duplicates++;
|
||||
transaction.assignedJobId = undefined;
|
||||
} else {
|
||||
transaction.assignedJobId = findMatchingJob(transaction.itemName);
|
||||
}
|
||||
});
|
||||
|
||||
// Convert map to array for display
|
||||
const transactionList = Array.from(pasteTransactionMap.values());
|
||||
setDuplicatesFound(duplicates);
|
||||
|
||||
|
Reference in New Issue
Block a user