"""Bank account routes — list, create, update, delete with tenant isolation.""" from __future__ import annotations import uuid from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.ext.asyncio import AsyncSession from app.core.db import get_db from app.deps import require_permission from app.schemas.bank_account import BankAccountCreate, BankAccountUpdate from app.services import bank_account_service router = APIRouter(prefix="/api/v1/bank-accounts", tags=["bank-accounts"]) @router.get("") async def list_bank_accounts( db: AsyncSession = Depends(get_db), current_user: dict = Depends(require_permission("bank-accounts:read")), ): """List all bank accounts for the current tenant.""" 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: return await bank_account_service.list_bank_accounts(db, tenant_id, 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_bank_account( body: BankAccountCreate, db: AsyncSession = Depends(get_db), current_user: dict = Depends(require_permission("bank-accounts:write")), ): """Create a new bank account.""" 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() try: return await bank_account_service.create_bank_account(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("/{account_id}") async def update_bank_account( account_id: str, body: BankAccountUpdate, db: AsyncSession = Depends(get_db), current_user: dict = Depends(require_permission("bank-accounts:write")), ): """Update a bank account.""" 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(account_id) except ValueError: raise HTTPException(400, detail={"detail": "Invalid account_id", "code": "invalid_id"}) from None data = body.model_dump(exclude_unset=True) try: result = await bank_account_service.update_bank_account(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": "Bank account not found", "code": "not_found"}) return result @router.delete("/{account_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_bank_account( account_id: str, db: AsyncSession = Depends(get_db), current_user: dict = Depends(require_permission("bank-accounts:write")), ): """Soft-delete a bank account.""" 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(account_id) except ValueError: raise HTTPException(400, detail={"detail": "Invalid account_id", "code": "invalid_id"}) from None try: deleted = await bank_account_service.delete_bank_account(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": "Bank account not found", "code": "not_found"})