Skip to content

Commit

Permalink
Reimplment >= competitor limit check (#673)
Browse files Browse the repository at this point in the history
* added check for registrations > competitor limit to also fail

* rubocop

* added integer conversion on redis retrieval

* wrapped entire cache call in integer conversion

* rubocop

* finn comments
  • Loading branch information
dunkOnIT authored Oct 29, 2024
1 parent 13704e8 commit 2233ae0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def show
def count
competition_id = params.require(:competition_id)

render json: { count: Registration.accepted_competitors_count(competition_id).to_i }
render json: { count: Registration.accepted_competitors_count(competition_id) }
end

def create
Expand Down
8 changes: 5 additions & 3 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ def self.get_registrations_by_status(competition_id, status)
end

def self.accepted_competitors_count(competition_id)
Rails.cache.fetch("#{competition_id}-accepted-count", expires_in: 60.minutes, raw: true) do
self.accepted_competitors(competition_id)
end
Integer(
Rails.cache.fetch("#{competition_id}-accepted-count", expires_in: 60.minutes, raw: true) do
self.accepted_competitors(competition_id)
end,
)
end

def self.decrement_competitors_count(competition_id)
Expand Down
16 changes: 15 additions & 1 deletion spec/services/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@
end
end

it 'organizer cant accept a user when registration list is full' do
it 'organizer cant accept a user when registration list is exactly full' do
FactoryBot.create_list(:registration, 3, registration_status: 'accepted')
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list')
override_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, competitor_limit: 3))
Expand All @@ -1094,6 +1094,20 @@
end
end

it 'organizer cant accept a user when registration list is over full' do
FactoryBot.create_list(:registration, 4, registration_status: 'accepted')
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list')
override_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, competitor_limit: 3))
update_request = FactoryBot.build(:update_request, :organizer_for_user, user_id: override_registration[:user_id], competing: { 'status' => 'accepted' })

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

it 'organizer can accept registrations up to the limit' do
FactoryBot.create_list(:registration, 2, registration_status: 'accepted')
registration = FactoryBot.create(:registration, registration_status: 'pending')
Expand Down

0 comments on commit 2233ae0

Please sign in to comment.