from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, text from app.models.gamification import UserStats, PointsLedger from app.models.identity import User class GamificationService: @staticmethod async def award_points(db: AsyncSession, user_id: int, points: int, reason: str): """Pontok jóváírása (SQL szinkronizált points mezővel).""" new_entry = PointsLedger( user_id=user_id, points=points, # Javítva: points_change helyett points reason=reason ) db.add(new_entry) result = await db.execute(select(UserStats).where(UserStats.user_id == user_id)) stats = result.scalar_one_or_none() if not stats: stats = UserStats(user_id=user_id, total_points=0, current_level=1) db.add(stats) stats.total_points += points await db.flush() return stats.total_points