Download backend/src/api/routes/billing.py from new-arc/vector: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/spaces/new-arc/vector/resolve/main/backend/src/api/routes/billing.py
- Command line
-
hf download hf://spaces/new-arc/vector/backend/src/api/routes/billing.py
-
curl -L -o billing.py https://huggingface.co/spaces/new-arc/vector/resolve/main/backend/src/api/routes/billing.py
2.58 kB
| import logging | |
| from typing import Literal | |
| from fastapi import APIRouter, Depends, HTTPException, Request | |
| from pydantic import BaseModel | |
| from sqlalchemy import select | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from ...auth_headers import get_authenticated_user_id | |
| from ...config import get_config | |
| from ...database import get_db | |
| from ...models import User | |
| from ...services.subscription_email_service import SubscriptionEmailService | |
| logger = logging.getLogger(__name__) | |
| router = APIRouter(prefix="/billing", tags=["billing"]) | |
| class SubscriptionEmailRequest(BaseModel): | |
| event: Literal["subscribed", "unsubscribed", "cancellation_scheduled"] | |
| def _get_user_id_from_headers(request: Request) -> str: | |
| config = get_config() | |
| return get_authenticated_user_id(request, config) | |
| async def send_subscription_email( | |
| body: SubscriptionEmailRequest, | |
| request: Request, | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| user_id = _get_user_id_from_headers(request) | |
| result = await db.execute(select(User).where(User.id == user_id)) | |
| user = result.scalar_one_or_none() | |
| if not user: | |
| raise HTTPException(status_code=404, detail="User not found") | |
| if not user.email: | |
| raise HTTPException(status_code=400, detail="User email is missing") | |
| service = SubscriptionEmailService(get_config()) | |
| try: | |
| if body.event == "subscribed": | |
| email_result = await service.send_subscribed_email(user) | |
| elif body.event == "cancellation_scheduled": | |
| # Re-check current state so a resumed or ended subscription does not | |
| # receive an outdated cancellation notice. | |
| if ( | |
| not user.subscription_cancel_at | |
| or user.subscription_provider != "stripe" | |
| or user.subscription_status not in {"active", "trialing", "past_due"} | |
| ): | |
| return {"status": "skipped", "reason": "no_scheduled_cancellation"} | |
| email_result = await service.send_cancellation_scheduled_email(user, db) | |
| else: | |
| email_result = await service.send_unsubscribed_email(user) | |
| except RuntimeError as exc: | |
| raise HTTPException(status_code=503, detail=str(exc)) from exc | |
| except Exception as exc: | |
| logger.exception( | |
| "Failed to send %s email for user %s", body.event, user_id | |
| ) | |
| raise HTTPException( | |
| status_code=502, detail="Failed to send subscription email" | |
| ) from exc | |
| return {"status": "ok", "provider": "ses", "email": email_result} | |