feat: enhance subscription management with discount calculations and update support messages

This commit is contained in:
2026-04-22 17:41:44 +07:00
parent 97c187bafb
commit e3e204d7af
7 changed files with 95 additions and 328 deletions

View File

@@ -1,9 +1,9 @@
import logging
import math
from aiogram import F, Router
from aiogram.fsm.context import FSMContext
from aiogram.types import CallbackQuery
from sqlalchemy.ext.asyncio import AsyncSession
from config import settings
from keyboards.client import (
@@ -14,22 +14,20 @@ from keyboards.client import (
)
from misc.utils import (
calculate_price,
convert_duration_to_int,
convert_int_to_duration,
get_discount,
)
from schemas.di import DependenciesDTO
from schemas.subscriptions import SubscriptionPlan
from services.user_service import UserServiceError
from states.buy import SubscriptionStorage
from texts import (
CALLBACK_FALLBACK,
CHECKOUT_PAYMENT_CHOICE,
CONTEXT_REINITIATED,
GENERAL_PROCESSING,
NOTHING_PLACEHOLDER,
STANDARD_FALLBACK,
SUBSCRIPTION_DEVICE_SELECTOR,
SUBSCRIPTION_DURATION_SELECTOR,
SUPPORT_MSG_SENT,
)
router = Router()
@@ -103,8 +101,12 @@ async def select_duration_init(cb: CallbackQuery, state: FSMContext):
return
try:
discount = get_discount(convert_duration_to_int(ctx.duration))
await cb.message.edit_text(
SUBSCRIPTION_DURATION_SELECTOR.format(price=calculate_price(ctx)),
SUBSCRIPTION_DURATION_SELECTOR.format(
price=math.ceil(calculate_price(ctx) * (1 - discount / 100)),
discount=math.floor(discount),
),
reply_markup=duration_selector(current=ctx.duration, back_cb=f"devices:{ctx.devices}"),
)
except Exception as exc:
@@ -132,8 +134,12 @@ async def select_duration(cb: CallbackQuery, state: FSMContext):
duration = int(cb_data[1]) if cb_data[1].isdigit() else 1
ctx.duration = convert_int_to_duration(duration)
try:
discount = get_discount(convert_duration_to_int(ctx.duration))
await cb.message.edit_text(
SUBSCRIPTION_DURATION_SELECTOR.format(price=calculate_price(ctx)),
SUBSCRIPTION_DURATION_SELECTOR.format(
price=math.ceil(calculate_price(ctx) * (1 - discount / 100)),
discount=math.floor(discount),
),
reply_markup=duration_selector(current=ctx.duration, back_cb=f"devices:{ctx.devices}"),
)
except Exception as exc:
@@ -164,28 +170,3 @@ async def proceed_to_checkout(cb: CallbackQuery, state: FSMContext):
await state.set_state(SubscriptionStorage.checkout)
await state.set_data({"ctx": ctx})
@router.callback_query(F.data == "checkout")
async def checkout_support(
cb: CallbackQuery, state: FSMContext, session: AsyncSession, deps: DependenciesDTO
):
data = await state.get_data()
await state.clear()
ctx: SubscriptionPlan | None = data.get("ctx")
if not ctx:
await cb.message.edit_text(STANDARD_FALLBACK, reply_markup=return_to_menu)
return
await cb.message.edit_text(GENERAL_PROCESSING)
try:
await deps.user_service.create_subscription_ticket(session, cb.from_user, ctx, cb.bot)
await cb.message.edit_text(SUPPORT_MSG_SENT)
except UserServiceError:
await cb.message.edit_text(STANDARD_FALLBACK)
raise
except Exception:
await cb.message.edit_text(STANDARD_FALLBACK)
logger.exception("Exception occured while trying to forward a msg to support")
raise

View File

@@ -4,9 +4,10 @@ from aiogram.fsm.context import FSMContext
from aiogram.types import CallbackQuery, Message
from sqlalchemy.ext.asyncio import AsyncSession
from keyboards.client import main_menu, return_to_menu
from keyboards.client import close_kb, main_menu, return_to_menu
from misc.utils import build_main_menu_text, format_subscription_link
from schemas.di import DependenciesDTO
from services.rw import get_user_by_telegram_id
from texts import FAQ_TEXT, GENERAL_PROCESSING
router = Router()
@@ -32,7 +33,7 @@ async def fetch_referal(
session, user_id=msg.from_user.id, referal=referal, rw_sdk=deps.rw_sdk
)
await msg.reply(
await msg.answer(
build_main_menu_text(format_subscription_link(user.subscription_link)),
reply_markup=main_menu,
)
@@ -49,7 +50,7 @@ async def standard_start(
user = await deps.user_service.add_user(session, user_id=msg.from_user.id, rw_sdk=deps.rw_sdk)
await msg.reply(
await msg.answer(
build_main_menu_text(format_subscription_link(user.subscription_link)),
reply_markup=main_menu,
)
@@ -88,3 +89,19 @@ async def faq(cb: CallbackQuery, state: FSMContext):
await state.clear()
await cb.message.edit_text(FAQ_TEXT, reply_markup=return_to_menu)
@router.callback_query(F.data == "sub_info")
async def sub_info(
cb: CallbackQuery, state: FSMContext, deps: DependenciesDTO, session: AsyncSession
):
await state.clear()
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)
await cb.message.edit_text(
deps.user_service.format_subscription_message(rw_user, link=user.subscription_link),
reply_markup=return_to_menu,
)

View File

@@ -4,14 +4,9 @@ from aiogram import F, Router
from aiogram.filters import Command
from aiogram.fsm.context import FSMContext
from aiogram.types import CallbackQuery, Message
from sqlalchemy.ext.asyncio import AsyncSession
from config import settings
from keyboards.client import return_to_menu
from schemas.di import DependenciesDTO
from services.user_service import UserServiceError
from states.support import SupportStorage
from texts import GENERAL_PROCESSING, STANDARD_FALLBACK, SUPPORT_INIT, SUPPORT_MSG_SENT
from texts import SUPPORT_INIT
router = Router()
logger = logging.getLogger(__name__)
@@ -22,7 +17,6 @@ async def support_init(cb: CallbackQuery, state: FSMContext):
await state.clear()
await cb.message.edit_text(text=SUPPORT_INIT, reply_markup=return_to_menu)
await state.set_state(SupportStorage.prompt)
@router.message(Command("support"))
@@ -31,60 +25,3 @@ async def support_init_cmd(msg: Message, state: FSMContext):
await msg.delete()
await msg.answer(text=SUPPORT_INIT, reply_markup=return_to_menu)
await state.set_state(SupportStorage.prompt)
@router.message(SupportStorage.prompt)
async def received_message(
msg: Message, state: FSMContext, session: AsyncSession, deps: DependenciesDTO
):
await state.clear()
notification_msg = await msg.reply(GENERAL_PROCESSING)
try:
await deps.user_service.support_forward_message(session, msg, deps.rw_sdk)
except UserServiceError:
await msg.reply(STANDARD_FALLBACK)
raise
except Exception:
await msg.reply(STANDARD_FALLBACK)
logger.exception("Exception occured while trying to forward a msg to support")
raise
await notification_msg.edit_text(SUPPORT_MSG_SENT, reply_markup=return_to_menu)
await state.set_state(SupportStorage.prompt)
@router.message(
F.chat.id == settings.admin_group_id,
F.message_thread_id.is_not(None),
F.from_user.is_bot == False, # noqa: E712
~Command("close"),
~Command("refpay"),
)
async def handle_admin_message(
msg: Message, state: FSMContext, session: AsyncSession, deps: DependenciesDTO
):
await state.clear()
print(str(msg))
await deps.user_service.support_answer(session, msg)
@router.message(F.chat.id == settings.admin_group_id, Command("close"))
async def close_ticket(
msg: Message, state: FSMContext, session: AsyncSession, deps: DependenciesDTO
):
await state.clear()
await deps.user_service.close_ticket(session, msg)
@router.callback_query(F.message.chat.id == settings.admin_group_id, F.data == "close")
async def close_ticket_cb(
cb: CallbackQuery, state: FSMContext, session: AsyncSession, deps: DependenciesDTO
):
await state.clear()
await deps.user_service.close_ticket(session, cb.message)