from aiogram.types import User from aiogram_dialog import Dialog, Window, DialogManager from aiogram_dialog.widgets.text import Format, Const from aiogram_dialog.widgets.kbd import SwitchTo, Back from dishka import FromDishka from dishka.integrations.aiogram_dialog import inject from dutylog.application.bot.user_dialogs.states import MainMenuSG from dutylog.infrastructure.database.repositories.users_repository import UsersRepository from dutylog.infrastructure.database.repositories.residents_repository import ResidentsRepository from dutylog.infrastructure.database.repositories.hours_transactions_repository import HoursTransactionsRepository from dutylog.infrastructure.utils.config import Config @inject async def get_main_menu_data( event_from_user: User, users_repository: FromDishka[UsersRepository], residents_repository: FromDishka[ResidentsRepository], config: FromDishka[Config], **kwargs, ): user = await users_repository.get_or_create_user( user_id=event_from_user.id, username=event_from_user.username, first_name=event_from_user.first_name, last_name=event_from_user.last_name, ) is_creator = event_from_user.id == config.bot.creator_id is_admin = user.is_admin if is_creator: greeting = "👑 Создатель" elif is_admin: greeting = "👨💼 Администратор" else: greeting = f"👋 Привет, {event_from_user.first_name}!" if not is_admin and not is_creator: resident = await residents_repository.get_resident_by_user_id(event_from_user.id) if not resident: content = f""" {greeting}
⚠️ Профиль не найденВы еще не привязаны к резиденту. Обратитесь к администратору для регистрации. """ else: content = f""" {greeting} ⏰ Ваши часы дежурств
🟢 Отработанные часы:""" else: content = f""" {greeting}{resident.active_hours}ч ━━━━━━━━━━━━━━━━ 🔴 Неотработанные часы:{resident.inactive_hours}ч
📋 Панель управленияДобро пожаловать в систему учета дежурств! """ return { "content": content, "is_regular_user": not is_admin and not is_creator, "has_resident": resident is not None if not is_admin and not is_creator else False, } @inject async def get_history_data( event_from_user: User, residents_repository: FromDishka[ResidentsRepository], transactions_repository: FromDishka[HoursTransactionsRepository], **kwargs, ): resident = await residents_repository.get_resident_by_user_id(event_from_user.id) if not resident: history_text = """
📜 История операцийПрофиль не найден """ else: transactions = await transactions_repository.get_resident_history(resident.id) last_10 = transactions[:10] if not last_10: history_text = """
📜 История операцийИстория операций пуста """ else: history_lines = [] for tx in last_10: emoji = "➕" if tx.transaction_type == "increase" else "➖" date_str = tx.created_at.strftime("%d.%m.%Y %H:%M") history_lines.append( f"{emoji}
{tx.amount} ч • {date_str}"
)
history_text = f"""
📜 История операций{"".join(f"{line}\n" for line in history_lines)} Показаны последние 10 операций """ return {"history_content": history_text} main_menu_dialog = Dialog( Window( Format("{content}"), SwitchTo( Const("📜 История"), id="history_btn", state=MainMenuSG.history, when="has_resident", ), state=MainMenuSG.main, getter=get_main_menu_data, ), Window( Format("{history_content}"), Back(Const("◀️ Назад")), state=MainMenuSG.history, getter=get_history_data, ), )