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.

Improve AddStoreCredit service error reporting

Damian Legawiec53d65a394ec4
diff --git a/core/app/services/spree/checkout/add_store_credit.rb b/core/app/services/spree/checkout/add_store_credit.rb
index f1a8844d82..57ff4f2649 100644
--- a/core/app/services/spree/checkout/add_store_credit.rb
+++ b/core/app/services/spree/checkout/add_store_credit.rb
@@ -9,9 +9,12 @@ module Spree
remaining_total = amount ? [amount, @order.outstanding_balance].min : @order.outstanding_balance
+ return failure(nil, Spree.t(:error_user_does_not_have_any_store_credits)) unless @order.user&.store_credits&.any?
+
ApplicationRecord.transaction do
@order.payments.store_credits.where(state: :checkout).map(&:invalidate!)
- apply_store_credits(remaining_total) if @order.user&.store_credits&.any?
+
+ apply_store_credits(remaining_total)
end
@order.reload.payments.store_credits.valid.any? ? success(@order) : failure(@order)
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index adb6f14946..bdd0a62907 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -986,6 +986,7 @@ en:
one: 1 error prohibited this record from being saved
other: ! '%{count} errors prohibited this record from being saved'
error_user_destroy_with_orders: User associated with orders cannot be destroyed
+ error_user_does_not_have_any_store_credits: 'User does not have any Store Credits available'
event: Event
events:
spree:
diff --git a/core/spec/services/spree/checkout/add_store_credit_spec.rb b/core/spec/services/spree/checkout/add_store_credit_spec.rb
index ab99f95801..7150b3c7e1 100644
--- a/core/spec/services/spree/checkout/add_store_credit_spec.rb
+++ b/core/spec/services/spree/checkout/add_store_credit_spec.rb
@@ -24,6 +24,11 @@ describe Spree::Checkout::AddStoreCredit, type: :service do
it 'does not create a store credit payment' do
expect(order.payments.count).to eq 0
end
+
+ it 'returns error' do
+ expect(subject.success?).to eq(false)
+ expect(subject.error.to_s).to eq('User does not have any Store Credits available')
+ end
end
context 'there is enough store credit to pay for the entire order' do