Skip to content

Commit

Permalink
Exclude canceled orders in promotion #usage_count
Browse files Browse the repository at this point in the history
This allows for a more accurate count of promotion/promotion_code usage.

The exclusion was kept as an optional boolean within`Adjustment`
`#in_completed_orders` to keep backward compatibility and allow
 for canceled orders to be excluded within a single query when required.
  • Loading branch information
ikraamg committed Jul 7, 2021
1 parent 7118543 commit 843df0b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
11 changes: 6 additions & 5 deletions core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ class Adjustment < Spree::Base
#
# @param excluded_orders [Array<Spree::Order>] Orders to exclude from query
# @return [ActiveRecord::Relation] Scoped Adjustments
def self.in_completed_orders(excluded_orders: [])
joins(:order).
merge(Spree::Order.complete).
where.not(spree_orders: { id: excluded_orders }).
distinct
def self.in_completed_orders(excluded_orders: [], exclude_canceled: false)
result = joins(:order)
.merge(Spree::Order.complete)
.where.not(spree_orders: { id: excluded_orders })
.distinct
exclude_canceled ? result.merge(Spree::Order.not_canceled) : result
end

def finalize!
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def usage_limit_exceeded?(excluded_orders: [])
def usage_count(excluded_orders: [])
Spree::Adjustment.promotion.
eligible.
in_completed_orders(excluded_orders: excluded_orders).
in_completed_orders(excluded_orders: excluded_orders, exclude_canceled: true).
where(source_id: actions).
count(:order_id)
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def usage_limit_exceeded?(excluded_orders: [])
def usage_count(excluded_orders: [])
adjustments.
eligible.
in_completed_orders(excluded_orders: excluded_orders).
in_completed_orders(excluded_orders: excluded_orders, exclude_canceled: true).
count(:order_id)
end

Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/promotion_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@
before { order.adjustments.promotion.update_all(eligible: false) }
it { is_expected.to eq 0 }
end
context "and the order is canceled" do
before { order.cancel! }
it { is_expected.to eq 0 }
it { expect(order.state).to eq 'canceled' }
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@
before { order.adjustments.promotion.update_all(eligible: false) }
it { is_expected.to eq 0 }
end
context "and the order is canceled" do
before { order.cancel! }
it { is_expected.to eq 0 }
it { expect(order.state).to eq 'canceled' }
end
end
end

Expand Down

0 comments on commit 843df0b

Please sign in to comment.