@@ -42,45 +42,31 @@ def members
4242 # POST /teams/:id/members
4343 # Adds a new member to the team.
4444 def add_member
45- # Find the user specified in the request.
4645 user = User . find ( team_participant_params [ :user_id ] )
47-
48- # Determine the correct type of participant (Assignment or Course) based on the team type.
49- participant_class = @team . is_a? ( AssignmentTeam ) ? AssignmentParticipant : CourseParticipant
50-
51- # Find the specific participant record for this user in the team's context.
52- participant = participant_class . find_by ( user_id : user . id , parent_id : @team . parent_id )
53-
54- # If no participant record exists, the user isn't part of the assignment/course.
55- unless participant
56- return render json : { errors : [ "#{ user . name } is not a participant in this context." ] } , status : :unprocessable_entity
57- end
58-
59- # Delegate the add operation to the Team model with the found participant.
60- result = @team . add_member ( participant )
46+ result = @team . add_member ( user )
6147
6248 if result [ :success ]
6349 render json : user , serializer : UserSerializer , status : :created
6450 else
6551 render json : { errors : [ result [ :error ] ] } , status : :unprocessable_entity
6652 end
53+ rescue ActiveRecord ::RecordNotFound
54+ render json : { errors : [ 'User not found' ] } , status : :not_found
6755 end
6856
6957 # DELETE /teams/:id/members/:user_id
7058 # Removes a member from the team based on user ID
7159 def remove_member
7260 user = User . find ( params [ :user_id ] )
73- participant = Participant . find_by ( user : user , parent_id : @team . parent_id )
74- tp = @team . teams_participants . find_by ( participant : participant )
61+ result = @team . remove_member ( user )
7562
76- if tp
77- tp . destroy
63+ if result [ :success ]
7864 head :no_content
7965 else
80- render json : { error : 'Member not found' } , status : :not_found
66+ render json : { errors : [ result [ :error ] ] } , status : :not_found
8167 end
8268 rescue ActiveRecord ::RecordNotFound
83- render json : { error : 'User not found' } , status : :not_found
69+ render json : { errors : [ 'User not found' ] } , status : :not_found
8470 end
8571
8672 # Placeholder method to get current user (can be replaced by actual auth logic)
@@ -99,7 +85,7 @@ def set_team
9985
10086 # Whitelists the parameters allowed for team creation/updation
10187 def team_params
102- params . require ( :team ) . permit ( :name , :max_team_size , :type , :assignment_id )
88+ params . require ( :team ) . permit ( :name , :max_team_size , :type , :assignment_id , :course_id , :parent_id )
10389 end
10490
10591 # Whitelists parameters required to add a team member
@@ -109,10 +95,14 @@ def team_participant_params
10995
11096 # Validates the team type before team creation to ensure it's among allowed types
11197 def validate_team_type
112- return unless params [ :team ] && params [ :team ] [ :type ]
113- valid_types = [ 'CourseTeam' , 'AssignmentTeam' , 'MentoredTeam' ]
114- unless valid_types . include? ( params [ :team ] [ :type ] )
115- render json : { error : 'Invalid team type' } , status : :unprocessable_entity
98+ # Use .dig to safely access nested params.
99+ # Check that the type is present AND included in the list.
100+ team_type = params . dig ( :team , :type )
101+
102+ unless team_type . in? ( [ 'CourseTeam' , 'AssignmentTeam' , 'MentoredTeam' ] )
103+ # If type is nil or not in the list, render an error and stop execution.
104+ render json : { errors : [ "Invalid or missing team type. Must be one of: CourseTeam, AssignmentTeam, MentoredTeam." ] } ,
105+ status : :unprocessable_entity
116106 end
117107 end
118108end
0 commit comments