in microservices/user_management/src/routes/learner_association_group.py [0:0]
def add_coach_to_learner_association_group(uuid: str,
input_coaches: AddCoachesModel):
"""Add the coach to the learner association group
with the uuid passed in the request body
### Args:
input_coaches: Required body to the add coaches in the learner
association group with the status field
NOTE: Currently we are accepting only single user_id in the coaches array
### Raises:
ResourceNotFoundException: If the group does not exist
Exception: 500 Internal Server Error if something went wrong
### Returns:
AddUserToAssociationGroupResponseModel: learner
association group Object
"""
try:
input_coaches_dict = {**input_coaches.dict()}
# TODO: Currently single coach can be associated
# to the Learner Association Group
if len(input_coaches_dict.get("coaches")) > 1:
raise ValidationError(
"Only one coach can be associated with the "\
"Learner Association Group"
)
existing_association_group = AssociationGroup.find_by_uuid(uuid)
group_fields = existing_association_group.get_fields()
if group_fields.get("association_type") != "learner":
raise ValidationError(
f"AssociationGroup for given uuid: {uuid} is not learner type")
if not isinstance(group_fields["associations"]["coaches"], list):
group_fields["associations"]["coaches"] = []
# FIXME: currently we are allowing to add one
# coach to the learner association group
if len(group_fields["associations"]["coaches"])>=1 and \
len(input_coaches_dict.get("coaches"))>0 and \
input_coaches_dict.get("coaches")[0] == \
group_fields["associations"]["coaches"][0]["coach"]:
raise ValidationError(
f"Leaner association group with uuid {uuid} "\
"already contains the given coach")
if len(group_fields["associations"]["coaches"]) == 1:
raise ValidationError(
f"Leaner association group with uuid {uuid} "\
"already contains coach"
)
user_group = UserGroup.find_by_name("coach")
for input_coach in input_coaches_dict.get("coaches"):
# checking if given coach id is valid and has user_type as coach
user = User.find_by_user_id(input_coach)
coach_fields = user.get_fields(reformat_datetime=True)
if user_group.uuid not in coach_fields["user_groups"]:
raise ValidationError(
f"User for given user_id {input_coach} is not of coach type")
# FIXME: To add validation wheather that user belongs
# to coach user-group or not
# any function return True if condition satisfy
if not any(coaches["coach"] == input_coach
for coaches in group_fields["associations"]["coaches"]):
group_fields["associations"]["coaches"].append({
"coach": input_coach,
"status": input_coaches_dict.get("status")
})
for key, value in group_fields.items():
setattr(existing_association_group, key, value)
# Update the data once all the incoming users validate
existing_association_group.update()
group_fields = existing_association_group.get_fields(reformat_datetime=True)
return {
"success": True,
"message": "Successfully added the coaches "\
"to the learner association group",
"data": group_fields
}
except ValidationError as e:
raise BadRequest(str(e)) from e
except ResourceNotFoundException as e:
raise ResourceNotFound(str(e)) from e
except Exception as e:
raise InternalServerError(str(e)) from e