Class: NotificationWorkers::VoucherGroupReport

Inherits:
ApplicationWorker show all
Includes:
MoneyRails::ActionViewExtension, ReservationExportHelpers, Sidekiq::Status::Worker
Defined in:
app/workers/notification_workers/voucher_group_report.rb

Overview

This worker generates excel voucher group and send download link to user whom requested the report through email

There are 1 kind of receiver

  1. hh: HungryHub Staff

Constant Summary

Constants included from ExportConstants

ExportConstants::CHANNEL_IDS_TO_SKIP, ExportConstants::DEFAULT_QUEUE, ExportConstants::EMAIL_BATCH_SIZE, ExportConstants::ERROR_CATEGORIES, ExportConstants::EXCEL_FILE_EXTENSION, ExportConstants::LARGE_DATASET_WARNING_THRESHOLD, ExportConstants::LONG_PROCESS_DAYS_THRESHOLD, ExportConstants::LONG_PROCESS_QUEUE, ExportConstants::MAX_RETRIES, ExportConstants::PDF_FILE_EXTENSION, ExportConstants::PROGRESS_MESSAGES, ExportConstants::PROGRESS_PHASES, ExportConstants::PROGRESS_UPDATE_BATCH_SIZE, ExportConstants::RESERVATION_BATCH_SIZE, ExportConstants::RETRY_BASE_INTERVAL, ExportConstants::RETRY_MAX_ELAPSED_TIME, ExportConstants::ZERO_COMMISSION_CHANNEL_ID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReservationExportHelpers

#calc_prepayment, #calculate_commission, #calculate_restaurant_revenue, #calculate_total_package_price, #date_format, #decimal_format, #format_add_on_names_with_quantity, #format_addon_names_for_report, #format_money, #format_package_names, #format_package_names_for_report, #format_package_names_with_quantity, #format_voucher_amount, #format_voucher_code, #get_reservation_distance, #paid_amount, #restaurant_delivery_fee, #sanitize_package_name, #sanitizer, #valid_email?, #voucher_amount, #voucher_code

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#date_filter_typeObject

Returns the value of attribute date_filter_type.



16
17
18
# File 'app/workers/notification_workers/voucher_group_report.rb', line 16

def date_filter_type
  @date_filter_type
end

#emailsObject

if receiver is hungryhub staff, then this attribute is required



20
21
22
# File 'app/workers/notification_workers/voucher_group_report.rb', line 20

def emails
  @emails
end

#end_dateObject

Returns the value of attribute end_date.



16
17
18
# File 'app/workers/notification_workers/voucher_group_report.rb', line 16

def end_date
  @end_date
end

#start_dateObject

Returns the value of attribute start_date.



17
18
19
# File 'app/workers/notification_workers/voucher_group_report.rb', line 17

def start_date
  @start_date
end

#voucher_typeObject

Returns the value of attribute voucher_type.



16
17
18
# File 'app/workers/notification_workers/voucher_group_report.rb', line 16

def voucher_type
  @voucher_type
end

Class Method Details

.fix_queue_perform_async(args) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/workers/notification_workers/voucher_group_report.rb', line 111

def self.fix_queue_perform_async(args)
  start_date = args[:start_date]
  end_date = args[:end_date]

  use_long_process = if start_date.present? && end_date.present?
                       (Date.parse(end_date.to_s) - Date.parse(start_date.to_s)).to_i >= 3
                     end

  attachment = Attachment.create(
    restaurant_id: args[:restaurant_id],
    exported_by: :hh_staff,
    report_type: :promocodegroup,
    name: NotificationWorkers::VoucherGroupReport.generate_name(args),
  )

  job_id = if use_long_process
             client = Sidekiq::Client.new
             client.push('class' => 'NotificationWorkers::VoucherGroupReport',
                         'queue' => 'longprocess',
                         'args' => [args.merge(use_long_process: true), attachment&.id])
           else
             NotificationWorkers::VoucherGroupReport.perform_async(args, attachment&.id)
           end
  if attachment&.persisted?
    attachment.job_id = job_id
    attachment.save!
  end

  job_id
end

.generate_name(args) ⇒ Object



142
143
144
# File 'app/workers/notification_workers/voucher_group_report.rb', line 142

def self.generate_name(args)
  "Promo_Code_Group_Report_#{args[:data_type]}_#{SecureRandom.hex(3)}"
end

Instance Method Details

#collect_voucher_groupsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/workers/notification_workers/voucher_group_report.rb', line 75

def collect_voucher_groups
  voucher_groups = VoucherGroup.includes(:vouchers)

  voucher_groups = case date_filter_type
                   when 'single'
                     filter_by_single_date(voucher_groups, start_date)
                   when 'range'
                     voucher_groups.where(vouchers: { created_at: start_date.beginning_of_day..end_date.end_of_day })
                   else
                     voucher_groups
                   end
  voucher_groups = filter_by_voucher_type(voucher_groups, voucher_type) if voucher_type?
  voucher_groups
rescue StandardError => e
  APMErrorHandler.report e
  raise e
end

#delete_keda_identifierObject



146
147
148
149
150
# File 'app/workers/notification_workers/voucher_group_report.rb', line 146

def delete_keda_identifier
  $persistent_redis.with do |redis|
    redis.lpop(LONG_PROCESS_MASTER)
  end
end

#filter_by_single_date(voucher_groups, start_date) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'app/workers/notification_workers/voucher_group_report.rb', line 93

def filter_by_single_date(voucher_groups, start_date)
  if start_date.present?
    voucher_groups = voucher_groups.where(vouchers: { created_at: start_date.all_day })
  end
  voucher_groups
rescue StandardError => e
  APMErrorHandler.report e
  raise e
end

#filter_by_voucher_type(voucher_groups, voucher_type) ⇒ Object



103
104
105
106
107
108
109
# File 'app/workers/notification_workers/voucher_group_report.rb', line 103

def filter_by_voucher_type(voucher_groups, voucher_type)
  voucher_groups = voucher_groups.where(vouchers: { voucher_category: voucher_type }) if voucher_type.present?
  voucher_groups
rescue StandardError => e
  APMErrorHandler.report e
  raise e
end

#perform(args, attachment_id = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/workers/notification_workers/voucher_group_report.rb', line 38

def perform(args, attachment_id = nil)
  args = args.to_h.with_indifferent_access
  use_keda = args.with_indifferent_access.fetch(:use_long_process, false).to_s == 'true'
  begin
    create_keda_identifier if use_keda

    args.each do |k, v|
      next if k.to_s == :use_long_process.to_s

      send("#{k}=".to_sym, v)
    end
    I18n.locale = :en
    self.voucher_groups = collect_voucher_groups

    total 100
    store status: Attachment::ON_PROGRESS_STATUS
    store email_status: Attachment::PENDING_EMAIL_STATUS
    at 0
    exist_attachment = Attachment.find_by(id: attachment_id)
    exist_attachment.update(status: :on_progress) if exist_attachment.present?

    generate_excel
    self.attachment = store_to_attachment(exist_attachment)

    subject = "Download Voucher Group - #{7.hours.from_now}"
    body = "Please download it here #{download_link}"
    StaffMailer.notify(subject, body, args[:emails]).deliver_later!

    delete_report(attachment)
  rescue StandardError => e
    APMErrorHandler.report e
    raise e
  ensure
    delete_keda_identifier if use_keda
  end
end