Skip to content

Commit cb8207c

Browse files
committed
fetch commit
1 parent 71d3b75 commit cb8207c

File tree

4 files changed

+39
-52
lines changed

4 files changed

+39
-52
lines changed

app/controllers/teams_controller.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,30 @@ def members
3939
end
4040

4141
# POST /teams/:id/members
42-
# Adds a new member to the team unless it's already full
42+
# Adds a new member to the team.
4343
def add_member
44-
return render json: { errors: ['Team is full'] }, status: :unprocessable_entity if @team.full?
45-
44+
# Find the user specified in the request.
4645
user = User.find(team_participant_params[:user_id])
47-
participant = Participant.find_by(user: user, parent_id: @team.parent_id)
4846

47+
# Determine the correct type of participant (Assignment or Course) based on the team type.
48+
participant_class = @team.is_a?(AssignmentTeam) ? AssignmentParticipant : CourseParticipant
49+
50+
# Find the specific participant record for this user in the team's context.
51+
participant = participant_class.find_by(user_id: user.id, parent_id: @team.parent_id)
52+
53+
# If no participant record exists, the user isn't part of the assignment/course.
4954
unless participant
50-
return render json: { error: 'Participant not found for this team context' }, status: :not_found
55+
return render json: { errors: ["#{user.name} is not a participant in this context."] }, status: :unprocessable_entity
5156
end
5257

53-
teams_participants = @team.teams_participants.build(participant: participant, user: participant.user)
58+
# Delegate the add operation to the Team model with the found participant.
59+
result = @team.add_member(participant)
5460

55-
if teams_participants.save
56-
render json: participant.user, serializer: UserSerializer, status: :created
61+
if result[:success]
62+
render json: user, serializer: UserSerializer, status: :created
5763
else
58-
render json: { errors: teams_participants.errors.full_messages }, status: :unprocessable_entity
64+
render json: { errors: [result[:error]] }, status: :unprocessable_entity
5965
end
60-
rescue ActiveRecord::RecordNotFound
61-
render json: { error: 'User not found' }, status: :not_found
6266
end
6367

6468
# DELETE /teams/:id/members/:user_id

app/models/mentored_team.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ def mentor
1414
alias_method :mentor_user, :mentor
1515

1616
# Adds members to the team who are not mentors
17-
def add_member(user)
18-
participant = assignment.participants.find_by(user_id: user.id)
19-
return false if participant&.duty&.name&.downcase&.include?('mentor')
20-
21-
res = super(user)
22-
if res.is_a?(Hash)
23-
res[:success]
24-
else
25-
!!res
17+
def add_member(participant)
18+
# Fail fast if the participant's duty is 'mentor'.
19+
if participant.duty&.name&.downcase&.include?('mentor')
20+
return { success: false, error: 'Mentors cannot be added as regular members.' }
2621
end
22+
23+
# If not a mentor, proceed with the standard add_member logic.
24+
super(participant)
2725
end
2826

2927
# Assigning a user as mentor of the team
@@ -44,7 +42,7 @@ def assign_mentor(user)
4442

4543
participant.save!
4644

47-
if participant.duty != duty
45+
unless participant.duty == duty
4846
participant.duty = duty
4947
participant.save!
5048
end

app/models/team.rb

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,21 @@ def participant_on_team?(participant)
4545
scope.teams.any? { |team| team.participants.include?(participant) }
4646
end
4747

48-
# Adds participant in the team
49-
def add_member(participant_or_user)
50-
participant =
51-
case participant_or_user
52-
when AssignmentParticipant, CourseParticipant
53-
participant_or_user
54-
when User
55-
participant_type = is_a?(AssignmentTeam) ? AssignmentParticipant : CourseParticipant
56-
participant_type.find_by(user_id: participant_or_user.id, parent_id: parent_id)
57-
else
58-
nil
59-
end
60-
61-
return { success: false, error: "#{participant_or_user.name} is not a participant in this #{is_a?(AssignmentTeam) ? 'assignment' : 'course'}" } if participant.nil?
62-
return { success: false, error: "Participant already on the team" } if participants.exists?(id: participant.id)
63-
return { success: false, error: "Unable to add participant: team is at full capacity." } if full?
64-
65-
team_participant = TeamsParticipant.create(
66-
participant_id: participant.id,
67-
team_id: id,
68-
user_id: participant.user_id
69-
)
70-
71-
if team_participant.persisted?
72-
{ success: true }
73-
else
74-
{ success: false, error: team_participant.errors.full_messages.join(', ') }
75-
end
76-
rescue StandardError => e
77-
{ success: false, error: e.message }
48+
# Adds a participant to the team.
49+
# This method now expects a Participant object directly.
50+
def add_member(participant)
51+
# Fail fast if the team is already full.
52+
return { success: false, error: "Team is at full capacity." } if full?
53+
54+
# Check if this participant is already on a team in this context.
55+
return { success: false, error: "Participant is already on a team for this context." } if participant_on_team?(participant)
56+
57+
# Use create! to add the participant to the team.
58+
teams_participants.create!(participant: participant, user: participant.user)
59+
{ success: true }
60+
rescue ActiveRecord::RecordInvalid => e
61+
# Catch potential validation errors from TeamsParticipant.
62+
{ success: false, error: e.record.errors.full_messages.join(', ') }
7863
end
7964

8065
# Determines whether a given participant is eligible to join the team.

config/database.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ default: &default
1313
adapter: mysql2
1414
encoding: utf8mb4
1515
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
16-
username: dev
16+
username: root
1717
password: expertiza
1818
host: <%= ENV.fetch("DB_HOST", "localhost") %>
1919
port: <%= ENV.fetch("DB_PORT", "3306") %>

0 commit comments

Comments
 (0)