Skip to content

Commit

Permalink
Do not attempt to process duplicate registrations (#10410)
Browse files Browse the repository at this point in the history
* added check for existing registration

* made changes to add registration job

* changed present to exists

* changed .where.exists? to .exists?
  • Loading branch information
dunkOnIT authored Dec 10, 2024
1 parent cc45034 commit 5623850
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/jobs/add_registration_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ def self.prepare_task(user_id, competition_id)
end

def perform(lane_name, competition_id, user_id, lane_params)
# Hotfix for mass stuck duplicate registrations in the queue
registration = Registration.find_by(competition_id: competition_id, user_id: user_id)
return if registration.present?
if Registration.exists?(competition_id: competition_id, user_id: user_id)
Rails.cache.delete(CacheAccess.registration_processing_cache_key(competition_id, user_id))
return
end

lane_model_name = lane_name.upcase_first

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export const REGISTRATION_CLOSED = -4008;
export const ORGANIZER_MUST_CANCEL_REGISTRATION = -4009;
export const INVALID_WAITING_LIST_POSITION = -4010;
export const QUALIFICATION_NOT_MET = -4012;
export const REGISTRATION_ALREADY_EXISTS = -4014;
export const PAYMENT_NOT_ENABLED = -6001;
export const PAYMENT_NOT_READY = -6002;
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,7 @@ en:
-4010: "The Waiting List Position is not Valid"
-4011: "You can only accept the first person on the waiting list"
-4012: "You are not qualified to compete in the selected events"
-4014: "You are already registered for this competition."
-6001: "WCA Payment is not enabled for this competition"
-6002: "You need to finish your registration before you can pay"
#context: and when an error occured
Expand Down
1 change: 1 addition & 0 deletions lib/registrations/error_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module ErrorCodes
ORGANIZER_MUST_CANCEL_REGISTRATION = -4009
INVALID_WAITING_LIST_POSITION = -4010
QUALIFICATION_NOT_MET = -4012
REGISTRATION_ALREADY_EXISTS = -4014

# Payment Errors
PAYMENT_NOT_ENABLED = -6001
Expand Down
3 changes: 3 additions & 0 deletions lib/registrations/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def self.bulk_update_allowed!(bulk_update_request, current_user)

class << self
def user_can_create_registration!(competition, current_user, target_user)
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::REGISTRATION_ALREADY_EXISTS) if
Registration.exists?(competition_id: competition.id, user_id: target_user.id)

# Only the user themselves can create a registration for the user
raise WcaExceptions::RegistrationError.new(:unauthorized, Registrations::ErrorCodes::USER_INSUFFICIENT_PERMISSIONS) unless current_user.id == target_user.id

Expand Down
17 changes: 17 additions & 0 deletions spec/lib/registrations/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

describe '#create' do
describe '#create_registration_allowed!' do
it 'user cant create a duplicate registration' do
existing_reg = FactoryBot.create(:registration, competition: default_competition)

registration_request = FactoryBot.build(
:registration_request, guests: 10, competition_id: default_competition.id, user_id: existing_reg.user_id
)

expect {
Registrations::RegistrationChecker.create_registration_allowed!(
registration_request, User.find(registration_request['submitted_by'])
)
}.to raise_error(WcaExceptions::RegistrationError) do |error|
expect(error.status).to eq(:forbidden)
expect(error.error).to eq(Registrations::ErrorCodes::REGISTRATION_ALREADY_EXISTS)
end
end

it 'guests can equal the maximum allowed' do
registration_request = FactoryBot.build(
:registration_request, guests: 10, competition_id: default_competition.id, user_id: default_user.id
Expand Down

0 comments on commit 5623850

Please sign in to comment.