STABLE: Final schema sync, optimized gitignore
This commit is contained in:
@@ -1,63 +1,68 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/services/config_service.py
|
||||
from typing import Any, Optional, Dict
|
||||
import logging
|
||||
from sqlalchemy import text
|
||||
from app.db.session import SessionLocal
|
||||
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 ConfigService:
|
||||
def __init__(self):
|
||||
self._cache: Dict[str, Any] = {}
|
||||
|
||||
async def get_setting(
|
||||
self,
|
||||
key: str,
|
||||
org_id: Optional[int] = None,
|
||||
region_code: Optional[str] = None,
|
||||
tier_id: Optional[int] = None,
|
||||
default: Any = None
|
||||
) -> Any:
|
||||
# 1. Cache kulcs generálása (hierarchiát is figyelembe véve)
|
||||
cache_key = f"{key}_{org_id}_{tier_id}_{region_code}"
|
||||
if cache_key in self._cache:
|
||||
return self._cache[cache_key]
|
||||
|
||||
query = text("""
|
||||
SELECT value_json
|
||||
FROM data.system_settings
|
||||
WHERE key_name = :key
|
||||
AND (
|
||||
(org_id = :org_id) OR
|
||||
(org_id IS NULL AND tier_id = :tier_id) OR
|
||||
(org_id IS NULL AND tier_id IS NULL AND region_code = :region_code) OR
|
||||
(org_id IS NULL AND tier_id IS NULL AND region_code IS NULL)
|
||||
)
|
||||
ORDER BY
|
||||
(org_id IS NOT NULL) DESC,
|
||||
(tier_id IS NOT NULL) DESC,
|
||||
(region_code IS NOT NULL) DESC
|
||||
LIMIT 1
|
||||
""")
|
||||
|
||||
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:
|
||||
async with SessionLocal() as db:
|
||||
result = await db.execute(query, {
|
||||
"key": key,
|
||||
"org_id": org_id,
|
||||
"tier_id": tier_id,
|
||||
"region_code": region_code
|
||||
})
|
||||
row = result.fetchone()
|
||||
val = row[0] if row else default
|
||||
|
||||
# 2. Mentés cache-be
|
||||
self._cache[cache_key] = val
|
||||
return val
|
||||
# 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:
|
||||
logger.error(f"ConfigService Error: {e}")
|
||||
return default
|
||||
await db.rollback()
|
||||
raise e
|
||||
|
||||
def clear_cache(self):
|
||||
self._cache = {}
|
||||
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()
|
||||
Reference in New Issue
Block a user