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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
rewrote Gift Card remove service
Damian Legawiec054d19a4c862
diff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rbindex 4cba51be4a..b01da1cc74 100644--- a/core/app/models/spree/gift_card.rb+++ b/core/app/models/spree/gift_card.rb@@ -4,6 +4,9 @@ module Spreeinclude Spree::SingleStoreResourceinclude Spree::Security::GiftCards if defined?(Spree::Security::GiftCards)+ #+ # State machine+ #state_machine :state, initial: :active doevent :cancel dotransition active: :canceled@@ -22,6 +25,11 @@ module Spreeendend+ #+ # Attributes+ #+ attribute :skip_expires_at_validation, :boolean, default: false+## Validations#@@ -38,8 +46,9 @@ module Spreebelongs_to :store, class_name: 'Spree::Store'belongs_to :user, class_name: Spree.user_class.to_s, optional: truebelongs_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 Spreereturn if code.present?self.code = loop do- random_token = SecureRandom.hex(8).to_s.upcase+ random_token = SecureRandom.hex(8).downcasebreak random_token unless self.class.exists?(code: random_token, store_id: store_id)endend@@ -127,20 +136,6 @@ module Spreeendalias_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 : :redeemedupdate!(state: new_state)@@ -148,8 +143,6 @@ module Spreeprivate- attr_accessor :skip_expires_at_validation-def set_amount_remainingreturn unless active?return if amount_remaining_changed?@@ -165,7 +158,7 @@ module Spreeenddef set_currency- self.currency ||= store.default_currency+ self.currency ||= store&.default_currencyendendenddiff --git a/core/app/models/spree/gift_card_batch.rb b/core/app/models/spree/gift_card_batch.rbindex 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 Spreeenddef set_currency- self.currency ||= store.default_currency+ self.currency ||= store&.default_currencyendendenddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 10ccf431fd..8f3107cd35 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -926,7 +926,7 @@ module Spreeenddef ensure_currency_presence- self.currency ||= store.default_currency+ self.currency ||= store&.default_currencyenddef collect_payment_methods(store = nil)diff --git a/core/app/services/spree/gift_cards/remove.rb b/core/app/services/spree/gift_cards/remove.rbindex 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 Spreegift_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)endsuccess(true)diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 8b1dffd129..9e3974ae49 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1094,8 +1094,8 @@ en:generate_code: Generate coupon codeget_back_to_the: Get back to thegift_card: Gift card- gift_card_batch: Gift card batchgift_card_applied: The gift card was successfully applied to your order!+ gift_card_batch: Gift card batchgift_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 ordergift_card_using_store_credit_error: You can't apply the gift card after you applied the store credit.