Improve transaction deduplication logic

Limit deduplication to transactions of the relevant jobs.
This commit is contained in:
gpt-engineer-app[bot]
2025-07-08 19:20:54 +00:00
parent b527ecebee
commit 74dbaab169

View File

@@ -68,16 +68,7 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
parsed.totalAmount.toString(), parsed.totalAmount.toString(),
parsed.buyer, parsed.buyer,
parsed.location parsed.location
].join('|'); ].join('|');
console.log('Created key from parsed transaction:', {
key,
date: normalizeDate(parsed.date.toISOString()),
itemName: parsed.itemName,
quantity: parsed.quantity,
totalAmount: parsed.totalAmount,
buyer: parsed.buyer,
location: parsed.location
});
return key; return key;
}; };
@@ -90,15 +81,6 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
tx.buyer, tx.buyer,
tx.location tx.location
].join('|'); ].join('|');
console.log('Created key from existing transaction:', {
key,
date: normalizeDate(tx.date),
itemName: tx.itemName,
quantity: tx.quantity,
totalPrice: tx.totalPrice,
buyer: tx.buyer,
location: tx.location
});
return key; return key;
}; };
@@ -107,19 +89,15 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
const lines = value.trim().split('\n'); const lines = value.trim().split('\n');
const pasteTransactionMap = new Map<string, ParsedTransaction>(); const pasteTransactionMap = new Map<string, ParsedTransaction>();
console.log('=== STEP 1: Processing pasted lines ===');
// STEP 1: First combine all identical transactions within the pasted data // STEP 1: First combine all identical transactions within the pasted data
lines.forEach((line, index) => { lines.forEach((line) => {
const parsed = parseTransactionLine(line); const parsed = parseTransactionLine(line);
if (parsed) { if (parsed) {
const transactionKey = createTransactionKey(parsed); const transactionKey = createTransactionKey(parsed);
console.log(`Line ${index + 1}: Key = ${transactionKey}`);
// Check if this exact transaction already exists in our paste data
if (pasteTransactionMap.has(transactionKey)) { if (pasteTransactionMap.has(transactionKey)) {
// Merge with existing transaction in paste // Merge with existing transaction in paste
const existing = pasteTransactionMap.get(transactionKey)!; const existing = pasteTransactionMap.get(transactionKey)!;
console.log(`Merging duplicate: adding ${parsed.quantity} to existing ${existing.quantity}`);
existing.quantity += parsed.quantity; existing.quantity += parsed.quantity;
existing.totalPrice += Math.abs(parsed.totalAmount); existing.totalPrice += Math.abs(parsed.totalAmount);
} else { } else {
@@ -136,31 +114,36 @@ const BatchTransactionForm: React.FC<BatchTransactionFormProps> = ({ onClose, on
wallet: parsed.wallet wallet: parsed.wallet
}; };
pasteTransactionMap.set(transactionKey, newTransaction); pasteTransactionMap.set(transactionKey, newTransaction);
console.log(`Added new transaction: ${parsed.itemName} x${parsed.quantity}`);
} }
} }
}); });
console.log('=== STEP 2: Checking against existing transactions ==='); // STEP 2: Identify which jobs these transactions belong to
// STEP 2: Now check combined transactions against existing job transactions const relevantJobIds = new Set<string>();
const existingTransactionKeys = new Set<string>(); pasteTransactionMap.forEach((transaction) => {
eligibleJobs.forEach(job => { const matchingJobId = findMatchingJob(transaction.itemName);
job.income.forEach(tx => { if (matchingJobId) {
const key = createTransactionKeyFromRecord(tx); relevantJobIds.add(matchingJobId);
existingTransactionKeys.add(key); }
console.log(`Existing transaction key: ${key}`);
});
}); });
// STEP 3: Only check against transactions from relevant jobs
const existingTransactionKeys = new Set<string>();
eligibleJobs.forEach(job => {
if (relevantJobIds.has(job.id)) {
job.income.forEach(tx => {
const key = createTransactionKeyFromRecord(tx);
existingTransactionKeys.add(key);
});
}
});
// STEP 4: Mark duplicates and assign jobs
let duplicates = 0; let duplicates = 0;
console.log('=== STEP 3: Marking duplicates and assigning jobs ===');
// Update each combined transaction with duplicate status and job assignment
pasteTransactionMap.forEach((transaction, key) => { pasteTransactionMap.forEach((transaction, key) => {
const isDuplicate = existingTransactionKeys.has(key); const isDuplicate = existingTransactionKeys.has(key);
transaction.isDuplicate = isDuplicate; transaction.isDuplicate = isDuplicate;
console.log(`Transaction ${transaction.itemName}: isDuplicate = ${isDuplicate}`);
if (isDuplicate) { if (isDuplicate) {
duplicates++; duplicates++;
transaction.assignedJobId = undefined; transaction.assignedJobId = undefined;