Verified Commit 3d6f3b95 authored by Jakob Moser's avatar Jakob Moser
Browse files

Return added transactions in sync

parent 81a97713
Loading
Loading
Loading
Loading
+12 −7
Changes for poolpay/model/BankAccount.py: 12 added lines, 7 removed lines.
Original line number Diff line number Diff line
from collections.abc import Generator
from collections.abc import Generator, Sequence
from contextlib import contextmanager

from fints.client import FinTS3PinTanClient
@@ -55,19 +55,24 @@ 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:
    def sync(self, password: str) -> Sequence[Transaction]:
        """
        Retrieve a list of transactions from the bank, and make sure all of them exist locally.

        :return: the newly created transactions, if any
        """

        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
            new_transactions = tuple(
                Transaction.from_corresponding(mt940_transaction, account=self)
                for mt940_transaction in mt940_transactions
                if not Transaction.get_corresponding(mt940_transaction, account=self)
            )
                    db.session.add(transaction)

            db.session.add_all(new_transactions)

        db.session.commit()
        return new_transactions