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()
+1 -1
View File
@@ -59,7 +59,7 @@ class TaskNote(Base):
id = Column(Integer, primary_key=True, index=True)
task_id = Column(String, ForeignKey("tasks.id", ondelete="CASCADE"), nullable=False)
author_id = Column(String, ForeignKey("users.id"), nullable=False)
author_id = Column(String, ForeignKey("users.id"), nullable=True)
body = Column(String, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
+1 -1
View File
@@ -62,7 +62,7 @@ class Tag(TagBase):
class TaskBase(BaseModel):
title: str
description: Optional[str] = None
assignee_id: str
assignee_id: Optional[str] = None
added_by: str
priority: str
source: str