Class: Quote::Mover

Inherits:
Object
  • Object
show all
Defined in:
app/services/quote/mover.rb

Overview

Service object: mover.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quote, options = {}) ⇒ Mover

Returns a new instance of Mover.



6
7
8
9
10
# File 'app/services/quote/mover.rb', line 6

def initialize(quote, options = {})
  @options = options
  @quote = quote
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'app/services/quote/mover.rb', line 4

def logger
  @logger
end

#quoteObject (readonly)

Returns the value of attribute quote.



4
5
6
# File 'app/services/quote/mover.rb', line 4

def quote
  @quote
end

#resultsObject (readonly)

Returns the value of attribute results.



4
5
6
# File 'app/services/quote/mover.rb', line 4

def results
  @results
end

Instance Method Details

#move_room_configuration(room_configuration, opportunity) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/quote/mover.rb', line 42

def move_room_configuration(room_configuration, opportunity)
  room_configuration.opportunity = opportunity
  room_configuration.save!
  room_configuration.reload
  # NOTE: engineering prefers to keep all for context
  room_configuration.uploads.where(category: %w[electrical_plan_pdf installation_plan_pdf]).find_each { |u| u.update(category: 'archive', note: "Was #{u.category}") }
  # Queue for regeneration after the surrounding Quote.transaction commits
  ActiveRecord.after_all_transactions_commit do
    GenerateRoomPlansWorker.perform_async(room_configuration.id)
  end
end

#move_to(opportunity) ⇒ Object



12
13
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
# File 'app/services/quote/mover.rb', line 12

def move_to(opportunity)
  @results = []
  success = false

  if @quote.orders.active.non_carts.present?
    @results << "Cannot move a quote with active orders, cancel them before."
    return false
  end

  Quote.transaction do
    @quote.opportunity = opportunity
    @quote.state = 'pending' unless quote.complete?
    @quote.contact_points.clear # Clear out to be safe
    @quote.save!
    @quote.reload
    @quote.room_configurations.each { |room_configuration| move_room_configuration(room_configuration, opportunity) } if @quote.room_configurations.present?
    # NOTE: engineering prefers to keep all for context
    @quote.uploads.each { |u| u.update(category: 'archive', note: "Was #{u.category}") }

    success = true
  rescue StandardError => e
    success = false
    @results << "Quote could not be moved: #{e}"
    @logger.error e.inspect
    raise ActiveRecord::Rollback
  end

  success
end