summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorAlessandro Desantis <desa.alessandro@gmail.com>2020-11-27 17:55:00 +0100
committerAlessandro Desantis <desa.alessandro@gmail.com>2021-01-30 15:23:41 +0100
commit784c1f07c61964c1a05549bffb0a709763bcf2d6 (patch)
treeb0f8523e29ef717d36c8ec338cfda11dbc091cf8 /app/services
parent2836765c00abc1ba76793ad88c07a3d89a44d672 (diff)
Pass one installment at a time to dispatcher classes
Diffstat (limited to 'app/services')
-rw-r--r--app/services/solidus_subscriptions/checkout.rb8
-rw-r--r--app/services/solidus_subscriptions/dispatcher/base.rb13
-rw-r--r--app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb5
-rw-r--r--app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb3
-rw-r--r--app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb9
-rw-r--r--app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb9
6 files changed, 15 insertions, 32 deletions
diff --git a/app/services/solidus_subscriptions/checkout.rb b/app/services/solidus_subscriptions/checkout.rb
index e348547..83fc35c 100644
--- a/app/services/solidus_subscriptions/checkout.rb
+++ b/app/services/solidus_subscriptions/checkout.rb
@@ -15,15 +15,15 @@ module SolidusSubscriptions
populate_order(order)
finalize_order(order)
- SolidusSubscriptions.configuration.success_dispatcher_class.new([installment], order).dispatch
+ SolidusSubscriptions.configuration.success_dispatcher_class.new(installment, order).dispatch
rescue StateMachines::InvalidTransition
if order.payments.any?(&:failed?)
- SolidusSubscriptions.configuration.payment_failed_dispatcher_class.new([installment], order).dispatch
+ SolidusSubscriptions.configuration.payment_failed_dispatcher_class.new(installment, order).dispatch
else
- SolidusSubscriptions.configuration.failure_dispatcher_class.new([installment], order).dispatch
+ SolidusSubscriptions.configuration.failure_dispatcher_class.new(installment, order).dispatch
end
rescue ::Spree::Order::InsufficientStock
- SolidusSubscriptions.configuration.out_of_stock_dispatcher_class.new([installment], order).dispatch
+ SolidusSubscriptions.configuration.out_of_stock_dispatcher_class.new(installment, order).dispatch
end
order
diff --git a/app/services/solidus_subscriptions/dispatcher/base.rb b/app/services/solidus_subscriptions/dispatcher/base.rb
index 84b8710..30c5034 100644
--- a/app/services/solidus_subscriptions/dispatcher/base.rb
+++ b/app/services/solidus_subscriptions/dispatcher/base.rb
@@ -3,17 +3,10 @@
module SolidusSubscriptions
module Dispatcher
class Base
- attr_reader :installments, :order
+ attr_reader :installment, :order
- # Returns a new instance of this dispatcher.
- #
- # @param installments [Array<SolidusSubscriptions::Installment>] The installments to process
- # with this dispatcher
- # @param order [Spree::Order] The order that was generated as a result of these installments
- #
- # @return [SolidusSubscriptions::Dispatcher]
- def initialize(installments, order = nil)
- @installments = installments
+ def initialize(installment, order)
+ @installment = installment
@order = order
end
diff --git a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb
index 286a4c9..54f8f2b 100644
--- a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb
+++ b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb
@@ -1,15 +1,12 @@
# frozen_string_literal: true
-# Handles failed installments.
module SolidusSubscriptions
module Dispatcher
class FailureDispatcher < Base
def dispatch
order.touch(:completed_at)
order.cancel
- installments.each do |installment|
- installment.failed!(order)
- end
+ installment.failed!(order)
end
end
end
diff --git a/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb
index ef79f84..fe3e701 100644
--- a/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb
+++ b/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb
@@ -1,11 +1,10 @@
# frozen_string_literal: true
-# Handles installments that cannot be processed for lack of stock.
module SolidusSubscriptions
module Dispatcher
class OutOfStockDispatcher < Base
def dispatch
- installments.each(&:out_of_stock)
+ installment.out_of_stock
end
end
end
diff --git a/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb
index 74bee88..a56332e 100644
--- a/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb
+++ b/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb
@@ -1,19 +1,16 @@
# frozen_string_literal: true
-# Handles payment failures for subscription installments.
module SolidusSubscriptions
module Dispatcher
class PaymentFailedDispatcher < Base
def dispatch
order.touch(:completed_at)
order.cancel
- installments.each do |installment|
- installment.payment_failed!(order)
- end
+ installment.payment_failed!(order)
::Spree::Event.fire(
- 'solidus_subscriptions.installments_failed_payment',
- installments: installments,
+ 'solidus_subscriptions.installment_failed_payment',
+ installment: installment,
order: order,
)
end
diff --git a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb
index 78563a4..0ae71dc 100644
--- a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb
+++ b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb
@@ -1,17 +1,14 @@
# frozen_string_literal: true
-# Handles installments that are processed successfully.
module SolidusSubscriptions
module Dispatcher
class SuccessDispatcher < Base
def dispatch
- installments.each do |installment|
- installment.success!(order)
- end
+ installment.success!(order)
::Spree::Event.fire(
- 'solidus_subscriptions.installments_succeeded',
- installments: installments,
+ 'solidus_subscriptions.installment_succeeded',
+ installment: installment,
order: order,
)
end