in components/user_management/src/routes/user.py [0:0]
def delete_user(user_id: str,
request: Request,
delete_inspace_user: Optional[bool] = False):
"""Delete a user with the given user_id from firestore
### Args:
user_id (str): Unique id of the user
### Raises:
ResourceNotFoundException: If the user does not exist
Exception: 500 Internal Server Error if something went wrong
### Returns:
JSON: Success/Fail Message
"""
try:
if not is_inspace_enabled(delete_inspace_user):
raise ValidationError("Inspace is disabled, please make "
"'delete_inspace_user' as false in "
"the API request body")
user = User.find_by_uuid(user_id)
user_fields = user.get_fields()
headers = {"Authorization": request.headers.get("Authorization")}
if user_fields.get("user_type", "") == "learner":
learner_id = user_fields.get("user_type_ref")
delete_learner_profile(headers, learner_id)
delete_learner(headers, learner_id)
agent_id = get_agent(headers, user_id)
delete_agent(headers, agent_id)
update_refs_for_user_by_type(user)
CollectionHandler.remove_doc_from_all_references(
user_id, "user_groups", user_fields.get("user_groups", []), "users")
if user_fields.get("user_type").lower() in STAFF_USERS:
staff_uuid = user_fields.get("user_type_ref", "")
Staff.delete_by_uuid(staff_uuid)
response_msg = "Successfully deleted the user and associated agent, "
response_msg += "learner/faculty"
if delete_inspace_user:
# Delete Inspace user
if user.inspace_user["is_inspace_user"] is True:
is_delete_successful = delete_inspace_user_helper(user)
if is_delete_successful is True:
response_msg = "Successfully deleted the user and associated agent, "
response_msg += "learner/faculty and Inspace User"
else:
response_msg = "Successfully deleted the user and associated agent, "
response_msg += "learner/faculty but could not delete Inspace User"
User.delete_by_uuid(user_id)
return {
"success": True,
"message": response_msg
}
except ResourceNotFoundException as e:
Logger.error(e)
Logger.error(traceback.print_exc())
raise ResourceNotFound(str(e)) from e
except Exception as e:
Logger.error(e)
Logger.error(traceback.print_exc())
raise InternalServerError(str(e)) from e