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: applied a seller's address correction to the order only

Damian Legawiec66845a9b3fb0
diff --git a/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
index 139edf7cb7..96d456b658 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
@@ -77,7 +77,11 @@ module Spree
# each lay their fields over the same snapshot, and the later one
# would undo the earlier one's lines.
with_order_lock do
- if @resource.update(address_params)
+ addresses = corrected_addresses
+ invalid = addresses.values.find(&:invalid?)
+ next render_validation_error(invalid.errors) if invalid
+
+ if @resource.update(addresses)
render json: serialize_resource(@resource.reload)
else
render_validation_error(@resource.errors)
@@ -115,9 +119,10 @@ module Spree
# Either address, as a correction rather than a replacement: the
# sent fields are laid over what the order already holds, so a
# request naming one line does not blank out the country and
- # postcode beside it. The nested writer builds a fresh address row
- # either way, which is what keeps a shared customer address book
- # entry from being rewritten by an order-level fix.
+ # postcode beside it. Written as a fresh ownerless snapshot straight
+ # onto the order — never through the buyer's nested writer, which
+ # files the address in their book and makes it their default for
+ # every later checkout on the marketplace.
def sent_addresses
@sent_addresses ||= params.permit(
shipping_address: address_permitted_keys,
@@ -125,32 +130,20 @@ module Spree
).slice(:shipping_address, :billing_address).reject { |_key, value| value.blank? }
end
- def address_params
- permitted = sent_addresses
-
- {}.tap do |attributes|
- if permitted[:shipping_address].present?
- attributes[:ship_address_attributes] =
- merged_address(@resource.ship_address, permitted[:shipping_address])
+ def corrected_addresses
+ {}.tap do |addresses|
+ if sent_addresses[:shipping_address].present?
+ addresses[:ship_address] = corrected_address(@resource.ship_address, sent_addresses[:shipping_address])
end
- if permitted[:billing_address].present?
- attributes[:bill_address_attributes] =
- merged_address(@resource.bill_address, permitted[:billing_address])
+ if sent_addresses[:billing_address].present?
+ addresses[:bill_address] = corrected_address(@resource.bill_address, sent_addresses[:billing_address])
end
end
end
- def merged_address(address, sent)
- current = address ? address.attributes.slice(*address_attribute_names) : {}
- current.merge(sent.to_h)
- end
-
- # The permitted keys that are real columns; the rest are the writer
- # aliases a client may send, which have no value to carry over.
- def address_attribute_names
- @address_attribute_names ||=
- address_permitted_keys.map(&:to_s) & Spree::Address.column_names
+ def corrected_address(address, sent)
+ (address&.snapshot || Spree::Address.new).tap { |copy| copy.assign_attributes(sent.to_h) }
end
def address_permitted_keys
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
index feb00b4bee..aaa41ad766 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
@@ -117,6 +117,32 @@ RSpec.describe Spree::Api::V3::Seller::OrdersController, type: :controller do
expect(original.reload.address1).to eq(original_line)
end
+ # The correction is this order's, not the buyer's account: their default
+ # addresses and address book serve every later checkout.
+ it "leaves the buyer's default addresses and address book alone" do
+ customer = mine.customer
+ defaults = [customer.ship_address_id, customer.bill_address_id]
+ book_size = customer.addresses.count
+
+ patch :address, params: {
+ id: mine.prefixed_id,
+ shipping_address: { address1: '9 Corrected Way' },
+ billing_address: { address1: '4 Invoice Street' }
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ customer.reload
+ expect([customer.ship_address_id, customer.bill_address_id]).to eq(defaults)
+ expect(customer.addresses.count).to eq(book_size)
+ expect(mine.reload.ship_address.owner).to be_nil
+ end
+
+ it 'refuses an address the correction leaves invalid' do
+ patch :address, params: { id: mine.prefixed_id, shipping_address: { address1: '' } }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
it 'corrects the billing address' do
patch :address, params: {
id: mine.prefixed_id,