fix: rate-limit daily subscription insufficient balance notifications to 6 hours

Users with daily subscriptions and low balance were getting
"Подписка приостановлена" notification every 30 minutes (on each
charge cycle). Now rate-limited via Redis cache to max 1 notification
per 6 hours per subscription.
This commit is contained in:
Fringg
2026-04-18 00:59:09 +03:00
parent 16bc1d4198
commit ecc4a6147d
+15 -2
View File
@@ -146,9 +146,22 @@ class DailySubscriptionService:
# Недостаточно средств - приостанавливаем подписку
await suspend_daily_subscription_insufficient_balance(db, subscription)
# Уведомляем пользователя
# Уведомляем пользователя (rate-limit: 1 раз в 6 часов)
if self._bot:
await self._notify_insufficient_balance(user, subscription, daily_price)
from app.utils.cache import cache
cache_key = f'daily_insuf_notify:{subscription.id}'
try:
already_notified = await cache.get(cache_key)
except Exception:
already_notified = None
if not already_notified:
await self._notify_insufficient_balance(user, subscription, daily_price)
try:
await cache.set(cache_key, '1', expire=21600) # 6 hours
except Exception:
pass
logger.info(
'Подписка приостановлена: недостаточно средств (баланс: требуется: )',