Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed schema, and added create/delete waitlist #4

Merged
merged 1 commit into from Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/models/sign_up_sheet.rb
Expand Up @@ -79,6 +79,11 @@ def self.create_SignUpTeam(assignment_id, sign_up, topic_id, user_id)
ExpertizaLogger.info LoggerMessage.new('SignUpSheet', user_id, "Sign up sheet created with teamId #{team_id}")
else
sign_up.is_waitlisted = true
team_id = TeamsUser.team_id(assignment_id, user_id)
unless WaitlistTeam.add_team_to_topic_waitlist(team_id, topic_id, user_id)
# flash[:error] = "Error occured while waitlisting to topic #{topic_id}"
raise ActiveRecord::Rollback
end
end
[team_id, topic_id]
end
Expand Down
1 change: 1 addition & 0 deletions app/models/sign_up_topic.rb
Expand Up @@ -98,6 +98,7 @@ def self.reassign_topic(session_user_id, assignment_id, topic_id)
end
end
end
WaitlistTeam.remove_team_from_topic_waitlist(users_team[0].t_id, topic_id, session_user_id)
signup_record.destroy unless signup_record.nil?
ExpertizaLogger.info LoggerMessage.new('SignUpTopic', session_user_id, "Topic dropped: #{topic_id}")
else
Expand Down
26 changes: 26 additions & 0 deletions app/models/waitlist_team.rb
Expand Up @@ -3,6 +3,32 @@ class WaitlistTeam < ApplicationRecord
belongs_to :team, class_name: 'Team'

validates :topic_id, :team_id, presence: true
validates :topic_id, uniqueness: { scope: :team_id }
scope :by_team_id, ->(team_id) { where('team_id = ?', team_id) }
scope :by_topic_id, ->(topic_id) { where('topic_id = ?', topic_id) }

def self.add_team_to_topic_waitlist(team_id, topic_id, user_id)
new_waitlist = WaitlistTeam.new
new_waitlist.topic_id = topic_id
new_waitlist.team_id = team_id
if new_waitlist.valid?
WaitlistTeam.create(topic_id: topic_id, team_id: team_id)
else
ExpertizaLogger.info LoggerMessage.new('WaitlistTeam', user_id, "Team #{team_id} cannot be added to waitlist for the topic #{topic_id}")
ExpertizaLogger.info LoggerMessage.new('WaitlistTeam', user_id, new_waitlist.errors.full_messages.join(" "))
return false
end
return true
end

def self.remove_team_from_topic_waitlist(team_id, topic_id, user_id)
waitlisted_team_topic = WaitlistTeam.find_by(topic_id: topic_id, team_id: team_id)
unless waitlisted_team_topic.nil?
waitlisted_team_topic.destroy
else
ExpertizaLogger.info LoggerMessage.new('WaitlistTeam', user_id, "Cannot find Team #{team_id} in waitlist for the topic #{topic_id} to be deleted.")
end
return true
end

end
7 changes: 4 additions & 3 deletions db/migrate/20220420015915_create_waitlist_teams.rb
@@ -1,10 +1,10 @@
class CreateWaitlistTeams < ActiveRecord::Migration[5.1]

def self.up
create_table 'waitlist_teams', id: false do |t|
create_table 'waitlist_teams' do |t|
t.column 'team_id', :integer
t.column 'topic_id', :integer
t.timestamps
t.column 'created_at', :datetime
end

add_index 'waitlist_teams', ['team_id'], name: 'fk_waitlist_teams'
Expand All @@ -18,8 +18,9 @@ def self.up
execute "alter table waitlist_teams
add constraint fk_waitlist_teams_sign_up_topics
foreign key (topic_id) references sign_up_topics(id)"

add_index 'waitlist_teams', ["team_id", "topic_id"], unique: true

execute "ALTER TABLE waitlist_teams ADD PRIMARY KEY (team_id, topic_id);"
end

def self.down
Expand Down