Class: CheckBookingAvailabilityWorker

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

Overview

worker for checking restaurant that can't be booked

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#generate_excel_report(restaurants) ⇒ Object



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

def generate_excel_report(restaurants)
  tmp_file = Tempfile.new(['can-not-booking-restaurant', '.xlsx'])
  Xlsxtream::Workbook.open(tmp_file.path) do |xlsx|
    xlsx.write_worksheet 'Sheet1' do |sheet|
      sheet.add_row ['ID', 'Restaurant Name', 'Restaurant Phone Number', 'Restaurant Email',
                     'Restaurant Account Manager']
      restaurants.each do |data|
        sheet.add_row data
      end
    end
  end
  tmp_file
end

#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
# File 'app/workers/check_booking_availability_worker.rb', line 8

def perform
  results = [] # hasil data restaurant yang ngga open
  Restaurant.includes(:user, :owner).active.not_expired.find_each do |restaurant|
    inv_checker = InvCheckerFactory.new(restaurant.id, restaurant.time_zone).create_inv_checker_service
    restaurant_open = []
    (Date.current_date..(Date.current_date + 1.week)).to_a.each do |date|
      data = inv_checker.get_inv_by_date(date)
      data_value = []
      if data.present? # check apakah data itu ada isinya
        data.each do |e|
          next unless e[1].key?(:open)

          value = e[1].fetch_values(:open)
          data_value << value # buat ngambil value open
        end
      else
        restaurant_open << false
      end
      if data_value.all?([false]) # jika value open false semua maka restaurant_open false
        restaurant_open << false
      end
    end
    if restaurant_open.include?(false)
       = restaurant.user.present? ? restaurant.user.name : ''
      results << [restaurant.id, restaurant.name, restaurant.phone, restaurant.owner.email, ]
    end
  end
  if results.size.positive?
    file = generate_excel_report(results)
    send_mail_to_staff(file)
  end
end

#send_mail_to_staff(file) ⇒ Object



55
56
57
58
59
60
# File 'app/workers/check_booking_availability_worker.rb', line 55

def send_mail_to_staff(file)
  attachment = Attachment.new
  attachment.excel = File.open(file)
  attachment.save!
  StaffMailer.can_not_booking_restaurant(attachment.excel_url).deliver_later!
end