diff options
author | Brendan Deere <brendan@stembolt.com> | 2016-10-12 10:16:04 -0700 |
---|---|---|
committer | Brendan Deere <brendan@stembolt.com> | 2016-10-12 10:16:04 -0700 |
commit | 694dc061bee8e86a29e67340605547c822d92a12 (patch) | |
tree | aa3363ed0b7e414f9cb8f6e420316bab6e7285af | |
parent | 5aefefed74c73debdcde0d69e3be62cdd06bdf48 (diff) |
Only process consolidated installments for the same user
Consolidated installments make the assumption that all installments
passed in share the same user. This is a critical assumption that I feel
merrits a validation.
An error will be raised with the ids of the installments which were
attempted to be consolidated.
4 files changed, 31 insertions, 0 deletions
diff --git a/app/models/solidus_subscriptions/consolidated_installment.rb b/app/models/solidus_subscriptions/consolidated_installment.rb index 114448a..13568c7 100644 --- a/app/models/solidus_subscriptions/consolidated_installment.rb +++ b/app/models/solidus_subscriptions/consolidated_installment.rb @@ -16,6 +16,7 @@ module SolidusSubscriptions # to be used when generating a new order def initialize(installments) @installments = installments + raise UserMismatchError.new(installments) if different_owners? end # Generate a new Spree::Order based on the information associated to the @@ -138,5 +139,9 @@ module SolidusSubscriptions Spree::PromotionHandler::Cart.new(order).activate order.updater.update # reload totals end + + def different_owners? + installments.map { |i| i.subscription.user }.uniq.length > 1 + end end end diff --git a/app/models/solidus_subscriptions/user_mismatch_error.rb b/app/models/solidus_subscriptions/user_mismatch_error.rb new file mode 100644 index 0000000..c3bf513 --- /dev/null +++ b/app/models/solidus_subscriptions/user_mismatch_error.rb @@ -0,0 +1,15 @@ +module SolidusSubscriptions + class UserMismatchError < StandardError + def initialize(installments) + @installments = installments + end + + def to_s + <<-MSG.squish + Installments must have the same user to be processed as a consolidated + installment. Could not process installments: + #{@installments.map(&:id).join(', ')} + MSG + end + end +end diff --git a/spec/jobs/solidus_subscriptions/process_installments_job_spec.rb b/spec/jobs/solidus_subscriptions/process_installments_job_spec.rb index 31a9af6..56b8b4d 100644 --- a/spec/jobs/solidus_subscriptions/process_installments_job_spec.rb +++ b/spec/jobs/solidus_subscriptions/process_installments_job_spec.rb @@ -5,6 +5,7 @@ RSpec.describe SolidusSubscriptions::ProcessInstallmentsJob do let(:installments) do traits = { subscription_traits: [{ + user: root_order.user, line_item_traits: [{ spree_line_item: root_order.line_items.first }] diff --git a/spec/models/solidus_subscriptions/consolidated_installment_spec.rb b/spec/models/solidus_subscriptions/consolidated_installment_spec.rb index 194915d..6bffdf4 100644 --- a/spec/models/solidus_subscriptions/consolidated_installment_spec.rb +++ b/spec/models/solidus_subscriptions/consolidated_installment_spec.rb @@ -17,6 +17,16 @@ RSpec.describe SolidusSubscriptions::ConsolidatedInstallment do create_list(:installment, 2, traits) end + context 'initialized with installments belonging to multiple users' do + subject { consolidated_installment } + let(:installments) { build_stubbed_list :installment, 2 } + + it 'raises an error' do + expect { subject }. + to raise_error SolidusSubscriptions::UserMismatchError, /must have the same user/ + end + end + describe '#process', :checkout do subject(:order) { consolidated_installment.process } let(:subscription_line_item) { installments.first.subscription.line_item } |