Class: SpreeCmCommissioner::CustomerPromotionCreator

Inherits:
BaseInteractor show all
Defined in:
app/interactors/spree_cm_commissioner/customer_promotion_creator.rb

Constant Summary collapse

CUSTOMER_PROMOTION_RULE_TYPE =
'SpreeCmCommissioner::Promotion::Rules::Customers'.freeze
PROMOTION_ACTION_TYPE =
'Spree::Promotion::Actions::CreateAdjustment'.freeze
CALCULATOR_TYPE =
'Spree::Calculator::FlatRate'.freeze

Instance Method Summary collapse

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/interactors/spree_cm_commissioner/customer_promotion_creator.rb', line 7

def call
  customer = SpreeCmCommissioner::Customer.find(context.customer_id)
  if customer.blank?
    context.fail!(message: 'Customer not found')
    return
  end

  promotion = find_or_initialize_promotion(customer)
  if promotion.new_record?
    create_promotion(promotion, customer, context.reason, context.discount_amount, context.store)
  else
    update_promotion(promotion, customer, context.reason, context.discount_amount)
  end

  context.success = true
rescue StandardError => e
  context.fail!(message: e.message)
end