abbe7a18fc
- P0: hooks.py 3-tuple fix, trigger_dispatcher Contract, contacts/plugin unregister_actions_by_owner - P0: 5 test files — check_permission mocks removed, hardcoded DB credential → env var - P1: attachment_service DmsFile via Contract helper, restore_registry/history_hooks dedup - P1: mail/plugin restore unregister, mcp_client datetime.now(UTC), saved_views/filters patterns - P1: ProtectedRoute fail-closed, 13 test assertion fixes (bcrypt, DB-URLs, SECRET_KEYs) - P2: deprecated notifications → post_system_message (3 files), forgejo Base, report_generator lazy import - P2: webhooks permissions, deps.py/roles.py plugin perms removed, import_export default - P2: address/tags/entity_links patterns removed, worker.py Contract-Umgehungen fixed - P2: 28 frontend TODOs (hardcoded constants, deprecated notification API) - P3: dead code, duplicates, deprecated imports, private attr, __import__ inline - P3: 8 frontend TODOs (LucideIcons, inline styles, XSS, i18n) - ruff: 838 → 0 (612 auto-fix + 246 manual + 27 F821 regression fix) - F821: 30 → 0 (AutomationDefinition, DmsFile, user_id, Path, Any, String) - Contract-Umgehungen: 2 neue gefunden (worker.py:169, worker.py:280) und gefixt
124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
"""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
|
|
from app.deps import require_permission
|
|
from app.schemas.address import AddressCreate, AddressUpdate
|
|
from app.services import address_service
|
|
|
|
router = APIRouter(prefix="/api/v1/addresses", tags=["addresses"])
|
|
|
|
|
|
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"},
|
|
)
|
|
|
|
|
|
@router.get("")
|
|
async def list_addresses(
|
|
entity_type: str = Query(...),
|
|
entity_id: str = Query(...),
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("addresses:read")),
|
|
):
|
|
"""List all addresses for a given entity (company or contact)."""
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
is_admin = current_user.get("is_system_admin", False)
|
|
|
|
_validate_entity_type(entity_type)
|
|
|
|
try:
|
|
eid = uuid.UUID(entity_id)
|
|
except ValueError:
|
|
raise HTTPException(400, detail={"detail": "Invalid entity_id", "code": "invalid_id"}) from None
|
|
|
|
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
|
|
|
|
|
|
@router.post("", status_code=status.HTTP_201_CREATED)
|
|
async def create_address(
|
|
body: AddressCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(require_permission("addresses:write")),
|
|
):
|
|
"""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"])
|
|
is_admin = current_user.get("is_system_admin", False)
|
|
|
|
data = body.model_dump()
|
|
_validate_entity_type(data["entity_type"])
|
|
try:
|
|
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
|
|
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),
|
|
current_user: dict = Depends(require_permission("addresses:write")),
|
|
):
|
|
"""Update an address."""
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
is_admin = current_user.get("is_system_admin", False)
|
|
|
|
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)
|
|
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
|
|
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),
|
|
current_user: dict = Depends(require_permission("addresses:write")),
|
|
):
|
|
"""Soft-delete an address."""
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
user_id = uuid.UUID(current_user["user_id"])
|
|
is_admin = current_user.get("is_system_admin", False)
|
|
|
|
try:
|
|
aid = uuid.UUID(address_id)
|
|
except ValueError:
|
|
raise HTTPException(400, detail={"detail": "Invalid address_id", "code": "invalid_id"}) from None
|
|
|
|
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
|
|
if not deleted:
|
|
raise HTTPException(404, detail={"detail": "Address not found", "code": "not_found"})
|