Class: ScheduleWorkers::Homes::PopularBranch

Inherits:
BaseWorker show all
Defined in:
app/workers/schedule_workers/homes/popular_branch.rb

Instance Method Summary collapse

Methods inherited from BaseWorker

#flexible_start_date, #generate_restaurant_revenues, #generate_restaurant_tag_revenues

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#performObject



8
9
10
11
12
13
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
42
43
44
45
46
# File 'app/workers/schedule_workers/homes/popular_branch.rb', line 8

def perform
  current_date = Date.current_date
  days_3_ago = current_date - 3.days

  start_date = days_3_ago.beginning_of_day
  end_date = current_date.end_of_day

  month = current_date.strftime('%m')
  year = current_date.strftime('%Y')

  Branch.find_each do |branch|
    restaurants = branch.restaurants.active.not_expired
    next if restaurants.count < 3

    revenues = []
    restaurants.each do |restaurant|
      reservations = restaurant.reservations
                               .hungryhub.has_package_scope
                               .reached_goal_scope.where(created_at: start_date..end_date)
      next if reservations.blank?

      revenue = reservations.select('SUM(reservation_properties.revenue) revenue')[0]['revenue'].to_i
      revenues.push(revenue)
    end

    flexible_start_date(restaurants.first.id, current_date).each do |date|
      day = date.strftime('%d')
      report = Report.find_or_initialize_by(
        restaurant_id: branch.id,
        name: :popular_branch,
        day: day,
        month: month,
        year: year
      )
      report.data = revenues.max.presence || 0
      report.save!
    end
  end
end