60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
import logging
|
||
from typing import Any, Dict
|
||
|
||
from aiogram_dialog import DialogManager
|
||
from sqlalchemy import select
|
||
from sqlalchemy.exc import SQLAlchemyError
|
||
|
||
from shopbot.bot.database.db import async_session
|
||
from shopbot.bot.database.models import Account
|
||
|
||
|
||
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}
|