Class: EnterOrderRoomsWorker

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

Overview

Sidekiq worker: enter order rooms.

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 options

Options Hash (options):

  • order_id (Integer)

    ID of the Order to assign rooms to

  • room_configuration_ids (Array<Integer>)

    room configuration IDs to assign

  • redirect_path (String)

    path to redirect to when the job completes



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
# File 'app/workers/enter_order_rooms_worker.rb', line 14

def perform(options = {})
  order_id = options[:order_id]
  new_rc_ids = options[:room_configuration_ids] || []
  redirect_path = options[:redirect_path] || "/orders/#{order_id}?tab=rooms"

  order = Order.find(order_id)

  total 3
  at(1, 'Loading order and room configurations...')

  old_rc_ids = order.room_configuration_ids.uniq.sort

  Order.transaction do
    at(2, 'Assigning room configurations...')
    order.room_configuration_ids = new_rc_ids.uniq.sort
    new_rc_ids_sorted = order.room_configuration_ids

    at(3, 'Calculating shipping...')
    order.recalculate_shipping = true if old_rc_ids != new_rc_ids_sorted
    order.save!
  end

  store redirect_to: redirect_path
  store message: 'Room configurations updated successfully.'
rescue StandardError => e
  store message: "Error updating rooms: #{e.message}"
  store error_message: "Failed to update room configurations: #{e.message}"
  store redirect_to: redirect_path
  Rails.logger.error("EnterOrderRoomsWorker error for order #{order_id}: #{e.message}\n#{e.backtrace.join("\n")}")
  raise # AppSignal catches this automatically
end