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

Reimplment >= competitor limit check #673

Merged
merged 9 commits into from
Oct 29, 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
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
Loading