From d6a913a896ea299ea3168f93523ff61cb1fb38b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Trkan?= Date: Wed, 29 Oct 2025 18:11:53 +0100 Subject: [PATCH] feat(worker): add transaction saving to db --- 7project/backend/app/services/bank_scraper.py | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/7project/backend/app/services/bank_scraper.py b/7project/backend/app/services/bank_scraper.py index a0c8207..ea1cf3c 100644 --- a/7project/backend/app/services/bank_scraper.py +++ b/7project/backend/app/services/bank_scraper.py @@ -1,17 +1,18 @@ import json import logging from os.path import dirname, join +from time import strptime from uuid import UUID import httpx from sqlalchemy import select from app.core.db import async_session_maker +from app.models.transaction import Transaction from app.models.user import User logger = logging.getLogger(__name__) -# Reuse CSAS mTLS certs used by OAuth profile calls OAUTH_DIR = join(dirname(__file__), "..", "oauth") CERTS = ( join(OAUTH_DIR, "public_key.pem"), @@ -20,10 +21,6 @@ CERTS = ( async def aload_ceska_sporitelna_transactions(user_id: str) -> None: - """ - Async entry point to load Česká spořitelna transactions for a single user. - Validates the user_id and performs a minimal placeholder action. - """ try: uid = UUID(str(user_id)) except Exception: @@ -34,9 +31,6 @@ async def aload_ceska_sporitelna_transactions(user_id: str) -> None: async def aload_all_ceska_sporitelna_transactions() -> None: - """ - Async entry point to load Česká spořitelna transactions for all users. - """ async with async_session_maker() as session: result = await session.execute(select(User)) users = result.unique().scalars().all() @@ -54,7 +48,7 @@ async def aload_all_ceska_sporitelna_transactions() -> None: async def _aload_ceska_sporitelna_transactions(user_id: UUID) -> None: - async with async_session_maker() as session: + async with (async_session_maker() as session): result = await session.execute(select(User).where(User.id == user_id)) user: User = result.unique().scalar_one_or_none() if user is None: @@ -106,16 +100,22 @@ async def _aload_ceska_sporitelna_transactions(user_id: UUID) -> None: if response.status_code != httpx.codes.OK: continue - # Placeholder: just print the account transactions - transactions = response.json()["transactions"] - pass for transaction in transactions: - #parse and store transaction to database - #create Transaction object and save to DB - #obj = + description = transaction.get("entryDetails", {}).get("transactionDetails", {}).get( + "additionalRemittanceInformation") + date_str = transaction.get("bookingDate", {}).get("date") + date = strptime(date_str, "%Y-%m-%d") if date_str else None + obj = Transaction( + amount=transaction['amount']['value'], + description=description, + date=date, + user_id=user_id, + ) + session.add(obj) + await session.commit() pass pass