Class: SpreeCmCommissioner::Trips::CreateSingleLeg

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

Overview

Service class responsible for creating a single-leg trip in the booking system. Orchestrates the creation of product variant, trip record, stops, calendar, and inventory.

Instance Method Summary collapse

Instance Method Details

#call(vendor:, trip_form:) ⇒ Object

Main service method that validates the trip form and orchestrates creation. Creates variant, trip, calendar, and inventory within a database transaction. Returns success with created objects or failure with error message on exceptions.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/spree_cm_commissioner/trips/create_single_leg.rb', line 11

def call(vendor:, trip_form:)
  return failure(nil, trip_form.errors.full_messages.to_sentence) unless trip_form.valid_data?

  ApplicationRecord.transaction do
    variant = create_variant!(vendor, trip_form)
    trip = create_trip!(vendor, trip_form, variant)
    calendar = setup_calendar!(variant.product, trip_form.service_calendar)
    generate_inventory!(variant)

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