Files
service-finder/backend/app/services/config_service.py
2026-02-26 08:19:25 +01:00

68 lines
2.6 KiB
Python
Executable File

# /opt/docker/dev/service_finder/backend/app/services/config_service.py
from typing import Any, Optional, Dict
import logging
from decimal import Decimal
from datetime import datetime, timezone
from sqlalchemy import select, text
from sqlalchemy.ext.asyncio import AsyncSession
# Modellek importálása a központi helyről
from app.models import ExchangeRate, AssetCost, AssetTelemetry
from app.db.session import AsyncSessionLocal
logger = logging.getLogger(__name__)
class CostService:
# A cost_in típusát 'Any'-re állítottam ideiglenesen, hogy ne dobjon újabb ImportError-t a hiányzó Pydantic séma miatt
async def record_cost(self, db: AsyncSession, cost_in: Any, user_id: int):
try:
# 1. Árfolyam lekérése (EUR Pivot)
rate_stmt = select(ExchangeRate).where(
ExchangeRate.target_currency == cost_in.currency_local
).order_by(ExchangeRate.id.desc()).limit(1)
rate_res = await db.execute(rate_stmt)
rate_obj = rate_res.scalar_one_or_none()
exchange_rate = rate_obj.rate if rate_obj else Decimal("1.0")
# 2. Kalkuláció
amt_eur = Decimal(str(cost_in.amount_local)) / exchange_rate
# 3. Mentés az új AssetCost modellbe
new_cost = AssetCost(
asset_id=cost_in.asset_id,
organization_id=cost_in.organization_id,
driver_id=user_id,
cost_type=cost_in.cost_type,
amount_local=cost_in.amount_local,
currency_local=cost_in.currency_local,
amount_eur=amt_eur,
exchange_rate_used=exchange_rate,
mileage_at_cost=cost_in.mileage_at_cost,
date=cost_in.date or datetime.now(timezone.utc)
)
db.add(new_cost)
# 4. Telemetria szinkron
if cost_in.mileage_at_cost:
tel_stmt = select(AssetTelemetry).where(AssetTelemetry.asset_id == cost_in.asset_id)
telemetry = (await db.execute(tel_stmt)).scalar_one_or_none()
if telemetry and cost_in.mileage_at_cost > (telemetry.current_mileage or 0):
telemetry.current_mileage = cost_in.mileage_at_cost
await db.commit()
return new_cost
except Exception as e:
await db.rollback()
raise e
class ConfigService:
"""
MB 2.0 Alapvető konfigurációs szerviz.
Ezt kereste az auth_service.py az induláshoz.
"""
pass
# A példány, amit a többi modul (pl. az auth_service) importálni próbál
config = ConfigService()