Class: OpportunityCopyWorker

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

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/workers/opportunity_copy_worker.rb', line 7

def perform(options = {})
  options = options.symbolize_keys
  customer_copying_to = Customer.find(options[:customer_id])
  current_opportunity = Opportunity.find(options[:opportunity_id])
  quotes_ids_to_copy = options[:quote_ids]
  notify_email = options[:notify_email]

  report = Opportunity::Copier.new(current_opportunity, quotes_ids_to_copy).copy_to(customer_copying_to) do |current_step, total_steps, message|
    total total_steps
    at(current_step, message)
  end

  new_opp_id = report[:new_opportunity].present? ? Opportunity.find(report[:new_opportunity].first.to_i) : nil
  if report[:errors].any?
    store redirect_to: "/opportunities/#{current_opportunity.id}"
    InternalMailer.opportunity_copy_complete(customer_copying_to, new_opp_id, user_email: notify_email, report: report).deliver_now
    message = report[:errors].first
    store message: message
  else
    message = report[:successes].first
    store redirect_to: "/opportunities/#{new_opp_id.id}"
    store message: message
  end
end