Class: HhPackage::GenerateOpeningHoursWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/hh_package/generate_opening_hours_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#packageObject (readonly)

Returns the value of attribute package.



3
4
5
# File 'app/workers/hh_package/generate_opening_hours_worker.rb', line 3

def package
  @package
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



3
4
5
# File 'app/workers/hh_package/generate_opening_hours_worker.rb', line 3

def restaurant
  @restaurant
end

#restaurant_packageObject (readonly)

Returns the value of attribute restaurant_package.



3
4
5
# File 'app/workers/hh_package/generate_opening_hours_worker.rb', line 3

def restaurant_package
  @restaurant_package
end

Instance Method Details

#generate(restaurant_package_id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
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
# File 'app/workers/hh_package/generate_opening_hours_worker.rb', line 23

def generate(restaurant_package_id)
  return unless setup_variables(restaurant_package_id)

  date = Time.now_in_tz(restaurant.time_zone).to_date
  x_days = 6

  opening_hours = []
  dates = date.upto(date + x_days.days).sort_by(&:cwday)
  # => [Mon, 06 Jan 2025, Tue, 07 Jan 2025, Wed, 08 Jan 2025, Thu, 02 Jan 2025, Fri, 03 Jan 2025, Sat, 04 Jan 2025, Sun, 05 Jan 2025]

  dates.each do |day|
    schedule = time_slots(day)

    # if current date is empty, then find the next available date, let's say
    # today is Monday 6th Jan 2025, and it's closed, then check at Monday 13th Jan 2025
    if schedule[:open].nil?
      next if restaurant_package.end_date.blank?

      loop do
        # try until restaurant package's end date
        break if restaurant_package.end_date < day

        day += 7.days
        schedule = time_slots(day)
        break if schedule[:open].present?
      end
    end

    schedule[:open] = schedule[:close] = 'closed' if schedule[:open].blank?
    opening_hour = schedule.merge(cwday: day.cwday, restaurant_package_id: restaurant_package_id)
    opening_hours.push(opening_hour)
  end

  Retriable.retriable on: [ActiveRecord::Deadlocked], tries: 10 do
    ::HhPackage::OpeningHour.import(opening_hours,
                                    raise_error: true,
                                    on_duplicate_key_update: %i[cwday open close restaurant_package_id])
  end
  restaurant_package.touch
end

#perform(restaurant_package_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/workers/hh_package/generate_opening_hours_worker.rb', line 8

def perform(restaurant_package_id)
  return unless setup_variables(restaurant_package_id)

  cache_key = [
    restaurant.inv_cache_key,
    restaurant_package.cache_key,
    package.cache_key,
    package.agendas.cache_key,
  ]
  Rails.cache.fetch("#{self.class}:#{restaurant_package_id}:#{CityHash.hash32(cache_key)}",
                    expires_in: CACHEFLOW.generate_expiry) do
    generate(restaurant_package_id)
  end
end