78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
from fastapi import APIRouter, HTTPException, Depends
|
|
from pydantic import BaseModel
|
|
from app.services.db_manager import DatabaseManager
|
|
from instance.configdb import get_database_manager
|
|
from uuid import UUID
|
|
import logging
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class BuySubscriptionRequest(BaseModel):
|
|
telegram_id: int
|
|
plan_id: str
|
|
|
|
class SubscriptionResponse(BaseModel):
|
|
id: str
|
|
plan: str
|
|
vpn_server_id: str
|
|
expiry_date: str
|
|
created_at: str
|
|
updated_at: str
|
|
|
|
# Эндпоинт для покупки подписки
|
|
@router.post("/subscription/buy", response_model=dict)
|
|
async def buy_subscription(
|
|
request_data: BuySubscriptionRequest,
|
|
database_manager: DatabaseManager = Depends(get_database_manager)
|
|
):
|
|
"""
|
|
Покупка подписки.
|
|
"""
|
|
try:
|
|
result = await database_manager.buy_sub(request_data.telegram_id, request_data.plan_id)
|
|
|
|
if result == "ERROR":
|
|
raise HTTPException(status_code=500, detail="Failed to buy subscription")
|
|
elif result == "INSUFFICIENT_FUNDS":
|
|
raise HTTPException(status_code=400, detail="Insufficient funds")
|
|
elif result == "TARIFF_NOT_FOUND":
|
|
raise HTTPException(status_code=400, detail="Tariff not found")
|
|
elif result == "ACTIVE_SUBSCRIPTION_EXISTS":
|
|
raise HTTPException(status_code=400, detail="User already had subscription",)
|
|
|
|
return {"message": "Subscription purchased successfully"}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
# Эндпоинт для получения последней подписки
|
|
@router.get("/subscription/{user_id}/last", response_model=SubscriptionResponse)
|
|
async def last_subscription(user_id: UUID, database_manager: DatabaseManager = Depends(get_database_manager)):
|
|
logger.info(f"Получение последней подписки для пользователя: {user_id}")
|
|
try:
|
|
sub = await database_manager.last_subscription(user_id)
|
|
if sub is None:
|
|
logger.warning(f"Подписки для пользователя {user_id} не найдены")
|
|
raise HTTPException(status_code=404, detail="No subscriptions found")
|
|
return {
|
|
"id": sub.id,
|
|
"plan": sub.plan,
|
|
"vpn_server_id": sub.vpn_server_id,
|
|
"expiry_date": sub.expiry_date.isoformat(),
|
|
"created_at": sub.created_at.isoformat(),
|
|
"updated_at": sub.updated_at.isoformat(),
|
|
}
|
|
except SQLAlchemyError as e:
|
|
logger.error(f"Ошибка базы данных при получении подписки для пользователя {user_id}: {e}")
|
|
raise HTTPException(status_code=500, detail="Database error")
|
|
except Exception as e:
|
|
logger.error(f"Неожиданная ошибка: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|