shopbot/bot/services/user.py
2024-12-25 04:30:33 +03:00

99 lines
4.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
from typing import Any, Dict
from aiogram_dialog import DialogManager
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession
from shopbot.bot.database.db import async_session
from shopbot.bot.database.models import UserModel
RESULT_MESSAGES = {
"not_enough_funds": "❌ Недостаточно средств на балансе пользователя!",
"not_found": "❌ Пользователь не найден!",
"no_user_id_or_amount": "Не указаны ID пользователя или сумма!",
"invalid_user_id_or_amount": "❌ Некорректный ID пользователя или сумма!",
"balance_increased": "✅ Баланс пользователя <b>{username}</b> пополнен на <b>{amount:.2f}₽</b>!",
"balance_decreased": "✅ Баланс пользователя <b>{username}</b> уменьшен на <b>{amount:.2f}₽</b>!",
}
async def _get_user_and_validate(
session: AsyncSession, user_id: str, amount: str
) -> Dict[str, Any]:
if not user_id or not amount:
return {"result": RESULT_MESSAGES["no_user_id_or_amount"]}
try:
user_id = int(user_id)
amount = float(amount)
except ValueError:
return {"result": RESULT_MESSAGES["invalid_user_id_or_amount"]}
try:
user = await session.scalar(select(UserModel).where(UserModel.tg_id == user_id))
if not user:
logging.warning(f"User with tg_id {user_id} not found.")
return {"result": RESULT_MESSAGES["not_found"]}
return {"user": user, "amount": amount}
except SQLAlchemyError as e:
logging.error(f"Database error: {e}", exc_info=True)
return {"result": "❌ Произошла ошибка при работе с базой данных."}
except Exception as e:
logging.error(f"An unexpected error occurred: {e}", exc_info=True)
return {"result": "❌ Произошла непредвиденная ошибка."}
async def take_balance_user(dialog_manager: DialogManager, **kwargs) -> Dict[str, str]:
user_id = dialog_manager.find("user_id_input").get_value()
amount = dialog_manager.find("amount_input").get_value()
async with async_session() as session:
user_data = await _get_user_and_validate(session, user_id, amount)
if "result" in user_data:
return user_data
user = user_data["user"]
amount = user_data["amount"]
try:
if user.balance >= amount:
user.balance -= amount
await session.commit()
return {
"result": RESULT_MESSAGES["balance_decreased"].format(
username=user.username, amount=amount
)
}
else:
return {"result": RESULT_MESSAGES["not_enough_funds"]}
except SQLAlchemyError as e:
logging.error(f"Database error: {e}", exc_info=True)
return {"result": "❌ Произошла ошибка при работе с базой данных."}
except Exception as e:
logging.error(f"An unexpected error occurred: {e}", exc_info=True)
return {"result": "❌ Произошла непредвиденная ошибка."}
async def gift_balance_data(dialog_manager: DialogManager, **kwargs) -> Dict[str, str]:
user_id = dialog_manager.find("user_id_input").get_value()
amount = dialog_manager.find("amount_input").get_value()
async with async_session() as session:
user_data = await _get_user_and_validate(session, user_id, amount)
if "result" in user_data:
return user_data
user = user_data["user"]
amount = user_data["amount"]
try:
user.balance += amount
await session.commit()
return {
"result": RESULT_MESSAGES["balance_increased"].format(
username=user.username, amount=amount
)
}
except SQLAlchemyError as e:
logging.error(f"Database error: {e}", exc_info=True)
return {"result": "❌ Произошла ошибка при работе с базой данных."}
except Exception as e:
logging.error(f"An unexpected error occurred: {e}", exc_info=True)
return {"result": "❌ Произошла непредвиденная ошибка."}