gateway/modules/routes/routeUsers.py

197 lines
6.3 KiB
Python

from fastapi import APIRouter, HTTPException, Depends, Body, Path, Request
from typing import List, Dict, Any, Optional
from fastapi import status
from datetime import datetime
import logging
# Import auth module
from modules.security.auth import getCurrentActiveUser, getRootInterface
# Import interfaces
from modules.interfaces.gatewayInterface import getInterface
from modules.interfaces.gatewayModel import User, getModelAttributes
# Configure logger
logger = logging.getLogger(__name__)
# Model attributes for User
userAttributes = getModelAttributes(User)
router = APIRouter(
prefix="/api/users",
tags=["Users"],
responses={404: {"description": "Not found"}}
)
@router.get("/", response_model=List[Dict[str, Any]], tags=["Users"])
async def get_users(currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)):
"""Get all users in the current mandate"""
try:
myInterface = getInterface(currentUser)
return myInterface.getUsers()
except Exception as e:
logger.error(f"Error getting users: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get users: {str(e)}"
)
@router.get("/{userId}", response_model=Dict[str, Any], tags=["Users"])
async def get_user(
userId: str,
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
):
"""Get a specific user by ID"""
try:
myInterface = getInterface(currentUser)
user = myInterface.getUserById(userId)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User {userId} not found"
)
return user
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting user {userId}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get user: {str(e)}"
)
@router.post("/", response_model=Dict[str, Any], tags=["Users"])
async def create_user(
userData: Dict[str, Any],
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
):
"""Create a new user"""
try:
# Get admin user for user creation
myInterface = getRootInterface()
# Check required fields
if not userData.get("username") or not userData.get("password"):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Username and password are required"
)
# Filter attributes based on model definition
filteredData = {}
for attr in userAttributes:
if attr in userData:
filteredData[attr] = userData[attr]
try:
createdUser = myInterface.createUser(**filteredData)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
if not createdUser:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create user"
)
return createdUser
except HTTPException:
raise
except Exception as e:
logger.error(f"Error creating user: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create user: {str(e)}"
)
@router.put("/{userId}", response_model=Dict[str, Any], tags=["Users"])
async def update_user(
userId: str,
userData: Dict[str, Any],
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
):
"""Update an existing user"""
try:
# Get admin user for user updates
myInterface = getRootInterface()
# Check if user exists
existingUser = myInterface.getUserById(userId)
if not existingUser:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User {userId} not found"
)
# Filter attributes based on model definition
filteredData = {}
for attr in userAttributes:
if attr in userData:
filteredData[attr] = userData[attr]
# Update user data
try:
updatedUser = myInterface.updateUser(userId, **filteredData)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
if not updatedUser:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update user"
)
return updatedUser
except HTTPException:
raise
except Exception as e:
logger.error(f"Error updating user {userId}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to update user: {str(e)}"
)
@router.delete("/{userId}", response_model=Dict[str, Any], tags=["Users"])
async def delete_user(
userId: str,
currentUser: Dict[str, Any] = Depends(getCurrentActiveUser)
):
"""Delete a user"""
try:
# Get admin user for user deletion
myInterface = getRootInterface()
# Check if user exists
existingUser = myInterface.getUserById(userId)
if not existingUser:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"User {userId} not found"
)
# Delete user
try:
myInterface.deleteUser(userId)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
return {"message": f"User {userId} deleted successfully"}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error deleting user {userId}: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to delete user: {str(e)}"
)