summaryrefslogtreecommitdiff
path: root/app/services/solidus_subscriptions/subscription_generator.rb
diff options
context:
space:
mode:
authorAlessandro Desantis <desa.alessandro@gmail.com>2021-02-05 13:30:47 +0100
committerGitHub <noreply@github.com>2021-02-05 13:30:47 +0100
commit3a3657b79bd5d98025136da4b39751e6a43b7b9f (patch)
treeb7a57b183641d64d05e5e70e281fd78cb5e37ebd /app/services/solidus_subscriptions/subscription_generator.rb
parent69f0ca038b66d0ca36971405e51c0d3aa916cf19 (diff)
parent4717d24c0e7203210efe8c23b55467c3e728cdc5 (diff)
Merge pull request #172 from solidusio-contrib/aldesantis/refactoring
Processor and Checkout refactoring
Diffstat (limited to 'app/services/solidus_subscriptions/subscription_generator.rb')
-rw-r--r--app/services/solidus_subscriptions/subscription_generator.rb65
1 files changed, 0 insertions, 65 deletions
diff --git a/app/services/solidus_subscriptions/subscription_generator.rb b/app/services/solidus_subscriptions/subscription_generator.rb
deleted file mode 100644
index 8153912..0000000
--- a/app/services/solidus_subscriptions/subscription_generator.rb
+++ /dev/null
@@ -1,65 +0,0 @@
-# frozen_string_literal: true
-
-# This module is responsible for taking SolidusSubscriptions::LineItem
-# objects and creating SolidusSubscriptions::Subscription Objects
-module SolidusSubscriptions
- module SubscriptionGenerator
- extend self
-
- SubscriptionConfiguration = Struct.new(:interval_length, :interval_units, :end_date)
-
- # Create and persist a subscription for a collection of subscription
- # line items
- #
- # @param subscription_line_items [Array<SolidusSubscriptions::LineItem>] The
- # subscription_line_items to be activated
- #
- # @return [SolidusSubscriptions::Subscription]
- def activate(subscription_line_items)
- return if subscription_line_items.empty?
-
- order = subscription_line_items.first.order
- configuration = subscription_configuration(subscription_line_items.first)
-
- subscription_attributes = {
- user: order.user,
- line_items: subscription_line_items,
- store: order.store,
- shipping_address: order.ship_address,
- billing_address: order.bill_address,
- payment_source: order.payments.valid.last&.payment_source,
- payment_method: order.payments.valid.last&.payment_method,
- **configuration.to_h
- }
-
- Subscription.create!(subscription_attributes) do |sub|
- sub.actionable_date = sub.next_actionable_date
- end
- end
-
- # Group a collection of line items by common subscription configuration
- # options. Grouped subscription_line_items can belong to a single
- # subscription.
- #
- # @param subscription_line_items [Array<SolidusSubscriptions::LineItem>] The
- # subscription_line_items to be grouped.
- #
- # @return [Array<Array<SolidusSubscriptions::LineItem>>]
- def group(subscription_line_items)
- subscription_line_items.group_by do |li|
- subscription_configuration(li)
- end.
- values
- end
-
- private
-
- def subscription_configuration(subscription_line_item)
- SubscriptionConfiguration.new(
- subscription_line_item.interval_length,
- subscription_line_item.interval_units,
- subscription_line_item.end_date
- )
- end
- end
-end