def patch(org_id, membership_id): # pylint:disable=unused-argument """Update a membership record with new member role.""" token = g.jwt_oidc_token_info role = request.get_json().get('role') membership_status = request.get_json().get('status') notify_user = request.get_json().get('notifyUser') updated_fields_dict = {} origin = request.environ.get('HTTP_ORIGIN', 'localhost') try: if role is not None: updated_role = MembershipService.get_membership_type_by_code(role) updated_fields_dict['membership_type'] = updated_role if membership_status is not None: updated_fields_dict['membership_status'] = \ MembershipService.get_membership_status_by_code(membership_status) membership = MembershipService.find_membership_by_id(membership_id, token) is_own_membership = membership.as_dict()['user']['username'] == \ UserService.find_by_jwt_token(token).as_dict()['username'] if not membership: response, status = {'message': 'The requested membership record could not be found.'}, \ http_status.HTTP_404_NOT_FOUND else: response, status = membership.update_membership(updated_fields=updated_fields_dict, token_info=token ).as_dict(), http_status.HTTP_200_OK # if user status changed to active , mail the user if membership_status == Status.ACTIVE.name: membership.send_notification_to_member(origin, NotificationType.MEMBERSHIP_APPROVED.value) elif notify_user and updated_role and updated_role.code != MEMBER and not is_own_membership: membership.send_notification_to_member(origin, NotificationType.ROLE_CHANGED.value) return response, status except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def delete(org_id, membership_id): # pylint:disable=unused-argument """Mark a membership record as inactive. Membership must match current user token.""" try: membership = MembershipService.find_membership_by_id(membership_id) if membership: response, status = membership.deactivate_membership().as_dict(), \ http_status.HTTP_200_OK else: response, status = {'message': 'The requested membership could not be found.'}, \ http_status.HTTP_404_NOT_FOUND except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status