Class: SpreeCmCommissioner::CheckIns::CreateBulk

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/check_ins/create_bulk.rb

Instance Method Summary collapse

Instance Method Details

#call(check_ins_attributes:, check_in_by: nil, confirmed_at: nil) ⇒ Object



6
7
8
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
# File 'app/services/spree_cm_commissioner/check_ins/create_bulk.rb', line 6

def call(check_ins_attributes:, check_in_by: nil, confirmed_at: nil)
  return failure(:guest_ids_must_not_blank) if check_ins_attributes.blank?

  confirmed_at ||= Time.current
  guest_ids = check_ins_attributes.pluck(:guest_id)

  indexed_guests = SpreeCmCommissioner::Guest
                   .includes(:event, line_item: :order)
                   .where(id: guest_ids)
                   .index_by(&:id)

  check_ins = ActiveRecord::Base.transaction do
    check_ins_attributes.map do |attrs|
      guest = indexed_guests[attrs[:guest_id].to_i]
      raise ActiveRecord::RecordNotFound, 'Guest or related record not found' if guest.blank?

      order = guest.line_item.order
      fail_check_in!(guest, 'Cannot check in Guest from cancelled order') if order.canceled?

      # Validate guest state for check-in
      fail_check_in!(guest, 'Guest is locked and cannot be checked in') if guest.locked?
      fail_check_in!(guest, 'Guest has been transferred and cannot be checked in') if guest.transferred?

      check_in = check_in!(guest, attrs, check_in_by, confirmed_at)
      update_guest!(guest, attrs[:guest_attributes]) if attrs[:guest_attributes].present?

      check_in
    end
  end

  success(check_ins: check_ins)
rescue ActiveRecord::RecordInvalid => e
  failure(:invalid_record, e.record.errors.full_messages.join(', '))
rescue ActiveRecord::RecordNotFound => e
  failure(:record_not_found, e.message)
end