From df15cfd25e1609050c65ce59cad11e1dc7d1a43f Mon Sep 17 00:00:00 2001 From: machka-pasla Date: Thu, 7 Aug 2025 18:46:19 +0300 Subject: [PATCH] Refactor promo handler functions to include session management - Updated the promo_delete_handler, promo_edit_select_handler, and promo_edit_field_handler functions to accept an AsyncSession parameter, improving database interaction consistency. - Enhanced the handling of expired subscriptions in the PanelWebhookService by modifying notification logic to only send messages if enabled, ensuring better control over user notifications. - Added an import for the 'and_' function in payment_dal.py to support more complex query conditions. --- bot/handlers/admin/promo/manage.py | 8 ++++---- bot/services/panel_webhook_service.py | 22 ++++++++++++---------- db/dal/payment_dal.py | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/bot/handlers/admin/promo/manage.py b/bot/handlers/admin/promo/manage.py index f8a5d97..c83b1d7 100644 --- a/bot/handlers/admin/promo/manage.py +++ b/bot/handlers/admin/promo/manage.py @@ -332,7 +332,7 @@ async def promo_export_all_handler(callback: types.CallbackQuery, i18n_data: dic @router.callback_query(F.data.startswith("promo_delete:")) -async def promo_delete_handler(callback: types.CallbackQuery, i18n_data: dict, session: AsyncSession): +async def promo_delete_handler(callback: types.CallbackQuery, i18n_data: dict, settings: Settings, session: AsyncSession): i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance") current_lang = i18n_data.get("current_language") if not i18n or not callback.message or not current_lang: @@ -345,7 +345,7 @@ async def promo_delete_handler(callback: types.CallbackQuery, i18n_data: dict, s if promo: await session.commit() await callback.answer(_("admin_promo_deleted_success", code=promo.code), show_alert=True) - await promo_management_handler(callback, i18n_data, get_settings(), session, 0) + await promo_management_handler(callback, i18n_data, settings, session, 0) else: await callback.answer(_("admin_promo_not_found"), show_alert=True) except (ValueError, IndexError): @@ -354,7 +354,7 @@ async def promo_delete_handler(callback: types.CallbackQuery, i18n_data: dict, s # --- Promo Edit Handlers --- @router.callback_query(F.data.startswith("promo_edit_select:")) -async def promo_edit_select_handler(callback: types.CallbackQuery, i18n_data: dict): +async def promo_edit_select_handler(callback: types.CallbackQuery, i18n_data: dict, session: AsyncSession): i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance") current_lang = i18n_data.get("current_language") if not i18n or not callback.message or not current_lang: @@ -373,7 +373,7 @@ async def promo_edit_select_handler(callback: types.CallbackQuery, i18n_data: di @router.callback_query(F.data.startswith("promo_edit_field:")) -async def promo_edit_field_handler(callback: types.CallbackQuery, state: FSMContext, i18n_data: dict): +async def promo_edit_field_handler(callback: types.CallbackQuery, state: FSMContext, i18n_data: dict, session: AsyncSession): i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance") current_lang = i18n_data.get("current_language") if not i18n or not callback.message or not current_lang: return diff --git a/bot/services/panel_webhook_service.py b/bot/services/panel_webhook_service.py index 818c04e..1d22689 100644 --- a/bot/services/panel_webhook_service.py +++ b/bot/services/panel_webhook_service.py @@ -133,18 +133,20 @@ class PanelWebhookService: user_name=first_name, end_date=user_payload.get("expireAt", "")[:10], ) - elif event_name == "user.expired" and self.settings.SUBSCRIPTION_NOTIFY_ON_EXPIRE: - # Check if this is a tribute user that should be auto-renewed + elif event_name == "user.expired": + # Check if this is a tribute user that should be auto-renewed (regardless of notification settings) await self._handle_expired_subscription(session, user_id, user_payload, lang, markup, first_name) - await self._send_message( - user_id, - lang, - "subscription_expired_notification", - reply_markup=markup, - user_name=first_name, - end_date=user_payload.get("expireAt", "")[:10], - ) + # Send notification only if enabled + if self.settings.SUBSCRIPTION_NOTIFY_ON_EXPIRE: + await self._send_message( + user_id, + lang, + "subscription_expired_notification", + reply_markup=markup, + user_name=first_name, + end_date=user_payload.get("expireAt", "")[:10], + ) elif event_name == "user.expired_24_hours_ago" and self.settings.SUBSCRIPTION_NOTIFY_AFTER_EXPIRE: await self._send_message( user_id, diff --git a/db/dal/payment_dal.py b/db/dal/payment_dal.py index 394e43a..5187e58 100644 --- a/db/dal/payment_dal.py +++ b/db/dal/payment_dal.py @@ -2,7 +2,7 @@ import logging from typing import Optional, List, Dict, Any from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select -from sqlalchemy import update, func +from sqlalchemy import update, func, and_ from sqlalchemy.orm import selectinload from db.models import Payment, User