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.

Fixed Storefront Account Store Credit section (#12500)

Damian Legawiec3eecbc464ebf
diff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rb
index 57385f202d..b8e5d89351 100644
--- a/core/app/models/spree/store_credit.rb
+++ b/core/app/models/spree/store_credit.rb
@@ -236,7 +236,7 @@ module Spree
event.update!(
amount: action_amount || amount,
authorization_code: action_authorization_code || event.authorization_code || generate_authorization_code,
- user_total_amount: user.total_available_store_credit,
+ user_total_amount: user&.total_available_store_credit || 0,
originator: action_originator
)
end
diff --git a/core/app/models/spree/store_credit_event.rb b/core/app/models/spree/store_credit_event.rb
index 82044d23b5..41c54338e0 100644
--- a/core/app/models/spree/store_credit_event.rb
+++ b/core/app/models/spree/store_credit_event.rb
@@ -29,5 +29,25 @@ module Spree
def order
store.payments.find_by(response_code: authorization_code).try(:order)
end
+
+ def allocation?
+ action == Spree::StoreCredit::ALLOCATION_ACTION
+ end
+
+ def credit?
+ action == Spree::StoreCredit::CREDIT_ACTION
+ end
+
+ def captured?
+ action == Spree::StoreCredit::CAPTURE_ACTION
+ end
+
+ def voided?
+ action == Spree::StoreCredit::VOID_ACTION
+ end
+
+ def authorized?
+ action == Spree::StoreCredit::AUTHORIZE_ACTION
+ end
end
end
diff --git a/core/spec/models/spree/store_credit_event_spec.rb b/core/spec/models/spree/store_credit_event_spec.rb
index 906eccb365..714e091c05 100644
--- a/core/spec/models/spree/store_credit_event_spec.rb
+++ b/core/spec/models/spree/store_credit_event_spec.rb
@@ -111,4 +111,84 @@ describe 'StoreCreditEvent' do
it { expect(subject.store).to eq(store) }
end
+
+ describe '#allocation?' do
+ subject { store_credit_event.allocation? }
+
+ let(:store_credit_event) { build(:store_credit_auth_event, action: action) }
+
+ context 'action is allocation' do
+ let(:action) { Spree::StoreCredit::ALLOCATION_ACTION }
+ it { is_expected.to be true }
+ end
+
+ context 'action is not allocation' do
+ let(:action) { Spree::StoreCredit::CAPTURE_ACTION }
+ it { is_expected.to be false }
+ end
+ end
+
+ describe '#credit?' do
+ subject { store_credit_event.credit? }
+
+ let(:store_credit_event) { build(:store_credit_auth_event, action: action) }
+
+ context 'action is credit' do
+ let(:action) { Spree::StoreCredit::CREDIT_ACTION }
+ it { is_expected.to be true }
+ end
+
+ context 'action is not credit' do
+ let(:action) { Spree::StoreCredit::CAPTURE_ACTION }
+ it { is_expected.to be false }
+ end
+ end
+
+ describe '#captured?' do
+ subject { store_credit_event.captured? }
+
+ let(:store_credit_event) { build(:store_credit_auth_event, action: action) }
+
+ context 'action is capture' do
+ let(:action) { Spree::StoreCredit::CAPTURE_ACTION }
+ it { is_expected.to be true }
+ end
+
+ context 'action is not capture' do
+ let(:action) { Spree::StoreCredit::ALLOCATION_ACTION }
+ it { is_expected.to be false }
+ end
+ end
+
+ describe '#voided?' do
+ subject { store_credit_event.voided? }
+
+ let(:store_credit_event) { build(:store_credit_auth_event, action: action) }
+
+ context 'action is void' do
+ let(:action) { Spree::StoreCredit::VOID_ACTION }
+ it { is_expected.to be true }
+ end
+
+ context 'action is not void' do
+ let(:action) { Spree::StoreCredit::CAPTURE_ACTION }
+ it { is_expected.to be false }
+ end
+ end
+
+ describe '#authorized?' do
+ subject { store_credit_event.authorized? }
+
+ let(:store_credit_event) { build(:store_credit_auth_event, action: action) }
+
+ context 'action is authorize' do
+ let(:action) { Spree::StoreCredit::AUTHORIZE_ACTION }
+ it { is_expected.to be true }
+ end
+
+ context 'action is not authorize' do
+ let(:action) { Spree::StoreCredit::CAPTURE_ACTION }
+ it { is_expected.to be false }
+ end
+ end
end
diff --git a/storefront/app/controllers/spree/account/store_credits_controller.rb b/storefront/app/controllers/spree/account/store_credits_controller.rb
index 5f996aa640..d55170f4da 100644
--- a/storefront/app/controllers/spree/account/store_credits_controller.rb
+++ b/storefront/app/controllers/spree/account/store_credits_controller.rb
@@ -2,7 +2,6 @@ module Spree
module Account
class StoreCreditsController < BaseController
def index
- user_store_credits = try_spree_current_user.store_credits.without_gift_card
@store_credit_events = Spree::StoreCreditEvent.where(store_credit: user_store_credits).
exposed_events.
reverse_chronological.
@@ -14,6 +13,10 @@ module Spree
def accurate_title
Spree.t(:store_credit_name)
end
+
+ def user_store_credits
+ try_spree_current_user.store_credits
+ end
end
end
end
diff --git a/storefront/spec/controllers/spree/account/store_credits_controller_spec.rb b/storefront/spec/controllers/spree/account/store_credits_controller_spec.rb
new file mode 100644
index 0000000000..655daf21ac
--- /dev/null
+++ b/storefront/spec/controllers/spree/account/store_credits_controller_spec.rb
@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+describe Spree::Account::StoreCreditsController, type: :controller do
+ let(:store) { Spree::Store.default }
+ let(:user) { create(:user) }
+ let(:store_credit) { create(:store_credit, user: user) }
+ let!(:store_credit_event) { store_credit.store_credit_events.first }
+
+ render_views
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ allow(controller).to receive(:spree_login_path).and_return('/login')
+ end
+
+ describe '#index' do
+ subject { get :index }
+
+ context 'when user is logged in' do
+ before do
+ allow(controller).to receive_messages try_spree_current_user: user
+ allow(controller).to receive_messages spree_current_user: user
+ end
+
+ it 'lists store credit events for the user' do
+ subject
+ expect(assigns(:store_credit_events)).to include(store_credit_event)
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'renders the index template' do
+ subject
+ expect(response).to render_template(:index)
+ end
+ end
+
+ context 'when user is not logged in' do
+ it 'redirects to login page' do
+ expect(subject).to have_http_status(302)
+ end
+ end
+ end
+end