def get_users_assigned_to_discipline()

in microservices/user_management/src/routes/discipline_association_group.py [0:0]


def get_users_assigned_to_discipline(
                  curriculum_pathway_id: str,
                  user_type: Optional[ALLOWED_FILTER_BY_TYPE_VALUES] = None,
                  fetch_tree: Optional[bool] = False):
  """The get users endpoint will fetch users from disicpline association group
     from firestore where provided discipline is associated

  ### Args:
      user_uuid (str): Unique id of the discipline for which users
                             are to be fetched
      user_type (str): Filter by user type(instructor/assessor)
      fetch_tree (bool): To fetch the entire object
                        instead of the UUID of the object

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

  ### Returns:
      UsersAssociatedToDisciplineResponseModel: Array of User Object
  """
  try:
    curriculum_pathway = CurriculumPathway.find_by_uuid(curriculum_pathway_id)
    if curriculum_pathway.alias != "discipline":
      raise ValidationError(
        f"Given curriculum pathway id {curriculum_pathway_id} "
        "is not of discipline type")

    collection_manager = AssociationGroup.collection.filter(
                              "association_type", "==", "discipline")

    groups = collection_manager.order("-created_time").fetch()

    association_groups = [i.get_fields(reformat_datetime=True) for i in groups]

    discipline_group = None
    for group in association_groups:
      for curriculum_pathway_dict in group.get("associations", {}).get(
        "curriculum_pathways", []):
        if isinstance(curriculum_pathway_dict, dict) and \
          curriculum_pathway_id == curriculum_pathway_dict.get(
            "curriculum_pathway_id") and curriculum_pathway_dict.get(
            "status") == "active":
          discipline_group = group
          break

    if discipline_group:
      if user_type is not None:
        users = [user["user"] for user in discipline_group["users"]
                if user["user_type"] == user_type and
                user["status"] == "active"]
      else:
        users = [user["user"] for user in discipline_group["users"]
                 if user["status"] == "active"]

    else:
      raise ValidationError(
        f"Given curriculum pathway id {curriculum_pathway_id} "
        "is not actively associated in any discipline association group")

    if fetch_tree:
      users = [CollectionHandler.get_document_from_collection("users", user) \
                for user in users]

    return {
        "success": True,
        "message": "Successfully fetched the users",
        "data": users
    }
  except ResourceNotFoundException as e:
    Logger.info(traceback.print_exc())
    raise ResourceNotFound(str(e)) from e
  except ValidationError as e:
    Logger.info(traceback.print_exc())
    raise BadRequest(str(e)) from e
  except Exception as e:
    Logger.info(traceback.print_exc())
    raise InternalServerError(str(e)) from e