From fff4ef479016ffd6065275ce6ca3f26bdd4696f1 Mon Sep 17 00:00:00 2001 From: Zakhar Izmaylov Date: Fri, 22 Nov 2024 00:43:23 +0300 Subject: [PATCH] Add search from username --- handlers/admin/admin_panel.py | 5 ++ handlers/admin/admin_user_editor.py | 81 +++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/handlers/admin/admin_panel.py b/handlers/admin/admin_panel.py index dd0b1aa5..186d35f6 100644 --- a/handlers/admin/admin_panel.py +++ b/handlers/admin/admin_panel.py @@ -168,6 +168,11 @@ async def user_editor_menu(callback_query: CallbackQuery): text="🆔 Поиск по Telegram ID", callback_data="search_by_tg_id" ) ) + builder.row( + InlineKeyboardButton( + text="🌐 Поиск по Username", callback_data="search_by_username" + ) + ) builder.row(InlineKeyboardButton(text="🔙 Вернуться назад", callback_data="admin")) await callback_query.message.edit_text( "👇 Выберите способ поиска пользователя:", reply_markup=builder.as_markup() diff --git a/handlers/admin/admin_user_editor.py b/handlers/admin/admin_user_editor.py index 66a0d52f..5b5495bd 100644 --- a/handlers/admin/admin_user_editor.py +++ b/handlers/admin/admin_user_editor.py @@ -21,6 +21,7 @@ router = Router() class UserEditorState(StatesGroup): waiting_for_tg_id = State() + waiting_for_username = State() displaying_user_info = State() waiting_for_new_balance = State() waiting_for_key_name = State() @@ -33,12 +34,27 @@ async def prompt_tg_id(callback_query: CallbackQuery, state: FSMContext): await state.set_state(UserEditorState.waiting_for_tg_id) -@router.message(UserEditorState.waiting_for_tg_id, F.text.isdigit(), IsAdminFilter()) -async def handle_tg_id_input(message: types.Message, state: FSMContext): - tg_id = int(message.text) +@router.callback_query(F.data == "search_by_username", IsAdminFilter()) +async def prompt_username(callback_query: CallbackQuery, state: FSMContext): + await callback_query.message.edit_text("🔍 Введите Username клиента:") + await state.set_state(UserEditorState.waiting_for_username) + +@router.message(UserEditorState.waiting_for_username, IsAdminFilter()) +async def handle_username_input(message: types.Message, state: FSMContext): + username = message.text.strip() conn = await asyncpg.connect(DATABASE_URL) try: + user_record = await conn.fetchrow( + "SELECT tg_id FROM users WHERE username = $1", username + ) + + if not user_record: + await message.reply("🔍 Пользователь с указанным username не найден. 🚫") + await state.clear() + return + + tg_id = user_record["tg_id"] balance = await conn.fetchval( "SELECT balance FROM connections WHERE tg_id = $1", tg_id ) @@ -73,7 +89,64 @@ async def handle_tg_id_input(message: types.Message, state: FSMContext): ) ) - builder.row(InlineKeyboardButton(text="🔙 Назад", callback_data="admin")) + builder.row(InlineKeyboardButton(text="🔙 Назад", callback_data="user_editor")) + + user_info = ( + f"📊 Информация о пользователе:\n" + f"💰 Баланс: {balance}\n" + f"👥 Количество рефералов: {referral_count}\n" + f"🔑 Ключи (для редактирования нажмите на ключ):" + ) + await message.reply( + user_info, reply_markup=builder.as_markup(), parse_mode="HTML" + ) + await state.set_state(UserEditorState.displaying_user_info) + + finally: + await conn.close() + + +@router.message(UserEditorState.waiting_for_tg_id, F.text.isdigit(), IsAdminFilter()) +async def handle_tg_id_input(message: types.Message, state: FSMContext): + tg_id = int(message.text) + + conn = await asyncpg.connect(DATABASE_URL) + try: + balance = await conn.fetchval( + "SELECT balance FROM connections WHERE tg_id = $1", tg_id + ) + key_records = await conn.fetch("SELECT email FROM keys WHERE tg_id = $1", tg_id) + referral_count = await conn.fetchval( + "SELECT COUNT(*) FROM referrals WHERE referrer_tg_id = $1", tg_id + ) + + if balance is None: + await message.reply("❌ Пользователь с указанным tg_id не найден. 🔍") + await state.clear() + return + + builder = InlineKeyboardBuilder() + + for (email,) in key_records: + builder.row( + InlineKeyboardButton( + text=f"🔑 {email}", callback_data=f"edit_key_{email}" + ) + ) + + builder.row( + InlineKeyboardButton( + text="📝 Изменить баланс", callback_data=f"change_balance_{tg_id}" + ) + ) + + builder.row( + InlineKeyboardButton( + text="🔄 Восстановить пробник", callback_data=f"restore_trial_{tg_id}" + ) + ) + + builder.row(InlineKeyboardButton(text="🔙 Назад", callback_data="user_editor")) user_info = ( f"📊 Информация о пользователе:\n"