Class: ScheduleWorkers::CheckPackageChargeTypeWorker

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

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#generate_excel_report(restaurants) ⇒ Object

Parameters:

  • restaurants (Array)

    of restaurant data example:

    [
      [restaurant_id, restaurant_name, package_name],
    ]
    


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

def generate_excel_report(restaurants)
  tmp_file = Tempfile.new(['blank_package_charge_type_list', '.xlsx'])
  Xlsxtream::Workbook.open(tmp_file.path) do |xlsx|
    xlsx.write_worksheet 'Sheet1' do |sheet|
      sheet.add_row ['Restaurant ID', 'Restaurant Name', 'Package Name']
      restaurants.each do |data|
        sheet.add_row data
      end
    end
  end
  tmp_file
end

#performObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/workers/schedule_workers/check_package_charge_type_worker.rb', line 2

def perform
  blank_packages = HhPackage::PACKAGE_LIST.map do |klass|
    "HhPackage::Package::#{klass}".constantize.where(require_cc: true, charge_type: nil)
  end
  restaurant_list = []
  blank_packages.each do |package_type|
    if !package_type.blank?
      package_type.each do |package|
        package.restaurant_packages.active_scope.map do |rp|
          restaurant_list << [rp.restaurant_id, rp.restaurant.name_en, rp.package.name_en]
        end
      end
    end
  end
  if restaurant_list.size.positive?
    file = generate_excel_report(restaurant_list)
    send_mail_to_staff(file)
  end
end

#send_mail_to_staff(file) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'app/workers/schedule_workers/check_package_charge_type_worker.rb', line 40

def send_mail_to_staff(file)
  attachment = Attachment.new
  attachment.excel = File.open(file)
  attachment.save!
  body = "Dear Operation team,
  Please kindly check this link below for package empty charge type list
  #{attachment.excel_url}"
  StaffMailer.notify('hello@hungryhub.com', body, 'Empty Package Charge Type').deliver_later!
end