fix: Upload-Sanitizing, atomare Import-Reihenfolge, idempotentes UNBEKANNT-Konto

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 18:28:15 +02:00
parent 19d870cf20
commit 8ca563cec1
4 changed files with 47 additions and 10 deletions

View File

@@ -12,12 +12,11 @@ from app.services.categorize import apply_rules
def _find_or_create_account(session: Session, bank: str, iban: str | None, filename: str) -> Account:
if iban:
acc = session.execute(select(Account).where(Account.iban == iban)).scalar()
if acc is not None:
return acc
else:
if not iban:
iban = f"UNBEKANNT-{filename}"
acc = session.execute(select(Account).where(Account.iban == iban)).scalar()
if acc is not None:
return acc
acc = Account(bank=bank, iban=iban, name=iban, type="giro")
session.add(acc)
session.flush()
@@ -89,11 +88,19 @@ def process_pdf(session: Session, path: Path) -> Statement:
apply_rules(session, drafts)
stmt.status = "draft"
# Invariant: a committed draft implies the PDF left the inbox. Move the
# file before committing so a failed move can never leave a committed
# draft with the PDF still sitting in the inbox.
uploads_dir = settings.uploads_dir
uploads_dir.mkdir(parents=True, exist_ok=True)
try:
path.replace(uploads_dir / filename)
except OSError:
session.rollback()
raise
session.commit()
session.refresh(stmt)
uploads_dir = settings.uploads_dir
uploads_dir.mkdir(parents=True, exist_ok=True)
path.replace(uploads_dir / filename)
return stmt