Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Accept non-waiting list leader but send an email #676

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,27 @@ def update_payment_lane(id, iso_amount, currency_iso, status)
end

def update_waiting_list(update_params, waiting_list)
raise ArgumentError.new('Can only accept waiting list leader') if update_params[:status] == 'accepted' && waiting_list_position(waiting_list) != 1
if update_params[:status] == 'accepted' && waiting_list_position(waiting_list) != 1
EmailApi.send_waiting_list_leader_email(
self.competition_id, self.user_id, waiting_list_position(waiting_list)
)
end

waiting_list.add(self.user_id) if update_params[:status] == 'waiting_list'
waiting_list.remove(self.user_id) if update_params[:status] == 'accepted'
waiting_list.remove(self.user_id) if update_params[:status] == 'cancelled' || update_params[:status] == 'pending'
waiting_list.move_to_position(self.user_id, update_params[:waiting_list_position].to_i) if
update_params[:waiting_list_position].present?
end

# Fields
field :user_id, :integer
field :guests, :integer
field :competition_id, :string
field :competing_status, :string
field :hide_name_publicly, :boolean
field :lanes, :array, of: Lane

# We only do this one way because Dynamoid doesn't allow us to overwrite the foreign_key for has_one see https://github.com/Dynamoid/dynamoid/issues/740
belongs_to :history, class: RegistrationHistory, foreign_key: :attendee_id

Expand Down
12 changes: 12 additions & 0 deletions lib/email_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def self.registration_email_path
"#{EnvConfig.WCA_HOST}/api/internal/v1/mailers/registration"
end

def self.waiting_list_leader_path
"#{EnvConfig.WCA_HOST}/api/internal/v1/mailers/waiting-list-leader"
end

def self.send_update_email(competition_id, user_id, status, current_user)
HTTParty.post(EmailApi.registration_email_path, headers: { WCA_API_HEADER => self.wca_token }, body: {
competition_id: competition_id,
Expand All @@ -27,4 +31,12 @@ def self.send_creation_email(competition_id, user_id)
current_user: user_id,
})
end

def self.send_waiting_list_leader_email(competition_id, user_id, position)
HTTParty.post(EmailApi.waiting_list_leader_path, headers: { WCA_API_HEADER => self.wca_token }, body: {
competition_id: competition_id,
user_id: user_id,
position: position,
})
end
end
2 changes: 1 addition & 1 deletion spec/controllers/registration_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
end
end

describe '#list_admin', :tag do
describe '#list_admin' do
before do
@competition = FactoryBot.build(:competition)
stub_json(CompetitionApi.url(@competition['id']), 200, @competition.except('qualifications'))
Expand Down
14 changes: 10 additions & 4 deletions spec/models/registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@
expect(@waiting_list.entries.include?(@reg1.user_id)).to eq(false)
end

it 'cant accept if not in leading position of waiting list' do
expect {
@reg2.update_competing_lane!({ status: 'accepted' }, @waiting_list)
}.to raise_error(ArgumentError, 'Can only accept waiting list leader')
it 'can accept if not in leading position of waiting list' do
stub_request(:post, EmailApi.waiting_list_leader_path).to_return(status: 200, body: { emails_sent: 1 }.to_json)

@reg2.update_competing_lane!({ status: 'accepted' }, @waiting_list)
@waiting_list.reload

expect(@reg2.competing_status).to eq('accepted')
expect(@reg1.waiting_list_position(@waiting_list)).to eq(1)
expect(@waiting_list.entries.include?(@reg2.user_id)).to eq(false)
expect(WebMock).to have_requested(:post, EmailApi.waiting_list_leader_path)
end
end

Expand Down
Loading