shopbot/bot/getters/history.py

54 lines
2.1 KiB
Python
Raw Normal View History

2024-12-24 18:08:03 +00:00
import logging
from aiogram_dialog import DialogManager
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from bot.database.models import Account
from bot.database.db import async_session
from typing import Dict, Any
def _format_account_short_info(account: Account) -> str:
return (
f"🌍 {account.region} | 🎮 {account.current_rank} | 💰 {account.price:.2f}"
)
async def get_purchase_history(dialog_manager: DialogManager, **kwargs) -> Dict[str, Any]:
user_tg_id = dialog_manager.event.from_user.id
try:
async with async_session() as session:
result = await session.execute(
select(Account).where(Account.buyer == user_tg_id)
)
purchased_accounts = result.scalars().all()
if purchased_accounts:
text = "🛍️ Ваша история покупок:"
else:
logging.warning(f"User with tg_id {user_tg_id} has no purchases")
text = "🛍️ У вас пока нет покупок, пора купить что-нибудь)"
return {
"purchased_accounts": [
(_format_account_short_info(account), account.id)
for account in purchased_accounts
],
"text": text
}
except SQLAlchemyError as e:
logging.error(f"Database error: {e}", exc_info=True)
return {"purchased_accounts": [], "text": "❌ Произошла ошибка при работе с базой данных."}
except Exception as e:
logging.error(f"An unexpected error occurred: {e}", exc_info=True)
return {"purchased_accounts": [], "text": "❌ Произошла непредвиденная ошибка."}
async def get_history_account_details(dialog_manager: DialogManager, **kwargs) -> Dict[str, str]:
account_info = dialog_manager.current_context().dialog_data.get("account_info")
if not account_info:
logging.warning("No account info found in dialog data")
return {"account_info": "❌ Информация об аккаунте недоступна."}
return {"account_info": account_info}