def unassign_user(self, data): validate_required_fields(data, ["user_id"]) current_user = self.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) self._validate_user(data["user_id"]) self.user_repository.unassign_user(data["user_id"], current_user.id)
def get_all_assigned_users(self): current_user = self.user_repository.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) assigned_users = self.user_repository.get_all_assigned_users_by_admin_id( current_user.id) return assigned_users
def delete_goal_by_id(self, goal_id): current_user = self.user_repository.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) self._validate_goal_id(goal_id) goal = self.goal_repository.get_goal_by_id(goal_id) self._validate_user_assignment(goal.user_id, current_user.id) self.goal_repository.delete_goal_by_id(goal_id)
def register_user(self, data): validate_required_fields(data, ["id", "username", "name", "password"]) current_user = self.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) new_user = User(data["id"], data["username"], data["name"], hash_password(data["password"]), is_admin=False) self.user_repository.save_user(new_user)
def update_user_profile(self, id, data): validate_required_fields(data, ["username", "name", "password"]) current_user = self.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) self._validate_user(id) user = User(id, data["username"], data["name"], hash_password(data["password"]), is_admin=False) self.user_repository.save_user(user)
def save_task(self, data): current_user = self.user_repository.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) validate_required_fields( data, ["id", "title", "description", "hint", "goal_id"]) self._validate_goal_id(data["goal_id"]) self._validate_goal_access(data["goal_id"], current_user) task = Task(data["id"], data["title"], data["description"], data["hint"], data["goal_id"]) self.goal_repository.save_task(task)
def save_goal(self, data): current_user = self.user_repository.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) validate_required_fields( data, ["id", "date", "title", "category", "status", "user_id"]) validate_date_format(data["date"]) validate_required_range_of_values({"status": data["status"]}, [0, 1]) self._validate_user(data["user_id"]) self._validate_user_assignment(data["user_id"], current_user.id) goal = Goal(data["id"], data["date"], data["title"], data["category"], data["status"], data["user_id"]) self.goal_repository.save_goal(goal)
def get_goals_by_date_and_assigned_user_id(self, date, user_id): current_user = self.user_repository.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) validate_date_format(date) self._validate_user(user_id) self._validate_user_assignment(user_id, current_user.id) all_goals = self.goal_repository.get_all_goals_by_user_id(user_id) result = [] for goal in all_goals: if goal.date == date: result.append(goal) return result
def get_progress_by_user_id(self, user_id): current_user = self.get_current_user() validate_user_authentication(current_user) validate_admin_role(current_user) self._validate_user(user_id) self._validate_user_assignment(user_id, current_user.id) all_goals = self.goal_repository.get_all_goals_by_user_id(user_id) progress_by_category = {} for goal in all_goals: current_value = progress_by_category.get( goal.category, (0, 0)) progress_by_category[goal.category] = ( current_value[0] + goal.status, current_value[1] + 1) return {key: {"completed": value[0], "total": value[1]} for key, value in progress_by_category.items()}