Files
DutyLog/src/dutylog/application/bot/user_dialogs/main_menu_dialog.py
T
2026-02-27 22:44:02 +03:00

141 lines
4.5 KiB
Python
Raw 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.
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 = "👑 <b>Создатель</b>"
elif is_admin:
greeting = "👨‍💼 <b>Администратор</b>"
else:
greeting = f"👋 <b>Привет, {event_from_user.first_name}!</b>"
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}
<blockquote>⚠️ <b>Профиль не найден</b></blockquote>
Вы еще не привязаны к резиденту.
Обратитесь к администратору для регистрации.
"""
else:
content = f"""
{greeting}
⏰ <b>Ваши часы дежурств</b>
<blockquote>🟢 <b>Отработанные часы:</b> <code>{resident.active_hours}</code> ч
━━━━━━━━━━━━━━━━
🔴 Неотработанные часы: <code>{resident.inactive_hours}</code> ч</blockquote>
"""
else:
content = f"""
{greeting}
<blockquote>📋 <b>Панель управления</b></blockquote>
Добро пожаловать в систему учета дежурств!
"""
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 = """
<blockquote>📜 <b>История операций</b></blockquote>
<i>Профиль не найден</i>
"""
else:
transactions = await transactions_repository.get_resident_history(resident.id)
last_10 = transactions[:10]
if not last_10:
history_text = """
<blockquote>📜 <b>История операций</b></blockquote>
<i>История операций пуста</i>
"""
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} <code>{tx.amount}</code> ч • <i>{date_str}</i>"
)
history_text = f"""
<blockquote>📜 <b>История операций</b></blockquote>
{"".join(f"{line}\n" for line in history_lines)}
<i>Показаны последние 10 операций</i>
"""
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,
),
)