diff options
author | Brendan Deere <brendan.g.deere@gmail.com> | 2016-12-23 08:59:30 -0800 |
---|---|---|
committer | Brendan Deere <brendan@stembolt.com> | 2017-01-12 14:17:47 -0800 |
commit | fd5d70e159cf458f6151d96d561ae67345cdde72 (patch) | |
tree | 69877304c3736ab94f4e40ac1fc22dfd835b791a /lib/solidus_subscriptions/processor.rb | |
parent | fb7e7efd02420334b56dfcda0313e20ac79dd713 (diff) |
Process user subs by address
Subscriptions are grouped together by user and and by shipping address.
A user has 3 subscriptions (A, B and C). A and B have the same shipping
address, but C will be shipped to a different location.
The processor will group and process installments for A and B together
and they will be fulfilled by the same order.
A second order will be created to fulfill C.
Diffstat (limited to 'lib/solidus_subscriptions/processor.rb')
-rw-r--r-- | lib/solidus_subscriptions/processor.rb | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/solidus_subscriptions/processor.rb b/lib/solidus_subscriptions/processor.rb index cae2ad6..4c1268b 100644 --- a/lib/solidus_subscriptions/processor.rb +++ b/lib/solidus_subscriptions/processor.rb @@ -1,6 +1,7 @@ # This class is responsible for finding subscriptions and installments # which need to be processed. It will group them together by user and attempts -# to process them together. +# to process them together. Subscriptions will also be grouped by their +# shiping address id. # # This class passes the reponsibility of actually creating the order off onto # the consolidated installment class. @@ -55,7 +56,15 @@ module SolidusSubscriptions # Create `ProcessInstallmentsJob`s for the users used to initalize the # instance def build_jobs - users.map { |user| ProcessInstallmentsJob.perform_later installments(user).map(&:id) } + users.map do |user| + installemts_by_address_and_user = installments(user).group_by do |i| + i.subscription.shipping_address_id + end + + installemts_by_address_and_user.values.each do |grouped_installments| + ProcessInstallmentsJob.perform_later grouped_installments.map(&:id) + end + end end private @@ -63,7 +72,7 @@ module SolidusSubscriptions def subscriptions_by_id @subscriptions_by_id ||= Subscription. actionable. - includes(:line_item, :user). + includes(:line_items, :user). where(user_id: user_ids). group_by(&:user_id) end |