Compare commits

1 Commits

Author SHA1 Message Date
d6a913a896 feat(worker): add transaction saving to db 2025-10-29 18:11:53 +01:00

View File

@@ -1,17 +1,18 @@
import json import json
import logging import logging
from os.path import dirname, join from os.path import dirname, join
from time import strptime
from uuid import UUID from uuid import UUID
import httpx import httpx
from sqlalchemy import select from sqlalchemy import select
from app.core.db import async_session_maker from app.core.db import async_session_maker
from app.models.transaction import Transaction
from app.models.user import User from app.models.user import User
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Reuse CSAS mTLS certs used by OAuth profile calls
OAUTH_DIR = join(dirname(__file__), "..", "oauth") OAUTH_DIR = join(dirname(__file__), "..", "oauth")
CERTS = ( CERTS = (
join(OAUTH_DIR, "public_key.pem"), join(OAUTH_DIR, "public_key.pem"),
@@ -20,10 +21,6 @@ CERTS = (
async def aload_ceska_sporitelna_transactions(user_id: str) -> None: 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: try:
uid = UUID(str(user_id)) uid = UUID(str(user_id))
except Exception: 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 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: async with async_session_maker() as session:
result = await session.execute(select(User)) result = await session.execute(select(User))
users = result.unique().scalars().all() 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 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)) result = await session.execute(select(User).where(User.id == user_id))
user: User = result.unique().scalar_one_or_none() user: User = result.unique().scalar_one_or_none()
if user is 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: if response.status_code != httpx.codes.OK:
continue continue
# Placeholder: just print the account transactions
transactions = response.json()["transactions"] transactions = response.json()["transactions"]
pass
for transaction in transactions: for transaction in transactions:
#parse and store transaction to database description = transaction.get("entryDetails", {}).get("transactionDetails", {}).get(
#create Transaction object and save to DB "additionalRemittanceInformation")
#obj = 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
pass pass