Skip to content

Commit

Permalink
Only Load MS Registration if needed in init_stripe (#10133)
Browse files Browse the repository at this point in the history
* Only Load MS Registration if needed in init_stripe

* Write general-purpose caching method for MS shadow access

* Use short-circuit method in mailer

* Better comments on Rails black magic

* Use simpler MS load call with ID as reference

* Fix ID loading quirk

---------

Co-authored-by: Gregor Billing <[email protected]>
  • Loading branch information
FinnIckler and gregorbg authored Oct 28, 2024
1 parent ed9a489 commit 56400ac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/internal/v1/mailers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def registration
requesting_user = params.require(:current_user)
registration_competition = params.require(:competition_id)
user = User.find(registration_user)
registration = user.microservice_registrations.find_by(competition_id: registration_competition)
registration = user.find_ms_registration_by(competition_id: registration_competition)

if registration_status == 'pending' && registration_action == 'create'
RegistrationsMailer.notify_organizers_of_new_registration(registration).deliver_later
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/api/internal/v1/payment_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def init_stripe
competition = Competition.find(competition_id)
return render json: { error: "Competition not found" }, status: :not_found unless competition.present?

ms_registration = competition.microservice_registrations
.includes(:competition, :user)
.find_by(user_id: registering_user_id)
ms_registration = competition.find_ms_registration_by(user_id: registering_user_id)

return render json: { error: "Registration not found" }, status: :not_found unless ms_registration.present?

Expand Down
17 changes: 17 additions & 0 deletions app/models/concerns/microservice_registration_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ def microservice_registrations
end
end
end

private def scoped_find_by(scope, **kwargs)
kwarg_symbols = kwargs.keys.map(&:to_sym)
own_assoc_key = self.class.model_name.element.to_sym

preload_keys = MicroserviceRegistration.reflect_on_all_associations.filter { |assoc|
# Pick by either the name (ie. `competition`) or the foreign key (ie. `competition_id`) of the association
kwarg_symbols.include?(assoc.name.to_sym) || kwargs.keys.include?(assoc.foreign_key.to_sym)
}.map(&:name)

scope.includes(own_assoc_key, *preload_keys)
.find_by(own_assoc_key => self, **kwargs)
end

def find_ms_registration_by(**)
self.scoped_find_by(MicroserviceRegistration, **) || self.scoped_find_by(self.microservice_registrations, **)
end
end

0 comments on commit 56400ac

Please sign in to comment.