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
ADD routes and chg link
Michał Brożek937d144ac0e4
diff --git a/admin/app/views/spree/admin/gift_cards/_gift_card.html.erb b/admin/app/views/spree/admin/gift_cards/_gift_card.html.erbindex 287229bdc9..9d34685bcf 100644--- a/admin/app/views/spree/admin/gift_cards/_gift_card.html.erb+++ b/admin/app/views/spree/admin/gift_cards/_gift_card.html.erb@@ -22,7 +22,7 @@</td><% else %><td class="actions">- <%= link_to_edit_url(+ <%= link_to_edit(spree.edit_admin_user_gift_card_path(gift_card, user_id: params[:user_id]),data: { 'turbo-frame': '_top' }) if can? :edit, gift_card %>diff --git a/admin/config/routes.rb b/admin/config/routes.rbindex 53d090bddb..e1aae1e9c1 100644--- a/admin/config/routes.rb+++ b/admin/config/routes.rb@@ -97,6 +97,7 @@ Spree::Core::Engine.add_routes doresources :store_creditsresources :orders, only: [:index]resources :checkouts, only: [:index]+ resources :gift_cardscollection doget :bulk_modaldiff --git a/api/config/routes.rb b/api/config/routes.rbindex 8335de1ef2..27e46051c9 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -206,6 +206,9 @@ Spree::Core::Engine.add_routes doresources :events, only: :indexresources :subscribersend++ # Gift Cards API+ resources :gift_cardsendnamespace :data_feeds dodiff --git a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb b/api/spec/requests/spree/api/v2/storefront/cart_spec.rbindex 25721a580f..fef0a625f6 100644--- a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb@@ -633,6 +633,23 @@ describe 'API V2 Storefront Cart Spec', type: :request doexpect(json_response[:included][0]).to have_attribute(:public_metadata).with_value(order.ship_address.public_metadata)endend++ context 'with gift card' do+ let(:gift_card) { create(:gift_card, amount: 10, store: store) }+ let(:coupon_code) { gift_card.code }++ before do+ order.update_column(:total, 30)+ patch '/api/v2/storefront/cart/apply_coupon_code', params: { coupon_code: coupon_code }, headers: headers_bearer+ get '/api/v2/storefront/cart', headers: headers_bearer, params: { include: 'gift_card' }+ end++ it 'should return an order with gift card' do+ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:code).with_value(gift_card.code)))+ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:display_amount).with_value('$10.00')))+ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:display_amount_remaining).with_value('$0.00')))+ end+ endenddescribe 'cart#apply_coupon_code' do@@ -680,11 +697,65 @@ describe 'API V2 Storefront Cart Spec', type: :request doendend+ shared_examples 'apply gift card coupon code' do+ before { execute }++ context 'apply gift card' do+ subject(:apply_gift_card) { patch '/api/v2/storefront/cart/apply_coupon_code', params: params, headers: headers }++ let(:execute) { nil }+ let(:gift_card) { create(:gift_card, amount: 10, store: store) }+ let(:coupon_code) { gift_card.code }+ let(:params) { { coupon_code: coupon_code, include: 'gift_card' } }++ before do+ # create(:stripe_gateway, stores: [store])+ end++ it 'applies the gift card' do+ apply_gift_card+ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:code).with_value(gift_card.code)))+ end++ context 'for a gift card with minimal order amount' do+ let(:order_total) { order.total }+ let(:gift_card) { create(:gift_card, store: store, amount: 10, minimum_order_amount: minimal_order_amount) }++ context 'when the condition is met' do+ let(:minimal_order_amount) { order.total }++ it 'applies the gift card' do+ apply_gift_card++ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:code).with_value(gift_card.code)))++ expect(order.reload.gift_card).to eq(gift_card)+ expect(order.total_applied_store_credit).to eq(gift_card.amount)+ end+ end++ context 'when the condition is not met' do+ let(:minimal_order_amount) { order.total + 1 }++ it 'skips applying the gift card' do+ apply_gift_card++ expect(json_response['error']).to eq("You can't apply the gift card. The order total must be at least #{gift_card.display_minimum_order_amount}, excluding shipping cost.")++ expect(order.reload.gift_card).to be(nil)+ expect(order.total_applied_store_credit).to eq(0)+ end+ end+ end+ end+ end+context 'as a guest user' doinclude_context 'creates guest order with guest token'context 'with existing order' doit_behaves_like 'apply coupon code'+ it_behaves_like 'apply gift card coupon code'endit_behaves_like 'no current order'@@ -695,6 +766,7 @@ describe 'API V2 Storefront Cart Spec', type: :request docontext 'with existing order' doit_behaves_like 'apply coupon code'+ it_behaves_like 'apply gift card coupon code'endit_behaves_like 'no current order'@@ -765,6 +837,23 @@ describe 'API V2 Storefront Cart Spec', type: :request doexpect(json_response['included']).not_to include(have_type('promotion'))endend++ context 'removing the gift card' do+ let(:gift_card) { create(:gift_card, store: store) }+ let(:coupon_code) { gift_card.code }++ before do+ order.apply_gift_card(gift_card)+ end++ context 'when gift card is applied' do+ before { execute }++ it 'changes the adjustment total to 0.0' do+ expect(order.reload.gift_card_id).to be_nil+ end+ end+ endendxcontext 'when multiple coupon codes are applied' dodiff --git a/core/spec/models/spree/promotion_handler/coupon_spec_with_gift_card.rb b/core/spec/models/spree/promotion_handler/coupon_spec_with_gift_card.rbnew file mode 100644index 0000000000..00d6363068--- /dev/null+++ b/core/spec/models/spree/promotion_handler/coupon_spec_with_gift_card.rb@@ -0,0 +1,68 @@+require 'spec_helper'++describe Spree::PromotionHandler::Coupon, type: :model do+ describe 'for a gift card' do+ describe 'apply' do+ let(:store) { Spree::Store.default }+ let(:order) { create(:order_with_line_items, store: store) }+ let(:gift_card) { create(:gift_card, store: store, amount: 10) }++ before do+ order.update_column(:total, 30)+ end++ context 'when gift card is not applied' do+ it 'applies gift card' do+ order.coupon_code = gift_card.code++ described_class.new(order).apply++ expect(order.reload.gift_card).to eq(gift_card)+ expect(order.total_applied_store_credit).to eq(10)+ end+ end++ context 'when the gift card is applied to another order' do+ let(:old_order) { create(:order, store: store) }++ before do+ old_order.update_column(:total, 30)+ end++ it "doesn't apply the gift card to a new order" do+ old_order.coupon_code = gift_card.code+ described_class.new(old_order).apply++ expect(old_order.reload.gift_card).to eq(gift_card)+ expect(old_order.total_applied_store_credit).to eq(10)++ order.coupon_code = gift_card.code+ described_class.new(order).apply++ expect(order.reload.gift_card).to eq(nil)+ expect(order.total_applied_store_credit).to eq(0)+ end+ end+ end+ end++ describe '#remove' do+ context 'when gift card is applied' do+ let(:store) { Spree::Store.default }+ let(:order) { create(:order_with_line_items, store: store, gift_card: gift_card) }+ let(:gift_card) { create(:gift_card, store: store, amount: 10) }++ subject { Spree::PromotionHandler::Coupon.new(order) }++ before do+ order.update_column(:total, 30)+ order.apply_gift_card(gift_card)+ end++ it 'removes gift card' do+ subject.remove(gift_card.code)+ expect(order.reload.gift_card).to be_nil+ end+ end+ end+end