Skip to content

Instantly share code, notes, and snippets.

@bhfailor
Created March 28, 2014 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhfailor/9842576 to your computer and use it in GitHub Desktop.
Save bhfailor/9842576 to your computer and use it in GitHub Desktop.
rake tasks for season pass events and teams
namespace :event do
desc "Add bib #s to event registrations"
task :add_bib => :environment do
event = Event.find(ENV['EVENT'])
resource_file_name = File.join(Rails.root, "lib", "tasks", "event_files", "#{ENV['EVENT']}.csv")
if event.nil?
puts "Unable to find event with id: #{ENV['EVENT']}"
return
elsif !File.exists?(resource_file_name)
puts "Unable to locate csv file"
return
else
puts "*** Adding bib numbers to event registrations for #{event.name}."
CSV.foreach(resource_file_name, :headers => true) do |row|
reg_id = row['Reg. ID']
bib = row['Bib']
if reg_id.blank? or bib.blank?
next
end
begin
reg = EventRegistration.find(reg_id)
rescue Exception => ex
puts "Cannot find registration with id = #{reg_id}, skipping."
next
end
# skip entry if the first or last name is missing, must be manually reviewed
if reg.first_name.blank? or reg.last_name.blank? or row['First Name'].blank? or row['Last Name'].blank?
puts "*REVIEW* - Missing first or last name, please manually verify reg_id = #{reg_id}."
next
end
if reg.first_name.downcase.strip != row['First Name'].downcase.strip
puts "*REVIEW* - First name mismatch for reg_id = #{reg_id}"
next
elsif reg.last_name.downcase.strip != row['Last Name'].downcase.strip
puts "*REVIEW* - Last name mistmach for reg_id = #{reg_id}"
next
end
reg.bib_number = bib
if reg.save(:validate => false)
puts "Successfully updated #{reg.first_name} #{reg.last_name} (#{reg_id}) with bib ##{bib}"
else
puts "*REVIEW* - Unable to save registration for #{reg.first_name} #{reg.last_name} (#{reg_id}) with bib ##{bib}"
end
end
puts "Finished updating bib numbers for #{event.name}."
end
end
desc "Get email addresses from previous RACE events."
task :get_email => :environment do
email_hash = {}
csv_data = ''
[9765, 10362, 10363, 10364, 10365, 12106, 13772, 13770, 13944].each do |id|
event = Event.find(id)
event.event_registrations.each do |reg|
begin
user = RmUser.find(reg.rm_user_id)
email = user.default_email || reg.email
email = email.try(:downcase)
email_hash[reg.rm_user_id] = email
rescue Exception => ex
puts "Unable to find RM user with id #{reg.rm_user_id}"
end
end
end
csv_data = CSV.generate do |csv|
csv << ["id", "First Name", "Last Name", "Email", "Link"]
email_hash.each do |k,v|
begin
user = RmUser.find(k)
email_link = "https://www.racemenu.com/register/registration_type?event=13991-Super-Sunday&invite=#{k}"
csv << [k, user.first_name, user.last_name, v, email_link]
rescue Exception => ex
puts "Unable to find RM user with id #{k}"
end
end
end
RNotifier.monthly_registration_data_email("November", csv_data).deliver
end
desc "Custom report for 2012 registrations"
task :all_regs => :environment do
registration_data = InternalDataGenerator.export_reg(1)
RNotifier.monthly_registration_data_email("2012", registration_data).deliver
end
desc "adjust times for events affected by time zone change (12/4/12 through 1/24/13)"
task :fix_event_times => :environment do
(14601..21011).step(10).each do |eid|
# skip all manually updated events created after 12/4/12
next if [17821,17831,17841,17851,14721,14681,17181,17191,17881,17891,19061,19161,21021,12154,21031,21081,21201,21211,21221,21231,21241,21251,19271].include?(eid)
begin
event = Event.find(eid)
# determine whether or not daylight savings is in place and set dst variable appropriately
date = event.event_date
if date < Date.new(2013,3,11)
dst = 5
elsif date < Date.new(2013,11,3)
dst = 4
elsif date < Date.new(2014,3,9)
dst = 5
else
dst = 4
end
begin
event.event_date = event.event_date - dst.hours
rescue
puts "event #{eid} does not have a start time."
end
begin
event.end_time = event.end_time - dst.hours
rescue
event.end_time = event.event_date
puts "event #{eid} does not have an end time using start time."
end
# save event
event.save(:validate => false)
#update sub-events for the event
event.races.each do |race|
race.start_time = race.start_time - dst.hours
race.save(:validate => false)
end
puts "updated event #{eid} successfully."
rescue Exception => ex
puts "Unable to find event with event id = #{eid}."
puts ex.message
puts ex.backtrace.join("\n")
end
end
end
desc "Set end_time for events that do not currently have them (prevents crash on event detail page)"
task :set_end_time => :environment do
Event.find(:all, conditions: 'end_time IS NULL').map do |event|
event.update_attribute(:end_time, event.event_date)
end
end
desc "Convert old team captains to new team captain structure"
task :convert_captains => :environment do
Team.find(:all).each do |team|
EventRegistration.find(:all, conditions: ['rm_user_id = ? AND event_id = ?', team.race_director_id, team.event_id]).each do |reg|
begin
if reg.race.supports_team_creation?
team.update_attribute(:captain_registration_id, reg.id)
break
end
rescue Exception => ex
puts "*** unable to find race for registration #{reg.id} ***"
end
end
end
end
desc "Update team_id's for season pass events"
# the team_id for event registrations was (and is still) not being properly saved for season pass registrations. team captains
# have a team_id of nil and team members have a team_id which corresponds to the team for the first event in the season pass group
# the task below will update all event registrations so the team_id matches the team associated with the team_entry since it's correct
#
# Summer Series - rake event:update_teams FIRST_EVENT=21201 ADDITIONAL_EVENTS=21211,21221,21231,21241,21251
# C5K - rake event:update_teams FIRST_EVENT=17821 ADDITIONAL_EVENTS=17831,17841,17851
task :update_teams => :environment do
initial_event_id = ENV['FIRST_EVENT']
event_array = ENV['ADDITIONAL_EVENTS'].split(',')
if initial_event_id.blank? || event_array.blank?
puts "*** expecting FIRST_EVENT (single event id) and ADDITIONAL_EVENTS (comma separated list) ***"
return
end
init_event = Event.find(initial_event_id)
init_event.teams.each do |team|
team_name = team.name
event_array.each do |additional_event_id|
additional_team = Team.find_by_name_and_event_id(team.name,additional_event_id)
if additional_team.present?
additional_team.team_entries.map do |entry|
reg = entry.event_registration
if reg.team_id.present?
old_team = "#{reg.team.name} (#{reg.team_id}) for event #{reg.team.event.name} (#{reg.team.event.id})"
else
old_team = 'nil'
end
puts "Reg Event: #{reg.event_id}, Reg: #{reg.id} | Old team: #{old_team} | New team: #{entry.team_id}"
reg.team_id = entry.team_id
reg.save(validate: false)
end
end
end
end
end
desc "Create missing team_entries for season pass events"
# some registrations are missing a team_entry and it must be manually created
#
# Summer Series - rake event:create_team_entries EVENTS=21201,21211,21221,21231,21241,21251
# C5K - rake event:create_team_entries EVENTS=17821,17831,17841,17851
task :create_team_entries => :environment do
event_array = ENV['EVENTS'].split(',')
if event_array.blank?
puts "*** expecting EVENTS (comma separated list) ***"
return
end
puts "\n"
event_array.map do |eid|
event = Event.find(eid)
event.event_registrations.map do |reg|
if reg.team_entry.present? || !reg.processed?
next
elsif reg.team_id.blank?
puts "Unable to find team entry or team_id for registration #{reg.id} - #{reg.club_team}"
puts "---------------------------------------------"
else
team = Team.find_by_name_and_event_id(reg.team.name, reg.event_id)
if team.present?
reg_name = "#{reg.first_name} #{reg.last_name}"
puts "#{reg_name}: #{reg.id} - #{reg.event_id} has no team entry."
puts "Club/Team: #{reg.club_team}, Team: #{reg.team.name} #{reg.team_id} for #{reg.team.event_id}"
puts "Found team #{team.name} #{team.id} for #{team.event_id}"
puts "---------------------------------------------"
# if the team for this registration belongs to another event update to the correct team_id
if reg.team.event_id != reg.event_id
reg.team_id = team.id
reg.save(validate: false)
end
# create the team entry
TeamEntry.create(team_id: team.id, event_registration_id: reg.id)
else
puts "Team #{reg.team.name} does not exist for #{reg.event.name} - #{reg.event_id}"
puts "---------------------------------------------"
end
end
end
end
end
desc 'load event registrations from an external source (ie: RD switching to RaceMenu from another registration site)'
task :load_external_registrations => :environment do
event = Event.find(ENV['EVENT'])
resource_file_name = File.join(Rails.root, "lib", "tasks", "event_files", "#{ENV['EVENT']}_reg.csv")
if event.nil?
puts "Unable to find event with id: #{ENV['EVENT']}"
return
elsif !File.exists?(resource_file_name)
puts "Unable to locate csv file"
return
else
puts "*** Creating manual registrations for #{event.name}."
CSV.foreach(resource_file_name, :headers => true) do |row|
manual_race = Race.find(row['Race'])
begin
birthday = row['Birthday'].to_date
rescue
birthday = nil
end
@reg = EventRegistration.new(event_id: event.id,
race_id: manual_race.id,
first_name: row['First Name'],
last_name: row['Last Name'],
birthday: birthday,
age: row['Age'],
sex: row['Gender'],
email: row['Email'],
phone: row['Phone'],
address: row['Address'],
city: row['City'],
state: row['State'],
zip: row['Zip'])
@reg.is_manual = true
# set team if present
if row['team_name'].present?
if row['team_name'].blank?
team = Team.find(ENV['DEFAULT_TEAM'])
else
team = Team.find_by_event_id_and_name(event.id, row['team_name'])
end
else
team = Team.find(ENV['DEFAULT_TEAM']) if ENV['DEFAULT_TEAM'].present?
end
@reg.team_id = team.id if team.present?
@reg.cost = if row['race_cost'].present?
row['race_cost'].to_f
else
manual_race.current_fee(Time.now)
end
if event.is_contact_mandatory
@reg.em_con_name = row['em_con_name'] || "N/A"
@reg.em_con_phone = row['em_con_phone'] || "N/A"
@reg.em_con_relationship = row['em_con_relationship'] || "N/A"
end
@reg.processed = true
if @reg.save
# create team entry and update processed member count
if @reg.team_id.present?
team_entry = TeamEntry.new(team_id: @reg.team_id, team_order: nil, approved: false, event_registration_id: @reg.id)
if team_entry.save
team_entry.team.update_processed_members_counter!
end
end
if ENV['SHIRT_Q'].present?
question = Question.find(ENV['SHIRT_Q'])
response = question.question_options.find_by_title(row['Shirt'])
if response.present?
Answer.create(question_id: question.id, question_option_id: response.id, event_registration_id: @reg.id, extra_info: nil)
end
end
if ENV['HEAT_Q'].present?
question = Question.find(ENV['HEAT_Q'])
response = question.question_options.find_by_title(row['heat'])
if response.present?
Answer.create(question_id: question.id, question_option_id: response.id, event_registration_id: @reg.id, extra_info: nil)
end
end
if ENV['MAKEUP_Q'].present?
question = Question.find(ENV['MAKEUP_Q'])
response = question.question_options.find_by_title(row['makeup'])
if response.present?
Answer.create(question_id: question.id, question_option_id: response.id, event_registration_id: @reg.id, extra_info: nil)
end
end
if ENV['HEAR_ABOUT_Q'].present?
question = Question.find(ENV['HEAR_ABOUT_Q'])
if question.present?
Answer.create(question_id: question.id, question_option_id: nil, event_registration_id: @reg.id, extra_info: row['hear_about'])
end
end
if ENV['AGE_CONFIRM_Q'].present?
question = Question.find(ENV['AGE_CONFIRM_Q'])
response = question.question_options.first
if response.present?
Answer.create(question_id: question.id, question_option_id: response.id, event_registration_id: @reg.id, extra_info: nil)
end
end
if ENV['MORE_INFO_Q'].present?
question = Question.find(ENV['MORE_INFO_Q'])
response = question.question_options.find_by_title(row['more_info'])
if response.present?
Answer.create(question_id: question.id, question_option_id: response.id, event_registration_id: @reg.id, extra_info: nil)
end
end
else
puts "Error saving reg #{@reg.to_label}: #{@reg.errors.full_messages}"
end # saved registration
end # reading CSV
end # creating manual registrations
end # load_external_registrations
desc 'Convert Fee Structures to new Display Fee implementation'
task :convert_fee_structures => :environment do
fee_structures = FeeStructure.find(:all, conditions: 'include_service_fee = true')
fee_structures.map do |fee_structure|
event = fee_structure.event
#if event && event.event_date > Date.today
if event.present?
puts "Updating #{event.id} - #{event.name}"
event.update_attribute(:display_fees, false)
# Update sub-events
event.races.map do |sub_event|
if sub_event.initial_fee && sub_event.initial_fee > 0
existing_svc_fee = FeeCalculator.service_fee(sub_event.initial_fee, true)
new_fee = sub_event.initial_fee - existing_svc_fee
sub_event.initial_fee = new_fee
sub_event.save(validate: false)
end
sub_event.race_fee_changes.map do |fee_change|
existing_svc_fee = FeeCalculator.service_fee(fee_change.fee, true)
new_fee = fee_change.fee - existing_svc_fee
fee_change.update_attribute(:fee, new_fee)
end
end
else
puts "Skipping fee structure #{fee_structure.id} which does not have an event."
end
end
end
end
@bhfailor
Copy link
Author

173 and 212 are the beginning lines for tasks that are concerned with season passes. There must be something more in the application itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment