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.

Fixed Gift Cards Storefront API

Damian Legawiecf507979ab011
diff --git a/api/app/serializers/spree/api/v2/platform/gift_card_serializer.rb b/api/app/serializers/spree/api/v2/platform/gift_card_serializer.rb
index f1d57e2144..f78be3e4b8 100644
--- a/api/app/serializers/spree/api/v2/platform/gift_card_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/gift_card_serializer.rb
@@ -3,13 +3,13 @@ module Spree
module V2
module Platform
class GiftCardSerializer < BaseSerializer
- set_type :gift_card
+ include ResourceSerializerConcern
- attributes :state, :code, :expires_at,
- :amount, :amount_remaining,
- :display_amount, :display_amount_remaining
+ set_type :gift_card
belongs_to :user
+
+ has_many :orders
end
end
end
diff --git a/api/app/serializers/spree/v2/storefront/cart_serializer.rb b/api/app/serializers/spree/v2/storefront/cart_serializer.rb
index 1af19133bc..33be38c0e1 100644
--- a/api/app/serializers/spree/v2/storefront/cart_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/cart_serializer.rb
@@ -58,7 +58,7 @@ module Spree
id_method_name: :ship_address_id,
serializer: :address,
record_type: :address
- belongs_to :gift_card
+ belongs_to :gift_card, serializer: :gift_card
end
end
end
diff --git a/api/app/serializers/spree/v2/storefront/gift_card_serializer.rb b/api/app/serializers/spree/v2/storefront/gift_card_serializer.rb
index 0f98f851ab..ece050200c 100644
--- a/api/app/serializers/spree/v2/storefront/gift_card_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/gift_card_serializer.rb
@@ -4,7 +4,10 @@ module Spree
class GiftCardSerializer < BaseSerializer
set_type :gift_card
- attributes :code, :amount, :amount_remaining, :display_amount, :display_amount_remaining
+ attributes :amount, :amount_used, :amount_remaining, :display_amount, :display_amount_used,
+ :display_amount_remaining, :expires_at
+
+ attribute :code, &:display_code
end
end
end
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 10c7895076..6984ae48aa 100644
--- a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
@@ -2,6 +2,7 @@ require 'spec_helper'
describe 'API V2 Storefront Cart Spec', type: :request do
let!(:store) { create(:store) }
+ let!(:store_credit_payment_method) { create(:store_credit_payment_method, stores: [store]) }
let(:currency) { store.default_currency }
let(:user) { create(:user) }
let(:order) { create(:order, user: user, store: store, currency: currency) }
@@ -635,19 +636,19 @@ describe 'API V2 Storefront Cart Spec', type: :request do
end
context 'with gift card' do
- let(:gift_card) { create(:gift_card, amount: 10, store: store) }
- let(:coupon_code) { gift_card.code }
+ let(:gift_card) { create(:gift_card, amount: 10, store: store, amount_used: 10) }
+ let(:order) { create(:order_with_line_items, store: store, user: nil, currency: store.default_currency, gift_card: gift_card) }
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' }
+ get '/api/v2/storefront/cart', headers: headers_order_token, 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(response.status).to eq(200)
+ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:code).with_value(gift_card.display_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')))
+ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:display_amount_used).with_value('$10.00')))
end
end
end
@@ -710,7 +711,8 @@ describe 'API V2 Storefront Cart Spec', type: :request do
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(response.status).to eq(200)
+ expect(json_response['included']).to include(have_type('gift_card').and(have_attribute(:code).with_value(gift_card.display_code)))
end
end
end
diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rb
index ebb507e07f..71efb63294 100644
--- a/storefront/spec/controllers/spree/checkout_controller_spec.rb
+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb
@@ -256,7 +256,7 @@ describe Spree::CheckoutController, type: :controller do
end
end
- xcontext 'removes expired gift card' do
+ context 'removes expired gift card' do
let!(:gift_card) { create :gift_card, code: '123', amount: 10, state: 'active' }
before do
@@ -1166,7 +1166,7 @@ describe Spree::CheckoutController, type: :controller do
expect(order.promotions).not_to include(promotion)
end
- xcontext 'for a gift card' do
+ context 'for a gift card' do
let(:gift_card) { create :gift_card, store: store }
let(:coupon_code) { gift_card.code.upcase }