def update_learner_association_status()

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


def update_learner_association_status(
                  uuid: str,
                  input_association_status:UpdateLearnerAssociationStatusModel):
  """Update status of users or associations provided in request body
  in learner association group for uuid given as path param.

  ### Args:
      uuid (str): Unique id of the association group
      request_body: Required body having user/association uuid and status
        that needs to be updated

      NOTE: User field will contain user_id for learner type of users only.

  ### Raises:
      ResourceNotFoundException: If the group does not exist
      Exception: 500 Internal Server Error if something went wrong

  ### Returns:
      UpdateLearnerAssociationGroupResponseModel: learner association group
        Object
  """
  try:
    existing_association_group = AssociationGroup.find_by_uuid(uuid)
    group_fields = existing_association_group.get_fields()

    if not is_learner_association_group(group_fields):
      raise ValidationError(f"AssociationGroup for given uuid: {uuid} "
        "is not learner type")

    input_dict = {**input_association_status.dict()}
    for key, val in input_dict.items():
      if key == "user" and val:
        is_user_present = False
        for user in group_fields.get("users"):
          if val.get("user_id") == user.get("user"):
            is_user_present = True
            if val.get("status") != user.get("status"):
              user["status"] = val["status"]
        if not is_user_present:
          raise ValidationError("User for given user_id is not present "
                                "in the learner association group")

      if key == "coach" and val:
        is_coach_present = False
        for coach in group_fields.get("associations").get("coaches"):
          if val.get("coach_id") == coach.get("coach"):
            is_coach_present = True
            if val.get("status") != coach.get("status"):
              coach["status"] = val["status"]

        if not is_coach_present:
          raise ValidationError("Coach for given coach_id is not present "
                                "in the learner association group")

      if key == "instructor" and val:
        is_instructor_present = False
        for instructor in group_fields.get("associations").get("instructors"):
          if val.get("instructor_id") == instructor.get("instructor"):
            is_instructor_present = True
            if val.get("status") == "active":
              # Fetch all discipline association groups
              discipline_groups = AssociationGroup.collection.filter(
                            "association_type", "==", "discipline").fetch()
              discipline_group_fields = [i.get_fields() for i in \
                                         discipline_groups]
              if check_instructor_discipline_association(val["instructor_id"],
                                                val["curriculum_pathway_id"],
                                                discipline_group_fields):
                instructor["status"] = val["status"]
              else:
                instructor_id = val["instructor_id"]
                curriculum_pathway_id = val["curriculum_pathway_id"]
                raise ValidationError("Instructor for given instructor_id " +
                  f"{instructor_id} is not actively associated with the " +
                  f"given curriculum_pathway_id {curriculum_pathway_id} in " +
                  "discipline association group")
            else:
              if val.get("status") != instructor.get("status"):
                instructor["status"] = val["status"]

        if not is_instructor_present:
          raise ValidationError("Instructor for given instructor_id is not "
                                "present in the learner association group")

    for key, value in group_fields.items():
      setattr(existing_association_group, key, value)

    # Update the data
    existing_association_group.update()
    group_fields = existing_association_group.get_fields(
      reformat_datetime=True)

    return {
          "success": True,
          "message": "Successfully updated the association group",
          "data": group_fields
    }

  except ResourceNotFoundException as e:
    raise ResourceNotFound(str(e)) from e
  except ValidationError as e:
    raise BadRequest(str(e)) from e
  except Exception as e:
    raise InternalServerError(str(e)) from e