Class: ScheduleWorkers::CheckPackageExpiryDate
- Inherits:
-
ApplicationWorker
- Object
- ApplicationWorker
- ScheduleWorkers::CheckPackageExpiryDate
- Defined in:
- app/workers/schedule_workers/check_package_expiry_date.rb
Overview
typed: ignore frozen_string_literal: true
Instance Method Summary collapse
Methods inherited from ApplicationWorker
Instance Method Details
#generate_excel_report(restaurants) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/workers/schedule_workers/check_package_expiry_date.rb', line 26 def generate_excel_report(restaurants) tmp_file = Tempfile.new(['upcoming-expired-package', '.xlsx']) Xlsxtream::Workbook.open(tmp_file.path) do |xlsx| xlsx.write_worksheet 'Sheet1' do |sheet| sheet.add_row ['Restaurant ID', 'Restaurant Name', 'Package Name', 'Expired Date'] restaurants.each do |data| sheet.add_row data end end end tmp_file end |
#perform ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/workers/schedule_workers/check_package_expiry_date.rb', line 7 def perform restaurant_data = [] HhPackage::RestaurantPackage.includes(:restaurant, :package) .where('end_date BETWEEN ? AND ?', Date.current_date - 7.days, Date.current_date + 14.days) .find_each do |r| if r.restaurant.present? && r.package.present? restaurant_data << [r.restaurant.id, r.restaurant.name, r.package.name, r.end_date] elsif r.restaurant.present? restaurant_data << [r.restaurant.id, r.restaurant.name, 'not found', r.end_date] else restaurant_data << ['not found', 'not found', r.package.name, r.end_date] end end if restaurant_data.size.positive? file = generate_excel_report(restaurant_data) send_mail_to_staff(file) end end |
#send_mail_to_staff(file) ⇒ Object
39 40 41 42 43 44 |
# File 'app/workers/schedule_workers/check_package_expiry_date.rb', line 39 def send_mail_to_staff(file) = Attachment.new .excel = File.open(file) .save! StaffMailer.package_expiry_date(.excel_url).deliver_later! end |