Verified Commit 355a8f43 authored by Jakob Moser's avatar Jakob Moser
Browse files

Add syncing with FinTS

Now this is the actual business logic: If you run BankAccount#sync, it will fetch all transactions for that account, deduplicate them, and add the new ones to the database
parent eb693b95
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ from fints.models import SEPAAccount
from schwifty import IBAN
from sqlalchemy.orm import Mapped, mapped_column, relationship

from poolpay import db
from poolpay.model.Base import Base
from poolpay.model.Iban import Iban
from poolpay.model.Transaction import Transaction
@@ -54,3 +55,19 @@ class BankAccount(Base):
        accounts = client.get_sepa_accounts()
        return next(account for account in accounts if account.iban == str(self.iban))

    def sync(self, password: str) -> None:
        """
        Retrieve a list of transactions from the bank, and make sure all of them exist locally.
        """
        with self._fints_client(password) as client:
            account = self._fints_account(client)
            mt940_transactions = client.get_transactions(account)

            for mt940_transaction in mt940_transactions:
                if not Transaction.get_corresponding(mt940_transaction, account=self):
                    transaction = Transaction.from_corresponding(
                        mt940_transaction, account=self
                    )
                    db.session.add(transaction)

        db.session.commit()