diff --git a/.env.example b/.env.example index 6a826be..69e3bf3 100644 --- a/.env.example +++ b/.env.example @@ -124,6 +124,7 @@ SUBSCRIPTION_NOTIFY_DAYS_BEFORE=3 # REFERRAL_ONE_BONUS_PER_REFEREE=False # Give a bonus only once per referee +REFERRAL_WELCOME_BONUS_DAYS=3 # Welcome bonus for newly registered user from referral link LEGACY_REFS=true # Allow ref_ links. Leave unset/true unless you want to disable old links # Referral Bonus Days # Bonus for the inviting user diff --git a/bot/handlers/user/start.py b/bot/handlers/user/start.py index cff7e98..f5f606e 100644 --- a/bot/handlers/user/start.py +++ b/bot/handlers/user/start.py @@ -449,6 +449,50 @@ async def start_command_handler(message: types.Message, f"New user {user_id} added to session. Referred by: {referred_by_user_id or 'N/A'}." ) + # Auto-grant referral welcome bonus to newly registered referred users. + referral_welcome_days = max( + 0, int(getattr(settings, "REFERRAL_WELCOME_BONUS_DAYS", 0) or 0) + ) + if referred_by_user_id and referral_welcome_days > 0: + try: + referral_bonus_end_date = await subscription_service.extend_active_subscription_days( + session, + user_id, + referral_welcome_days, + reason="referral_welcome_bonus", + ) + if referral_bonus_end_date: + await session.commit() + logging.info( + "Referral welcome bonus applied: user %s got %s days, new end date %s.", + user_id, + referral_welcome_days, + referral_bonus_end_date.isoformat(), + ) + await message.answer( + _( + "referral_welcome_bonus_applied", + days=referral_welcome_days, + end_date=referral_bonus_end_date.strftime("%d.%m.%Y %H:%M:%S"), + ), + parse_mode="HTML", + ) + else: + await session.rollback() + logging.warning( + "Referral welcome bonus was not applied for user %s (referred by %s).", + user_id, + referred_by_user_id, + ) + except Exception as referral_bonus_error: + await session.rollback() + logging.error( + "Failed to apply referral welcome bonus for user %s: %s", + user_id, + referral_bonus_error, + exc_info=True, + ) + # Send notification about new user registration try: from bot.services.notification_service import NotificationService diff --git a/config/settings.py b/config/settings.py index c9c6bc5..cc927c3 100644 --- a/config/settings.py +++ b/config/settings.py @@ -170,6 +170,10 @@ class Settings(BaseSettings): default=True, description="When true, referral bonuses (for inviter and referee) are applied only once per invited user - on their first successful payment." ) + REFERRAL_WELCOME_BONUS_DAYS: int = Field( + default=3, + description="Welcome bonus days granted to a newly registered user who joined via referral link.", + ) LEGACY_REFS: bool = Field( default=True, description="Allow legacy referral links like ref_ to continue working. Defaults to True when unset." diff --git a/locales/en.json b/locales/en.json index 3bcf172..01c7b27 100644 --- a/locales/en.json +++ b/locales/en.json @@ -92,6 +92,7 @@ "trial_confirm_activate_button": "✅ Activate!", "trial_activated_alert": "Trial activated!", "trial_activated_details_message": "✅ Trial activated!\nYour {days}-day trial is active until {end_date}.\n\nConnection key:\n{config_link}\n\nTo connect, open the link and follow the instructions 👇", + "referral_welcome_bonus_applied": "🎁 You have received {days} bonus day(s) for registering via a referral link!\nYour subscription is active until {end_date}.", "yes_button": "Yes", "no_button": "No", "referral_program_info_new": "🎁 Referral Program\n\n📊 Your stats:\n👥 Friends invited: {invited_count}\n💳 Purchased subscription: {purchased_count}\n\n🔗 Your link:\n{referral_link}\n\n💰 Invitation bonuses:\n{bonus_details}\n\n📢 Share the link with friends and get bonuses!", diff --git a/locales/ru.json b/locales/ru.json index 5bbf47c..f2eec1d 100644 --- a/locales/ru.json +++ b/locales/ru.json @@ -92,6 +92,7 @@ "trial_confirm_activate_button": "✅ Активировать!", "trial_activated_alert": "Пробный период активирован!", "trial_activated_details_message": "✅ Пробный доступ активирован!\nВаш триал на {days} дн. действует до {end_date}.\n\nКлюч подключения:\n{config_link}\n\nЧтобы подключиться, перейдите по ссылке и следуйте инструкции 👇", + "referral_welcome_bonus_applied": "🎁 Вам начислено {days} бонусных дн. за регистрацию по реферальной ссылке!\nПодписка активна до {end_date}.", "yes_button": "Да", "no_button": "Нет", "referral_program_info_new": "🎁 Реферальная программа\n\n📊 Твоя статистика:\n👥 Приглашено друзей: {invited_count}\n💳 Купили подписку: {purchased_count}\n\n🔗 Твоя ссылка:\n{referral_link}\n\n💰 Бонусы за приглашения:\n{bonus_details}\n\n📢 Поделись ссылкой с друзьями и получай бонусы!",