Class: PartnerService::Inventories::FetchService

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/partner_service/inventories/fetch_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, time, restaurant = nil, restaurants = []) ⇒ FetchService

Returns a new instance of FetchService.



19
20
21
22
23
24
# File 'app/services/partner_service/inventories/fetch_service.rb', line 19

def initialize(type, time, restaurant = nil, restaurants = [])
  @type = type || 'daily'
  @time = time.try(:to_date) || Time.zone.today
  @restaurant = restaurant
  @restaurants = restaurants
end

Class Method Details

.inv_worker_locked?(restaurant_id) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/services/partner_service/inventories/fetch_service.rb', line 15

def self.inv_worker_locked?(restaurant_id)
  Rails.cache.read("inventory_of_restaurant_#{restaurant_id}").present?
end

.lock_inv_worker(restaurant_id) ⇒ Object



7
8
9
# File 'app/services/partner_service/inventories/fetch_service.rb', line 7

def self.lock_inv_worker(restaurant_id)
  Rails.cache.write("inventory_of_restaurant_#{restaurant_id}", true, expires_in: 1.day)
end

.unlock_inv_worker(restaurant_id) ⇒ Object



11
12
13
# File 'app/services/partner_service/inventories/fetch_service.rb', line 11

def self.unlock_inv_worker(restaurant_id)
  Rails.cache.delete("inventory_of_restaurant_#{restaurant_id}")
end

Instance Method Details

#fetch_all_branchesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/partner_service/inventories/fetch_service.rb', line 34

def fetch_all_branches
  inventories_data = {}

  @restaurants.each do |restaurant|
    if no_inventory?(restaurant)
      add_no_inventory_to_data(inventories_data, restaurant)
      next
    end

    service = fetch_inventory_service(@type, @time, restaurant)
    inventory_data = inventory_response(service, @type)

    # Add inventory data for each time slot
    inventory_data.each do |time_slot, slot_data|
      next if time_slot == :dashboard # Skip the dashboard key

      if slot_data.present? && !slot_data.is_a?(String)
        slot_data[:nick_name] = restaurant&.nick_name || restaurant&.id&.to_s
      end
      inventories_data[time_slot] ||= []
      inventories_data[time_slot] << { restaurant.id.to_s => slot_data }
    end

    # Add dashboard data
    inventories_data[:dashboard] ||= []
    inventories_data[:dashboard] << { restaurant.id.to_s => inventory_data[:dashboard] }
  end

  inventories_data
end

#fetch_single_restaurantObject



26
27
28
29
30
31
# File 'app/services/partner_service/inventories/fetch_service.rb', line 26

def fetch_single_restaurant
  return { error: 'No inventory' } if no_inventory?(@restaurant)

  service = fetch_inventory_service(@type, @time, @restaurant)
  inventory_response(service, @type)
end