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.

Promotion#with_coupon_code returns the newest created promotion with actions

Damian Legawiec6f2b0c390c63
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb
index 8d967c4b26..745df37a51 100644
--- a/core/app/models/spree/promotion.rb
+++ b/core/app/models/spree/promotion.rb
@@ -45,7 +45,7 @@ module Spree
def self.with_coupon_code(coupon_code)
where("lower(#{table_name}.code) = ?", coupon_code.strip.downcase).
includes(:promotion_actions).where.not(spree_promotion_actions: { id: nil }).
- first
+ last
end
def self.active
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index e1eb796c39..89ba8e78bb 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -5,59 +5,63 @@ describe Spree::Promotion, type: :model do
let(:promotion) { create(:promotion) }
describe 'validations' do
- before do
- @valid_promotion = build(:promotion, name: 'A promotion', stores: [create(:store)])
- end
+ let!(:valid_promotion) { build(:promotion, name: 'A promotion', stores: [create(:store)]) }
it 'valid_promotion is valid' do
- expect(@valid_promotion).to be_valid
+ expect(valid_promotion).to be_valid
end
it 'validates usage limit' do
- @valid_promotion.usage_limit = -1
- expect(@valid_promotion).not_to be_valid
+ valid_promotion.usage_limit = -1
+ expect(valid_promotion).not_to be_valid
- @valid_promotion.usage_limit = 100
- expect(@valid_promotion).to be_valid
+ valid_promotion.usage_limit = 100
+ expect(valid_promotion).to be_valid
end
it 'validates name' do
- @valid_promotion.name = nil
- expect(@valid_promotion).not_to be_valid
+ valid_promotion.name = nil
+ expect(valid_promotion).not_to be_valid
+ end
+
+ it 'can create multiple promos with the same code' do
+ create(:promotion, code: 'ABC')
+ valid_promotion.code = 'ABC'
+ expect(valid_promotion).to be_valid
end
describe 'expires_at_must_be_later_than_starts_at' do
before do
- @valid_promotion.starts_at = Date.today
+ valid_promotion.starts_at = Date.today
end
context 'starts_at is a date earlier than expires_at' do
- before { @valid_promotion.expires_at = 5.days.from_now }
+ before { valid_promotion.expires_at = 5.days.from_now }
it 'is valid' do
- expect(@valid_promotion).to be_valid
+ expect(valid_promotion).to be_valid
end
end
context 'starts_at is a date earlier than expires_at' do
- before { @valid_promotion.expires_at = 5.days.ago }
+ before { valid_promotion.expires_at = 5.days.ago }
context 'is not valid' do
- before { @valid_promotion.valid? }
+ before { valid_promotion.valid? }
- it { expect(@valid_promotion).not_to be_valid }
- it { expect(@valid_promotion.errors[:expires_at]).to include(I18n.t(:invalid_date_range, scope: 'activerecord.errors.models.spree/promotion.attributes.expires_at')) }
+ it { expect(valid_promotion).not_to be_valid }
+ it { expect(valid_promotion.errors[:expires_at]).to include(I18n.t(:invalid_date_range, scope: 'activerecord.errors.models.spree/promotion.attributes.expires_at')) }
end
end
context 'starts_at and expires_at are nil' do
before do
- @valid_promotion.expires_at = nil
- @valid_promotion.starts_at = nil
+ valid_promotion.expires_at = nil
+ valid_promotion.starts_at = nil
end
it 'is valid' do
- expect(@valid_promotion).to be_valid
+ expect(valid_promotion).to be_valid
end
end
end
@@ -604,10 +608,16 @@ describe Spree::Promotion, type: :model do
end
context 'when promotion has no actions' do
- let!(:promotion_without_actions) { create(:promotion, code: 'MY-COUPON-123').actions.clear }
+ let!(:promotion_without_actions) { create(:promotion, code: 'MY-COUPON-123') }
+ let!(:promotion_with_actions) { create(:promotion_with_order_adjustment, code: 'MY-COUPON-123') }
it 'then returns the one with an action' do
- expect(described_class.with_coupon_code('MY-COUPON-123')).to be_nil
+ expect(described_class.with_coupon_code('MY-COUPON-123')).to eq(promotion_with_actions)
+ end
+
+ it 'return the last one created' do
+ promotion_with_actions_2 = create(:promotion_with_order_adjustment, code: 'MY-COUPON-123')
+ expect(described_class.with_coupon_code('MY-COUPON-123')).to eq(promotion_with_actions_2)
end
end
end