Untitled

 avatar
unknown
plain_text
2 years ago
1.0 kB
3
Indexable
module Orders
  class NextFacilityForBooking < ModernService
    attribute :facility
    attribute :created_by_venue
    attribute :internal_order_uuid

    validates :facility, presence: true
    validate  :validate_any_non_custom_booking_option_presence

    after_validation :find_next_facility

    attr_reader :next_facility

    private

    def process
      true
    rescue StandardError => e
      Exc.capture(e)
      false
    end

    def validate_any_non_custom_booking_option_presence
      errors.add(:base, :zero_not_custom_booking_options) unless facility.booking_options.not_custom.any?
    end

    def find_next_facility
      @next_facility = facilities.first
    end

    def facilities
      scope = Facility.not_soft_deleted.visible_only
      scope = (created_by_venue || internal_order_uuid) ? scope : scope.published.bookable
      scope = scope.where(venue_id: facility.venue_id) if facility.present?
      scope = scope.with_booking_options_visible_to_all
      scope
    end
  end
end
Editor is loading...