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

Allow organizer to edit reg for a user with an accepted registration in another competition in the series #694

Merged
merged 4 commits into from
Sep 23, 2024
Merged
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
5 changes: 5 additions & 0 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Registration

REGISTRATION_STATES = %w[pending waiting_list accepted cancelled rejected].freeze
ADMIN_ONLY_STATES = %w[pending waiting_list accepted rejected].freeze # Only admins are allowed to change registration state to one of these states
MIGHT_ATTEND_STATES = %w[pending waiting_list accepted].freeze

# Pre-validations
before_validation :set_competing_status
Expand Down Expand Up @@ -171,6 +172,10 @@ def update_payment_lane(id, iso_amount, currency_iso, status)
update_attributes!(lanes: updated_lanes)
end

def might_attend?
MIGHT_ATTEND_STATES.include?(self.competing_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

Expand Down
8 changes: 6 additions & 2 deletions app/services/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def user_can_modify_registration!
raise RegistrationError.new(:unauthorized, ErrorCodes::USER_INSUFFICIENT_PERMISSIONS) unless can_administer_or_current_user?
raise RegistrationError.new(:forbidden, ErrorCodes::USER_EDITS_NOT_ALLOWED) unless @competition_info.registration_edits_allowed? || UserApi.can_administer?(@requester_user_id, @competition_info.id)
raise RegistrationError.new(:unauthorized, ErrorCodes::REGISTRATION_IS_REJECTED) if user_is_rejected?
raise RegistrationError.new(:forbidden, ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if existing_registration_in_series?
raise RegistrationError.new(:forbidden, ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if
existing_registration_in_series? && !UserApi.can_administer?(@requester_user_id, @competition_info.id)
end

def user_is_rejected?
Expand Down Expand Up @@ -173,6 +174,8 @@ def validate_update_status!
raise RegistrationError.new(:unprocessable_entity, ErrorCodes::INVALID_REQUEST_DATA) unless Registration::REGISTRATION_STATES.include?(new_status)
raise RegistrationError.new(:forbidden, ErrorCodes::COMPETITOR_LIMIT_REACHED) if
new_status == 'accepted' && Registration.accepted_competitors_count(@competition_info.competition_id) == @competition_info.competitor_limit
raise RegistrationError.new(:forbidden, ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if
new_status == 'accepted' && existing_registration_in_series?

# Otherwise, organizers can make any status change they want to
return if UserApi.can_administer?(@requester_user_id, @competition_info.id)
Expand Down Expand Up @@ -211,7 +214,8 @@ def existing_registration_in_series?
return false if @competition_info.other_series_ids.nil?

@competition_info.other_series_ids.each do |comp_id|
return Registration.find("#{comp_id}-#{@requestee_user_id}").competing_status != 'cancelled'
series_reg = Registration.find("#{comp_id}-#{@requestee_user_id}")
return series_reg.might_attend?
rescue Dynamoid::Errors::RecordNotFound
next
end
Expand Down
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
38 changes: 37 additions & 1 deletion spec/services/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@
end
end

it 'cannot move to less than position 1', :tag2 do
it 'cannot move to less than position 1' do
@waiting_list.add(FactoryBot.create(:registration, registration_status: 'waiting_list').user_id)
@waiting_list.add(FactoryBot.create(:registration, registration_status: 'waiting_list').user_id)
@waiting_list.add(FactoryBot.create(:registration, registration_status: 'waiting_list').user_id)
Expand Down Expand Up @@ -1557,6 +1557,42 @@
it_behaves_like 'update succeed: qualification enforced', 'can register when minx average exists for ranking-average', ['minx']
end
end

describe '#update_registration_allowed!.organizer updates series reg' do
it 'organizer cant set status to accepted if attendee is accepted for another series comp' do
cancelled_registration = FactoryBot.create(:registration, registration_status: 'cancelled')
FactoryBot.create(:registration, user_id: cancelled_registration['user_id'], registration_status: 'accepted', competition_id: 'CubingZAWarmup2023')

series_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, :series))

update_request = FactoryBot.build(:update_request, :organizer_for_user, user_id: cancelled_registration[:user_id], competing: { 'status' => 'accepted' })

expect {
RegistrationChecker.update_registration_allowed!(update_request, series_competition_info, update_request['submitted_by'])
}.to raise_error(RegistrationError) do |error|
expect(error.error).to eq(ErrorCodes::ALREADY_REGISTERED_IN_SERIES)
expect(error.http_status).to eq(:forbidden)
end
end

it 'organizer can update admin comment in attendees non-accepted series comp registration' do
cancelled_registration = FactoryBot.create(:registration, registration_status: 'cancelled')
FactoryBot.create(:registration, user_id: cancelled_registration['user_id'], registration_status: 'accepted', competition_id: 'CubingZAWarmup2023')

series_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, :series))

update_request = FactoryBot.build(
:update_request,
:organizer_for_user,
user_id: cancelled_registration[:user_id],
competing: { 'admin_comment' => 'why they were cancelled' },
)

expect {
RegistrationChecker.update_registration_allowed!(update_request, series_competition_info, update_request['submitted_by'])
}.not_to raise_error
end
end
end

describe '#bulk_update' do
Expand Down
Loading