Class: NotificationWorkers::VoucherReport
- Inherits:
-
ApplicationWorker
- Object
- ApplicationWorker
- NotificationWorkers::VoucherReport
- Includes:
- MoneyRails::ActionViewExtension, Sidekiq::Status::Worker
- Defined in:
- app/workers/notification_workers/voucher_report.rb
Overview
This worker generates excel report and send download link to user whom requested the report through email
There are 3 kinds of receiver
-
hh: HungryHub Staff
-
restaurant: For restaurant that we send every end of the month, it contains finance report
-
owner_staff: For restaurant, when they download from owner dashboard, excel just contain voucher only
Instance Attribute Summary collapse
-
#data_type ⇒ Object
Returns the value of attribute data_type.
-
#date_filter_type ⇒ Object
Returns the value of attribute date_filter_type.
-
#date_type ⇒ Object
Returns the value of attribute date_type.
-
#emails ⇒ Object
if receiver is hungryhub staff, then this attribute is required.
-
#end_date ⇒ Object
Returns the value of attribute end_date.
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#receiver ⇒ Object
Returns the value of attribute receiver.
-
#restaurant_id ⇒ Object
Returns the value of attribute restaurant_id.
-
#start_date ⇒ Object
Returns the value of attribute start_date.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
Class Method Summary collapse
Instance Method Summary collapse
- #collect_tickets ⇒ Object
- #delete_keda_identifier ⇒ Object
- #filter_by_restaurant(tickets) ⇒ Object
- #filter_by_single_date(data_tickets, start_date, start_time, end_time) ⇒ Object
- #perform(receiver_type, args, attachment_id = nil) ⇒ Object
- #total_staled_keda_identifier ⇒ Object
Methods inherited from ApplicationWorker
Instance Attribute Details
#data_type ⇒ Object
Returns the value of attribute data_type.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def data_type @data_type end |
#date_filter_type ⇒ Object
Returns the value of attribute date_filter_type.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def date_filter_type @date_filter_type end |
#date_type ⇒ Object
Returns the value of attribute date_type.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def date_type @date_type end |
#emails ⇒ Object
if receiver is hungryhub staff, then this attribute is required
21 22 23 |
# File 'app/workers/notification_workers/voucher_report.rb', line 21 def emails @emails end |
#end_date ⇒ Object
Returns the value of attribute end_date.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def end_date @end_date end |
#end_time ⇒ Object
Returns the value of attribute end_time.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def end_time @end_time end |
#receiver ⇒ Object
Returns the value of attribute receiver.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def receiver @receiver end |
#restaurant_id ⇒ Object
Returns the value of attribute restaurant_id.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def restaurant_id @restaurant_id end |
#start_date ⇒ Object
Returns the value of attribute start_date.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def start_date @start_date end |
#start_time ⇒ Object
Returns the value of attribute start_time.
17 18 19 |
# File 'app/workers/notification_workers/voucher_report.rb', line 17 def start_time @start_time end |
Class Method Details
.fix_queue_perform_async(receiver, args) ⇒ Object
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 141 142 143 144 145 146 147 |
# File 'app/workers/notification_workers/voucher_report.rb', line 116 def self.fix_queue_perform_async(receiver, 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 = if receiver == :hh_staff Attachment.create( restaurant_id: args[:restaurant_id], exported_by: receiver, report_type: :voucher, name: NotificationWorkers::VoucherReport.generate_name(receiver, args), ) end job_id = if use_long_process client = Sidekiq::Client.new client.push('class' => 'NotificationWorkers::VoucherReport', 'queue' => 'longprocess', 'args' => [receiver, args.merge(use_long_process: true), &.id]) else NotificationWorkers::VoucherReport.perform_async(receiver, args, &.id) end if .present? && .persisted? .job_id = job_id .save! end job_id end |
.generate_name(_receiver, args) ⇒ Object
149 150 151 |
# File 'app/workers/notification_workers/voucher_report.rb', line 149 def self.generate_name(_receiver, args) "Voucher_Report_#{args[:data_type]}_#{SecureRandom.hex(3)}" end |
Instance Method Details
#collect_tickets ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/workers/notification_workers/voucher_report.rb', line 81 def collect_tickets data_tickets = Ticket.includes(:ticket_transaction, :user, :guest) tickets = if date_filter_type == 'single' filter_by_single_date(data_tickets, start_date, start_time, end_time) else data_tickets.where('created_at >= ? AND created_at <= ?', start_date, end_date) end tickets = filter_by_restaurant(tickets) if report_restaurant? tickets rescue StandardError => e APMErrorHandler.report e raise e end |
#delete_keda_identifier ⇒ Object
153 154 155 156 157 |
# File 'app/workers/notification_workers/voucher_report.rb', line 153 def delete_keda_identifier $persistent_redis.with do |redis| redis.lpop(LONG_PROCESS_MASTER) end end |
#filter_by_restaurant(tickets) ⇒ Object
108 109 110 111 112 113 114 |
# File 'app/workers/notification_workers/voucher_report.rb', line 108 def filter_by_restaurant(tickets) ticket_ids = TicketTransaction.where(restaurant_id: restaurant_id).pluck(:id) tickets.where(ticket_transaction_id: ticket_ids) rescue StandardError => e APMErrorHandler.report e raise e end |
#filter_by_single_date(data_tickets, start_date, start_time, end_time) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/workers/notification_workers/voucher_report.rb', line 96 def filter_by_single_date(data_tickets, start_date, start_time, end_time) tickets = data_tickets.where('DATE(created_at) = ?', start_date) if start_time == end_time tickets.where('TIME(created_at) = ?', start_time) else tickets.where('TIME(created_at) >= ? AND TIME(created_at) <= ?', start_time, end_time) end rescue StandardError => e APMErrorHandler.report e raise e end |
#perform(receiver_type, args, attachment_id = nil) ⇒ Object
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 74 75 76 77 78 79 |
# File 'app/workers/notification_workers/voucher_report.rb', line 39 def perform(receiver_type, args, = nil) use_keda = args.with_indifferent_access.fetch(:use_long_process, false).to_s == 'true' begin create_keda_identifier if use_keda self.receiver = receiver_type.to_sym args.to_h.with_indifferent_access.each do |k, v| next if k.to_s == :use_long_process.to_s send("#{k}=".to_sym, v) end I18n.locale = :en @tickets = collect_tickets total 100 store status: Attachment::ON_PROGRESS_STATUS store email_status: Attachment::PENDING_EMAIL_STATUS at 0 = Attachment.find_by(id: ) .update(status: :on_progress) if .present? if for_hh? || for_invoice_to_restaurant? || for_owner_staff? generate_excel_report_for_hh self. = () StaffMailer.ticket_report(@emails, download_link).deliver_later! elsif @tickets.count.zero? return false else APMErrorHandler.report('unknown voucher report receiver') raise NotImplementedError end delete_report() if excel? rescue StandardError => e APMErrorHandler.report e raise e ensure delete_keda_identifier if use_keda end end |
#total_staled_keda_identifier ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'app/workers/notification_workers/voucher_report.rb', line 159 def total_staled_keda_identifier queue = Sidekiq::Queue.new('longprocess') queue_size = queue.size keda_identifier = nil $persistent_redis.with do |redis| keda_identifier = redis.llen(LONG_PROCESS_MASTER) end keda_identifier.to_i - queue_size.to_i end |