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.

Move serializers and change model

Michał Brożek50f303c66f6c
diff --git a/api/app/controllers/spree/api/v2/platform/gift_cards_controller.rb b/api/app/controllers/spree/api/v2/platform/gift_cards_controller.rb
new file mode 100644
index 0000000000..9d8ada2616
--- /dev/null
+++ b/api/app/controllers/spree/api/v2/platform/gift_cards_controller.rb
@@ -0,0 +1,15 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class GiftCardsController < ResourceController
+ private
+
+ def model_class
+ Spree::GiftCard
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/gift_card_serializer.rb b/api/app/serializers/spree/api/v2/gift_card_serializer.rb
new file mode 100644
index 0000000000..7332b687da
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/gift_card_serializer.rb
@@ -0,0 +1,17 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class GiftCardSerializer < BaseSerializer
+ set_type :gift_card
+
+ attributes :state, :code, :expires_at,
+ :amount, :amount_remaining, :minimum_order_amount,
+ :display_amount, :display_amount_remaining, :display_minimum_order_amount
+
+ belongs_to :user
+ end
+ end
+ 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 9a6e639400..1af19133bc 100644
--- a/api/app/serializers/spree/v2/storefront/cart_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/cart_serializer.rb
@@ -58,6 +58,7 @@ module Spree
id_method_name: :ship_address_id,
serializer: :address,
record_type: :address
+ belongs_to :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
new file mode 100644
index 0000000000..0f98f851ab
--- /dev/null
+++ b/api/app/serializers/spree/v2/storefront/gift_card_serializer.rb
@@ -0,0 +1,11 @@
+module Spree
+ module V2
+ module Storefront
+ class GiftCardSerializer < BaseSerializer
+ set_type :gift_card
+
+ attributes :code, :amount, :amount_remaining, :display_amount, :display_amount_remaining
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rb
index e55e2b76af..e524221658 100644
--- a/core/app/models/spree/gift_card.rb
+++ b/core/app/models/spree/gift_card.rb
@@ -17,7 +17,12 @@ module Spree
belongs_to :batch, class_name: 'Spree::GiftCardBatch', optional: true, foreign_key: :gift_card_batch_id
has_many :store_credits, class_name: 'Spree::StoreCredit'
- has_many :orders, -> { without_vendor }, through: :store_credits, source: :orders
+ if defined?(Spree::Vendor)
+ has_many :orders, -> { without_vendor }, through: :store_credits, source: :orders
+ else
+ has_many :orders, through: :store_credits, source: :orders
+ end
+
has_many :users, through: :orders
scope :active, -> { where(state: [:active, :partialy_redeemed]).where(expires_at: [nil, Time.current..]) }
diff --git a/core/app/services/spree/cart/recalculate.rb b/core/app/services/spree/cart/recalculate.rb
index 56ccd24e5c..2c6df9c311 100644
--- a/core/app/services/spree/cart/recalculate.rb
+++ b/core/app/services/spree/cart/recalculate.rb
@@ -4,6 +4,7 @@ module Spree
prepend Spree::ServiceModule::Base
def call(order:, line_item:, line_item_created: false, options: {})
+ order.remove_gift_card if order.gift_card.present?
order_updater = ::Spree::OrderUpdater.new(order)
order.payments.store_credits.checkout.destroy_all