From bf755c235a186592f0fb7232934271aecee077ce Mon Sep 17 00:00:00 2001 From: Duncan Date: Mon, 9 Sep 2024 12:59:19 +0200 Subject: [PATCH 1/3] removed waiting list leader check --- app/models/registration.rb | 2 +- spec/controllers/registration_controller_spec.rb | 2 +- spec/models/registration_spec.rb | 11 +++++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/models/registration.rb b/app/models/registration.rb index ddfbca22..92954cc4 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -172,7 +172,7 @@ 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 + # TODO: Send email if update_params[:status] == 'accepted' && waiting_list_position(waiting_list) != 1 waiting_list.add(self.user_id) if update_params[:status] == 'waiting_list' waiting_list.remove(self.user_id) if update_params[:status] == 'accepted' 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..37f1c7d4 100644 --- a/spec/models/registration_spec.rb +++ b/spec/models/registration_spec.rb @@ -84,10 +84,13 @@ 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 + @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) end end From 8251ecb554f192579aef929ad7b7ea17ef41e650 Mon Sep 17 00:00:00 2001 From: Duncan Date: Mon, 9 Sep 2024 16:17:09 +0200 Subject: [PATCH 2/3] added email for accepting non-waiting-list-leader --- app/models/registration.rb | 6 +++++- lib/email_api.rb | 13 +++++++++++++ spec/models/registration_spec.rb | 3 +++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/registration.rb b/app/models/registration.rb index 92954cc4..47088196 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -172,7 +172,9 @@ def update_payment_lane(id, iso_amount, currency_iso, status) end def update_waiting_list(update_params, waiting_list) - # TODO: Send email 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) + ) if update_params[:status] == 'accepted' && waiting_list_position(waiting_list) != 1 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 +182,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 +190,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..a44915a0 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,13 @@ 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/models/registration_spec.rb b/spec/models/registration_spec.rb index 37f1c7d4..21484930 100644 --- a/spec/models/registration_spec.rb +++ b/spec/models/registration_spec.rb @@ -85,12 +85,15 @@ end 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 From 9c24ff3ed4b1e6a3b590c61b09ede9c9fc6629e3 Mon Sep 17 00:00:00 2001 From: Duncan Date: Mon, 9 Sep 2024 16:26:53 +0200 Subject: [PATCH 3/3] rubocop --- app/models/registration.rb | 8 +++++--- lib/email_api.rb | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/registration.rb b/app/models/registration.rb index 47088196..56392783 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -172,9 +172,11 @@ def update_payment_lane(id, iso_amount, currency_iso, status) end def update_waiting_list(update_params, waiting_list) - EmailApi.send_waiting_list_leader_email( - self.competition_id, self.user_id, waiting_list_position(waiting_list) - ) 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' diff --git a/lib/email_api.rb b/lib/email_api.rb index a44915a0..5858d57e 100644 --- a/lib/email_api.rb +++ b/lib/email_api.rb @@ -38,6 +38,5 @@ def self.send_waiting_list_leader_email(competition_id, user_id, position) user_id: user_id, position: position, }) - end end