feat(core): add currency routes
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
"""Currency routes — list, create, update, delete."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, 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.currency import CurrencyCreate, CurrencyUpdate
|
||||
from app.services import currency_service
|
||||
|
||||
router = APIRouter(prefix="/api/v1/currencies", tags=["currencies"])
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_currencies(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""List all currencies for the current tenant."""
|
||||
tenant_id = uuid.UUID(current_user["tenant_id"])
|
||||
return await currency_service.list_currencies(db, tenant_id)
|
||||
|
||||
|
||||
@router.post("", status_code=status.HTTP_201_CREATED)
|
||||
async def create_currency(
|
||||
body: CurrencyCreate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Create a currency. Requires admin permission."""
|
||||
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, "settings", "create"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail={"detail": "Insufficient permissions", "code": "forbidden"},
|
||||
)
|
||||
|
||||
data = body.model_dump()
|
||||
return await currency_service.create_currency(db, tenant_id, user_id, data)
|
||||
|
||||
|
||||
@router.patch("/{currency_id}")
|
||||
async def update_currency(
|
||||
currency_id: str,
|
||||
body: CurrencyUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Update a currency. Requires admin permission."""
|
||||
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, "settings", "update"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail={"detail": "Insufficient permissions", "code": "forbidden"},
|
||||
)
|
||||
|
||||
try:
|
||||
cid = uuid.UUID(currency_id)
|
||||
except ValueError:
|
||||
raise HTTPException(400, detail={"detail": "Invalid currency_id", "code": "invalid_id"}) from None
|
||||
|
||||
data = body.model_dump(exclude_unset=True)
|
||||
result = await currency_service.update_currency(db, tenant_id, user_id, cid, data)
|
||||
if result is None:
|
||||
raise HTTPException(404, detail={"detail": "Currency not found", "code": "not_found"})
|
||||
return result
|
||||
|
||||
|
||||
@router.delete("/{currency_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_currency(
|
||||
currency_id: str,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user),
|
||||
):
|
||||
"""Delete a currency. Requires admin permission."""
|
||||
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, "settings", "delete"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail={"detail": "Insufficient permissions", "code": "forbidden"},
|
||||
)
|
||||
|
||||
try:
|
||||
cid = uuid.UUID(currency_id)
|
||||
except ValueError:
|
||||
raise HTTPException(400, detail={"detail": "Invalid currency_id", "code": "invalid_id"}) from None
|
||||
|
||||
deleted = await currency_service.delete_currency(db, tenant_id, user_id, cid)
|
||||
if not deleted:
|
||||
raise HTTPException(404, detail={"detail": "Currency not found", "code": "not_found"})
|
||||
Reference in New Issue
Block a user