Class: SpreeCmCommissioner::Trips::AddOns::Create

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

Overview

Service responsible for inline-creating a purchasable add-on from a trip.

Phase 1 (simple): the operator only enters a relation_type + price. We auto-provision a new ecommerce product (one master variant, backorderable, priced at the entered value), then link it back to the trip’s product via a ProductRelation.

Direction matches Trip#open_dated_relation (resolved by related_product_id = trip.product_id), so existing search/purchase code keeps finding the add-on:

product         = new add-on product
related_product = trip.product

Constant Summary collapse

RELATION_LABELS =

Human label per relation type, used to auto-name the add-on product.

{
  'open_dated_pair' => 'Return Trip'
}.freeze
SUPPORTED_RELATION_TYPES =

Only open_dated_pair is supported in Phase 1.

%w[open_dated_pair].freeze

Instance Method Summary collapse

Instance Method Details

#call(vendor:, trip:, relation_type:, price:) ⇒ Object



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
52
53
54
# File 'app/services/spree_cm_commissioner/trips/add_ons/create.rb', line 25

def call(vendor:, trip:, relation_type:, price:)
  relation_type = relation_type.to_s

  return failure(nil, 'vendor must be present') if vendor.blank?
  return failure(nil, 'trip must be present') if trip.blank?
  return failure(nil, 'price must be present') if price.blank?
  return failure(nil, "relation type '#{relation_type}' is not supported") unless SUPPORTED_RELATION_TYPES.include?(relation_type)
  return failure(nil, "an add-on for relation type '#{relation_type}' already exists") if existing_relation?(trip, relation_type)
  return failure(nil, 'no eligible return trip exists for this route') unless eligible_return_trip?(vendor, trip)

  ApplicationRecord.transaction do
    product = create_add_on_product!(vendor, trip, relation_type, price)
    relation = SpreeCmCommissioner::ProductRelation.create!(
      product: product,
      related_product: trip.product,
      relation_type: relation_type
    )

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