diff --git a/app/api/v1/admin/subscriptions.py b/app/api/v1/admin/subscriptions.py index 5942f27d..a61a7bb4 100644 --- a/app/api/v1/admin/subscriptions.py +++ b/app/api/v1/admin/subscriptions.py @@ -14,7 +14,8 @@ import logging from fastapi import APIRouter, Depends, Path, Query from sqlalchemy.orm import Session -from app.api.deps import get_current_admin, get_db +from app.api.deps import get_current_admin_api +from app.core.database import get_db from app.services.admin_subscription_service import admin_subscription_service from models.database.user import User from models.schema.billing import ( @@ -43,7 +44,7 @@ logger = logging.getLogger(__name__) @router.get("/tiers", response_model=SubscriptionTierListResponse) def list_subscription_tiers( include_inactive: bool = Query(False, description="Include inactive tiers"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """ @@ -62,7 +63,7 @@ def list_subscription_tiers( @router.get("/tiers/{tier_code}", response_model=SubscriptionTierResponse) def get_subscription_tier( tier_code: str = Path(..., description="Tier code"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """Get a specific subscription tier by code.""" @@ -73,7 +74,7 @@ def get_subscription_tier( @router.post("/tiers", response_model=SubscriptionTierResponse, status_code=201) def create_subscription_tier( tier_data: SubscriptionTierCreate, - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """Create a new subscription tier.""" @@ -87,7 +88,7 @@ def create_subscription_tier( def update_subscription_tier( tier_data: SubscriptionTierUpdate, tier_code: str = Path(..., description="Tier code"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """Update a subscription tier.""" @@ -101,7 +102,7 @@ def update_subscription_tier( @router.delete("/tiers/{tier_code}", status_code=204) def delete_subscription_tier( tier_code: str = Path(..., description="Tier code"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """ @@ -125,7 +126,7 @@ def list_vendor_subscriptions( status: str | None = Query(None, description="Filter by status"), tier: str | None = Query(None, description="Filter by tier"), search: str | None = Query(None, description="Search vendor name"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """ @@ -161,7 +162,7 @@ def list_vendor_subscriptions( @router.get("/{vendor_id}", response_model=VendorSubscriptionWithVendor) def get_vendor_subscription( vendor_id: int = Path(..., description="Vendor ID"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """Get subscription details for a specific vendor.""" @@ -181,7 +182,7 @@ def get_vendor_subscription( def update_vendor_subscription( update_data: VendorSubscriptionUpdate, vendor_id: int = Path(..., description="Vendor ID"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """ @@ -219,7 +220,7 @@ def list_billing_history( per_page: int = Query(20, ge=1, le=100), vendor_id: int | None = Query(None, description="Filter by vendor"), status: str | None = Query(None, description="Filter by status"), - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """List billing history (invoices) across all vendors.""" @@ -267,7 +268,7 @@ def list_billing_history( @router.get("/stats", response_model=SubscriptionStatsResponse) def get_subscription_stats( - current_user: User = Depends(get_current_admin), + current_user: User = Depends(get_current_admin_api), db: Session = Depends(get_db), ): """Get subscription statistics for admin dashboard.""" diff --git a/app/services/admin_subscription_service.py b/app/services/admin_subscription_service.py index 49df376a..afae0815 100644 --- a/app/services/admin_subscription_service.py +++ b/app/services/admin_subscription_service.py @@ -15,7 +15,12 @@ from math import ceil from sqlalchemy import func from sqlalchemy.orm import Session -from app.exceptions import NotFoundException +from app.exceptions import ( + BusinessLogicException, + ConflictException, + ResourceNotFoundException, + TierNotFoundException, +) from models.database.subscription import ( BillingHistory, SubscriptionStatus, @@ -54,7 +59,7 @@ class AdminSubscriptionService: ) if not tier: - raise NotFoundException(f"Tier '{tier_code}' not found") + raise TierNotFoundException(tier_code) return tier @@ -67,7 +72,7 @@ class AdminSubscriptionService: .first() ) if existing: - raise NotFoundException( + raise ConflictException( f"Tier with code '{tier_data['code']}' already exists" ) @@ -107,7 +112,7 @@ class AdminSubscriptionService: ) if active_subs > 0: - raise NotFoundException( + raise BusinessLogicException( f"Cannot delete tier: {active_subs} active subscriptions are using it" ) @@ -172,7 +177,7 @@ class AdminSubscriptionService: ) if not result: - raise NotFoundException(f"Subscription for vendor {vendor_id} not found") + raise ResourceNotFoundException(f"Subscription for vendor {vendor_id} not found") return result