Class: SpreeCmCommissioner::Seats::BlocksHolder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/seats/blocks_holder.rb

Constant Summary collapse

HOLD_DURATION =
8.minutes

Instance Method Summary collapse

Constructor Details

#initialize(line_item_ids:, hold_by: nil) ⇒ BlocksHolder

Returns a new instance of BlocksHolder.



6
7
8
9
# File 'app/models/spree_cm_commissioner/seats/blocks_holder.rb', line 6

def initialize(line_item_ids:, hold_by: nil)
  @line_item_ids = line_item_ids
  @hold_by = hold_by
end

Instance Method Details

#hold_blocks!Object

Hold blocks for the given line items, preventing dublicate block selection.

It ensures:

  • Raises an error if already reserved.

  • Raises an error if currently on_hold by someone else.

  • Allows re-locking if already on_hold by the same guest.

  • Otherwise, proceeds to lock the given blocks.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/spree_cm_commissioner/seats/blocks_holder.rb', line 18

def hold_blocks!
  line_items = Spree::LineItem.where(id: @line_item_ids).includes(:inventory_items, guests_with_blocks: :block)
  guests_with_blocks = line_items.flat_map(&:guests_with_blocks).compact
  inventory_items = line_items.flat_map(&:inventory_items).compact

  ActiveRecord::Base.transaction do
    guests_with_blocks.each do |guest|
      inventory_items.each do |inventory_item|
        hold_specific_block!(inventory_item, guest)
      end
    end
  end
end