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.

Fix updating address's first name when there is already completed order with that address (#12393)

Damian Kaczmarczyka3a3aaa982f4
diff --git a/core/app/models/spree/order/address_book.rb b/core/app/models/spree/order/address_book.rb
index 4d70b7de96..7e113e4afe 100644
--- a/core/app/models/spree/order/address_book.rb
+++ b/core/app/models/spree/order/address_book.rb
@@ -71,7 +71,7 @@ module Spree
end
def find_existing_address(attributes)
- address_attributes = attributes.except(:firstname, :state_name)
+ address_attributes = attributes.except(:state_name)
state_name = attributes[:state_name]
scope = Spree::Address.not_deleted.where(address_attributes)
diff --git a/core/spec/models/spree/order/address_spec.rb b/core/spec/models/spree/order/address_spec.rb
index 68f866c085..0b7d66e575 100644
--- a/core/spec/models/spree/order/address_spec.rb
+++ b/core/spec/models/spree/order/address_spec.rb
@@ -82,5 +82,35 @@ describe Spree::Order, type: :model do
expect(@order.bill_address_id).to eq @order.ship_address_id
end
end
+
+ context 'when user wants to update firstname of the address with already completed order' do
+ let(:address) { create(:address, user: order.user) }
+ let!(:completed_order) { create(:completed_order_with_totals, user: order.user, ship_address: address) }
+
+ it 'creates new address with updated attributes' do
+ expect(
+ order.update(
+ bill_address_attributes: {
+ firstname: 'New name',
+ **address.attributes.except(
+ 'firstname',
+ 'created_at',
+ 'updated_at',
+ 'deleted_at',
+ 'quick_checkout',
+ 'public_metadata',
+ 'private_metadata',
+ 'latitude',
+ 'longitude',
+ 'preferences'
+ )
+ }
+ )
+ ).to be_truthy
+
+ expect(order.bill_address_id).not_to eq address.id
+ expect(order.bill_address.firstname).to eq 'New name'
+ end
+ end
end
end