Problem 1: Pass role_id through users route create/update, include in responses
This commit is contained in:
+27
-1
@@ -18,6 +18,22 @@ from app.services.user_service import user_service
|
||||
router = APIRouter(prefix="/api/v1/users", tags=["users"])
|
||||
|
||||
|
||||
def _parse_role_id(raw: str | None) -> uuid.UUID | None:
|
||||
"""Convert a string body value into a UUID or None.
|
||||
|
||||
Empty string and None are both treated as "no role_id".
|
||||
"""
|
||||
if raw is None or raw == "":
|
||||
return None
|
||||
try:
|
||||
return uuid.UUID(raw)
|
||||
except (ValueError, AttributeError):
|
||||
raise HTTPException(
|
||||
400,
|
||||
detail={"detail": "Invalid role_id", "code": "invalid_role_id"},
|
||||
) from None
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_users(
|
||||
page: int = Query(1, ge=1),
|
||||
@@ -40,6 +56,7 @@ async def create_user(
|
||||
"""Create a new user (admin only)."""
|
||||
tenant_id = uuid.UUID(current_user["tenant_id"])
|
||||
user_id = uuid.UUID(current_user["user_id"])
|
||||
role_id = _parse_role_id(body.role_id)
|
||||
|
||||
user = await user_service.create_user(
|
||||
db,
|
||||
@@ -48,6 +65,7 @@ async def create_user(
|
||||
body.name,
|
||||
body.password,
|
||||
body.role,
|
||||
role_id,
|
||||
body.is_active,
|
||||
)
|
||||
|
||||
@@ -59,7 +77,7 @@ async def create_user(
|
||||
"create",
|
||||
"user",
|
||||
user.id,
|
||||
changes={"email": body.email, "name": body.name, "role": body.role},
|
||||
changes={"email": body.email, "name": body.name, "role": body.role, "role_id": body.role_id},
|
||||
)
|
||||
|
||||
# Notification
|
||||
@@ -77,6 +95,7 @@ async def create_user(
|
||||
"email": user.email,
|
||||
"name": user.name,
|
||||
"role": user.role,
|
||||
"role_id": str(user.role_id) if user.role_id else None,
|
||||
"is_active": user.is_active,
|
||||
"tenant_id": str(user.tenant_id),
|
||||
}
|
||||
@@ -106,6 +125,7 @@ async def get_user(
|
||||
"email": user.email,
|
||||
"name": user.name,
|
||||
"role": user.role,
|
||||
"role_id": str(user.role_id) if user.role_id else None,
|
||||
"is_active": user.is_active,
|
||||
"tenant_id": str(user.tenant_id),
|
||||
}
|
||||
@@ -133,15 +153,20 @@ async def update_user(
|
||||
changes["name"] = body.name
|
||||
if body.role is not None:
|
||||
changes["role"] = body.role
|
||||
if body.role_id is not None:
|
||||
changes["role_id"] = body.role_id
|
||||
if body.is_active is not None:
|
||||
changes["is_active"] = body.is_active
|
||||
|
||||
role_id = _parse_role_id(body.role_id)
|
||||
|
||||
user = await user_service.update_user(
|
||||
db,
|
||||
tenant_id,
|
||||
uid,
|
||||
body.name,
|
||||
body.role,
|
||||
role_id,
|
||||
body.is_active,
|
||||
)
|
||||
if user is None:
|
||||
@@ -154,6 +179,7 @@ async def update_user(
|
||||
"email": user.email,
|
||||
"name": user.name,
|
||||
"role": user.role,
|
||||
"role_id": str(user.role_id) if user.role_id else None,
|
||||
"is_active": user.is_active,
|
||||
"tenant_id": str(user.tenant_id),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user