feat: implement subscription management and billing enhancements
This commit is contained in:
@@ -4,10 +4,19 @@ from aiogram.fsm.context import FSMContext
|
||||
from aiogram.types import CallbackQuery, Message
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from bot.keyboards.client import close_kb, main_menu, return_to_menu
|
||||
from bot.texts import FAQ_TEXT, GENERAL_PROCESSING
|
||||
from bot.keyboards.client import close_kb, main_menu, return_to_menu, subscription_controls
|
||||
from bot.texts import (
|
||||
CALLBACK_FALLBACK,
|
||||
FAQ_TEXT,
|
||||
GENERAL_PROCESSING,
|
||||
GENERAL_SUCCESS,
|
||||
NOTHING_PLACEHOLDER,
|
||||
)
|
||||
from db.models.users import User
|
||||
from misc import utils
|
||||
from misc.utils import build_main_menu_text, format_subscription_link
|
||||
from schemas.di import DependenciesDTO
|
||||
from schemas.subscriptions import InternalSquads, SubscriptionPlan
|
||||
from services.rw import get_user_by_telegram_id
|
||||
|
||||
router = Router()
|
||||
@@ -107,9 +116,72 @@ async def sub_info(
|
||||
|
||||
await cb.message.edit_text(GENERAL_PROCESSING, reply_markup=close_kb)
|
||||
rw_user = await get_user_by_telegram_id(deps.rw_sdk, cb.from_user.id)
|
||||
user = await deps.user_service.update_user_link(session, cb.from_user.id, deps.rw_sdk)
|
||||
user: User | None = await deps.user_service.update_user_link(
|
||||
session, cb.from_user.id, deps.rw_sdk
|
||||
)
|
||||
sub = await deps.subscriptions_repository.get_subscription_by_user_id(session, cb.from_user.id)
|
||||
|
||||
if not user:
|
||||
await cb.answer(CALLBACK_FALLBACK)
|
||||
return
|
||||
|
||||
if not sub:
|
||||
try:
|
||||
dto = SubscriptionPlan(
|
||||
devices=rw_user.hwid_device_limit,
|
||||
whitelists=bool(rw_user.active_squads.count(InternalSquads.WHITELISTS)),
|
||||
discount=0,
|
||||
)
|
||||
sub = await deps.subscriptions_repository.create_subscription(
|
||||
session,
|
||||
user_id=user.id,
|
||||
end_date=rw_user.expire_at,
|
||||
devices=rw_user.hwid_device_limit,
|
||||
duration_days=30,
|
||||
whitelists=dto.whitelists,
|
||||
plan_price_paid=utils.calculate_price(dto),
|
||||
)
|
||||
except Exception:
|
||||
await cb.answer(CALLBACK_FALLBACK)
|
||||
return
|
||||
|
||||
await cb.message.edit_text(
|
||||
deps.user_service.format_subscription_message(rw_user, link=user.subscription_link),
|
||||
reply_markup=return_to_menu,
|
||||
deps.user_service.format_subscription_message(
|
||||
rw_user, link=user.subscription_link, autorenew=sub.autorenew
|
||||
),
|
||||
reply_markup=subscription_controls(sub.autorenew),
|
||||
)
|
||||
|
||||
|
||||
@router.callback_query(F.data == "toggle:autorenewal")
|
||||
async def toggle_autorenewal(
|
||||
cb: CallbackQuery, state: FSMContext, session: AsyncSession, deps: DependenciesDTO
|
||||
):
|
||||
await state.clear()
|
||||
await cb.answer(NOTHING_PLACEHOLDER + " В разработке.")
|
||||
return
|
||||
|
||||
await cb.message.edit_text(GENERAL_PROCESSING)
|
||||
sub = await deps.subscriptions_repository.get_subscription_by_user_id(session, cb.from_user.id)
|
||||
|
||||
if not sub:
|
||||
await cb.answer(CALLBACK_FALLBACK)
|
||||
return
|
||||
|
||||
new_autorenew = not sub.autorenew
|
||||
sub = await deps.subscriptions_repository.update_subscription_autorenew(
|
||||
session, sub.user_id, new_autorenew
|
||||
)
|
||||
|
||||
rw_user = await get_user_by_telegram_id(deps.rw_sdk, cb.from_user.id)
|
||||
user: User | None = await deps.user_service.update_user_link(
|
||||
session, cb.from_user.id, deps.rw_sdk
|
||||
)
|
||||
|
||||
await cb.answer(GENERAL_SUCCESS)
|
||||
await cb.message.edit_text(
|
||||
deps.user_service.format_subscription_message(
|
||||
rw_user, link=user.subscription_link, autorenew=sub.autorenew
|
||||
),
|
||||
reply_markup=subscription_controls(sub.autorenew),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user