fix: track new transactions explicitly for post-import categorization
session.new is emptied by auto-flush during duplicate detection queries, so we track new Transaction objects in a list instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.models.transaction import Transaction
|
||||
from src.services.categorizer import RuleBasedCategorizer
|
||||
from src.services.csv_reader import read_csv
|
||||
from src.services.normalizer import normalize_description
|
||||
|
||||
@@ -23,6 +24,7 @@ class ImportService:
|
||||
rows = read_csv(file_path)
|
||||
imported = 0
|
||||
duplicates = 0
|
||||
new_transactions: list[Transaction] = []
|
||||
|
||||
# Track how many times each key appears in the current batch so that
|
||||
# legitimate repeated transactions (same date/amount/description) in a
|
||||
@@ -65,8 +67,18 @@ class ImportService:
|
||||
source_category=source_cat,
|
||||
)
|
||||
self.session.add(txn)
|
||||
new_transactions.append(txn)
|
||||
imported += 1
|
||||
|
||||
categorizer = RuleBasedCategorizer(self.session)
|
||||
for txn in new_transactions:
|
||||
if txn.category_id is None:
|
||||
result = categorizer.categorize(txn)
|
||||
if result:
|
||||
txn.category_id = result.category_id
|
||||
txn.tag = result.tag
|
||||
txn.attributed_to_id = result.attributed_to_id
|
||||
|
||||
self.session.commit()
|
||||
return {"imported": imported, "duplicates": duplicates, "total_rows": len(rows)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user