Skip to content

Commit

Permalink
Exclude #usage_count of cancelled order promotions
Browse files Browse the repository at this point in the history
This allows for a more accurate count of promotion/promotion_code usage
  • Loading branch information
ikraamg committed Jun 28, 2021
1 parent 1f5fea7 commit 299396d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def usage_count(excluded_orders: [])
Spree::Adjustment.promotion.
eligible.
in_completed_orders(excluded_orders: excluded_orders).
where(spree_orders: { id: Spree::Order.not_canceled }).
where(source_id: actions).
count(:order_id)
end
Expand Down
1 change: 1 addition & 0 deletions core/app/models/spree/promotion_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def usage_count(excluded_orders: [])
adjustments.
eligible.
in_completed_orders(excluded_orders: excluded_orders).
where(spree_orders: { id: Spree::Order.not_canceled }).
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 299396d

Please sign in to comment.