Skip to content

Commit f300449

Browse files
committed
feat: Add Admin Delete User and Appointment endpoints
1 parent 90444ae commit f300449

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

backend/admin.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,36 @@ def get_recent_users(
7373
@router.put("/users/{user_id}/role")
7474
def update_user_role(
7575
user_id: int,
76-
role: str,
77-
db: Session = Depends(database.get_db),
78-
admin: models.User = Depends(get_current_admin)
76+
role: str,
77+
admin: models.User = Depends(get_current_admin),
78+
db: Session = Depends(database.get_db)
7979
):
8080
"""Update a user's system role (patient, doctor, admin)."""
8181
user = db.query(models.User).filter(models.User.id == user_id).first()
8282
if not user:
8383
raise HTTPException(status_code=404, detail="User not found")
84-
84+
8585
if role not in ["patient", "doctor", "admin"]:
8686
raise HTTPException(status_code=400, detail="Invalid role. Must be patient, doctor, or admin.")
8787

8888
user.role = role
8989
db.commit()
90-
return {"status": "success", "message": f"User {user.username} promoted to {role}"}
90+
return {"message": f"User role updated to {role}"}
91+
92+
@router.delete("/users/{user_id}")
93+
def delete_user(
94+
user_id: int,
95+
admin: models.User = Depends(get_current_admin),
96+
db: Session = Depends(database.get_db)
97+
):
98+
"""Permanently delete a user and their data."""
99+
user = db.query(models.User).filter(models.User.id == user_id).first()
100+
if not user:
101+
raise HTTPException(status_code=404, detail="User not found")
102+
103+
if user.id == admin.id:
104+
raise HTTPException(status_code=400, detail="Cannot delete yourself.")
105+
106+
db.delete(user)
107+
db.commit()
108+
return {"message": "User deleted successfully"}

backend/appointments.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,21 @@ def reschedule_appointment(
126126
appt.status = "Rescheduled"
127127
db.commit()
128128
return {"message": "Appointment rescheduled"}
129+
130+
@router.delete("/{appointment_id}")
131+
def delete_appointment(
132+
appointment_id: int,
133+
db: Session = Depends(database.get_db),
134+
current_user: models.User = Depends(auth.get_current_user)
135+
):
136+
"""Admin or Owner can delete an appointment."""
137+
appt = db.query(models.Appointment).filter(models.Appointment.id == appointment_id).first()
138+
if not appt:
139+
raise HTTPException(status_code=404, detail="Appointment not found")
140+
141+
if current_user.role != "admin" and appt.user_id != current_user.id:
142+
raise HTTPException(status_code=403, detail="Not authorized")
143+
144+
db.delete(appt)
145+
db.commit()
146+
return {"message": "Appointment deleted"}

0 commit comments

Comments
 (0)