Files
bakery-ia/services/tenant/app/api/tenants.py

158 lines
5.0 KiB
Python
Raw Normal View History

2025-07-19 17:49:03 +02:00
# services/tenant/app/api/tenants.py
"""
Tenant API endpoints
"""
2025-07-26 18:46:52 +02:00
from fastapi import APIRouter, Depends, HTTPException, status, Path
2025-07-19 17:49:03 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
2025-07-21 20:48:41 +02:00
from typing import List, Dict, Any
2025-07-19 17:49:03 +02:00
import structlog
2025-07-26 18:46:52 +02:00
from uuid import UUID
2025-07-19 17:49:03 +02:00
from app.core.database import get_db
from app.schemas.tenants import (
BakeryRegistration, TenantResponse, TenantAccessResponse,
TenantUpdate, TenantMemberResponse
)
from app.services.tenant_service import TenantService
2025-07-21 14:41:33 +02:00
# Import unified authentication
from shared.auth.decorators import (
get_current_user_dep,
get_current_tenant_id_dep,
require_role
)
2025-07-19 17:49:03 +02:00
logger = structlog.get_logger()
router = APIRouter()
2025-07-20 23:15:57 +02:00
@router.post("/tenants/register", response_model=TenantResponse)
2025-07-19 17:49:03 +02:00
async def register_bakery(
bakery_data: BakeryRegistration,
2025-07-21 14:41:33 +02:00
current_user: Dict[str, Any] = Depends(get_current_user_dep),
2025-07-19 17:49:03 +02:00
db: AsyncSession = Depends(get_db)
):
try:
2025-07-21 14:41:33 +02:00
result = await TenantService.create_bakery(bakery_data, current_user["user_id"], db)
logger.info(f"Bakery registered: {bakery_data.name} by {current_user['email']}")
2025-07-19 17:49:03 +02:00
return result
except Exception as e:
logger.error(f"Bakery registration failed: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Bakery registration failed"
)
@router.get("/tenants/{tenant_id}/access/{user_id}", response_model=TenantAccessResponse)
async def verify_tenant_access(
user_id: str,
2025-07-26 18:46:52 +02:00
tenant_id: UUID = Path(..., description="Tenant ID"),
2025-07-19 17:49:03 +02:00
db: AsyncSession = Depends(get_db)
):
"""Verify if user has access to tenant - Called by Gateway"""
try:
access_info = await TenantService.verify_user_access(user_id, tenant_id, db)
return access_info
except Exception as e:
logger.error(f"Access verification failed: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Access verification failed"
)
2025-07-26 18:46:52 +02:00
@router.get("/tenants/users/{user_id}", response_model=List[TenantResponse])
2025-07-19 17:49:03 +02:00
async def get_user_tenants(
user_id: str,
2025-07-21 14:41:33 +02:00
current_user: Dict[str, Any] = Depends(get_current_user_dep),
2025-07-19 17:49:03 +02:00
db: AsyncSession = Depends(get_db)
):
2025-07-21 14:41:33 +02:00
2025-07-19 17:49:03 +02:00
# Users can only see their own tenants
if current_user["user_id"] != user_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied"
)
try:
tenants = await TenantService.get_user_tenants(user_id, db)
return tenants
except Exception as e:
logger.error(f"Failed to get user tenants: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve tenants"
)
@router.get("/tenants/{tenant_id}", response_model=TenantResponse)
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-07-19 17:49:03 +02:00
db: AsyncSession = Depends(get_db)
):
2025-07-21 14:41:33 +02:00
2025-07-19 17:49:03 +02:00
# Verify user has access to tenant
2025-07-21 14:41:33 +02:00
access = await TenantService.verify_user_access(current_user["user_id"], tenant_id, db)
2025-07-19 17:49:03 +02:00
if not access.has_access:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied to tenant"
)
tenant = await TenantService.get_tenant_by_id(tenant_id, db)
if not tenant:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Tenant not found"
)
return tenant
@router.put("/tenants/{tenant_id}", response_model=TenantResponse)
async def update_tenant(
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-07-19 17:49:03 +02:00
db: AsyncSession = Depends(get_db)
):
2025-07-21 14:41:33 +02:00
2025-07-19 17:49:03 +02:00
try:
2025-07-21 14:41:33 +02:00
result = await TenantService.update_tenant(tenant_id, update_data, current_user["user_id"], db)
2025-07-19 17:49:03 +02:00
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"Tenant update failed: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Tenant update failed"
)
@router.post("/tenants/{tenant_id}/members", response_model=TenantMemberResponse)
async def add_team_member(
user_id: str,
role: str,
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-07-19 17:49:03 +02:00
db: AsyncSession = Depends(get_db)
):
2025-07-21 14:41:33 +02:00
2025-07-19 17:49:03 +02:00
try:
result = await TenantService.add_team_member(
tenant_id, user_id, role, current_user["user_id"], db
)
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"Add team member failed: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to add team member"
)