Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

[NUT-201] Refactor used_by? method for one_use_per_user rule (#10554)

Kacper Mękarskid002bbbe78b0
diff --git a/api/spec/controllers/spree/api/v1/orders_controller_spec.rb b/api/spec/controllers/spree/api/v1/orders_controller_spec.rb
index fb76256007..d04bdf4673 100755
--- a/api/spec/controllers/spree/api/v1/orders_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v1/orders_controller_spec.rb
@@ -708,6 +708,82 @@ module Spree
end
end
+ describe '#apply_coupon_code' do
+ subject(:apply_coupon_code) do
+ api_put :apply_coupon_code, id: order.number,
+ order_token: order.token,
+ coupon_code: coupon_code
+ end
+
+ let(:order) { create(:order_with_line_items, user: current_api_user) }
+ let(:coupon_code) { 'coupon_code' }
+
+ context 'when coupon is not found' do
+ it 'returns coupon not found response' do
+ apply_coupon_code
+
+ expect(response.status).to eq 422
+ expect(json_response[:successful]).to eq false
+ expect(json_response[:status_code]).to eq 'coupon_code_not_found'
+ expect(json_response[:error]).to eq "The coupon code you entered doesn't exist. Please try again."
+ expect(json_response[:success]).to be_nil
+ end
+
+ it 'does not apply any promotion to order' do
+ apply_coupon_code
+
+ expect(order.promotions).to be_empty
+ end
+ end
+
+ context 'when coupon is found' do
+ let!(:promotion) { create(:promotion_with_one_use_per_user_rule, code: coupon_code) }
+
+ context 'when coupon is applied' do
+ it 'returns coupon applied response' do
+ apply_coupon_code
+
+ expect(response.status).to eq 200
+ expect(json_response[:successful]).to eq true
+ expect(json_response[:status_code]).to eq 'coupon_code_applied'
+ expect(json_response[:success]).to eq 'The coupon code was successfully applied to your order.'
+ expect(json_response[:error]).to be_nil
+ end
+
+ it 'applies specified promotion to order' do
+ expect(order.promotions).to be_empty
+
+ apply_coupon_code
+
+ expect(order.promotions.count).to eq 1
+ expect(order.promotions.first.code).to eq coupon_code
+ end
+ end
+
+ context 'when coupon can not be applied' do
+ let!(:order_with_line_item_promotion) do
+ create(:order_with_line_items, coupon_code: coupon_code, user: current_api_user).tap do |order|
+ Spree::PromotionHandler::Coupon.new(order).apply
+ order.update_column(:completed_at, Time.current)
+ order.update_column(:state, 'complete')
+ end
+ end
+
+ context 'when coupon was used by user already' do
+ it 'returns coupon was used already' do
+ apply_coupon_code
+
+ expect(response.status).to eq 422
+ expect(json_response[:successful]).to eq false
+ expect(json_response[:status_code]).to be_nil
+ expect(json_response[:error]).to eq 'This coupon code can only be used once per user.'
+ expect(json_response[:success]).to be_nil
+ end
+ end
+ end
+ end
+ end
+
context 'PUT remove_coupon_code' do
let(:order) { create(:order_with_line_items) }
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb
index ca63197b85..a267c93d4a 100644
--- a/core/app/models/spree/promotion.rb
+++ b/core/app/models/spree/promotion.rb
@@ -194,21 +194,10 @@ module Spree
end
def used_by?(user, excluded_orders = [])
- [
- :adjustments,
- :line_item_adjustments,
- :shipment_adjustments
- ].any? do |adjustment_type|
- user.orders.complete.joins(adjustment_type).where(
- spree_adjustments: {
- source_type: 'Spree::PromotionAction',
- source_id: actions.map(&:id),
- eligible: true
- }
- ).where.not(
- id: excluded_orders.map(&:id)
- ).any?
- end
+ user.orders.complete.joins(:promotions).joins(:all_adjustments).
+ where.not(spree_orders: { id: excluded_orders.map(&:id) }).
+ where(spree_promotions: { id: id }).
+ where(spree_adjustments: { source_type: 'Spree::PromotionAction', eligible: true }).any?
end
private
diff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rb
index 01d418a5e3..dfb5948804 100644
--- a/core/lib/spree/testing_support/factories/promotion_factory.rb
+++ b/core/lib/spree/testing_support/factories/promotion_factory.rb
@@ -13,7 +13,16 @@ FactoryBot.define do
Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator: calculator, promotion: promotion)
end
end
+
+ trait :with_one_use_per_user_rule do
+ after(:create) do |promotion|
+ rule = Spree::Promotion::Rules::OneUsePerUser.create!
+ promotion.rules << rule
+ end
+ end
+
factory :promotion_with_item_adjustment, traits: [:with_line_item_adjustment]
+ factory :promotion_with_one_use_per_user_rule, traits: [:with_line_item_adjustment, :with_one_use_per_user_rule]
trait :with_order_adjustment do
transient do
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index ac78948fbd..018925b669 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -672,7 +672,7 @@ describe Spree::Promotion, type: :model do
context 'when the order is complete' do
it { is_expected.to be true }
- context 'when the promotion was not eligible' do
+ context 'when the promotion is not eligible' do
let(:adjustment) { order.adjustments.first }
before do