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.

`Spree::Addresses::Update`: allow to always create a new address on update (#12223)

Łukasz Adamczykc33fa99c7f16
diff --git a/core/app/services/spree/addresses/update.rb b/core/app/services/spree/addresses/update.rb
index bf4ab2a994..57aa26bc43 100644
--- a/core/app/services/spree/addresses/update.rb
+++ b/core/app/services/spree/addresses/update.rb
@@ -11,6 +11,7 @@ module Spree
default_billing = opts.fetch(:default_billing, false)
default_shipping = opts.fetch(:default_shipping, false)
address_changes_except = opts.fetch(:address_changes_except, [])
+ create_new_address_on_update = opts.fetch(:create_new_address_on_update, false)
prepare_address_params!(address, address_params)
address.assign_attributes(address_params)
@@ -28,7 +29,7 @@ module Spree
return success(address) unless address_changed
- if address.editable?
+ if address.editable? && !create_new_address_on_update
if address.update(address_params)
if address.user.present?
assign_to_user_as_default(
@@ -46,20 +47,23 @@ module Spree
failure(address)
end
elsif new_address(address_params).valid?
- address.destroy
+ address.destroy unless create_new_address_on_update
if new_address.user.present?
+ default_billing = (!create_new_address_on_update && address.user_default_billing?) || default_billing
+ default_shipping = (!create_new_address_on_update && address.user_default_shipping?) || default_shipping
+
assign_to_user_as_default(
user: new_address.user,
address_id: new_address.id,
- default_billing: address.user_default_billing? || default_billing,
- default_shipping: address.user_default_shipping? || default_shipping
+ default_billing: default_billing,
+ default_shipping: default_shipping
)
end
if order.present?
- order.ship_address = new_address if order.ship_address_id == address.id
- order.bill_address = new_address if order.bill_address_id == address.id
+ order.ship_address = new_address if !create_new_address_on_update && order.ship_address_id == address.id
+ order.bill_address = new_address if !create_new_address_on_update && order.bill_address_id == address.id
order.state = 'address'
order.save
end
diff --git a/core/spec/services/spree/addresses/update_spec.rb b/core/spec/services/spree/addresses/update_spec.rb
index 7065eceba8..5043b42357 100644
--- a/core/spec/services/spree/addresses/update_spec.rb
+++ b/core/spec/services/spree/addresses/update_spec.rb
@@ -49,6 +49,12 @@ RSpec.describe Spree::Addresses::Update do
expect { subject.call(address: address, address_params: new_address_params, order: order) }.not_to change { address.reload }
expect { subject.call(address: address, address_params: new_address_params, order: order) }.not_to change { Spree::Address.count }
end
+
+ context 'when setting the create_new_address_on_update param to true' do
+ it 'does not create new address' do
+ expect { subject.call(address: address, address_params: new_address_params, order: order, create_new_address_on_update: true) }.not_to change { Spree::Address.count }
+ end
+ end
end
context 'when user only sets the address as default shipping' do
@@ -78,6 +84,72 @@ RSpec.describe Spree::Addresses::Update do
end
end
+ shared_examples 'always create a new address on update' do
+ context 'when the create_new_address_on_update param is set to true' do
+ let(:result) { subject.call(address: address, address_params: new_address_params, order: order, **params) }
+ let(:params) { { create_new_address_on_update: true } }
+
+ it 'creates a new address and keeps the previous one' do
+ expect { result }.to change(Spree::Address, :count).by(1)
+
+ expect(result).to be_success
+ expect(value).to have_attributes(new_address_params)
+ expect(value.id).not_to eq(address.id)
+ expect(value.country).to eq(country)
+ expect(value.state).to eq(state)
+ expect(value.user).to eq(user)
+ expect(address.deleted_at).to be_nil
+ end
+
+ context 'with a user' do
+ before do
+ new_address_params.merge!(user_id: user.id)
+ user.update!(ship_address: address, bill_address: address)
+ end
+
+ it "doesn't change the user's bill and ship addresses by default" do
+ expect(result).to be_success
+
+ expect(user.reload.bill_address).to eq(address.reload)
+ expect(user.ship_address).to eq(address)
+ end
+
+ context 'when the default_billing param is set to true' do
+ let(:params) { { create_new_address_on_update: true, default_billing: true } }
+
+ it 'changes user\'s bill address' do
+ expect(result).to be_success
+
+ expect(user.reload.bill_address).to eq(value)
+ expect(user.ship_address).to eq(address.reload)
+ end
+ end
+
+ context 'when the default_shipping param is set to true' do
+ let(:params) { { create_new_address_on_update: true, default_shipping: true } }
+
+ it 'changes user\'s ship address' do
+ expect(result).to be_success
+
+ expect(user.reload.ship_address).to eq(value)
+ expect(user.bill_address).to eq(address.reload)
+ end
+ end
+ end
+
+ context 'with an order' do
+ let(:order) { create(:order, user: user, state: 'delivery', ship_address: address, bill_address: address) }
+
+ it "doesn't change the order addresses" do
+ expect(result).to be_success
+
+ expect(order.reload.bill_address).to eq(address)
+ expect(order.reload.ship_address).to eq(address)
+ end
+ end
+ end
+ end
+
context 'when address is editable' do
it 'updates address' do
expect { result }.not_to change(Spree::Address, :count)
@@ -122,9 +194,10 @@ RSpec.describe Spree::Addresses::Update do
end
it_behaves_like 'updating with same params'
+ include_examples 'always create a new address on update'
end
- context 'when address in uneditable' do
+ context 'when address is uneditable' do
let!(:completed_order) { create(:completed_order_with_totals, user: user, ship_address: address, bill_address: address) }
context 'when there have been created same address with new params' do
@@ -234,6 +307,7 @@ RSpec.describe Spree::Addresses::Update do
end
it_behaves_like 'updating with same params'
+ include_examples 'always create a new address on update'
end
end