Class: ScheduleWorkers::EmptyInventoryWorker
- Inherits:
-
ApplicationWorker
- Object
- ApplicationWorker
- ScheduleWorkers::EmptyInventoryWorker
- 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
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 |
#perform ⇒ Object
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? account_manager = restaurant.user.present? ? restaurant.user.name : '' result << [restaurant.id, restaurant.name, restaurant.phone, restaurant.owner&.email, account_manager] 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.new .excel = File.open(file) .save! StaffMailer.empty_inventory(.excel_url).deliver_later! end |