feat: enhance subscription management with discount calculations and update support messages
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user