Class: RequoteWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/requote_worker.rb

Overview

Sidekiq worker: requote.

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    Job arguments

Options Hash (options):

  • quote_id (Integer)
  • rma_id (Integer)

    ID of the RMA driving the revision, if any

  • redirect_path (String)

    Path to redirect to on failure (defaults to the quote page)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/workers/requote_worker.rb', line 14

def perform(options = {})
  quote_id = options[:quote_id]
  rma_id = options[:rma_id]
  redirect_path = options[:redirect_path] || "/quotes/#{quote_id}"

  total 3
  at(1, 'Loading quote...')

  quote = Quote.find(quote_id)

  at(2, 'Creating revised quote...')

  reviser = Quote::Reviser.new(quote, rma_id: rma_id)
  new_quote = reviser.revise
  errors = reviser.errors

  at(3, 'Finalizing...')

  if errors.empty?
    store redirect_to: "/quotes/#{new_quote.id}/rooms"
    store message: 'Quote revised successfully.'
  else
    store redirect_to: redirect_path
    store message: "Could not requote: #{errors.join(', ')}"
    store error_message: "Could not requote: #{errors.join(', ')}"
  end
rescue StandardError => e
  store message: "Error requoting: #{e.message}"
  store error_message: "Failed to requote: #{e.message}"
  store redirect_to: redirect_path
  Rails.logger.error("RequoteWorker error for quote #{quote_id}: #{e.message}\n#{e.backtrace.join("\n")}")
  raise # AppSignal catches this automatically
end