diff options
-rw-r--r-- | app/models/solidus_subscriptions/installment.rb | 4 | ||||
-rw-r--r-- | lib/solidus_subscriptions/processor.rb | 2 | ||||
-rw-r--r-- | spec/lib/solidus_subscriptions/processor_spec.rb | 11 |
3 files changed, 16 insertions, 1 deletions
diff --git a/app/models/solidus_subscriptions/installment.rb b/app/models/solidus_subscriptions/installment.rb index 9185743..6d6baaa 100644 --- a/app/models/solidus_subscriptions/installment.rb +++ b/app/models/solidus_subscriptions/installment.rb @@ -23,6 +23,10 @@ module SolidusSubscriptions where.not(id: fulfilled_ids).distinct end) + scope :with_active_subscription, (lambda do + joins(:subscription).where.not(Subscription.table_name => {state: "canceled"}) + end) + scope :actionable, (lambda do unfulfilled.where("#{table_name}.actionable_date <= ?", Time.zone.today) end) diff --git a/lib/solidus_subscriptions/processor.rb b/lib/solidus_subscriptions/processor.rb index 4946b5a..d56816a 100644 --- a/lib/solidus_subscriptions/processor.rb +++ b/lib/solidus_subscriptions/processor.rb @@ -34,7 +34,7 @@ module SolidusSubscriptions ). where( SolidusSubscriptions::Subscription.actionable.arel.constraints.reduce(:and). - or(SolidusSubscriptions::Installment.actionable.arel.constraints.reduce(:and)) + or(SolidusSubscriptions::Installment.actionable.with_active_subscription.arel.constraints.reduce(:and)) ). distinct. find_in_batches diff --git a/spec/lib/solidus_subscriptions/processor_spec.rb b/spec/lib/solidus_subscriptions/processor_spec.rb index 1c94565..3ef9aa2 100644 --- a/spec/lib/solidus_subscriptions/processor_spec.rb +++ b/spec/lib/solidus_subscriptions/processor_spec.rb @@ -129,6 +129,17 @@ RSpec.describe SolidusSubscriptions::Processor, :checkout do expect { subject }.to change { Spree::Order.complete.count }.by 2 end end + + context 'the subscription is cancelled with pending installments' do + let!(:cancelled_installment) do + installment = create(:installment, actionable_date: Date.today) + installment.subscription.cancel! + end + + it 'does not process the installment' do + expect { subject }.to change { Spree::Order.complete.count }.by expected_orders + end + end end describe '.run' do |