Class: ScheduleWorkers::CheckPackageExpiryDate

Inherits:
ApplicationWorker show all
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

unlimited_retry

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

#performObject



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 = Attachment.new
  attachment.excel = File.open(file)
  attachment.save!
  StaffMailer.package_expiry_date(attachment.excel_url).deliver_later!
end