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.

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.erb
index 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.rb
index 53d090bddb..e1aae1e9c1 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -97,6 +97,7 @@ Spree::Core::Engine.add_routes do
resources :store_credits
resources :orders, only: [:index]
resources :checkouts, only: [:index]
+ resources :gift_cards
collection do
get :bulk_modal
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 8335de1ef2..27e46051c9 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -206,6 +206,9 @@ Spree::Core::Engine.add_routes do
resources :events, only: :index
resources :subscribers
end
+
+ # Gift Cards API
+ resources :gift_cards
end
namespace :data_feeds do
diff --git a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
index 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 do
expect(json_response[:included][0]).to have_attribute(:public_metadata).with_value(order.ship_address.public_metadata)
end
end
+
+ 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
+ end
end
describe 'cart#apply_coupon_code' do
@@ -680,11 +697,65 @@ describe 'API V2 Storefront Cart Spec', type: :request do
end
end
+ 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' do
include_context 'creates guest order with guest token'
context 'with existing order' do
it_behaves_like 'apply coupon code'
+ it_behaves_like 'apply gift card coupon code'
end
it_behaves_like 'no current order'
@@ -695,6 +766,7 @@ describe 'API V2 Storefront Cart Spec', type: :request do
context 'with existing order' do
it_behaves_like 'apply coupon code'
+ it_behaves_like 'apply gift card coupon code'
end
it_behaves_like 'no current order'
@@ -765,6 +837,23 @@ describe 'API V2 Storefront Cart Spec', type: :request do
expect(json_response['included']).not_to include(have_type('promotion'))
end
end
+
+ 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
+ end
end
xcontext 'when multiple coupon codes are applied' do
diff --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.rb
new file mode 100644
index 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