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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 2b8fe157dc..b410213ff3 100644--- a/core/app/models/spree/gift_card.rb+++ b/core/app/models/spree/gift_card.rb@@ -15,11 +15,13 @@ module Spreeevent :redeem dotransition active: :redeemed+ transition partially_redeemed: :redeemedendafter_transition to: :redeemed, do: :after_redeemevent :partial_redeem dotransition active: :partially_redeemed+ transition partially_redeemed: :partially_redeemedendenddiff --git a/core/spec/models/spree/gift_card_spec.rb b/core/spec/models/spree/gift_card_spec.rbindex 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 doexpect(subject[7]).to eq(user.email)endend++ 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+ endend