Skip to content

Commit

Permalink
Allow V2 Competitions to add On the Spot Registrations (thewca#9948)
Browse files Browse the repository at this point in the history
* allow V2 Competitions to add On the Spot Registrations

* fix rubocop

* remove to_json
  • Loading branch information
FinnIckler authored Sep 14, 2024
1 parent b728c40 commit 8fa5278
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
27 changes: 18 additions & 9 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,25 @@ def do_add
end
ActiveRecord::Base.transaction do
user, locked_account_created = user_for_registration!(params[:registration_data])
registration = @competition.registrations.find_or_initialize_by(user_id: user.id)
raise I18n.t("registrations.add.errors.already_registered") unless registration.new_record?
registration_comment = params.dig(:registration_data, :comments)
registration.assign_attributes(comments: registration_comment) if registration_comment.present?
registration.assign_attributes(accepted_at: Time.now, accepted_by: current_user.id)
params[:registration_data][:event_ids]&.each do |event_id|
competition_event = @competition.competition_events.find { |ce| ce.event_id == event_id }
registration.registration_competition_events.build(competition_event_id: competition_event.id)
if @competition.uses_new_registration_service?
Microservices::Registrations.add_registration(@competition.id,
user.id,
params[:registration_data][:event_ids],
params[:registration_data][:comments],
"accepted",
@current_user.id)
else
registration = @competition.registrations.find_or_initialize_by(user_id: user.id)
raise I18n.t("registrations.add.errors.already_registered") unless registration.new_record?
registration_comment = params.dig(:registration_data, :comments)
registration.assign_attributes(comments: registration_comment) if registration_comment.present?
registration.assign_attributes(accepted_at: Time.now, accepted_by: current_user.id)
params[:registration_data][:event_ids]&.each do |event_id|
competition_event = @competition.competition_events.find { |ce| ce.event_id == event_id }
registration.registration_competition_events.build(competition_event_id: competition_event.id)
end
registration.save!
end
registration.save!
if locked_account_created
RegistrationsMailer.notify_registrant_of_locked_account_creation(user, @competition).deliver_later
end
Expand Down
19 changes: 19 additions & 0 deletions lib/microservices/registrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def self.get_registration_path(attendee_id)
"/api/internal/v1/#{attendee_id}"
end

def self.add_registration_path(competition_id)
"/api/internal/v1/#{competition_id}/add"
end

def self.get_competitor_count_path(competition_id)
"/api/v1/#{competition_id}/count"
end
Expand All @@ -48,6 +52,21 @@ def self.registration_connection
)
end

# rubocop:disable Metrics/ParameterLists
def self.add_registration(competition_id, user_id, event_ids, comment, competing_status, current_user)
response = self.registration_connection.post(self.add_registration_path(competition_id)) do |req|
req.body = { competition_id: competition_id,
user_id: user_id,
event_ids: event_ids,
comment: comment,
competing_status: competing_status,
current_user: current_user }
end
raise I18n.t("registrations.add.errors.already_registered") unless response.success?
response.body
end
# rubocop:enable Metrics/ParameterLists

def self.registrations_by_user(user_id, cache: true)
response = self.registration_connection.get(self.registrations_by_user_path(user_id))

Expand Down

0 comments on commit 8fa5278

Please sign in to comment.