Class: SpreeCmCommissioner::IntercityTaxiOrder::Create

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trip_id:, from_date:, to_date:, user_id:, quantity:) ⇒ Create

Returns a new instance of Create.



16
17
18
19
20
21
22
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 16

def initialize(trip_id:, from_date:, to_date:, user_id:, quantity:)
  @trip_id = trip_id
  @from_date = from_date
  @to_date = to_date
  @user_id = user_id
  @quantity = quantity.to_i <= 0 ? 1 : quantity.to_i
end

Instance Attribute Details

#from_dateObject (readonly)

Returns the value of attribute from_date.



4
5
6
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 4

def from_date
  @from_date
end

#quantityObject (readonly)

Returns the value of attribute quantity.



4
5
6
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 4

def quantity
  @quantity
end

#to_dateObject (readonly)

Returns the value of attribute to_date.



4
5
6
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 4

def to_date
  @to_date
end

#trip_idObject (readonly)

Returns the value of attribute trip_id.



4
5
6
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 4

def trip_id
  @trip_id
end

#user_idObject (readonly)

Returns the value of attribute user_id.



4
5
6
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 4

def user_id
  @user_id
end

Class Method Details

.call(trip_id:, from_date:, to_date:, user_id: nil, quantity: 1) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 6

def self.call(trip_id:, from_date:, to_date:, user_id: nil, quantity: 1)
  new(
    trip_id: trip_id,
    from_date: from_date,
    to_date: to_date,
    user_id: user_id,
    quantity: quantity
  ).call
end

Instance Method Details

#callObject



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
# File 'app/services/spree_cm_commissioner/intercity_taxi_order/create.rb', line 24

def call
  validate!

  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_id: user_id)

    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)

    order.save!
    order.update_with_updater!

    order
  end
end