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.

Associate customer returns with store

Szymon Iwacz1ee141d24883
diff --git a/backend/app/controllers/spree/admin/customer_returns_controller.rb b/backend/app/controllers/spree/admin/customer_returns_controller.rb
index fb288a8bb3..e11fdcbff1 100644
--- a/backend/app/controllers/spree/admin/customer_returns_controller.rb
+++ b/backend/app/controllers/spree/admin/customer_returns_controller.rb
@@ -31,15 +31,14 @@ module Spree
end
def find_resource
- Spree::CustomerReturn.accessible_by(current_ability, :show).find(params[:id])
+ current_store.customer_returns.accessible_by(current_ability, :show).find(params[:id])
end
def collection
parent # trigger loading the order
- @collection ||= Spree::ReturnItem.
- accessible_by(current_ability).
- where(inventory_unit_id: @order.inventory_units.pluck(:id)).
- map(&:customer_return).uniq.compact
+
+ order_customer_return_ids = @order.return_items.accessible_by(current_ability).pluck(:customer_return_id).uniq.compact
+ @collection ||= current_store.customer_returns.where(id: order_customer_return_ids)
@customer_returns = @collection
end
diff --git a/backend/app/controllers/spree/admin/return_index_controller.rb b/backend/app/controllers/spree/admin/return_index_controller.rb
index 23c17b4a24..f634d90738 100644
--- a/backend/app/controllers/spree/admin/return_index_controller.rb
+++ b/backend/app/controllers/spree/admin/return_index_controller.rb
@@ -7,7 +7,7 @@ module Spree
end
def customer_returns
- collection(Spree::CustomerReturn)
+ collection(current_store.customer_returns)
respond_with(@collection)
end
diff --git a/backend/spec/controllers/spree/admin/customer_returns_controller_spec.rb b/backend/spec/controllers/spree/admin/customer_returns_controller_spec.rb
index 7699f606e4..30473fb81c 100644
--- a/backend/spec/controllers/spree/admin/customer_returns_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/customer_returns_controller_spec.rb
@@ -13,15 +13,29 @@ module Spree
let(:order) { customer_return.order }
let(:customer_return) { create(:customer_return) }
- before { subject }
-
it 'loads the order' do
+ subject
expect(assigns(:order)).to eq order
end
it 'loads the customer return' do
+ subject
expect(assigns(:customer_returns)).to include(customer_return)
end
+
+ context 'for different store' do
+ let!(:new_store) { create(:store) }
+
+ before { allow_any_instance_of(described_class).to receive(:current_store).and_return(new_store) }
+
+ it 'should not load any orders' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it 'should not load any customer returns' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
end
describe '#new' do
@@ -120,9 +134,12 @@ module Spree
post :create, params: customer_return_params
end
- let(:order) { create(:shipped_order, line_items_count: 1) }
+ let(:current_store) { Spree::Store.default }
+ let(:order) { create(:shipped_order, line_items_count: 1, store: current_store) }
let!(:return_authorization) { create :return_authorization, order: order, return_items: [create(:return_item, inventory_unit: order.inventory_units.shipped.last)] }
+ before { allow_any_instance_of(described_class).to receive(:current_store).and_return(current_store) }
+
context 'valid customer return' do
let(:stock_location) { order.shipments.last.stock_location }
@@ -144,7 +161,7 @@ module Spree
end
it 'creates a customer return' do
- expect { subject }.to change { Spree::CustomerReturn.count }.by(1)
+ expect { subject }.to change { current_store.customer_returns.count }.by(1)
end
it 'redirects to the index page' do
@@ -171,7 +188,7 @@ module Spree
end
it "doesn't create a customer return" do
- expect { subject }.not_to change { Spree::CustomerReturn.count }
+ expect { subject }.not_to change { current_store.customer_returns.count }
end
it 'renders the new page' do
diff --git a/backend/spec/controllers/spree/admin/return_index_controller_spec.rb b/backend/spec/controllers/spree/admin/return_index_controller_spec.rb
index 359596825c..28cb2b7aa2 100644
--- a/backend/spec/controllers/spree/admin/return_index_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/return_index_controller_spec.rb
@@ -26,11 +26,21 @@ module Spree
let(:customer_return) { create(:customer_return) }
- before { subject }
-
it 'loads customer returns' do
+ subject
expect(assigns(:collection)).to include(customer_return)
end
+
+ context 'when current store is different than customer return order store' do
+ let(:new_store) { create(:store) }
+
+ before { allow_any_instance_of(described_class).to receive(:current_store).and_return(new_store) }
+
+ it 'should not load customer returns' do
+ subject
+ expect(assigns(:collection)).to be_empty
+ end
+ end
end
end
end
diff --git a/core/app/models/spree/customer_return.rb b/core/app/models/spree/customer_return.rb
index a9007987d4..512aae71ca 100644
--- a/core/app/models/spree/customer_return.rb
+++ b/core/app/models/spree/customer_return.rb
@@ -2,6 +2,7 @@ module Spree
class CustomerReturn < Spree::Base
include Spree::Core::NumberGenerator.new(prefix: 'CR', length: 9)
belongs_to :stock_location
+ belongs_to :store, class_name: 'Spree::Store', inverse_of: :customer_returns
has_many :reimbursements, inverse_of: :customer_return
has_many :return_items, inverse_of: :customer_return
@@ -9,6 +10,7 @@ module Spree
after_create :process_return!
+ validates :store, presence: true
validates :number, uniqueness: { case_sensitive: true }
validates :return_items, :stock_location, presence: true
validate :must_have_return_authorization, on: :create
@@ -53,7 +55,7 @@ module Spree
end
def must_have_return_authorization
- if item = return_items.find { |ri| ri.return_authorization.blank? }
+ if (item = return_items.find { |ri| ri.return_authorization.blank? })
errors.add(:base, Spree.t(:missing_return_authorization, item_name: item.inventory_unit.variant.name))
end
end
diff --git a/core/app/models/spree/inventory_unit.rb b/core/app/models/spree/inventory_unit.rb
index 760062fffe..1b16f2f160 100644
--- a/core/app/models/spree/inventory_unit.rb
+++ b/core/app/models/spree/inventory_unit.rb
@@ -4,7 +4,7 @@ module Spree
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
belongs_to :order, class_name: 'Spree::Order'
belongs_to :shipment, class_name: 'Spree::Shipment', touch: true, optional: true
- has_many :return_items, inverse_of: :inventory_unit
+ has_many :return_items, class_name: 'Spree::ReturnItem', inverse_of: :inventory_unit
has_many :return_authorizations, class_name: 'Spree::ReturnAuthorization', through: :return_items
belongs_to :line_item, class_name: 'Spree::LineItem'
end
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index dd910646c7..31a4c1aa83 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -96,6 +96,7 @@ module Spree
has_many :reimbursements, inverse_of: :order, class_name: 'Spree::Reimbursement'
has_many :line_item_adjustments, through: :line_items, source: :adjustments
has_many :inventory_units, inverse_of: :order, class_name: 'Spree::InventoryUnit'
+ has_many :return_items, through: :inventory_units, class_name: 'Spree::ReturnItem'
has_many :variants, through: :line_items
has_many :products, through: :variants
has_many :refunds, through: :payments
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 08aced0d0d..8e21778964 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -21,7 +21,8 @@ module Spree
has_many :product_properties, through: :products, class_name: 'Spree::ProductProperty'
has_many :variants, through: :products, class_name: 'Spree::Variant', source: :variants_including_master
has_many :stock_items, through: :variants, class_name: 'Spree::StockItem'
- has_many :inventory_units, through: :variants, class_name: 'InventoryUnit'
+ has_many :inventory_units, through: :variants, class_name: 'Spree::InventoryUnit'
+ has_many :customer_returns, class_name: 'Spree::CustomerReturn', inverse_of: :store
has_many :store_credits, class_name: 'Spree::StoreCredit'
has_many :store_credit_events, through: :store_credits, class_name: 'Spree::StoreCreditEvent'
diff --git a/core/db/migrate/20210722090705_add_store_id_to_spree_customer_returns.rb b/core/db/migrate/20210722090705_add_store_id_to_spree_customer_returns.rb
new file mode 100644
index 0000000000..ee89c944c5
--- /dev/null
+++ b/core/db/migrate/20210722090705_add_store_id_to_spree_customer_returns.rb
@@ -0,0 +1,10 @@
+class AddStoreIdToSpreeCustomerReturns < ActiveRecord::Migration[5.2]
+ def change
+ unless column_exists?(:spree_customer_returns, :store_id)
+ add_column :spree_customer_returns, :store_id, :bigint
+ add_index :spree_customer_returns, :store_id
+ Spree::CustomerReturn.reset_column_information
+ Spree::CustomerReturn.find_each { |cr| cr.update_column(:store_id, cr.order.store_id) }
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/customer_return_factory.rb b/core/lib/spree/testing_support/factories/customer_return_factory.rb
index 0472ad08c9..b3c499c079 100644
--- a/core/lib/spree/testing_support/factories/customer_return_factory.rb
+++ b/core/lib/spree/testing_support/factories/customer_return_factory.rb
@@ -1,29 +1,36 @@
FactoryBot.define do
- factory :customer_return, class: Spree::CustomerReturn do
+ # for the case when you want to supply existing return items instead of generating some
+ factory :customer_return_without_return_items, class: Spree::CustomerReturn do
association(:stock_location, factory: :stock_location)
- transient do
- line_items_count { 1 }
- return_items_count { line_items_count }
+ before(:create) do |customer_return, _evaluator|
+ if customer_return.store.nil?
+ default_store = Spree::Store.default.persisted? ? Spree::Store.default : nil
+ store = default_store || create(:store)
+
+ customer_return.store = store
+ end
end
- before(:create) do |customer_return, evaluator|
- shipped_order = create(:shipped_order, line_items_count: evaluator.line_items_count)
+ factory :customer_return, class: Spree::CustomerReturn do
+ transient do
+ line_items_count { 1 }
+ return_items_count { line_items_count }
+ end
+
+ before(:create) do |customer_return, evaluator|
+ shipped_order = create(:shipped_order, line_items_count: evaluator.line_items_count, store: customer_return.store)
- shipped_order.inventory_units.take(evaluator.return_items_count).each do |inventory_unit|
- customer_return.return_items << build(:return_item, inventory_unit: inventory_unit)
+ shipped_order.inventory_units.take(evaluator.return_items_count).each do |inventory_unit|
+ customer_return.return_items << build(:return_item, inventory_unit: inventory_unit)
+ end
end
- end
- factory :customer_return_with_accepted_items do
- after(:create) do |customer_return|
- customer_return.return_items.each(&:accept!)
+ factory :customer_return_with_accepted_items do
+ after(:create) do |customer_return|
+ customer_return.return_items.each(&:accept!)
+ end
end
end
end
-
- # for the case when you want to supply existing return items instead of generating some
- factory :customer_return_without_return_items, class: Spree::CustomerReturn do
- association(:stock_location, factory: :stock_location)
- end
end
diff --git a/core/spec/models/spree/customer_return_spec.rb b/core/spec/models/spree/customer_return_spec.rb
index 915fee6bbf..b84a284108 100644
--- a/core/spec/models/spree/customer_return_spec.rb
+++ b/core/spec/models/spree/customer_return_spec.rb
@@ -9,7 +9,7 @@ describe Spree::CustomerReturn, type: :model do
describe '#must_have_return_authorization' do
subject { customer_return.valid? }
- let(:customer_return) { build(:customer_return) }
+ let(:customer_return) { build(:customer_return, store: create(:store)) }
let(:inventory_unit) { create(:inventory_unit) }
let(:return_item) { build(:return_item, inventory_unit: inventory_unit) }
@@ -43,7 +43,7 @@ describe Spree::CustomerReturn, type: :model do
describe '#return_items_belong_to_same_order' do
subject { customer_return.valid? }
- let(:customer_return) { build(:customer_return) }
+ let(:customer_return) { build(:customer_return, store: create(:store)) }
let(:first_inventory_unit) { create(:inventory_unit) }
let(:first_return_item) { build(:return_item, inventory_unit: first_inventory_unit) }
diff --git a/core/spec/models/spree/reimbursement_spec.rb b/core/spec/models/spree/reimbursement_spec.rb
index 31fd1a1848..896342afaa 100644
--- a/core/spec/models/spree/reimbursement_spec.rb
+++ b/core/spec/models/spree/reimbursement_spec.rb
@@ -47,7 +47,7 @@ describe Spree::Reimbursement, type: :model do
let(:inventory_unit) { line_item.inventory_units.first }
let(:payment) { order.payments.first }
let(:payment_amount) { order.total }
- let(:customer_return) { build(:customer_return, return_items: [return_item]) }
+ let(:customer_return) { build(:customer_return, return_items: [return_item], store: store) }
let(:return_item) { build(:return_item, inventory_unit: inventory_unit) }
let!(:default_refund_reason) { Spree::RefundReason.find_or_create_by!(name: Spree::RefundReason::RETURN_PROCESSING_REASON, mutable: false) }
diff --git a/emails/spec/models/spree/reimbursement_spec.rb b/emails/spec/models/spree/reimbursement_spec.rb
index 2474058a23..ce7a9a73cc 100644
--- a/emails/spec/models/spree/reimbursement_spec.rb
+++ b/emails/spec/models/spree/reimbursement_spec.rb
@@ -16,7 +16,7 @@ describe Spree::Reimbursement, type: :model do
let(:inventory_unit) { line_item.inventory_units.first }
let(:payment) { order.payments.first }
let(:payment_amount) { order.total }
- let(:customer_return) { build(:customer_return, return_items: [return_item]) }
+ let(:customer_return) { build(:customer_return, return_items: [return_item], store: store) }
let(:return_item) { build(:return_item, inventory_unit: inventory_unit) }
let!(:default_refund_reason) { Spree::RefundReason.find_or_create_by!(name: Spree::RefundReason::RETURN_PROCESSING_REASON, mutable: false) }