Class: PickItemsSaveWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/pick_items_save_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



9
10
11
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
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
# File 'app/workers/pick_items_save_worker.rb', line 9

def perform(options = {})
  options = options.with_indifferent_access
  itemizable_type = options[:itemizable_type]
  itemizable_id = options[:itemizable_id]
  itemizable_params = options[:itemizable_params]
  version_timestamp = options[:version_timestamp]
  redirect_path = options[:redirect_path]
  return_path = options[:return_path]

  itemizable = itemizable_type.constantize.find(itemizable_id)

  total 5
  at(1, 'Validating version timestamp...')

  # Check version timestamp to prevent concurrent edit conflicts
  if version_timestamp.to_i != itemizable.line_items.version
    store error_message: 'Line items were modified already by another save. Please reset and redo your changes.'
    store redirect_to: redirect_path
    return
  end

  at(2, 'Updating line items...')

  # Handle credit order negative quantities
  if itemizable.is_a?(Order) && itemizable.order_type == 'CO'
    itemizable_params[:line_items_attributes]&.each_value do |li_atr|
      li_atr[:quantity] = (li_atr[:quantity].to_i * -1).to_s if li_atr[:quantity].to_i.positive?
    end
  end

  ActiveRecord::Base.transaction do
    itemizable.update!(itemizable_params)
    itemizable.reload

    at(3, 'Synchronizing line items with rooms...')
    itemizable.synchronize_lines(nil, true)

    at(4, 'Checking for custom products and agreements...')
    itemizable.try(:set_custom_order_agreement)
    check_for_opm_and_unlinked_replacement_items(itemizable, options)
    itemizable.try(:update_linked_po_if_st)
  end

  at(5, 'Determining redirect path...')

  # Determine the appropriate redirect path
  final_redirect = determine_redirect_path(itemizable, return_path)
  store redirect_to: final_redirect
  store info_message: 'Line items saved successfully.'
rescue StandardError => e
  error_msg = e.to_s
  if itemizable.respond_to?(:messaging_logs)
    error_msg2 = MessagingLog.last_shipping_rate_message_error(itemizable.messaging_logs)
    error_msg << ". #{error_msg2}" if error_msg2.present?
  end

  store error_message: error_msg&.first(1000)
  store redirect_to: redirect_path
  Rails.logger.error("PickItemsSaveWorker error for #{itemizable_type} #{itemizable_id}: #{e.message}\n#{e.backtrace.join("\n")}")
  raise
end