"""Admin handler for spa-telegram-bot — secure with password.""" from aiogram import Router, F from aiogram.types import Message, CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton from aiogram.filters import Command, CommandObject from aiogram import Bot from app.config import USE_POSTGRES from app.config import ADMIN_PASSWORD, BOT_TOKEN from app.logger import logger router = Router() ADMIN_IDS = {991309145} _approved = set() _notify_bot = None def _get_bot(): global _notify_bot if _notify_bot is None: _notify_bot = Bot(token=BOT_TOKEN) return _notify_bot def is_admin(user_id: int, password: str | None = None) -> bool: if user_id in ADMIN_IDS: return True if user_id in _approved: return True if password and password == ADMIN_PASSWORD: _approved.add(user_id) return True return False @router.message(Command("admin")) async def admin_panel(message: Message, command: CommandObject): uid = message.from_user.id password = command.args if not is_admin(uid, password): return logger.info("Admin {uid} opened admin panel", uid=uid) if USE_POSTGRES: from app.db.postgres import PostgresDB active = await PostgresDB.get_all_active_bookings() else: from app.db.memory import load_bookings bookings = load_bookings() active = [b for b in bookings if b.get("status") == "active"] if not active: await message.answer("\U0001f4cb \u0410\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043d\u0435\u0442") return for booking in active: buttons = [[ InlineKeyboardButton( text="\u274c \u041e\u0442\u043c\u0435\u043d\u0438\u0442\u044c", callback_data=f"admin_cancel_{booking['id']}" ) ]] user_name = booking.get("user_name") or f"id{booking['user_id']}" text = ( "\U0001f4cb \u0417\u0430\u043f\u0438\u0441\u044c #" + str(booking['id']) + "\n" "\U0001f464 \u041a\u043b\u0438\u0435\u043d\u0442: " + user_name + "\n" "\U0001f486 \u0423\u0441\u043b\u0443\u0433\u0430: " + booking.get('service', '?') + "\n" "\U0001f464 \u041c\u0430\u0441\u0442\u0435\u0440: " + booking.get('master', '?') + "\n" "\U0001f4c5 \u0414\u0430\u0442\u0430: " + booking.get('date', '?') + "\n" "\U0001f552 \u0412\u0440\u0435\u043c\u044f: " + booking.get('time', '?') ) await message.answer(text, reply_markup=InlineKeyboardMarkup(inline_keyboard=buttons)) @router.callback_query(F.data.startswith("admin_cancel_")) async def admin_cancel_booking(callback: CallbackQuery): if not is_admin(callback.from_user.id): await callback.answer("\u26d4 \u041d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0430") return booking_id = int(callback.data.split("_")[2]) logger.info("Admin {uid} cancelled booking #{bid}", uid=callback.from_user.id, bid=booking_id) if USE_POSTGRES: from app.db.postgres import PostgresDB success = await PostgresDB.cancel_booking(booking_id) booking = await PostgresDB.get_booking_by_id(booking_id) if success else None else: from app.db.memory import cancel_booking as json_cancel, load_bookings success = json_cancel(booking_id) bookings = load_bookings() booking = next((b for b in bookings if b["id"] == booking_id), None) if not success: await callback.message.edit_text("\u0417\u0430\u043f\u0438\u0441\u044c \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430 \u0438\u043b\u0438 \u0443\u0436\u0435 \u043e\u0442\u043c\u0435\u043d\u0435\u043d\u0430") await callback.answer() return await callback.message.edit_text("\u2705 \u0417\u0430\u043f\u0438\u0441\u044c #" + str(booking_id) + " \u043e\u0442\u043c\u0435\u043d\u0435\u043d\u0430 \u0430\u0434\u043c\u0438\u043d\u043e\u043c") if booking: try: bot = _get_bot() user_id = booking["user_id"] notify_text = ( "\u26a0\ufe0f \u0412\u0430\u0448\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u043e\u0442\u043c\u0435\u043d\u0435\u043d\u0430 \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u043e\u043c.\n\n" "\U0001f486 \u0423\u0441\u043b\u0443\u0433\u0430: " + booking.get('service', '?') + "\n" "\U0001f464 \u041c\u0430\u0441\u0442\u0435\u0440: " + booking.get('master', '?') + "\n" "\U0001f4c5 \u0414\u0430\u0442\u0430: " + booking.get('date', '?') + "\n" "\U0001f552 \u0412\u0440\u0435\u043c\u044f: " + booking.get('time', '?') + "\n\n" "\U0001f4cb \u0427\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u0441\u043d\u043e\u0432\u0430, \u043d\u0430\u0436\u043c\u0438\u0442\u0435 \u00ab\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f\u00bb" ) await bot.send_message(user_id, notify_text) logger.info("Notified user {uid} about cancellation", uid=user_id) except Exception as e: logger.warning("Failed to notify user: {err}", err=e) await callback.answer()