Class: SpreeCmCommissioner::Trips::Variants::Create

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

Overview

Service class responsible for creating product and variant for a trip. Handles validations, product creation with options, variant pricing, and stock setup.

Instance Method Summary collapse

Instance Method Details

#call(vendor:, trip_form:, price:, capacity:, option_value_name: nil) ⇒ Object

Main method that validates inputs and creates product, options, variant, and stock in transaction. Returns success with product and variant or failure with error message.

Parameters:

  • option_value_name (String, nil) (defaults to: nil)

    Seat-type option value name (e.g. ‘open dated’). Defaults to ‘Normal’ when not passed. Pass explicitly to create variants with custom seat types.



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/trips/variants/create.rb', line 13

def call(vendor:, trip_form:, price:, capacity:, option_value_name: nil)
  return failure(nil, 'vendor must be present') if vendor.blank?
  return failure(nil, 'trip_form must be present') if trip_form.blank?
  return failure(nil, 'price must be present') if price.blank?
  return failure(nil, 'capacity must be present') if capacity.blank?
  return failure(nil, 'capacity must be greater than 0') if capacity <= 0
  return failure(nil, 'name must be present') if trip_form.name.blank?

  option_value_name = option_value_name.presence || 'Normal'

  ApplicationRecord.transaction do
    product = create_product!(vendor, trip_form)
    option_values = setup_option_values!(product, option_value_name)
    variant = create_variant!(product, price, option_values)

    create_stock_item!(variant, capacity)

    success(product: product, variant: variant)
  end
rescue StandardError => e
  CmAppLogger.error(
    label: 'SpreeCmCommissioner::Trips::Variants::Create#call',
    data: {
      error_class: e.class.name,
      error_message: e.message
    }
  )
  failure(nil, e.message)
end