Skip to content

Commit

Permalink
🐛 Fix response model error 500
Browse files Browse the repository at this point in the history
  • Loading branch information
rapeeza1598 committed May 1, 2024
1 parent 53b0a23 commit c5bffaf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
9 changes: 4 additions & 5 deletions app/crud/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ def update_user(db: Session, user_id: str, user: updateUser):

def update_user_by_super_admin(db: Session, user_id: str, user: updateUserBySuperAdmin):
db_user = db.query(User).filter(User.id == user_id).first() # type: ignore
setattr(db_user, "firstName", user.firstName)
setattr(db_user, "lastName", user.lastName)
setattr(db_user, "phoneNumber", user.phoneNumber)
setattr(db_user, "role", user.role)
setattr(db_user, "is_active", user.is_active)
list_data = ["firstName", "lastName", "phoneNumber", "role", "is_active"]
try:
for data in list_data:
if hasattr(user, data):
setattr(db_user, data, getattr(user, data))
db.commit()
db.refresh(db_user)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion app/crud/user_avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def update_user_avatar(db: Session, user_id: str, avatar_img_b64: str):
.filter(UserAvatar.userId == user_id) # type: ignore
.first()
):
setattr(db_user_avatar, "avatar_img_b64", avatar_img_b64)
setattr(db_user_avatar, "avatar", avatar_img_b64)
try:
db.commit()
db.refresh(db_user_avatar)
Expand Down
17 changes: 11 additions & 6 deletions app/routers/super_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ async def read_users(

@router.put(
"/users/{user_id}",
response_model=updateUserBySuperAdmin,
)
async def update_user_by_id(
user_id: str,
Expand All @@ -78,11 +77,17 @@ async def update_user_by_id(
):
if current_user.role != "superadmin":
raise HTTPException(status_code=401, detail="Unauthorized")
if update_user_by_super_admin(db, user_id, user):
user_activity = f"User {current_user.email} updated user {user_id}"
create_log_info(db, str(current_user.id), user_activity, type_log="user")
return {"message": "User updated successfully"}
raise HTTPException(status_code=400, detail="User update failed")
try:
user = get_user_by_id(db, user_id)
if not user:
raise HTTPException(status_code=404, detail="User not found")
if update_user_by_super_admin(db, user_id, user):
user_activity = f"User {current_user.email} updated user {user_id}"
create_log_info(db, str(current_user.id), user_activity, type_log="user")
return {"message": "User updated successfully"}
except Exception as e:
print(e)
raise HTTPException(status_code=400, detail="User not updated") from e


@router.put("/users/{user_id}/disable")
Expand Down
1 change: 0 additions & 1 deletion app/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class updateUserBySuperAdmin(BaseModel):
firstName: Optional[str]
lastName: Optional[str]
phoneNumber: Optional[str]
email: Optional[str]
role: Optional[str]
is_active: Optional[bool]

Expand Down

0 comments on commit c5bffaf

Please sign in to comment.