From 2bcf3e5c5574312a89c9049663f450bd3b8a0897 Mon Sep 17 00:00:00 2001 From: Leopoldadmin Date: Sat, 4 Jul 2026 01:23:40 +0000 Subject: [PATCH] =?UTF-8?q?A:=20Add=20address=20routes=20=E2=80=94=20list,?= =?UTF-8?q?=20create,=20update,=20delete=20with=20entity=20filtering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/routes/addresses.py | 114 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 app/routes/addresses.py diff --git a/app/routes/addresses.py b/app/routes/addresses.py new file mode 100644 index 0000000..de71811 --- /dev/null +++ b/app/routes/addresses.py @@ -0,0 +1,114 @@ +"""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.auth import check_permission +from app.core.db import get_db +from app.deps import get_current_user +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="^(company|contact)$"), + entity_id: str = Query(...), + db: AsyncSession = Depends(get_db), + current_user: dict = Depends(get_current_user), +): + """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(get_current_user), +): + """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"]) + role = current_user.get("role", "viewer") + + if not check_permission(role, "companies", "create") and not check_permission(role, "contacts", "create"): + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail={"detail": "Insufficient permissions", "code": "forbidden"}, + ) + + 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(get_current_user), +): + """Update an address.""" + tenant_id = uuid.UUID(current_user["tenant_id"]) + user_id = uuid.UUID(current_user["user_id"]) + role = current_user.get("role", "viewer") + + if not check_permission(role, "companies", "update") and not check_permission(role, "contacts", "update"): + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail={"detail": "Insufficient permissions", "code": "forbidden"}, + ) + + 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(get_current_user), +): + """Soft-delete an address.""" + tenant_id = uuid.UUID(current_user["tenant_id"]) + user_id = uuid.UUID(current_user["user_id"]) + role = current_user.get("role", "viewer") + + if not check_permission(role, "companies", "delete") and not check_permission(role, "contacts", "delete"): + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail={"detail": "Insufficient permissions", "code": "forbidden"}, + ) + + 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"})