Class: SpreeCmCommissioner::Trips::Variants::Create
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Trips::Variants::Create
- 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
-
#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.
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.
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. } ) failure(nil, e.) end |