2025-07-19 17:49:03 +02:00
|
|
|
"""
|
2025-10-06 15:27:01 +02:00
|
|
|
Tenant API - ATOMIC operations
|
|
|
|
|
Handles basic CRUD operations for tenants
|
2025-07-19 17:49:03 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import structlog
|
2025-10-06 15:27:01 +02:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Path
|
|
|
|
|
from typing import Dict, Any
|
2025-08-08 09:08:41 +02:00
|
|
|
from uuid import UUID
|
2025-07-19 17:49:03 +02:00
|
|
|
|
2025-10-06 15:27:01 +02:00
|
|
|
from app.schemas.tenants import TenantResponse, TenantUpdate
|
2025-08-08 09:08:41 +02:00
|
|
|
from app.services.tenant_service import EnhancedTenantService
|
2025-10-06 15:27:01 +02:00
|
|
|
from shared.auth.decorators import get_current_user_dep
|
2025-10-15 16:12:49 +02:00
|
|
|
from shared.auth.access_control import admin_role_required
|
2025-10-06 15:27:01 +02:00
|
|
|
from shared.routing.route_builder import RouteBuilder
|
2025-08-08 09:08:41 +02:00
|
|
|
from shared.database.base import create_database_manager
|
|
|
|
|
from shared.monitoring.metrics import track_endpoint_metrics
|
2025-07-19 17:49:03 +02:00
|
|
|
|
|
|
|
|
logger = structlog.get_logger()
|
|
|
|
|
router = APIRouter()
|
2025-10-06 15:27:01 +02:00
|
|
|
route_builder = RouteBuilder("tenants")
|
2025-07-19 17:49:03 +02:00
|
|
|
|
2025-08-08 09:08:41 +02:00
|
|
|
# Dependency injection for enhanced tenant service
|
|
|
|
|
def get_enhanced_tenant_service():
|
2025-08-15 22:40:19 +02:00
|
|
|
try:
|
|
|
|
|
from app.core.config import settings
|
|
|
|
|
database_manager = create_database_manager(settings.DATABASE_URL, "tenant-service")
|
|
|
|
|
return EnhancedTenantService(database_manager)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("Failed to create enhanced tenant service", error=str(e))
|
|
|
|
|
raise HTTPException(status_code=500, detail="Service initialization failed")
|
2025-08-08 09:08:41 +02:00
|
|
|
|
2025-10-06 15:27:01 +02:00
|
|
|
@router.get(route_builder.build_base_route("{tenant_id}", include_tenant_prefix=False), response_model=TenantResponse)
|
2025-08-08 09:08:41 +02:00
|
|
|
@track_endpoint_metrics("tenant_get")
|
2025-10-06 15:27:01 +02:00
|
|
|
async def get_tenant(
|
2025-07-26 18:46:52 +02:00
|
|
|
tenant_id: UUID = Path(..., description="Tenant ID"),
|
2025-07-21 14:41:33 +02:00
|
|
|
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
2025-08-08 09:08:41 +02:00
|
|
|
tenant_service: EnhancedTenantService = Depends(get_enhanced_tenant_service)
|
2025-07-19 17:49:03 +02:00
|
|
|
):
|
2025-10-06 15:27:01 +02:00
|
|
|
"""Get tenant by ID - ATOMIC operation"""
|
|
|
|
|
|
2025-08-08 09:08:41 +02:00
|
|
|
tenant = await tenant_service.get_tenant_by_id(str(tenant_id))
|
2025-07-19 17:49:03 +02:00
|
|
|
if not tenant:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
|
|
|
detail="Tenant not found"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-08 09:08:41 +02:00
|
|
|
return tenant
|
|
|
|
|
|
2025-10-06 15:27:01 +02:00
|
|
|
@router.put(route_builder.build_base_route("{tenant_id}", include_tenant_prefix=False), response_model=TenantResponse)
|
2025-10-15 16:12:49 +02:00
|
|
|
@admin_role_required
|
2025-10-06 15:27:01 +02:00
|
|
|
async def update_tenant(
|
2025-07-19 17:49:03 +02:00
|
|
|
update_data: TenantUpdate,
|
2025-07-26 18:46:52 +02:00
|
|
|
tenant_id: UUID = Path(..., description="Tenant ID"),
|
2025-07-21 14:41:33 +02:00
|
|
|
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
2025-08-08 09:08:41 +02:00
|
|
|
tenant_service: EnhancedTenantService = Depends(get_enhanced_tenant_service)
|
2025-07-19 17:49:03 +02:00
|
|
|
):
|
2025-10-15 16:12:49 +02:00
|
|
|
"""Update tenant information - ATOMIC operation (Admin+ only)"""
|
2025-07-21 14:41:33 +02:00
|
|
|
|
2025-07-19 17:49:03 +02:00
|
|
|
try:
|
2025-08-08 09:08:41 +02:00
|
|
|
result = await tenant_service.update_tenant(
|
2025-10-06 15:27:01 +02:00
|
|
|
str(tenant_id),
|
|
|
|
|
update_data,
|
2025-08-08 09:08:41 +02:00
|
|
|
current_user["user_id"]
|
|
|
|
|
)
|
2025-07-19 17:49:03 +02:00
|
|
|
return result
|
2025-10-06 15:27:01 +02:00
|
|
|
|
2025-07-19 17:49:03 +02:00
|
|
|
except HTTPException:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
2025-08-08 09:08:41 +02:00
|
|
|
logger.error("Tenant update failed",
|
|
|
|
|
tenant_id=str(tenant_id),
|
|
|
|
|
user_id=current_user["user_id"],
|
|
|
|
|
error=str(e))
|
2025-07-19 17:49:03 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
detail="Tenant update failed"
|
|
|
|
|
)
|