Class: SpreeCmCommissioner::Seats::LockBlocks

Inherits:
Object
  • Object
show all
Extended by:
SpreeCmCommissioner::ServiceModuleThrowable
Includes:
Spree::ServiceModule::Base, BlockDateIteration
Defined in:
app/services/spree_cm_commissioner/seats/lock_blocks.rb

Overview

Takes specific seats off sale on specific dates.

Writes one ReservedBlock with status :locked per (seat x date) and deducts 1 from that date's quantity_available. Both must move together: for seat-selection trips the numeric counter is still live (RedisStock::Unstock decrements it on order completion), so a lock that only wrote a row would leave the trip advertising seats nobody can book.

PARTIAL SUCCESS IS THE CONTRACT, not an edge case. Locking a seat across a month when one date is already sold must lock the other dates and report the skip.

success(applied: [ReservedBlock, ...], skipped: [{ date:, block_label:, reason: }, ...])

Takes block_ids rather than a variant: Block carries its own variant_id, and a trip's seat map can assign different seats to different fare-class variants, so a selection may span variants.

Instance Method Summary collapse

Methods included from SpreeCmCommissioner::ServiceModuleThrowable

call!

Instance Method Details

#call(block_ids:, dates:, reason:, note: nil, actor: nil) ⇒ Object



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
# File 'app/services/spree_cm_commissioner/seats/lock_blocks.rb', line 22

def call(block_ids:, dates:, reason:, note: nil, actor: nil)
  blocks = lockable_blocks(block_ids)
  return failure(nil, :no_blocks_given) if blocks.empty?

  precondition = unenforceable_reason(blocks)
  return failure(nil, precondition) if precondition

  # Constant for the whole call, so held as state rather than threaded through every private
  # method — which is what pushed the earlier signatures past six parameters.
  @reason = reason.to_s
  @note = note
  @actor = actor

  applied = []
  skipped = []

  each_block_date(blocks, dates, skipped) do |block, date, item|
    lock_one(item, block, date, applied, skipped)
  end

  log_outcome(block_ids, dates, applied, skipped, reason: @reason)

  success(applied: applied, skipped: skipped)
rescue StandardError => e
  # Same convention as ReservedBlocks::Hold/Reserve/Cancel: any unexpected error (a bad `reason`
  # failing the model validation, a lost DB connection mid-loop) becomes a graceful failure()
  # instead of an unhandled 500 — the controller only ever branches on result.failure?.
  error = { error_type: e.class.name.demodulize, message: e.message }
  CmAppLogger.error(label: "#{self.class.name}#call failed", data: error)
  failure(nil, e.message)
end