diff --git a/lib/registrations/helper.rb b/lib/registrations/helper.rb index dfb53a298e..64034cbd66 100644 --- a/lib/registrations/helper.rb +++ b/lib/registrations/helper.rb @@ -18,7 +18,7 @@ module Helper def self.action_type(request, current_user_id) self_updating = request[:user_id].to_i == current_user_id status = request.dig('competing', 'status') - if status == STATUS_DELETED || status == STATUS_CANCELLED + if [STATUS_DELETED, STATUS_CANCELLED].include?(status) return self_updating ? 'Competitor delete' : 'Admin delete' end if status == STATUS_REJECTED diff --git a/lib/registrations/lanes/competing.rb b/lib/registrations/lanes/competing.rb index c24b267e89..123516c7b7 100644 --- a/lib/registrations/lanes/competing.rb +++ b/lib/registrations/lanes/competing.rb @@ -31,10 +31,6 @@ def self.update!(update_params, competition, current_user_id) registration = Registration.find_by(competition_id: competition.id, user_id: user_id) old_status = registration.competing_status - if old_status == Registrations::Helper::STATUS_WAITING_LIST || status == Registrations::Helper::STATUS_WAITING_LIST - waiting_list = competition.waiting_list || competition.waiting_list.build(entries: []) - end - ActiveRecord::Base.transaction do update_status(registration, status) registration.comments = comment if comment.present? @@ -43,8 +39,12 @@ def self.update!(update_params, competition, current_user_id) changes = registration.changes.transform_values { |change| change[1] } + if old_status == Registrations::Helper::STATUS_WAITING_LIST || status == Registrations::Helper::STATUS_WAITING_LIST + waiting_list = competition.waiting_list || competition.create_waiting_list(entries: []) + update_waiting_list(update_params, waiting_list) + end + if waiting_list_position.present? - waiting_list.move_to_position(user_id, waiting_list_position) changes[:waiting_list_position] = waiting_list_position end @@ -61,6 +61,19 @@ def self.update!(update_params, competition, current_user_id) registration.reload end + def self.update_waiting_list(update_params, waiting_list) + user_id = update_params[:user_id] + status = update_params.dig('competing', 'status') + + should_add = status == Registrations::Helper::STATUS_WAITING_LIST + should_remove = !should_add + should_move = update_params[:waiting_list_position].present? + + waiting_list.add(user_id) if should_add + waiting_list.remove(user_id) if should_remove + waiting_list.move_to_position(update_params[:user_id], update_params[:waiting_list_position].to_i) if should_move + end + def self.update_status(registration, status) return unless status.present? @@ -86,8 +99,9 @@ def self.update_status(registration, status) end def self.send_status_change_email(registration, status, user_id, current_user_id) - # TODO: V3-REG Cleanup, at new waiting list email case status + when Registrations::Helper::STATUS_WAITING_LIST + # TODO: V3-REG Cleanup, at new waiting list email when Registrations::Helper::STATUS_PENDING RegistrationsMailer.notify_registrant_of_pending_registration(registration).deliver_later when Registrations::Helper::STATUS_ACCEPTED