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.

Fix/gift card transitions for multiple partial redemptions and final redemption #13153 (#13176)

Ahmed7cd354cdd305
diff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rb
index 2b8fe157dc..b410213ff3 100644
--- a/core/app/models/spree/gift_card.rb
+++ b/core/app/models/spree/gift_card.rb
@@ -15,11 +15,13 @@ module Spree
event :redeem do
transition active: :redeemed
+ transition partially_redeemed: :redeemed
end
after_transition to: :redeemed, do: :after_redeem
event :partial_redeem do
transition active: :partially_redeemed
+ transition partially_redeemed: :partially_redeemed
end
end
diff --git a/core/spec/models/spree/gift_card_spec.rb b/core/spec/models/spree/gift_card_spec.rb
index 0764c393fe..6b3d9c872b 100644
--- a/core/spec/models/spree/gift_card_spec.rb
+++ b/core/spec/models/spree/gift_card_spec.rb
@@ -149,4 +149,47 @@ RSpec.describe Spree::GiftCard, type: :model do
expect(subject[7]).to eq(user.email)
end
end
+
+ describe 'State transitions' do
+ let(:store) { Spree::Store.default }
+
+ context 'when active' do
+ let(:gift_card) { create(:gift_card, state: :active, amount: 100, amount_used: 0, store: store) }
+
+ it 'transitions from active to partially_redeemed' do
+ expect { gift_card.partial_redeem! }
+ .to change(gift_card, :state).from('active').to('partially_redeemed')
+ end
+
+ it 'transitions from active to redeemed' do
+ expect { gift_card.redeem! }
+ .to change(gift_card, :state).from('active').to('redeemed')
+ end
+ end
+
+ context 'when partially_redeemed' do
+ let(:gift_card) { create(:gift_card, state: :partially_redeemed, amount: 100, amount_used: 50, store: store) }
+
+ it 'allows multiple partial redemptions (remains partially_redeemed)' do
+ expect { gift_card.partial_redeem! }
+ .to_not change(gift_card, :state)
+ expect(gift_card.state).to eq('partially_redeemed')
+ end
+
+ it 'transitions from partially_redeemed to redeemed when fully used' do
+ gift_card.update!(amount_used: 100)
+ expect { gift_card.redeem! }
+ .to change(gift_card, :state).from('partially_redeemed').to('redeemed')
+ end
+ end
+
+ context 'when redeemed' do
+ let(:gift_card) { create(:gift_card, state: :redeemed, amount: 100, amount_used: 100, store: store) }
+
+ it 'does not allow further redemption' do
+ expect { gift_card.partial_redeem! }.to raise_error(StateMachines::InvalidTransition)
+ expect { gift_card.redeem! }.to raise_error(StateMachines::InvalidTransition)
+ end
+ end
+ end
end