diff --git a/app/models/registration.rb b/app/models/registration.rb index 57dde88a..b3e4ece6 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -77,6 +77,10 @@ def event_details competing_lane&.lane_details&.[]('event_details') end + def event_details_for(event_id) + competing_lane.lane_details['event_details'].find { |e| e['event_id'] == event_id } + end + def competing_waiting_list_position competing_lane&.lane_details&.[]('waiting_list_position') end @@ -141,9 +145,12 @@ def update_competing_lane!(update_params) lane.lane_state = update_params[:status] lane.lane_details['event_details'].each do |event| - # NOTE: Currently event_registration_state is not used - when per-event registrations are added, we need to add validation logic to support cases like - # limited registrations and waiting lists for certain events - event['event_registration_state'] = update_params[:status] + competition = CompetitionApi.find(competition_id) + if competition.get_qualification_for(event['event_id'])['type'] == 'ranking' && update_params[:status] == 'accepted' + event['event_registration_state'] = 'waiting_list' + else + event['event_registration_state'] = update_params[:status] + end end end diff --git a/lib/lane_factory.rb b/lib/lane_factory.rb index ae180fbb..63151d10 100644 --- a/lib/lane_factory.rb +++ b/lib/lane_factory.rb @@ -9,7 +9,7 @@ def self.competing_lane(event_ids: [], comment: '', admin_comment: '', registrat competing_lane.completed_steps = ['Event Registration'] competing_lane.lane_state = registration_status competing_lane.lane_details = { - 'event_details' => event_ids.map { |event_id| { event_id: event_id, event_registration_state: registration_status } }, + 'event_details' => event_ids.map { |event_id| { 'event_id' => event_id, 'event_registration_state' => registration_status } }, 'comment' => comment, 'admin_comment' => admin_comment, 'waiting_list_position' => waiting_list_position.to_i, diff --git a/spec/models/registration_spec.rb b/spec/models/registration_spec.rb index 30221eef..8b166794 100644 --- a/spec/models/registration_spec.rb +++ b/spec/models/registration_spec.rb @@ -19,28 +19,21 @@ end describe '#update_competing_lane!' do - it 'given accepted status, it changes the users status to accepted' do - registration = FactoryBot.create(:registration, registration_status: 'pending') - registration.update_competing_lane!({ status: 'accepted' }) - expect(registration.competing_status).to eq('accepted') - end - - it 'accepted given cancelled, it sets competing_status accordingly' do - registration = FactoryBot.create(:registration, registration_status: 'accepted') - registration.update_competing_lane!({ status: 'cancelled' }) - expect(registration.competing_status).to eq('cancelled') - end - - it 'accepted given pending, it sets competing_status accordingly' do - registration = FactoryBot.create(:registration, registration_status: 'accepted') - registration.update_competing_lane!({ status: 'pending' }) - expect(registration.competing_status).to eq('pending') + RSpec.shared_examples 'competing_status updates' do |old_status, new_status| + it "given #{new_status}, #{old_status} updates as expected" do + registration = FactoryBot.create(:registration, registration_status: old_status) + registration.update_competing_lane!({ status: new_status }) + expect(registration.competing_status).to eq(new_status) + end end - it 'accepted given waiting_list, it sets competing_status' do - registration = FactoryBot.create(:registration, registration_status: 'accepted') - registration.update_competing_lane!({ status: 'waiting_list' }) - expect(registration.competing_status).to eq('waiting_list') + [ + { old_status: 'pending', new_status: 'accepted' }, + { old_status: 'accepted', new_status: 'cancelled' }, + { old_status: 'accepted', new_status: 'pending' }, + { old_status: 'accepted', new_status: 'waiting_list' }, + ].each do |params| + it_behaves_like 'competing_status updates', params[:old_status], params[:new_status] end end @@ -307,6 +300,26 @@ end end + describe '#update_competing_lane#event_waiting_lists', :tag do + RSpc.shared_examples 'ranking qualification: event_registration_state updates' do + it 'event_status must correspond to updated competing_status' do |start_status, new_status, event_status| + competition = FactoryBot.build(:competition, :has_qualifications) + stub_json(CompetitionApi.url(competition['id']), 200, competition) + stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications']) + + registration = FactoryBot.create(:registration, registration_status: start_status, events: ['pyram']) + registration.update_competing_lane!({ status: new_status }) + + expect(registration.event_details_for('pyram')['event_registration_state']).to eq(event_status) + end + end + [ + { starting_competing_status: 'pending', new_competing_status: 'accepted', expected_event_state: 'waiting_list' }, + ].each do |params| + it_behaves_like 'ranking qualification: event_registration_state updates', params[:starting_competing_status], params[:new_competing_status], params[:expected_event_state] + end + end + describe '#accepted_competitors' do it 'returns the number of accepted competitors only for a specific competition' do target_comp = 'TargetCompId'