diff --git a/app/models/registration.rb b/app/models/registration.rb index ddfbca22..56392783 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -172,7 +172,11 @@ 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' @@ -180,6 +184,7 @@ def update_waiting_list(update_params, waiting_list) 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 @@ -187,6 +192,7 @@ def update_waiting_list(update_params, waiting_list) 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 diff --git a/lib/email_api.rb b/lib/email_api.rb index 50fa8090..5858d57e 100644 --- a/lib/email_api.rb +++ b/lib/email_api.rb @@ -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, @@ -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 diff --git a/spec/controllers/registration_controller_spec.rb b/spec/controllers/registration_controller_spec.rb index 5c3dfb3b..aa3d3e17 100644 --- a/spec/controllers/registration_controller_spec.rb +++ b/spec/controllers/registration_controller_spec.rb @@ -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')) diff --git a/spec/models/registration_spec.rb b/spec/models/registration_spec.rb index 18de5608..21484930 100644 --- a/spec/models/registration_spec.rb +++ b/spec/models/registration_spec.rb @@ -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