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.

rewrote Gift Card remove service

Damian Legawiec054d19a4c862
diff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rb
index 4cba51be4a..b01da1cc74 100644
--- a/core/app/models/spree/gift_card.rb
+++ b/core/app/models/spree/gift_card.rb
@@ -4,6 +4,9 @@ module Spree
include Spree::SingleStoreResource
include Spree::Security::GiftCards if defined?(Spree::Security::GiftCards)
+ #
+ # State machine
+ #
state_machine :state, initial: :active do
event :cancel do
transition active: :canceled
@@ -22,6 +25,11 @@ module Spree
end
end
+ #
+ # Attributes
+ #
+ attribute :skip_expires_at_validation, :boolean, default: false
+
#
# Validations
#
@@ -38,8 +46,9 @@ module Spree
belongs_to :store, class_name: 'Spree::Store'
belongs_to :user, class_name: Spree.user_class.to_s, optional: true
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, through: :store_credits, source: :orders
+ has_many :orders, inverse_of: :gift_card, class_name: 'Spree::Order'
has_many :users, through: :orders, class_name: Spree.user_class.to_s
#
@@ -93,7 +102,7 @@ module Spree
return if code.present?
self.code = loop do
- random_token = SecureRandom.hex(8).to_s.upcase
+ random_token = SecureRandom.hex(8).downcase
break random_token unless self.class.exists?(code: random_token, store_id: store_id)
end
end
@@ -127,20 +136,6 @@ module Spree
end
alias_method :amount_used, :used_amount
- def undo_apply!(amount:)
- transaction do
- self.amount_remaining = [amount_remaining + amount, self.amount].min
- self.state = amount_remaining == self.amount ? :active : :partially_redeemed
- self.skip_expires_at_validation = true
- save!
-
- self.skip_expires_at_validation = false
-
- store_credit = store_credits.available.find_by(amount: amount)
- store_credit.destroy!
- end
- end
-
def redeem!
new_state = amount_remaining.positive? ? :partially_redeemed : :redeemed
update!(state: new_state)
@@ -148,8 +143,6 @@ module Spree
private
- attr_accessor :skip_expires_at_validation
-
def set_amount_remaining
return unless active?
return if amount_remaining_changed?
@@ -165,7 +158,7 @@ module Spree
end
def set_currency
- self.currency ||= store.default_currency
+ self.currency ||= store&.default_currency
end
end
end
diff --git a/core/app/models/spree/gift_card_batch.rb b/core/app/models/spree/gift_card_batch.rb
index 8515e1d6dc..e00e3e4556 100644
--- a/core/app/models/spree/gift_card_batch.rb
+++ b/core/app/models/spree/gift_card_batch.rb
@@ -70,7 +70,7 @@ module Spree
end
def set_currency
- self.currency ||= store.default_currency
+ self.currency ||= store&.default_currency
end
end
end
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 10ccf431fd..8f3107cd35 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -926,7 +926,7 @@ module Spree
end
def ensure_currency_presence
- self.currency ||= store.default_currency
+ self.currency ||= store&.default_currency
end
def collect_payment_methods(store = nil)
diff --git a/core/app/services/spree/gift_cards/remove.rb b/core/app/services/spree/gift_cards/remove.rb
index 37bd3c0fa1..cb944c0b86 100644
--- a/core/app/services/spree/gift_cards/remove.rb
+++ b/core/app/services/spree/gift_cards/remove.rb
@@ -9,14 +9,27 @@ module Spree
gift_card = order.gift_card
- order.transaction do
- payments = order.payments.checkout.store_credits
- payment_total = payments.sum(:amount)
+ return failure(:gift_card_not_found) if gift_card.nil?
+ order.with_lock do
+ payments = order.payments.checkout.store_credits.where(source: gift_card.store_credits)
+ payment_total = payments.sum(:amount)
payments.each(&:invalidate!)
- gift_card.undo_apply!(amount: payment_total)
- order.update_columns(gift_card_id: nil, updated_at: Time.current)
+ gift_card.with_lock do
+ gift_card.amount_remaining = [gift_card.amount_remaining + payment_total, gift_card.amount].min
+ gift_card.state = gift_card.amount_remaining == gift_card.amount ? :active : :partially_redeemed
+ gift_card.skip_expires_at_validation = true
+ gift_card.save!
+ end
+
+ # we need to destroy the store credits here because they are not associated with the order
+ # and we need to remove them from the gift card
+ payments.each do |payment|
+ payment.source.destroy!
+ end
+
+ order.update!(gift_card: nil)
end
success(true)
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 8b1dffd129..9e3974ae49 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1094,8 +1094,8 @@ en:
generate_code: Generate coupon code
get_back_to_the: Get back to the
gift_card: Gift card
- gift_card_batch: Gift card batch
gift_card_applied: The gift card was successfully applied to your order!
+ gift_card_batch: Gift card batch
gift_card_no_amount_remaining: You can't apply the gift card with no balance on it.
gift_card_removed: The gift card was successfully removed from your order
gift_card_using_store_credit_error: You can't apply the gift card after you applied the store credit.