fix: Ein Uebertrag im Eingang fragt nicht mehr nach der Kategorie
Die Zeile im Eingang zeigte die Kategorie-Auswahl bedingungslos – auch bei einem Kontouebertrag, der gar keine bekommt. Die Oberflaeche konnte es auch nicht wissen: InboxItemOut hatte kein Feld, an dem sich ein Uebertrag erkennen laesst. Jetzt liefert der Eintrag transfer_account_id mit – das jeweils ANDERE Konto, aus seiner Sicht. Steht es, zeigt die Zeile '↔ Kontoname' statt der Auswahl. Ermittelt wird es mit einer Sammelabfrage, nicht je Zeile: Die Liste geht bis 200 Eintraege. Dazu die Luecke dahinter: set_category schrieb die Kategorie auch auf die Buchung und konnte sie so auf einem TxnType.transfer ablegen – genau das, was das Buchungsformular beim Anlegen verhindert. Bei einem Uebertrag lehnt der Endpunkt jetzt ab; bei allem anderen bleibt es, wie es war. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -72,19 +72,55 @@ def delete_token(token_id: int, db: Session = Depends(get_db), user: User = Depe
|
||||
# --- Eingang -----------------------------------------------------------------
|
||||
|
||||
|
||||
def _gegenkonten(db: Session, user: User, eintraege: list[InboxItem]) -> dict[int, int]:
|
||||
"""Je Eintrag das Gegenkonto, falls seine Buchung ein Übertrag ist.
|
||||
|
||||
Als Sammelabfrage und nicht je Zeile: Die Liste geht bis 200 Einträge.
|
||||
"""
|
||||
txn_ids = {e.transaction_id for e in eintraege if e.transaction_id is not None}
|
||||
if not txn_ids:
|
||||
return {}
|
||||
buchungen = {
|
||||
b.id: b
|
||||
for b in db.query(Transaction).filter(
|
||||
Transaction.owner_id == user.id,
|
||||
Transaction.id.in_(txn_ids),
|
||||
Transaction.type == TxnType.transfer,
|
||||
)
|
||||
}
|
||||
gegen: dict[int, int] = {}
|
||||
for e in eintraege:
|
||||
buchung = buchungen.get(e.transaction_id)
|
||||
if buchung is None:
|
||||
continue
|
||||
# Das jeweils ANDERE Konto, aus Sicht dieses Eintrags.
|
||||
gegen[e.id] = (buchung.to_account_id if buchung.account_id == e.account_id
|
||||
else buchung.account_id)
|
||||
return gegen
|
||||
|
||||
|
||||
def _out(db: Session, user: User, eintraege: list[InboxItem]) -> list[InboxItemOut]:
|
||||
gegen = _gegenkonten(db, user, eintraege)
|
||||
return [
|
||||
InboxItemOut.model_validate(e).model_copy(
|
||||
update={"transfer_account_id": gegen.get(e.id)})
|
||||
for e in eintraege
|
||||
]
|
||||
|
||||
|
||||
@router.get("", response_model=list[InboxItemOut])
|
||||
def list_inbox(
|
||||
limit: int = 200,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
return (
|
||||
return _out(db, user, (
|
||||
db.query(InboxItem)
|
||||
.filter(InboxItem.owner_id == user.id, InboxItem.status != InboxStatus.discarded)
|
||||
.order_by(InboxItem.date.desc(), InboxItem.id.desc())
|
||||
.limit(min(limit, 1000))
|
||||
.all()
|
||||
)
|
||||
))
|
||||
|
||||
|
||||
@router.post("/{item_id}/book", response_model=InboxItemOut)
|
||||
@@ -99,7 +135,7 @@ def book_item(item_id: int, db: Session = Depends(get_db), user: User = Depends(
|
||||
db.commit()
|
||||
eingang.suche_uebertraege(db, user)
|
||||
db.refresh(eintrag)
|
||||
return eintrag
|
||||
return _out(db, user, [eintrag])[0]
|
||||
|
||||
|
||||
class InboxTransferIn(BaseModel):
|
||||
@@ -142,7 +178,7 @@ def book_as_transfer(item_id: int, payload: InboxTransferIn,
|
||||
detail="Lässt sich nicht buchen")
|
||||
db.commit()
|
||||
db.refresh(eintrag)
|
||||
return eintrag
|
||||
return _out(db, user, [eintrag])[0]
|
||||
|
||||
|
||||
@router.post("/book-all")
|
||||
@@ -194,11 +230,19 @@ def set_category(
|
||||
|
||||
eintrag = get_owned(db, InboxItem, item_id, user)
|
||||
kategorie = get_owned(db, Category, category_id, user)
|
||||
txn = (db.get(Transaction, eintrag.transaction_id)
|
||||
if eintrag.transaction_id is not None else None)
|
||||
if txn is not None and txn.owner_id == user.id:
|
||||
# Ein Übertrag ist keine Ausgabe. Ohne diese Sperre landete über den
|
||||
# Eingang eine Kategorie auf einer Übertrags-Buchung – genau das, was
|
||||
# das Buchungsformular beim Anlegen verhindert.
|
||||
if txn.type == TxnType.transfer:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Ein Kontoübertrag bekommt keine Kategorie.",
|
||||
)
|
||||
txn.category_id = kategorie.id
|
||||
eintrag.category_id = kategorie.id
|
||||
if eintrag.transaction_id is not None:
|
||||
txn = db.get(Transaction, eintrag.transaction_id)
|
||||
if txn is not None and txn.owner_id == user.id:
|
||||
txn.category_id = kategorie.id
|
||||
db.commit()
|
||||
db.refresh(eintrag)
|
||||
return eintrag
|
||||
return _out(db, user, [eintrag])[0]
|
||||
|
||||
Reference in New Issue
Block a user