Introduce balance field to User model with Alembic migration. Add referral menu, admin /refpay command, and deep link sharing. Update Dockerfile to multi-stage build and add Postgres service to compose. Fix deep link parsing logic and conditional proxy initialization.
72 lines
2.0 KiB
Python
72 lines
2.0 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
|
|
|
|
async def update_user_balance(
|
|
self, session: AsyncSession, user_id: int, balance: int
|
|
) -> Optional[User]:
|
|
user = await self.get_user_by_id(session, user_id=user_id)
|
|
if not user:
|
|
return None
|
|
user.balance = balance
|
|
|
|
await session.commit()
|
|
return user
|
|
|
|
async def increase_user_balance(
|
|
self, session: AsyncSession, user_id: int, amount: int
|
|
) -> Optional[User]:
|
|
user = await self.get_user_by_id(session, user_id=user_id)
|
|
if not user:
|
|
return None
|
|
user.balance += amount
|
|
|
|
await session.commit()
|
|
return user
|