Class: NotificationWorkers::VouchersReport

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

Overview

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

There are 1 kind of receiver

  1. hh: HungryHub Staff

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#create_date_filter_typeObject

Returns the value of attribute create_date_filter_type.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def create_date_filter_type
  @create_date_filter_type
end

#create_end_dateObject

Returns the value of attribute create_end_date.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def create_end_date
  @create_end_date
end

#create_start_dateObject

Returns the value of attribute create_start_date.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def create_start_date
  @create_start_date
end

#emailsObject

Returns the value of attribute emails.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def emails
  @emails
end

#expiry_date_filter_typeObject

Returns the value of attribute expiry_date_filter_type.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def expiry_date_filter_type
  @expiry_date_filter_type
end

#expiry_end_dateObject

Returns the value of attribute expiry_end_date.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def expiry_end_date
  @expiry_end_date
end

#expiry_start_dateObject

Returns the value of attribute expiry_start_date.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def expiry_start_date
  @expiry_start_date
end

#voucher_typesObject

Returns the value of attribute voucher_types.



15
16
17
# File 'app/workers/notification_workers/vouchers_report.rb', line 15

def voucher_types
  @voucher_types
end

Class Method Details

.fix_queue_perform_async(args) ⇒ Object

Fixes queue for long-running jobs



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/workers/notification_workers/vouchers_report.rb', line 120

def self.fix_queue_perform_async(args)
  start_date = args[:create_start_date]
  end_date = args[:create_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: :promocode,
    name: NotificationWorkers::VouchersReport.generate_name(args),
  )

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

  job_id
end

.generate_name(args) ⇒ Object



151
152
153
# File 'app/workers/notification_workers/vouchers_report.rb', line 151

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

Instance Method Details

#collect_vouchersObject

This method collects and filters the vouchers



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/workers/notification_workers/vouchers_report.rb', line 90

def collect_vouchers
  # Start with all vouchers
  vouchers = Voucher.all

  # Apply the create date filters based on the date type
  if create_date_filter_type == 'single' && create_start_date.present?
    vouchers = vouchers.where(created_at: create_start_date.beginning_of_day..create_start_date.end_of_day)
  elsif create_date_filter_type == 'range' && create_start_date.present? && create_end_date.present?
    vouchers = vouchers.where(created_at: create_start_date.beginning_of_day..create_end_date.end_of_day)
  end

  # Apply the expiry date filters based on the date type
  if expiry_date_filter_type == 'single' && expiry_start_date.present?
    vouchers = vouchers.where(expiry_date: expiry_start_date.beginning_of_day..expiry_start_date.end_of_day)
  elsif expiry_date_filter_type == 'range' && expiry_start_date.present? && expiry_end_date.present?
    vouchers = vouchers.where(expiry_date: expiry_start_date.beginning_of_day..expiry_end_date.end_of_day)
  end

  # Apply the voucher types filter if present
  if voucher_types.present?
    vouchers = vouchers.where(voucher_category: voucher_types)
  end

  vouchers # Return the filtered vouchers
rescue StandardError => e
  APMErrorHandler.report(e, 'Error occurred while collecting vouchers')
  raise e
end

#parse_date(date) ⇒ Object

Parse and store date values with logging for easier debugging



20
21
22
23
24
25
26
27
28
29
30
# File 'app/workers/notification_workers/vouchers_report.rb', line 20

def parse_date(date)
  return nil if date.blank?

  parsed_date = begin
    Date.parse(date.to_s)
  rescue StandardError
    nil
  end
  HH_LOGGER.warn("Failed to parse date: #{date}") unless parsed_date
  parsed_date
end

#perform(args, attachment_id = nil) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/workers/notification_workers/vouchers_report.rb', line 48

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

  begin
    create_keda_identifier if use_keda

    # Assign all parameters
    args.each do |k, v|
      next if k.to_s == 'use_long_process'

      send("#{k}=", v)
    end

    I18n.locale = :en
    self.vouchers = collect_vouchers

    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 Report - #{Time.zone.now + 7.hours}"
    body = "Please download it here: #{download_link}"
    StaffMailer.notify(subject, body, emails).deliver_later!

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