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
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.rbindex 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 Spreeenddef find_resource- Spree::CustomerReturn.accessible_by(current_ability, :show).find(params[:id])+ current_store.customer_returns.accessible_by(current_ability, :show).find(params[:id])enddef collectionparent # 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 = @collectionenddiff --git a/backend/app/controllers/spree/admin/return_index_controller.rb b/backend/app/controllers/spree/admin/return_index_controller.rbindex 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 Spreeenddef customer_returns- collection(Spree::CustomerReturn)+ collection(current_store.customer_returns)respond_with(@collection)enddiff --git a/backend/spec/controllers/spree/admin/customer_returns_controller_spec.rb b/backend/spec/controllers/spree/admin/customer_returns_controller_spec.rbindex 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 Spreelet(:order) { customer_return.order }let(:customer_return) { create(:customer_return) }- before { subject }-it 'loads the order' do+ subjectexpect(assigns(:order)).to eq orderendit 'loads the customer return' do+ subjectexpect(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+ endenddescribe '#new' do@@ -120,9 +134,12 @@ module Spreepost :create, params: customer_return_paramsend- 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' dolet(:stock_location) { order.shipments.last.stock_location }@@ -144,7 +161,7 @@ module Spreeendit '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)endit 'redirects to the index page' do@@ -171,7 +188,7 @@ module Spreeendit "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 }endit 'renders the new page' dodiff --git a/backend/spec/controllers/spree/admin/return_index_controller_spec.rb b/backend/spec/controllers/spree/admin/return_index_controller_spec.rbindex 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 Spreelet(:customer_return) { create(:customer_return) }- before { subject }-it 'loads customer returns' do+ subjectexpect(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+ endendendenddiff --git a/core/app/models/spree/customer_return.rb b/core/app/models/spree/customer_return.rbindex a9007987d4..512aae71ca 100644--- a/core/app/models/spree/customer_return.rb+++ b/core/app/models/spree/customer_return.rb@@ -2,6 +2,7 @@ module Spreeclass CustomerReturn < Spree::Baseinclude Spree::Core::NumberGenerator.new(prefix: 'CR', length: 9)belongs_to :stock_location+ belongs_to :store, class_name: 'Spree::Store', inverse_of: :customer_returnshas_many :reimbursements, inverse_of: :customer_returnhas_many :return_items, inverse_of: :customer_return@@ -9,6 +10,7 @@ module Spreeafter_create :process_return!+ validates :store, presence: truevalidates :number, uniqueness: { case_sensitive: true }validates :return_items, :stock_location, presence: truevalidate :must_have_return_authorization, on: :create@@ -53,7 +55,7 @@ module Spreeenddef 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))endenddiff --git a/core/app/models/spree/inventory_unit.rb b/core/app/models/spree/inventory_unit.rbindex 760062fffe..1b16f2f160 100644--- a/core/app/models/spree/inventory_unit.rb+++ b/core/app/models/spree/inventory_unit.rb@@ -4,7 +4,7 @@ module Spreebelongs_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_unithas_many :return_authorizations, class_name: 'Spree::ReturnAuthorization', through: :return_itemsbelongs_to :line_item, class_name: 'Spree::LineItem'enddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex dd910646c7..31a4c1aa83 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -96,6 +96,7 @@ module Spreehas_many :reimbursements, inverse_of: :order, class_name: 'Spree::Reimbursement'has_many :line_item_adjustments, through: :line_items, source: :adjustmentshas_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_itemshas_many :products, through: :variantshas_many :refunds, through: :paymentsdiff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 08aced0d0d..8e21778964 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -21,7 +21,8 @@ module Spreehas_many :product_properties, through: :products, class_name: 'Spree::ProductProperty'has_many :variants, through: :products, class_name: 'Spree::Variant', source: :variants_including_masterhas_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: :storehas_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.rbnew file mode 100644index 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+enddiff --git a/core/lib/spree/testing_support/factories/customer_return_factory.rb b/core/lib/spree/testing_support/factories/customer_return_factory.rbindex 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 doassociation(: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+ endend- 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)+ endend- 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!)+ endendendend-- # 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)- endenddiff --git a/core/spec/models/spree/customer_return_spec.rb b/core/spec/models/spree/customer_return_spec.rbindex 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 dodescribe '#must_have_return_authorization' dosubject { 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 dodescribe '#return_items_belong_to_same_order' dosubject { 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.rbindex 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 dolet(: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.rbindex 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 dolet(: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) }