Module: ModelExt::Restaurants::ClassMethods::ClassMethods

Defined in:
lib/model_ext/restaurants/class_methods.rb

Overview

ActiveSupport::Concern will load ClassMethods module as class methods automatically

Instance Method Summary collapse

Instance Method Details

#filter(params = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/model_ext/restaurants/class_methods.rb', line 51

def filter(params = {})
  query = {}
  if params.key?(:active)
    active_param = params.delete(:active)
    query[:active] = ActiveRecord::Type::Boolean.new.cast(active_param)
  end

  where(query)
end

#generate_reportsObject



43
44
45
46
47
48
49
# File 'lib/model_ext/restaurants/class_methods.rb', line 43

def generate_reports
  instant = ::Restaurant.where(active: true, instant_confirm: true).count
  non_instant = ::Restaurant.where(active: true, instant_confirm: false).count
  active_total = ::Restaurant.where(active: true).count
  non_active = ::Restaurant.where(active: false).count
  { instant: instant, non_instant: non_instant, active_total: active_total, non_active: non_active }
end

#generate_reviews_scoreObject



72
73
74
75
76
77
78
79
80
# File 'lib/model_ext/restaurants/class_methods.rb', line 72

def generate_reviews_score
  ::Restaurant.find_each do |r|
    rf = Api::V5::ReviewsFilter.new
    rf.scope_by restaurant_id: r.id, branch_id: r.branch_id
    r.reviews_score = rf.average
    r.sneaky_save!
    r.run_callbacks :update
  end
end

#generate_schedule(restaurant_id = 0, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/model_ext/restaurants/class_methods.rb', line 61

def generate_schedule(restaurant_id = 0, options = {})
  options = ::HashWithIndifferentAccess.new(options)
  remake = options.key?(:remake) ? options[:remake] == true : false

  generator = HungryHub::InventoryGenerator.new(restaurant_id)
  generator.remake = remake
  generator.random = true if options[:random].to_s == 'true'
  generator.inventory_type = options[:inventory_type]
  generator.generate
end

#refresh_inv_checker_by_dates(restaurant_id, dates, class_name = nil) ⇒ Object

Refreshes the inventory cache for a given restaurant and date range. It publishes an event to reset the cache for the specified dates.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/model_ext/restaurants/class_methods.rb', line 14

def refresh_inv_checker_by_dates(restaurant_id, dates, class_name = nil)
  return if dates.blank?

  dates.in_groups_of(1000, false).each do |dates_batch|
    payload = {
      event_type: EVENTS::INVENTORY::CACHE::TYPES::RESET_CACHE,
      restaurant_id: restaurant_id,
      restaurant_level: true,
      reset_mode: true,
      dates: dates_batch,
      triggered_at: Time.current.to_s,
      # Debug attributes
      trigger_source: 'Restaurant::ClassMethods',
      trigger_method: 'refresh_inv_checker_by_dates',
      trigger_context: {
        total_dates: dates.count,
        calling_class: class_name,
      },
      caller: Rails.backtrace_cleaner.clean(caller),
    }
    Karafka.producer.produce_async(
      topic: EVENTS::INVENTORY::CACHE::TOPIC,
      payload: payload.to_json,
    )
    Rails.logger.debug { "Published cache reset event for restaurant \\#{restaurant_id}" }
    Rails.logger.debug { "Payload: #{payload}" }
  end
end

#update_reviews_count(identifier_type, identifier_id) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/model_ext/restaurants/class_methods.rb', line 82

def update_reviews_count(identifier_type, identifier_id)
  restaurant = case identifier_type.to_s
               when 'Review'
                 review = Review.find_by(id: identifier_id)
                 review&.reservation&.restaurant
               when 'Restaurant'
                 Restaurant.find_by(id: identifier_id)
               when 'Reservation'
                 Reservation.find_by(id: identifier_id)&.restaurant
               else
                 HH_LOGGER.info('Unknown identifier while updating restaurant reviews_count',
                                identifier_type: identifier_type, identifier_id: identifier_id)
                 nil
               end
  return if restaurant.blank?

  reviews_filter = Api::V5::ReviewsFilter.new
  reviews_filter.scope_by(branch_id: restaurant.branch_id, restaurant_id: restaurant.id)
  restaurant.update_column :reviews_count, reviews_filter.total
  restaurant.branch.restaurants.each(&:touch) if restaurant.branch.present?
end