Skip to content

Commit

Permalink
Updating participant_id
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Mar 24, 2024
1 parent a6a05b2 commit e72aa67
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
98 changes: 98 additions & 0 deletions app/models/teams_participant.rb
@@ -0,0 +1,98 @@
class TeamsParticipant < ApplicationRecord
belongs_to :user
belongs_to :participant
belongs_to :team
has_one :team_user_node, foreign_key: 'node_object_id', dependent: :destroy
has_paper_trail

# Enhances readability by providing a clearer method name and leveraging Ruby's conditional expressions
def display_name(ip_address = nil)
display_name = user.name(ip_address)
display_name += ' (Mentor)' if MentorManagement.user_a_mentor?(user)
display_name
end

# Simplifies the delete method while preserving logic
def remove
TeamUserNode.find_by(node_object_id: id)&.destroy
destroy
team.destroy if team.teams_users.empty?
end

def get_team_members(team_id)
# Method body if needed
end

# Class methods for handling team and user associations and queries
class << self
# Streamlines the removal process
def remove_team(user_id, team_id)
find_by(team_id: team_id, user_id: user_id)&.destroy
end

def first_by_team_id(team_id)
where(team_id: team_id).first
end

def team_empty?(team_id)
where(team_id: team_id).blank?
end

def add_member_to_invited_team(invitee_user_id, invited_user_id, assignment_id)
can_add_member = false
where(user_id: invitee_user_id).each do |team|
new_team = AssignmentTeam.find_by(id: team.team_id, parent_id: assignment_id)
can_add_member = new_team.add_member(User.find(invited_user_id), assignment_id) unless new_team.nil?
end
can_add_member
end

def team_id(assignment_id, user_id)
find_team_id_for_user_and_assignment(assignment_id, user_id)
end

def find_by_team_and_user(team_id, user_id)
find_by_team_id_and_user_id_or_participant_id(team_id, user_id)
end

def where_users_and_assignment(user_ids, assignment_id)
participants = Participant.where(user_id: user_ids, parent_id: assignment_id)
where(user_id: user_ids).or(where(participant_id: participants.ids))
end

private

def find_team_id_for_user_and_assignment(assignment_id, user_id)
return nil if assignment_id.nil?

participant_id = Assignment.find(assignment_id).participants.find_by(user_id: user_id).id
where(user_id: user_id).or(where(participant_id: participant_id)).find_each do |team_user|
return team_user.team_id if team_user.team.parent_id == assignment_id
end

nil
end

def find_by_team_id_and_user_id_or_participant_id(team_id, user_id)
team_user = find_by(team_id: team_id, user_id: user_id)
return team_user if team_user.present?

participant_id = find_by_user_and_assignment(user_id, Team.find(team_id).parent_id)&.id
find_by(team_id: team_id, participant_id: participant_id) unless participant_id.nil?
end
end

def user
participant&.user || super
end

def user_id
user&.id || super
end

# Encapsulates the query logic within a helper method
def self.find_by_user_and_assignment(user_id, assignment_id)
Assignment.find(assignment_id)&.participants&.find_by(user_id: user_id)
end
end

@@ -0,0 +1,27 @@
class PopulateParticipantIdInTeamsParticipants < ActiveRecord::Migration[5.1]
def up
TeamsParticipant.find_each do |teams_participant|
next unless Team.exists?(id: teams_participant.team_id)

associated_team = Team.find(teams_participant.team_id)
# Assuming `parent_id` in `teams` table refers to an `assignment_id`
corresponding_assignment_id = associated_team.parent_id

# Locate a participant with matching `user_id` and `parent_id` (assignment)
matching_participant = Participant.find_by(user_id: teams_participant.user_id, parent_id: corresponding_assignment_id)

if matching_participant
# Populate the participant_id in teams_participants with the identified participant's ID
teams_participant.update_column(:participant_id, matching_participant.id)
else
# Actions or logs for cases where no corresponding participant is found
Rails.logger.info "No matching participant found for TeamsParticipant ID: #{teams_participant.id}"
end
end
end

def down
# Reset the participant_id column for all records if the migration is rolled back
TeamsParticipant.update_all(participant_id: nil)
end
end

0 comments on commit e72aa67

Please sign in to comment.