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) => {
|
const handlePaste = (value: string) => {
|
||||||
setPastedData(value);
|
setPastedData(value);
|
||||||
const lines = value.trim().split('\n');
|
const lines = value.trim().split('\n');
|
||||||
const transactions: ParsedTransaction[] = [];
|
|
||||||
const seenTransactions = new Set<string>();
|
|
||||||
const pasteTransactionMap = new Map<string, ParsedTransaction>();
|
const pasteTransactionMap = new Map<string, ParsedTransaction>();
|
||||||
|
|
||||||
// Pre-populate seenTransactions with existing transactions from jobs
|
// STEP 1: First combine all identical transactions within the pasted data
|
||||||
eligibleJobs.forEach(job => {
|
|
||||||
job.income.forEach(tx => {
|
|
||||||
const key = createTransactionKeyFromRecord(tx);
|
|
||||||
seenTransactions.add(key);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
let duplicates = 0;
|
|
||||||
lines.forEach((line, index) => {
|
lines.forEach((line, index) => {
|
||||||
const parsed = parseTransactionLine(line);
|
const parsed = parseTransactionLine(line);
|
||||||
if (parsed) {
|
if (parsed) {
|
||||||
const transactionKey = createTransactionKey(parsed);
|
const transactionKey = createTransactionKey(parsed);
|
||||||
const isDuplicate = seenTransactions.has(transactionKey);
|
|
||||||
|
|
||||||
if (isDuplicate) {
|
|
||||||
duplicates++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if this exact transaction already exists in our paste data
|
// Check if this exact transaction already exists in our paste data
|
||||||
if (pasteTransactionMap.has(transactionKey)) {
|
if (pasteTransactionMap.has(transactionKey)) {
|
||||||
@@ -145,20 +130,37 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
|
|||||||
buyer: parsed.buyer,
|
buyer: parsed.buyer,
|
||||||
location: parsed.location,
|
location: parsed.location,
|
||||||
corporation: parsed.corporation,
|
corporation: parsed.corporation,
|
||||||
wallet: parsed.wallet,
|
wallet: parsed.wallet
|
||||||
assignedJobId: !isDuplicate ? findMatchingJob(parsed.itemName) : undefined,
|
|
||||||
isDuplicate
|
|
||||||
};
|
};
|
||||||
pasteTransactionMap.set(transactionKey, newTransaction);
|
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());
|
const transactionList = Array.from(pasteTransactionMap.values());
|
||||||
setDuplicatesFound(duplicates);
|
setDuplicatesFound(duplicates);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user