189 lines
No EOL
6.3 KiB
Python
189 lines
No EOL
6.3 KiB
Python
from fastapi import APIRouter, HTTPException, Depends, Body, status
|
|
from typing import Dict, Any, List
|
|
import logging
|
|
|
|
from modules.security.auth import getCurrentActiveUser
|
|
from modules.interfaces.gatewayInterface import getInterface
|
|
from modules.interfaces.gatewayModel import Mandate, getModelAttributes
|
|
|
|
# Configure logger
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Model attributes for Mandate
|
|
mandateAttributes = getModelAttributes(Mandate)
|
|
|
|
router = APIRouter(
|
|
prefix="/api/mandates",
|
|
tags=["Mandates"],
|
|
responses={404: {"description": "Not found"}}
|
|
)
|
|
|
|
@router.get("/", response_model=List[Dict[str, Any]], tags=["Mandates"])
|
|
async def get_mandates(currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)):
|
|
"""Get all mandates"""
|
|
try:
|
|
myInterface = getInterface(currentUser)
|
|
return myInterface.getMandates()
|
|
except Exception as e:
|
|
logger.error(f"Error getting mandates: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to get mandates: {str(e)}"
|
|
)
|
|
|
|
@router.get("/{mandateId}", response_model=Dict[str, Any], tags=["Mandates"])
|
|
async def get_mandate(
|
|
mandateId: str,
|
|
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
|
|
):
|
|
"""Get a specific mandate by ID"""
|
|
try:
|
|
myInterface = getInterface(currentUser)
|
|
mandate = myInterface.getMandateById(mandateId)
|
|
|
|
if not mandate:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Mandate {mandateId} not found"
|
|
)
|
|
|
|
return mandate
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"Error getting mandate {mandateId}: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to get mandate: {str(e)}"
|
|
)
|
|
|
|
@router.post("/", response_model=Dict[str, Any], tags=["Mandates"])
|
|
async def create_mandate(
|
|
mandateData: Dict[str, Any],
|
|
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
|
|
):
|
|
"""Create a new mandate"""
|
|
try:
|
|
myInterface = getInterface(currentUser)
|
|
|
|
# Check required fields
|
|
if not mandateData.get("name"):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Mandate name is required"
|
|
)
|
|
|
|
# Filter attributes based on model definition
|
|
filteredData = {}
|
|
for attr in mandateAttributes:
|
|
if attr in mandateData:
|
|
filteredData[attr] = mandateData[attr]
|
|
|
|
try:
|
|
createdMandate = myInterface.createMandate(**filteredData)
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e)
|
|
)
|
|
|
|
if not createdMandate:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Failed to create mandate"
|
|
)
|
|
|
|
return createdMandate
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"Error creating mandate: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to create mandate: {str(e)}"
|
|
)
|
|
|
|
@router.put("/{mandateId}", response_model=Dict[str, Any], tags=["Mandates"])
|
|
async def update_mandate(
|
|
mandateId: str,
|
|
mandateData: Dict[str, Any],
|
|
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
|
|
):
|
|
"""Update an existing mandate"""
|
|
try:
|
|
myInterface = getInterface(currentUser)
|
|
|
|
# Check if mandate exists
|
|
existingMandate = myInterface.getMandateById(mandateId)
|
|
if not existingMandate:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Mandate {mandateId} not found"
|
|
)
|
|
|
|
# Filter attributes based on model definition
|
|
filteredData = {}
|
|
for attr in mandateAttributes:
|
|
if attr in mandateData:
|
|
filteredData[attr] = mandateData[attr]
|
|
|
|
# Update mandate data
|
|
try:
|
|
updatedMandate = myInterface.updateMandate(mandateId, **filteredData)
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e)
|
|
)
|
|
|
|
if not updatedMandate:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Failed to update mandate"
|
|
)
|
|
|
|
return updatedMandate
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"Error updating mandate {mandateId}: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to update mandate: {str(e)}"
|
|
)
|
|
|
|
@router.delete("/{mandateId}", response_model=Dict[str, Any], tags=["Mandates"])
|
|
async def delete_mandate(
|
|
mandateId: str,
|
|
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
|
|
):
|
|
"""Delete a mandate"""
|
|
try:
|
|
myInterface = getInterface(currentUser)
|
|
|
|
# Check if mandate exists
|
|
existingMandate = myInterface.getMandateById(mandateId)
|
|
if not existingMandate:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Mandate {mandateId} not found"
|
|
)
|
|
|
|
# Delete mandate
|
|
try:
|
|
myInterface.deleteMandate(mandateId)
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e)
|
|
)
|
|
|
|
return {"message": f"Mandate {mandateId} deleted successfully"}
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"Error deleting mandate {mandateId}: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to delete mandate: {str(e)}"
|
|
) |