2026-07-04 01:23:40 +00:00
|
|
|
"""Address routes — list, create, update, delete with entity filtering."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.db import get_db
|
2026-07-15 22:35:50 +02:00
|
|
|
from app.deps import require_permission
|
2026-07-04 01:23:40 +00:00
|
|
|
from app.schemas.address import AddressCreate, AddressUpdate
|
|
|
|
|
from app.services import address_service
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/v1/addresses", tags=["addresses"])
|
|
|
|
|
|
|
|
|
|
|
2026-08-16 01:17:18 +02:00
|
|
|
def _validate_entity_type(entity_type: str) -> None:
|
|
|
|
|
"""Validate entity_type against the ENTITY_MODELS registry."""
|
|
|
|
|
from app.services.entity_permission_service import ENTITY_MODELS
|
|
|
|
|
|
|
|
|
|
if entity_type not in ENTITY_MODELS:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=400,
|
|
|
|
|
detail={"detail": f"Invalid entity_type: {entity_type}", "code": "invalid_entity_type"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-07-04 01:23:40 +00:00
|
|
|
@router.get("")
|
|
|
|
|
async def list_addresses(
|
2026-08-16 01:17:18 +02:00
|
|
|
entity_type: str = Query(...),
|
2026-07-04 01:23:40 +00:00
|
|
|
entity_id: str = Query(...),
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("addresses:read")),
|
2026-07-04 01:23:40 +00:00
|
|
|
):
|
|
|
|
|
"""List all addresses for a given entity (company or contact)."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
2026-07-29 01:52:47 +02:00
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
|
|
|
is_admin = current_user.get("is_system_admin", False)
|
|
|
|
|
|
2026-08-16 01:17:18 +02:00
|
|
|
_validate_entity_type(entity_type)
|
|
|
|
|
|
2026-07-04 01:23:40 +00:00
|
|
|
try:
|
|
|
|
|
eid = uuid.UUID(entity_id)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise HTTPException(400, detail={"detail": "Invalid entity_id", "code": "invalid_id"}) from None
|
|
|
|
|
|
2026-07-29 01:52:47 +02:00
|
|
|
try:
|
|
|
|
|
return await address_service.list_addresses(db, tenant_id, entity_type, eid, user_id=user_id, is_system_admin=is_admin)
|
|
|
|
|
except PermissionError as e:
|
|
|
|
|
raise HTTPException(status_code=403, detail=str(e)) from e
|
2026-07-04 01:23:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("", status_code=status.HTTP_201_CREATED)
|
|
|
|
|
async def create_address(
|
|
|
|
|
body: AddressCreate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("addresses:write")),
|
2026-07-04 01:23:40 +00:00
|
|
|
):
|
|
|
|
|
"""Create a new address for a company or contact."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
2026-07-29 01:52:47 +02:00
|
|
|
is_admin = current_user.get("is_system_admin", False)
|
2026-07-04 01:23:40 +00:00
|
|
|
|
|
|
|
|
data = body.model_dump()
|
2026-08-16 01:17:18 +02:00
|
|
|
_validate_entity_type(data["entity_type"])
|
2026-07-04 01:23:40 +00:00
|
|
|
try:
|
2026-07-29 01:52:47 +02:00
|
|
|
return await address_service.create_address(db, tenant_id, user_id, data, is_system_admin=is_admin)
|
|
|
|
|
except PermissionError as e:
|
|
|
|
|
raise HTTPException(status_code=403, detail=str(e)) from e
|
2026-07-04 01:23:40 +00:00
|
|
|
except ValueError as exc:
|
|
|
|
|
raise HTTPException(400, detail={"detail": str(exc), "code": "invalid_value"}) from exc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.patch("/{address_id}")
|
|
|
|
|
async def update_address(
|
|
|
|
|
address_id: str,
|
|
|
|
|
body: AddressUpdate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("addresses:write")),
|
2026-07-04 01:23:40 +00:00
|
|
|
):
|
|
|
|
|
"""Update an address."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
2026-07-29 01:52:47 +02:00
|
|
|
is_admin = current_user.get("is_system_admin", False)
|
2026-07-04 01:23:40 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
aid = uuid.UUID(address_id)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise HTTPException(400, detail={"detail": "Invalid address_id", "code": "invalid_id"}) from None
|
|
|
|
|
|
|
|
|
|
data = body.model_dump(exclude_unset=True)
|
2026-07-29 01:52:47 +02:00
|
|
|
try:
|
|
|
|
|
result = await address_service.update_address(db, tenant_id, user_id, aid, data, is_system_admin=is_admin)
|
|
|
|
|
except PermissionError as e:
|
|
|
|
|
raise HTTPException(status_code=403, detail=str(e)) from e
|
2026-07-04 01:23:40 +00:00
|
|
|
if result is None:
|
|
|
|
|
raise HTTPException(404, detail={"detail": "Address not found", "code": "not_found"})
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{address_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
async def delete_address(
|
|
|
|
|
address_id: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("addresses:write")),
|
2026-07-04 01:23:40 +00:00
|
|
|
):
|
|
|
|
|
"""Soft-delete an address."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
user_id = uuid.UUID(current_user["user_id"])
|
2026-07-29 01:52:47 +02:00
|
|
|
is_admin = current_user.get("is_system_admin", False)
|
2026-07-04 01:23:40 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
aid = uuid.UUID(address_id)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise HTTPException(400, detail={"detail": "Invalid address_id", "code": "invalid_id"}) from None
|
|
|
|
|
|
2026-07-29 01:52:47 +02:00
|
|
|
try:
|
|
|
|
|
deleted = await address_service.delete_address(db, tenant_id, user_id, aid, is_system_admin=is_admin)
|
|
|
|
|
except PermissionError as e:
|
|
|
|
|
raise HTTPException(status_code=403, detail=str(e)) from e
|
2026-07-04 01:23:40 +00:00
|
|
|
if not deleted:
|
|
|
|
|
raise HTTPException(404, detail={"detail": "Address not found", "code": "not_found"})
|