Add subscription link field to user model
- Add subscription_link column to users table (nullable, unique) - Add SUBSCRIPTION_URL to config settings - Update user creation to include subscription link from Remnawave - Add support subscription ticket creation with formatted message - Add "update link" button to main menu - Refactor support subscription formatting to include user details
This commit is contained in:
@@ -26,6 +26,7 @@ class RWUserInfo:
|
||||
hwid_device_limit: Optional[int] # None = unlimited
|
||||
active_squads: list[dict] # [{"uuid": "...", "name": "..."}]
|
||||
description: Optional[str]
|
||||
short_uuid: str
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────
|
||||
@@ -61,6 +62,7 @@ def _parse_user(user_dto) -> RWUserInfo:
|
||||
hwid_device_limit=user_dto.hwid_device_limit,
|
||||
active_squads=active_squads,
|
||||
description=user_dto.description,
|
||||
short_uuid=user_dto.short_uuid,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
from typing import Optional
|
||||
from aiogram import Bot
|
||||
from aiogram.types import Message, ReactionTypeEmoji
|
||||
from aiogram.types import User as TelegramUser
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from remnawave import RemnawaveSDK
|
||||
|
||||
from config import settings
|
||||
from db.models.tickets import TicketStatus
|
||||
from db.models.users import User
|
||||
from schemas.subscriptions import SubscriptionPlan
|
||||
from schemas.tickets import NewTicketDTO
|
||||
from schemas.users import NewUserDTO, TicketCreator
|
||||
from repositories import UserRepository
|
||||
@@ -84,16 +87,76 @@ class UserService:
|
||||
)
|
||||
|
||||
async def add_user(
|
||||
self, session: AsyncSession, user_id: int, referal: Optional[int] = None
|
||||
self,
|
||||
session: AsyncSession,
|
||||
user_id: int,
|
||||
rw_sdk: RemnawaveSDK,
|
||||
referal: Optional[int] = None,
|
||||
) -> User:
|
||||
user = await self.user_repository.get_user_by_id(session, user_id)
|
||||
if user:
|
||||
return user
|
||||
|
||||
rw_user = await get_user_by_telegram_id(rw_sdk, user_id)
|
||||
link = utils.get_subscription_link(rw_user.short_uuid)
|
||||
referal = int(referal) if str(referal).isdigit() else None
|
||||
model = NewUserDTO(id=user_id, referal=referal)
|
||||
|
||||
model = NewUserDTO(id=user_id, referal=referal, link=link)
|
||||
return await self.user_repository.create_user(session, model)
|
||||
|
||||
async def create_subscription_ticket(
|
||||
self,
|
||||
session: AsyncSession,
|
||||
user: TelegramUser,
|
||||
subscription: SubscriptionPlan,
|
||||
bot: Bot,
|
||||
):
|
||||
ticket = await self.ticket_repository.get_available_ticket_by_user_id(
|
||||
session, user.id
|
||||
)
|
||||
|
||||
if ticket:
|
||||
try:
|
||||
await bot.send_message(
|
||||
chat_id=settings.admin_group_id,
|
||||
message_thread_id=ticket.thread_id,
|
||||
text=utils.format_support_subscription(
|
||||
subscription, user.full_name, user.id, user.username
|
||||
),
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
await self.ticket_repository.update_ticket_status(
|
||||
session, ticket, TicketStatus.CLOSED
|
||||
)
|
||||
raise
|
||||
creator = TicketCreator(id=user.id, username=user.username)
|
||||
|
||||
ticket_dto = NewTicketDTO(
|
||||
creator_id=user.id,
|
||||
)
|
||||
ticket = await self.ticket_repository.create_ticket(
|
||||
session, ticket_data=ticket_dto
|
||||
)
|
||||
|
||||
thread = await bot.create_forum_topic(
|
||||
settings.admin_group_id,
|
||||
self.format_ticket_name(
|
||||
creator, ticket_id=ticket.id, status=TicketStatus.PENDING
|
||||
),
|
||||
)
|
||||
ticket = await self.ticket_repository.update_ticket_thread_id(
|
||||
session, ticket, thread.message_thread_id
|
||||
)
|
||||
|
||||
await bot.send_message(
|
||||
chat_id=settings.admin_group_id,
|
||||
message_thread_id=ticket.thread_id,
|
||||
text=utils.format_support_subscription(
|
||||
subscription, user.full_name, user.id, user.username
|
||||
),
|
||||
)
|
||||
|
||||
async def support_forward_message(
|
||||
self, session: AsyncSession, msg: Message, rw: RemnawaveSDK
|
||||
):
|
||||
@@ -195,3 +258,14 @@ class UserService:
|
||||
await msg.bot.close_forum_topic(
|
||||
settings.admin_group_id, message_thread_id=thread_id
|
||||
)
|
||||
|
||||
async def update_user_link(
|
||||
self, session: AsyncSession, user_id: int, rw_sdk: RemnawaveSDK
|
||||
) -> Optional[User]:
|
||||
rw_user = await get_user_by_telegram_id(rw_sdk, user_id)
|
||||
|
||||
link = utils.get_subscription_link(rw_user.short_uuid)
|
||||
|
||||
user = await self.user_repository.update_user_link(session, user_id, link)
|
||||
|
||||
return user
|
||||
|
||||
Reference in New Issue
Block a user