Class: Inventory::GenerateSummaryWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/inventory/generate_summary_worker.rb

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#generate_all_date(restaurant) ⇒ Object



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 'app/workers/inventory/generate_summary_worker.rb', line 15

def generate_all_date(restaurant)
  days_in_advance = restaurant.days_in_advance

  inventory_summaries = []
  restaurant.restaurant_packages.each do |restaurant_package|
    today      = Time.now_in_tz(restaurant.time_zone).to_date
    start_date = if restaurant_package.start_date >= today
                   restaurant_package.start_date
                 else
                   today
                 end
    end_date   = if restaurant_package.auto_extend?
                   today + days_in_advance.days
                 else
                   restaurant_package.end_date
                 end

    (start_date..end_date).each do |date|
      inv_summary = inv_summary_instance(date, restaurant_package)
      inventory_summaries.push inv_summary
    end
  end

  ActiveRecord::Base.transaction do
    InventorySummary.bulk_import!(inventory_summaries, on_duplicate_key_update: [:total_seat_left], raise_error: true)
  end
end

#generate_specific_date(date, restaurant) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'app/workers/inventory/generate_summary_worker.rb', line 43

def generate_specific_date(date, restaurant)
  inventory_summaries = []
  restaurant.restaurant_packages.each do |restaurant_package|
    inv_summary = inv_summary_instance(date, restaurant_package)
    inventory_summaries.push inv_summary
  end

  ActiveRecord::Base.transaction do
    InventorySummary.bulk_import!(inventory_summaries, on_duplicate_key_update: [:total_seat_left], raise_error: true)
  end
end

#inv_summary_instance(date, restaurant_package) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/workers/inventory/generate_summary_worker.rb', line 55

def inv_summary_instance(date, restaurant_package)
  inv_checker = InvCheckerFactory.new(
    restaurant_package.restaurant.id,
    restaurant_package.restaurant.time_zone,
  ).create_inv_checker_service

  invs            = inv_checker.get_inv_by_date(date, restaurant_package.slug)
  total_seat_left = invs.values.select { |inv| inv['open'] == true }.sum { |inv| inv['seat_left'] }

  inv_summary = InventorySummary.find_or_initialize_by(restaurant_id: restaurant_package.restaurant_id,
                                                       restaurant_package_id: restaurant_package.id, date: date)
  inv_summary.total_seat_left = total_seat_left
  inv_summary
end

#perform(restaurant_id, date = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'app/workers/inventory/generate_summary_worker.rb', line 4

def perform(restaurant_id, date = nil)
  return if AdminSetting.enable_package_sort_feature.to_s == 'false'

  restaurant = Restaurant.fetch restaurant_id
  if date.present?
    generate_specific_date(date, restaurant)
  else
    generate_all_date(restaurant)
  end
end