diff --git a/app/models/conference.rb b/app/models/conference.rb index c48dcf02..a3787477 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -30,6 +30,7 @@ class Conference < ApplicationRecord has_many :payments, dependent: :destroy has_many :supporters, through: :ticket_purchases, source: :user has_many :tickets, dependent: :destroy + has_many :registration_tickets, -> { for_registration }, class_name: 'Ticket' has_many :resources, dependent: :destroy has_many :booths, dependent: :destroy has_many :confirmed_booths, -> { where(state: 'confirmed') }, class_name: 'Booth' @@ -111,6 +112,13 @@ class Conference < ApplicationRecord user.present? && registrations.where(user_id: user.id).count > 0 end + ## + # True when there is at least one ticket marked as "registration" + # A user must get a registration ticket before registering. + def registration_ticket_required? + tickets.for_registration.any? + end + ## # Delete all EventSchedules that are not in the hours range # After the conference has been successfully updated diff --git a/app/models/registration.rb b/app/models/registration.rb index 759003fd..0e5a9d44 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -32,6 +32,8 @@ class Registration < ApplicationRecord if: -> { conference.try(:code_of_conduct).present? } } + validate :user_has_registration_ticket, if: -> { conference.registration_ticket_required? } + after_create :set_week, :subscribe_to_conference, :send_registration_mail ## @@ -65,4 +67,12 @@ class Registration < ApplicationRecord errors.add(:base, 'Registration limit exceeded') end end + + def user_has_registration_ticket + return if TicketPurchase.where(user: user, ticket: conference.registration_tickets).paid.any? + errors.add(:base, 'You must purchase a registration ticket before registering') + if TicketPurchase.where(user: user, ticket: conference.registration_tickets).unpaid.any? + errors.add(:base, 'You currently have a ticket with an unfinished purchase') + end + end end