Add user delete process
This commit is contained in:
@@ -13,8 +13,10 @@ from sqlalchemy import select
|
||||
from shared.auth.decorators import get_current_user_dep, require_admin_role_dep
|
||||
from shared.routing import RouteBuilder
|
||||
from app.core.database import get_db
|
||||
from app.models.tenants import Subscription
|
||||
from app.models.tenants import Subscription, Tenant
|
||||
from app.services.subscription_limit_service import SubscriptionLimitService
|
||||
from shared.clients.stripe_client import StripeProvider
|
||||
from app.core.config import settings
|
||||
|
||||
logger = structlog.get_logger()
|
||||
router = APIRouter()
|
||||
@@ -65,6 +67,18 @@ class SubscriptionStatusResponse(BaseModel):
|
||||
days_until_inactive: int | None
|
||||
|
||||
|
||||
class InvoiceResponse(BaseModel):
|
||||
"""Response model for an invoice"""
|
||||
id: str
|
||||
date: str
|
||||
amount: float
|
||||
currency: str
|
||||
status: str
|
||||
description: str | None = None
|
||||
invoice_pdf: str | None = None
|
||||
hosted_invoice_url: str | None = None
|
||||
|
||||
|
||||
@router.post("/api/v1/subscriptions/cancel", response_model=SubscriptionCancellationResponse)
|
||||
async def cancel_subscription(
|
||||
request: SubscriptionCancellationRequest,
|
||||
@@ -251,3 +265,65 @@ async def get_subscription_status(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to get subscription status"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/subscriptions/{tenant_id}/invoices", response_model=list[InvoiceResponse])
|
||||
async def get_tenant_invoices(
|
||||
tenant_id: str,
|
||||
current_user: dict = Depends(get_current_user_dep),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Get invoice history for a tenant from Stripe
|
||||
"""
|
||||
try:
|
||||
# Verify tenant exists
|
||||
query = select(Tenant).where(Tenant.id == UUID(tenant_id))
|
||||
result = await db.execute(query)
|
||||
tenant = result.scalar_one_or_none()
|
||||
|
||||
if not tenant:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Tenant not found"
|
||||
)
|
||||
|
||||
# Check if tenant has a Stripe customer ID
|
||||
if not tenant.stripe_customer_id:
|
||||
logger.info("no_stripe_customer_id", tenant_id=tenant_id)
|
||||
return []
|
||||
|
||||
# Initialize Stripe provider
|
||||
stripe_provider = StripeProvider(
|
||||
api_key=settings.STRIPE_SECRET_KEY,
|
||||
webhook_secret=settings.STRIPE_WEBHOOK_SECRET
|
||||
)
|
||||
|
||||
# Fetch invoices from Stripe
|
||||
stripe_invoices = await stripe_provider.get_invoices(tenant.stripe_customer_id)
|
||||
|
||||
# Transform to response format
|
||||
invoices = []
|
||||
for invoice in stripe_invoices:
|
||||
invoices.append(InvoiceResponse(
|
||||
id=invoice.id,
|
||||
date=invoice.created_at.strftime('%Y-%m-%d'),
|
||||
amount=invoice.amount,
|
||||
currency=invoice.currency.upper(),
|
||||
status=invoice.status,
|
||||
description=invoice.description,
|
||||
invoice_pdf=invoice.invoice_pdf,
|
||||
hosted_invoice_url=invoice.hosted_invoice_url
|
||||
))
|
||||
|
||||
logger.info("invoices_retrieved", tenant_id=tenant_id, count=len(invoices))
|
||||
return invoices
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("get_invoices_failed", error=str(e), tenant_id=tenant_id)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to retrieve invoices"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user