Class: SpreeCmCommissioner::IntercityTaxiOrder::Create

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

Instance Method Summary collapse

Instance Method Details

#call(trip_id:, from_date:, to_date:, user: nil, quantity: 1) ⇒ 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
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 6

def call(trip_id:, from_date:, to_date:, user: nil, quantity: 1)
  validate!(trip_id: trip_id, from_date: from_date, to_date: to_date, quantity: quantity)

  quantity = quantity.to_i

  order = ActiveRecord::Base.transaction do
    trip = SpreeCmCommissioner::Trip.find(trip_id)
    variant = trip.product.variants.first
    raise ArgumentError, 'No variant found for trip' if variant.blank?

    order = Spree::Order.new(state: 'cart', use_billing: true, user: user)

    line_item = order.line_items.new(
      variant_id: variant.id,
      quantity: quantity,
      from_date: from_date,
      to_date: to_date,
      private_metadata: {
        trip_id: trip.id
      }
    )

    build_guests_for!(line_item, quantity)
    assign_saved_guests_to_line_item(line_item)

    order.save!
    order.update_with_updater!

    order
  end

  success(order)
rescue StandardError => e
  failure(nil, e.message)
end