Improve the frontend 2
This commit is contained in:
@@ -17,6 +17,7 @@ from app.schemas.tenants import (
|
||||
from app.services.tenant_service import EnhancedTenantService
|
||||
from app.services.subscription_limit_service import SubscriptionLimitService
|
||||
from app.services.payment_service import PaymentService
|
||||
from app.models import AuditLog
|
||||
from shared.auth.decorators import (
|
||||
get_current_user_dep,
|
||||
require_admin_role_dep
|
||||
@@ -33,7 +34,7 @@ router = APIRouter()
|
||||
route_builder = RouteBuilder("tenants")
|
||||
|
||||
# Initialize audit logger
|
||||
audit_logger = create_audit_logger("tenant-service")
|
||||
audit_logger = create_audit_logger("tenant-service", AuditLog)
|
||||
|
||||
# Global Redis client
|
||||
_redis_client = None
|
||||
@@ -555,6 +556,73 @@ async def get_tenant_statistics(
|
||||
# SUBSCRIPTION OPERATIONS
|
||||
# ============================================================================
|
||||
|
||||
@router.get("/api/v1/subscriptions/{tenant_id}/tier")
|
||||
async def get_tenant_subscription_tier_fast(
|
||||
tenant_id: UUID = Path(..., description="Tenant ID"),
|
||||
redis_client = Depends(get_tenant_redis_client)
|
||||
):
|
||||
"""
|
||||
Fast cached lookup for tenant subscription tier
|
||||
|
||||
This endpoint is optimized for high-frequency access (e.g., from gateway middleware)
|
||||
with Redis caching (10-minute TTL). No authentication required for internal service calls.
|
||||
"""
|
||||
try:
|
||||
from app.services.subscription_cache import get_subscription_cache_service
|
||||
|
||||
cache_service = get_subscription_cache_service(redis_client)
|
||||
tier = await cache_service.get_tenant_tier_cached(str(tenant_id))
|
||||
|
||||
return {
|
||||
"tenant_id": str(tenant_id),
|
||||
"tier": tier,
|
||||
"cached": True
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get subscription tier",
|
||||
tenant_id=str(tenant_id),
|
||||
error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to get subscription tier"
|
||||
)
|
||||
|
||||
@router.get(route_builder.build_base_route("subscriptions/{tenant_id}/active", include_tenant_prefix=False))
|
||||
async def get_tenant_active_subscription(
|
||||
tenant_id: UUID = Path(..., description="Tenant ID"),
|
||||
redis_client = Depends(get_tenant_redis_client)
|
||||
):
|
||||
"""
|
||||
Get full active subscription with caching
|
||||
|
||||
Returns complete subscription details with 10-minute Redis cache.
|
||||
"""
|
||||
try:
|
||||
from app.services.subscription_cache import get_subscription_cache_service
|
||||
|
||||
cache_service = get_subscription_cache_service(redis_client)
|
||||
subscription = await cache_service.get_tenant_subscription_cached(str(tenant_id))
|
||||
|
||||
if not subscription:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No active subscription found"
|
||||
)
|
||||
|
||||
return subscription
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Failed to get active subscription",
|
||||
tenant_id=str(tenant_id),
|
||||
error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to get active subscription"
|
||||
)
|
||||
|
||||
@router.get(route_builder.build_base_route("subscriptions/{tenant_id}/limits", include_tenant_prefix=False))
|
||||
async def get_subscription_limits(
|
||||
tenant_id: UUID = Path(..., description="Tenant ID"),
|
||||
@@ -760,6 +828,25 @@ async def upgrade_subscription_plan(
|
||||
new_plan=new_plan,
|
||||
user_id=current_user["user_id"])
|
||||
|
||||
# Invalidate subscription cache to ensure immediate availability of new tier
|
||||
try:
|
||||
from app.services.subscription_cache import get_subscription_cache_service
|
||||
import shared.redis_utils
|
||||
from app.core.config import settings
|
||||
|
||||
redis_client = await shared.redis_utils.initialize_redis(settings.REDIS_URL)
|
||||
cache_service = get_subscription_cache_service(redis_client)
|
||||
await cache_service.invalidate_subscription_cache(str(tenant_id))
|
||||
|
||||
logger.info("Subscription cache invalidated after upgrade",
|
||||
tenant_id=str(tenant_id),
|
||||
new_plan=new_plan)
|
||||
except Exception as cache_error:
|
||||
logger.error("Failed to invalidate subscription cache after upgrade",
|
||||
tenant_id=str(tenant_id),
|
||||
error=str(cache_error))
|
||||
# Don't fail the upgrade if cache invalidation fails
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"Plan successfully upgraded to {new_plan}",
|
||||
|
||||
Reference in New Issue
Block a user