- 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
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from typing import Optional
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from db.models import User
|
|
from schemas.users import NewUserDTO
|
|
|
|
|
|
class UserRepository:
|
|
async def get_user_by_id(
|
|
self, session: AsyncSession, user_id: int
|
|
) -> Optional[User]:
|
|
stmt = select(User).where(User.id == user_id)
|
|
result = await session.execute(stmt)
|
|
|
|
return result.scalar_one_or_none()
|
|
|
|
async def create_user(self, session: AsyncSession, user_data: NewUserDTO) -> User:
|
|
user = User(
|
|
id=user_data.id,
|
|
referal_id=user_data.referal,
|
|
subscription_link=user_data.link,
|
|
)
|
|
session.add(user)
|
|
await session.commit()
|
|
|
|
return user
|
|
|
|
async def update_user_topic(
|
|
self, session: AsyncSession, user_id: int, thread_id: int
|
|
) -> Optional[User]:
|
|
user = await self.get_user_by_id(session, user_id=user_id)
|
|
if not user:
|
|
return None
|
|
user.support_thread_id = thread_id
|
|
|
|
await session.commit()
|
|
return user
|
|
|
|
async def update_user_link(
|
|
self, session: AsyncSession, user_id: int, link: str
|
|
) -> Optional[User]:
|
|
user = await self.get_user_by_id(session, user_id=user_id)
|
|
if not user:
|
|
return None
|
|
user.subscription_link = link
|
|
|
|
await session.commit()
|
|
return user
|