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 customer gift cards API endpoint

Damian Legawiecd3dfae341bdc
diff --git a/api/app/controllers/spree/api/v3/store/customer/gift_cards_controller.rb b/api/app/controllers/spree/api/v3/store/customer/gift_cards_controller.rb
new file mode 100644
index 0000000000..968b8a7622
--- /dev/null
+++ b/api/app/controllers/spree/api/v3/store/customer/gift_cards_controller.rb
@@ -0,0 +1,36 @@
+module Spree
+ module Api
+ module V3
+ module Store
+ module Customer
+ class GiftCardsController < ResourceController
+ prepend_before_action :require_authentication!
+
+ protected
+
+ def set_parent
+ @parent = current_user
+ end
+
+ def parent_association
+ :gift_cards
+ end
+
+ def model_class
+ Spree::GiftCard
+ end
+
+ def serializer_class
+ Spree.api.gift_card_serializer
+ end
+
+ # Override scope to filter by current store and order by created_at desc
+ def scope
+ super.where(store: current_store).order(created_at: :desc)
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v3/gift_card_serializer.rb b/api/app/serializers/spree/api/v3/gift_card_serializer.rb
new file mode 100644
index 0000000000..d60d95698b
--- /dev/null
+++ b/api/app/serializers/spree/api/v3/gift_card_serializer.rb
@@ -0,0 +1,78 @@
+module Spree
+ module Api
+ module V3
+ class GiftCardSerializer < BaseSerializer
+ typelize code: :string,
+ state: :string,
+ amount: :number,
+ amount_used: :number,
+ amount_authorized: :number,
+ amount_remaining: :number,
+ display_amount: :string,
+ display_amount_used: :string,
+ display_amount_remaining: :string,
+ currency: :string,
+ expires_at: 'string | null',
+ redeemed_at: 'string | null',
+ expired: :boolean,
+ active: :boolean
+
+ attribute :code do |gift_card|
+ gift_card.display_code
+ end
+
+ attribute :state do |gift_card|
+ gift_card.display_state
+ end
+
+ attributes :currency
+
+ attribute :amount do |gift_card|
+ gift_card.amount.to_f
+ end
+
+ attribute :amount_used do |gift_card|
+ gift_card.amount_used.to_f
+ end
+
+ attribute :amount_authorized do |gift_card|
+ gift_card.amount_authorized.to_f
+ end
+
+ attribute :amount_remaining do |gift_card|
+ gift_card.amount_remaining.to_f
+ end
+
+ attribute :display_amount do |gift_card|
+ gift_card.display_amount.to_s
+ end
+
+ attribute :display_amount_used do |gift_card|
+ gift_card.display_amount_used.to_s
+ end
+
+ attribute :display_amount_remaining do |gift_card|
+ gift_card.display_amount_remaining.to_s
+ end
+
+ attribute :expires_at do |gift_card|
+ gift_card.expires_at&.iso8601
+ end
+
+ attribute :redeemed_at do |gift_card|
+ gift_card.redeemed_at&.iso8601
+ end
+
+ attribute :expired do |gift_card|
+ gift_card.expired?
+ end
+
+ attribute :active do |gift_card|
+ gift_card.active?
+ end
+
+ attributes created_at: :iso8601, updated_at: :iso8601
+ end
+ end
+ end
+end
diff --git a/api/config/routes.rb b/api/config/routes.rb
index ce9abd5b6f..e1e049f216 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -53,6 +53,7 @@ Spree::Core::Engine.add_routes do
patch '/', to: 'account#update'
resources :addresses, only: [:index, :show, :create, :update, :destroy]
resources :credit_cards, only: [:index, :show, :destroy]
+ resources :gift_cards, only: [:index, :show]
end
# Wishlists
diff --git a/api/lib/spree/api/dependencies.rb b/api/lib/spree/api/dependencies.rb
index 9e46f9d941..b398a653ee 100644
--- a/api/lib/spree/api/dependencies.rb
+++ b/api/lib/spree/api/dependencies.rb
@@ -118,6 +118,7 @@ module Spree
taxon_serializer: 'Spree::Api::V3::TaxonSerializer',
order_promotion_serializer: 'Spree::Api::V3::OrderPromotionSerializer',
digital_link_serializer: 'Spree::Api::V3::DigitalLinkSerializer',
+ gift_card_serializer: 'Spree::Api::V3::GiftCardSerializer',
metafield_serializer: 'Spree::Api::V3::MetafieldSerializer',
# platform serializers
diff --git a/api/spec/controllers/spree/api/v3/store/customer/gift_cards_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/customer/gift_cards_controller_spec.rb
new file mode 100644
index 0000000000..9ecf6432ff
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/customer/gift_cards_controller_spec.rb
@@ -0,0 +1,179 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::Customer::GiftCardsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let!(:gift_card) { create(:gift_card, user: user, store: store, amount: 100, amount_used: 25) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ describe 'GET #index' do
+ it 'returns the user gift cards' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].length).to eq(user.gift_cards.where(store: store).count)
+ end
+
+ it 'returns gift card attributes' do
+ get :index
+
+ card_data = json_response['data'].first
+ expect(card_data).to include(
+ 'id',
+ 'code',
+ 'state',
+ 'amount',
+ 'amount_used',
+ 'amount_remaining',
+ 'display_amount',
+ 'display_amount_used',
+ 'display_amount_remaining',
+ 'currency',
+ 'expires_at',
+ 'expired',
+ 'active'
+ )
+ end
+
+ it 'returns correct amounts' do
+ get :index
+
+ card_data = json_response['data'].first
+ expect(card_data['amount']).to eq(100.0)
+ expect(card_data['amount_used']).to eq(25.0)
+ expect(card_data['amount_remaining']).to eq(75.0)
+ end
+
+ it 'only returns gift cards belonging to the current user' do
+ other_user = create(:user)
+ other_card = create(:gift_card, user: other_user, store: store)
+
+ get :index
+
+ ids = json_response['data'].map { |c| c['id'] }
+ expect(ids).to include(gift_card.prefix_id)
+ expect(ids).not_to include(other_card.prefix_id)
+ end
+
+ it 'only returns gift cards from the current store' do
+ other_store = create(:store)
+ other_store_card = create(:gift_card, user: user, store: other_store)
+
+ get :index
+
+ ids = json_response['data'].map { |c| c['id'] }
+ expect(ids).to include(gift_card.prefix_id)
+ expect(ids).not_to include(other_store_card.prefix_id)
+ end
+
+ it 'returns gift cards ordered by created_at desc' do
+ older_card = create(:gift_card, user: user, store: store, created_at: 1.day.ago)
+ newer_card = create(:gift_card, user: user, store: store, created_at: 1.day.from_now)
+
+ get :index
+
+ ids = json_response['data'].map { |c| c['id'] }
+ expect(ids.first).to eq(newer_card.prefix_id)
+ expect(ids.last).to eq(older_card.prefix_id)
+ end
+
+ context 'with expired gift card' do
+ let!(:expired_card) { create(:gift_card, user: user, store: store, expires_at: 1.day.ago) }
+
+ it 'includes expired gift cards' do
+ get :index
+
+ ids = json_response['data'].map { |c| c['id'] }
+ expect(ids).to include(expired_card.prefix_id)
+ end
+
+ it 'shows correct expired state' do
+ get :index
+
+ expired_data = json_response['data'].find { |c| c['id'] == expired_card.prefix_id }
+ expect(expired_data['expired']).to be true
+ expect(expired_data['active']).to be false
+ expect(expired_data['state']).to eq('expired')
+ end
+ end
+
+ context 'with partially redeemed gift card' do
+ let!(:partial_card) { create(:gift_card, user: user, store: store, state: :partially_redeemed, amount: 50, amount_used: 20) }
+
+ it 'shows correct state and amounts' do
+ get :index
+
+ partial_data = json_response['data'].find { |c| c['id'] == partial_card.prefix_id }
+ expect(partial_data['state']).to eq('partially_redeemed')
+ expect(partial_data['amount_remaining']).to eq(30.0)
+ end
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns the gift card' do
+ get :show, params: { id: gift_card.prefix_id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(gift_card.prefix_id)
+ end
+
+ it 'returns gift card with all attributes' do
+ get :show, params: { id: gift_card.prefix_id }
+
+ expect(json_response['code']).to eq(gift_card.display_code)
+ expect(json_response['amount']).to eq(100.0)
+ expect(json_response['amount_used']).to eq(25.0)
+ expect(json_response['amount_remaining']).to eq(75.0)
+ expect(json_response['currency']).to eq(gift_card.currency)
+ end
+
+ context 'when gift card belongs to another user' do
+ let(:other_user) { create(:user) }
+ let(:other_card) { create(:gift_card, user: other_user, store: store) }
+
+ it 'returns not found' do
+ get :show, params: { id: other_card.prefix_id }
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'when gift card belongs to another store' do
+ let(:other_store) { create(:store) }
+ let(:other_store_card) { create(:gift_card, user: user, store: other_store) }
+
+ it 'returns not found' do
+ get :show, params: { id: other_store_card.prefix_id }
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ get :show, params: { id: gift_card.prefix_id }
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/permission_sets/default_customer.rb b/core/app/models/spree/permission_sets/default_customer.rb
index 5c8a594949..507b72d585 100644
--- a/core/app/models/spree/permission_sets/default_customer.rb
+++ b/core/app/models/spree/permission_sets/default_customer.rb
@@ -60,6 +60,9 @@ module Spree
# Credit card management
can [:read, :destroy], Spree::CreditCard, user_id: user.id
+ # Gift card management - users can view their own gift cards
+ can :read, Spree::GiftCard, user_id: user.id
+
# Wishlist management
can :manage, Spree::Wishlist, user_id: user.id
can :show, Spree::Wishlist do |wishlist|