From 864a4ed7005195ff3be3a8bb2e7666bc5a7f3e4e Mon Sep 17 00:00:00 2001 From: Fringg Date: Wed, 11 Mar 2026 02:17:35 +0300 Subject: [PATCH] fix: record transactions for free tariff switches and admin tariff changes - Add transaction records for free tariff switches (downgrade, upgrade_cost=0) in miniapp and cabinet - Add atomic transaction records for admin tariff changes in bot handler and cabinet API - Use commit=False for admin flows to ensure subscription change and transaction are committed together --- app/cabinet/routes/admin_users.py | 12 ++++++++++++ app/cabinet/routes/subscription.py | 10 ++++++++++ app/handlers/admin/users.py | 12 ++++++++++++ app/webapi/routes/miniapp.py | 10 ++++++++++ 4 files changed, 44 insertions(+) diff --git a/app/cabinet/routes/admin_users.py b/app/cabinet/routes/admin_users.py index 9c8e7449..c4e12c52 100644 --- a/app/cabinet/routes/admin_users.py +++ b/app/cabinet/routes/admin_users.py @@ -1134,6 +1134,18 @@ async def update_user_subscription( if settings.RESET_TRAFFIC_ON_TARIFF_SWITCH: subscription.traffic_used_gb = 0.0 + # Записываем транзакцию о смене тарифа + from app.database.crud.transaction import create_transaction + + await create_transaction( + db=db, + user_id=user.id, + type=TransactionType.SUBSCRIPTION_PAYMENT, + amount_kopeks=0, + description=f"Смена тарифа администратором на '{tariff.name}'", + commit=False, + ) + await db.commit() await db.refresh(subscription) diff --git a/app/cabinet/routes/subscription.py b/app/cabinet/routes/subscription.py index 5fa3af2f..c8d37c5d 100644 --- a/app/cabinet/routes/subscription.py +++ b/app/cabinet/routes/subscription.py @@ -4350,6 +4350,16 @@ async def switch_tariff( description=description, payment_method=PaymentMethod.BALANCE, ) + else: + # Free switch (downgrade) — record in history + description = f"Переход на тариф '{new_tariff.name}'" + await create_transaction( + db=db, + user_id=user.id, + type=TransactionType.SUBSCRIPTION_PAYMENT, + amount_kopeks=0, + description=description, + ) # Update subscription old_tariff_name = current_tariff.name if current_tariff else 'Unknown' diff --git a/app/handlers/admin/users.py b/app/handlers/admin/users.py index 4101f3da..caddac5f 100644 --- a/app/handlers/admin/users.py +++ b/app/handlers/admin/users.py @@ -5329,6 +5329,18 @@ async def confirm_admin_tariff_change(callback: types.CallbackQuery, db_user: Us if settings.RESET_TRAFFIC_ON_TARIFF_SWITCH: subscription.traffic_used_gb = 0.0 + # Записываем транзакцию о смене тарифа + from app.database.crud.transaction import create_transaction + + await create_transaction( + db=db, + user_id=user.id, + type=TransactionType.SUBSCRIPTION_PAYMENT, + amount_kopeks=0, + description=f"Смена тарифа администратором на '{tariff.name}'", + commit=False, + ) + await db.commit() # Синхронизируем с RemnaWave (сброс трафика по админ-настройке) diff --git a/app/webapi/routes/miniapp.py b/app/webapi/routes/miniapp.py index fbb9de5c..18ef9317 100644 --- a/app/webapi/routes/miniapp.py +++ b/app/webapi/routes/miniapp.py @@ -7061,6 +7061,16 @@ async def switch_tariff_endpoint( amount_kopeks=upgrade_cost, description=description, ) + else: + # Бесплатный переход (downgrade) — записываем в историю + description = f"Переход на тариф '{new_tariff.name}'" + await create_transaction( + db=db, + user_id=user.id, + type=TransactionType.SUBSCRIPTION_PAYMENT, + amount_kopeks=0, + description=description, + ) # Получаем список серверов из тарифа squads = new_tariff.allowed_squads or []