From a7c4e06102a2ab41d082660db80b8e858cbfff30 Mon Sep 17 00:00:00 2001 From: Capybara-z Date: Wed, 22 Oct 2025 02:07:18 +0300 Subject: [PATCH] fix: add subgroup/special group support to key renewals and cluster sync --- handlers/admin/clusters/clusters_handler.py | 19 +++++++++++++++++-- handlers/admin/gifts/gifts_handler.py | 1 - handlers/admin/users/users_handler.py | 7 ++++++- handlers/coupons.py | 6 ++++++ .../notifications/general_notifications.py | 4 ++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/handlers/admin/clusters/clusters_handler.py b/handlers/admin/clusters/clusters_handler.py index 8295967b..7556637f 100644 --- a/handlers/admin/clusters/clusters_handler.py +++ b/handlers/admin/clusters/clusters_handler.py @@ -615,6 +615,7 @@ async def handle_sync_cluster( traffic_limit_bytes = 0 hwid_limit = 0 + subgroup_title = None if key["tariff_id"]: tariff = await session.get(Tariff, key["tariff_id"]) if tariff: @@ -623,12 +624,22 @@ async def handle_sync_cluster( else: traffic_limit_bytes = 0 hwid_limit = tariff.device_limit + subgroup_title = tariff.subgroup_title else: logger.warning( f"[Sync] Ключ {key['client_id']} с несуществующим тарифом ID={key['tariff_id']} — обновим без лимитов" ) - inbound_ids = [s["inbound_id"] for s in cluster_servers if s.get("inbound_id")] + filtered_servers = cluster_servers + if subgroup_title: + filtered_servers = [s for s in cluster_servers if subgroup_title in s.get("tariff_subgroups", [])] + if not filtered_servers: + logger.warning( + f"[Sync] В кластере {cluster_name} не найдено серверов для подгруппы '{subgroup_title}'. Использую весь кластер." + ) + filtered_servers = cluster_servers + + inbound_ids = [s["inbound_id"] for s in filtered_servers if s.get("inbound_id")] success = await remna.update_user( uuid=key["client_id"], @@ -765,9 +776,10 @@ async def handle_days_input(message: Message, state: FSMContext, session: AsyncS traffic_limit = 0 device_limit = 0 + key_subgroup = None if key.tariff_id: result = await session.execute( - select(Tariff.traffic_limit, Tariff.device_limit).where( + select(Tariff.traffic_limit, Tariff.device_limit, Tariff.subgroup_title).where( Tariff.id == key.tariff_id, Tariff.is_active.is_(True) ) ) @@ -775,6 +787,7 @@ async def handle_days_input(message: Message, state: FSMContext, session: AsyncS if tariff: traffic_limit = int(tariff[0]) if tariff[0] is not None else 0 device_limit = int(tariff[1]) if tariff[1] is not None else 0 + key_subgroup = tariff[2] await renew_key_in_cluster( cluster_name, @@ -785,6 +798,8 @@ async def handle_days_input(message: Message, state: FSMContext, session: AsyncS session=session, hwid_device_limit=device_limit, reset_traffic=False, + target_subgroup=key_subgroup, + old_subgroup=key_subgroup, ) await update_key_expiry(session, key.client_id, new_expiry) diff --git a/handlers/admin/gifts/gifts_handler.py b/handlers/admin/gifts/gifts_handler.py index 52699ec6..76c755ea 100644 --- a/handlers/admin/gifts/gifts_handler.py +++ b/handlers/admin/gifts/gifts_handler.py @@ -249,7 +249,6 @@ async def view_gift(callback: CallbackQuery, session: AsyncSession): f"ID: {gift.gift_id}\n" f"Срок: {duration_text}\n" f"Активаций: {usage_text}\n" - f"Истекает: {gift.expiry_time.strftime('%d.%m.%Y')}\n" f"Ссылка для активации:\n
{gift.gift_link}
" ) diff --git a/handlers/admin/users/users_handler.py b/handlers/admin/users/users_handler.py index 65451a20..282f86a2 100644 --- a/handlers/admin/users/users_handler.py +++ b/handlers/admin/users/users_handler.py @@ -1223,14 +1223,17 @@ async def change_expiry_time(expiry_time: int, email: str, session: AsyncSession traffic_limit = 0 device_limit = None + key_subgroup = None if tariff_id: result = await session.execute( - select(Tariff.traffic_limit, Tariff.device_limit).where(Tariff.id == tariff_id, Tariff.is_active.is_(True)) + select(Tariff.traffic_limit, Tariff.device_limit, Tariff.subgroup_title).where(Tariff.id == tariff_id, Tariff.is_active.is_(True)) ) tariff = result.first() if tariff: traffic_limit = int(tariff[0]) if tariff[0] is not None else 0 device_limit = int(tariff[1]) if tariff[1] is not None else 0 + key_subgroup = tariff[2] + servers = await get_servers(session=session) @@ -1255,6 +1258,8 @@ async def change_expiry_time(expiry_time: int, email: str, session: AsyncSession session=session, hwid_device_limit=device_limit, reset_traffic=False, + target_subgroup=key_subgroup, + old_subgroup=key_subgroup, ) await update_key_expiry(session, client_id, expiry_time) diff --git a/handlers/coupons.py b/handlers/coupons.py index 7ba12e07..da4a2461 100644 --- a/handlers/coupons.py +++ b/handlers/coupons.py @@ -225,6 +225,10 @@ async def handle_key_extension( total_gb = int(tariff["traffic_limit"]) if tariff and tariff.get("traffic_limit") else 0 device_limit = int(tariff["device_limit"]) if tariff and tariff.get("device_limit") else 0 + key_subgroup = None + if tariff: + key_subgroup = tariff.get('subgroup_title') + await renew_key_in_cluster( cluster_id=key.server_id, email=key.email, @@ -234,6 +238,8 @@ async def handle_key_extension( session=session, hwid_device_limit=device_limit, reset_traffic=False, + target_subgroup=key_subgroup, + old_subgroup=key_subgroup, ) await update_key_expiry(session, client_id, new_expiry) await update_coupon_usage_count(session, coupon.id) diff --git a/handlers/notifications/general_notifications.py b/handlers/notifications/general_notifications.py index a013807a..936afb02 100644 --- a/handlers/notifications/general_notifications.py +++ b/handlers/notifications/general_notifications.py @@ -566,6 +566,8 @@ async def process_auto_renew_or_notify( f"Продление подписки {email} на {duration_days} дней для пользователя {tg_id}. Баланс: {balance}, списываем: {renewal_cost}" ) + key_subgroup = selected_tariff.get('subgroup_title') + await renew_key_in_cluster( cluster_id=server_id, email=email, @@ -574,6 +576,8 @@ async def process_auto_renew_or_notify( total_gb=total_gb, hwid_device_limit=device_limit, session=conn, + target_subgroup=key_subgroup, + old_subgroup=key_subgroup, ) await update_balance(conn, tg_id, -renewal_cost) await update_key_expiry(conn, client_id, int(new_expiry_time))