Class: ScheduleWorkers::EmptyInventoryWorker

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

Overview

this class was made for check empty inventory, store it in excel and send mail to staff

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#generate_excel_report(restaurants) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/workers/schedule_workers/empty_inventory_worker.rb', line 28

def generate_excel_report(restaurants)
  tmp_file = Tempfile.new(['missing-inventory', '.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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/workers/schedule_workers/empty_inventory_worker.rb', line 9

def perform
  result = []
  ::Restaurant.includes(:owner, :user).active.not_expired.find_each do |restaurant|
    checker = EmptyInventoryChecker.new(restaurant)
    checker.valid?
    if checker.errors.present?
       = restaurant.user.present? ? restaurant.user.name : ''
      result << [restaurant.id, restaurant.name, restaurant.phone, restaurant.owner&.email, ]
    end
  end

  if result.size.positive?
    file = generate_excel_report(result)
    send_mail_to_staff(file)
  else
    'complete inventory'
  end
end

#send_mail_to_staff(file) ⇒ Object



41
42
43
44
45
46
# File 'app/workers/schedule_workers/empty_inventory_worker.rb', line 41

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