Skip to content

Commit

Permalink
fix waiting lists
Browse files Browse the repository at this point in the history
  • Loading branch information
FinnIckler committed Nov 1, 2024
1 parent f81cad7 commit a1c97d1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/registrations/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions lib/registrations/lanes/competing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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

Expand All @@ -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?

Expand All @@ -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
Expand Down

0 comments on commit a1c97d1

Please sign in to comment.