2024-12-24 18:08:03 +00:00
|
|
|
|
import logging
|
2024-12-25 01:30:33 +00:00
|
|
|
|
from typing import Any, Dict
|
2024-12-24 18:08:03 +00:00
|
|
|
|
|
|
|
|
|
from aiogram_dialog import DialogManager
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2024-12-25 01:30:33 +00:00
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
2024-12-24 18:08:03 +00:00
|
|
|
|
|
2024-12-25 01:30:33 +00:00
|
|
|
|
from shopbot.bot.database.db import async_session
|
|
|
|
|
from shopbot.bot.database.models import UserModel
|
2024-12-24 18:08:03 +00:00
|
|
|
|
|
|
|
|
|
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:
|
2024-12-25 01:30:33 +00:00
|
|
|
|
return {"result": RESULT_MESSAGES["no_user_id_or_amount"]}
|
2024-12-24 18:08:03 +00:00
|
|
|
|
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(
|
2024-12-25 01:30:33 +00:00
|
|
|
|
username=user.username, amount=amount
|
|
|
|
|
)
|
2024-12-24 18:08:03 +00:00
|
|
|
|
}
|
|
|
|
|
else:
|
2024-12-25 01:30:33 +00:00
|
|
|
|
return {"result": RESULT_MESSAGES["not_enough_funds"]}
|
2024-12-24 18:08:03 +00:00
|
|
|
|
except SQLAlchemyError as e:
|
2024-12-25 01:30:33 +00:00
|
|
|
|
logging.error(f"Database error: {e}", exc_info=True)
|
|
|
|
|
return {"result": "❌ Произошла ошибка при работе с базой данных."}
|
2024-12-24 18:08:03 +00:00
|
|
|
|
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:
|
2024-12-25 01:30:33 +00:00
|
|
|
|
user.balance += amount
|
|
|
|
|
await session.commit()
|
|
|
|
|
return {
|
|
|
|
|
"result": RESULT_MESSAGES["balance_increased"].format(
|
|
|
|
|
username=user.username, amount=amount
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-12-24 18:08:03 +00:00
|
|
|
|
except SQLAlchemyError as e:
|
2024-12-25 01:30:33 +00:00
|
|
|
|
logging.error(f"Database error: {e}", exc_info=True)
|
|
|
|
|
return {"result": "❌ Произошла ошибка при работе с базой данных."}
|
2024-12-24 18:08:03 +00:00
|
|
|
|
except Exception as e:
|
2024-12-25 01:30:33 +00:00
|
|
|
|
logging.error(f"An unexpected error occurred: {e}", exc_info=True)
|
|
|
|
|
return {"result": "❌ Произошла непредвиденная ошибка."}
|