def create_user()

in components/user_management/src/routes/user.py [0:0]


def create_user(input_user: UserModel, request: Request,
                create_inspace_user: Optional[bool] = False):
  """The create user endpoint will add the user in request body to the
  firestore.
  This endpoint also creates corresponding learner, learner_profile, agent
  and staff document based on the user_type or the user_group in which the
  user that is to be created is assigned.

  ### Args:
      input_user (UserModel): input user to be inserted
      create_inspace_user (bool): To create inspace user after
              GP-Core user creation

  ### Raises:
      Exception: 500 Internal Server Error if something went wrong

  ### Returns:
      PostUserResponseModel: User Object
  """
  try:
    if not is_inspace_enabled(create_inspace_user):
      raise ValidationError("Inspace is disabled, please make "
                            "'create_inspace_user' as false in "
                            "the API request body")
    existing_user = User.find_by_email(input_user.email)
    if existing_user is not None:
      raise ConflictError(
          f"User with the given email: {input_user.email} "
          "already exists")
    input_user_dict = {**input_user.dict(),
                       "create_inspace_user": create_inspace_user}
    headers = {"Authorization": request.headers.get("Authorization")}
    user_id = add_user_to_db(headers, input_user_dict)
    new_user = User.find_by_id(user_id)

    response_message = "Successfully created User and corresponding agent"

    # ---- Inspace User creation ----
    if create_inspace_user:
      if create_inspace_user_helper(new_user):
        response_message = ("Successfully created new User, "
                            "corresponding agent and Inspace User")
      else:
        response_message = ("Successfully created User and "
                            "corresponding agent. Cannot create "
                            "Inspace User for current User")

      # fetch and return updated user
      new_user = User.find_by_id(user_id)
      user_fields = new_user.get_fields(reformat_datetime=True)
    else:
      user_fields = new_user.get_fields(reformat_datetime=True)

    return {
      "success": True,
      "message": response_message,
      "data": user_fields
    }
  except ConflictError as e:
    Logger.error(e)
    Logger.error(traceback.print_exc())
    raise Conflict(str(e)) from e
  except ValidationError as e:
    Logger.error(e)
    Logger.error(traceback.print_exc())
    raise BadRequest(str(e)) from e
  except Exception as e:
    Logger.error(e)
    Logger.error(traceback.print_exc())
    raise InternalServerError(str(e)) from e