From 8fa527844bbc3df8dd372b23f3785ebb807a43fb Mon Sep 17 00:00:00 2001 From: FinnIckler Date: Sat, 14 Sep 2024 14:53:44 +0200 Subject: [PATCH] Allow V2 Competitions to add On the Spot Registrations (#9948) * allow V2 Competitions to add On the Spot Registrations * fix rubocop * remove to_json --- app/controllers/registrations_controller.rb | 27 ++++++++++++++------- lib/microservices/registrations.rb | 19 +++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 3308d91f58..4f9afb8b3a 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -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 diff --git a/lib/microservices/registrations.rb b/lib/microservices/registrations.rb index 4892b7b886..dfe0f97663 100644 --- a/lib/microservices/registrations.rb +++ b/lib/microservices/registrations.rb @@ -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 @@ -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))