Deleting user now works, and forces you to allocate tasks from each user to another user before deleting them

This commit is contained in:
NPS Agent
2026-05-13 10:40:57 +09:30
parent ee5f55bc8e
commit fd84caef63
7 changed files with 24 additions and 5 deletions
+13
View File
@@ -96,6 +96,19 @@ def delete_user(user_id: str, db: Session = Depends(get_db), current_user: model
db_user = db.query(models.User).filter(models.User.id == user_id).first()
if not db_user:
raise HTTPException(status_code=404, detail="User not found")
# Check for OPEN assigned tasks (that are not in the trash)
open_tasks = db.query(models.Task).filter(
models.Task.assignee_id == user_id,
models.Task.status == "open",
models.Task.deleted_at == None
).first()
if open_tasks:
raise HTTPException(status_code=400, detail="Cannot delete user: They still have OPEN tasks assigned to them. Reassign them first.")
# Nullify references in closed tasks and notes so we don't lose history
db.query(models.Task).filter(models.Task.assignee_id == user_id).update({"assignee_id": None})
db.query(models.TaskNote).filter(models.TaskNote.author_id == user_id).update({"author_id": None})
db.delete(db_user)
db.commit()