b15a62bec6
- Removed: app/routes/companies.py, app/services/company_service.py, app/models/company.py, app/schemas/company.py - Updated: main.py, routes/__init__.py, models/__init__.py (removed company imports) - Updated: action_mapper.py (company intents → contact intents, /api/v1/companies → /api/v1/contacts) - Updated: workflows/engine.py (company.created → contact.created) - Updated: worker.py (removed index_company) - Updated: roles.py, permission_registry.py, permissions.py, deps.py (companies: → contacts:) - Updated: permission_registry.py CORE_FIELD_DEFINITIONS (old field names → unified contact fields) - Updated: address model/schema/service (entity_type company → contact only) - Updated: ai_copilot_service.py (_exec_companies removed, _exec_contacts extended) - Updated: ai_proactive services/jobs/context_tools (Company → Contact, company_contacts → contact_persons) - Updated: unified_search jobs.py (removed index_company), query_understanding.py - Updated: calendar/models.py, addresses.py docstrings - Updated: conftest.py (Company → Contact, removed companies/company_contacts from TRUNCATE) - Updated: test_unified_search.py (index_company → index_contact)
96 lines
3.2 KiB
Python
96 lines
3.2 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"])
|
|
|
|
|
|
@router.get("")
|
|
async def list_addresses(
|
|
entity_type: str = Query(..., pattern="^contact$"),
|
|
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"])
|
|
try:
|
|
eid = uuid.UUID(entity_id)
|
|
except ValueError:
|
|
raise HTTPException(400, detail={"detail": "Invalid entity_id", "code": "invalid_id"}) from None
|
|
|
|
return await address_service.list_addresses(db, tenant_id, entity_type, eid)
|
|
|
|
|
|
@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"])
|
|
|
|
|
|
data = body.model_dump()
|
|
try:
|
|
return await address_service.create_address(db, tenant_id, user_id, data)
|
|
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"])
|
|
|
|
|
|
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)
|
|
result = await address_service.update_address(db, tenant_id, user_id, aid, data)
|
|
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"])
|
|
|
|
|
|
try:
|
|
aid = uuid.UUID(address_id)
|
|
except ValueError:
|
|
raise HTTPException(400, detail={"detail": "Invalid address_id", "code": "invalid_id"}) from None
|
|
|
|
deleted = await address_service.delete_address(db, tenant_id, user_id, aid)
|
|
if not deleted:
|
|
raise HTTPException(404, detail={"detail": "Address not found", "code": "not_found"})
|