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.

Simplify `Spree::Api::V3::Orders::Update` service

Damian Legawiec7e2e1de4c393
diff --git a/api/app/services/spree/api/v3/orders/update.rb b/api/app/services/spree/api/v3/orders/update.rb
index 38b03526bc..d81e55d8e1 100644
--- a/api/app/services/spree/api/v3/orders/update.rb
+++ b/api/app/services/spree/api/v3/orders/update.rb
@@ -41,21 +41,6 @@ module Spree
ship_address_id
].freeze
- PERMITTED_ADDRESS_PARAMS = %i[
- id
- firstname
- lastname
- address1
- address2
- city
- zipcode
- phone
- company
- country_iso
- state_abbr
- state_name
- ].freeze
-
def call(order:, params:)
@order = order
@params = params.to_h.deep_symbolize_keys
@@ -64,11 +49,9 @@ module Spree
ApplicationRecord.transaction do
update_currency if @params[:currency].present?
- update_email if @params[:email].present?
- update_special_instructions if @params[:special_instructions].present?
- update_ship_address if @params[:ship_address].present?
- update_bill_address if @params[:bill_address].present?
- update_address_ids
+ update_order_attributes
+ update_address(:ship_address)
+ update_address(:bill_address)
order.save!
end
@@ -86,30 +69,29 @@ module Spree
def validation_error
@validation_error ||= begin
- error = validate_currency
- error ||= validate_address_ownership(:ship_address)
+ error = validate_address_ownership(:ship_address)
error ||= validate_address_ownership(:bill_address)
error
end
end
- def validate_currency
- new_currency = params[:currency]
- return nil unless new_currency.present?
-
- supported_currencies = order.store.supported_currencies_list.map(&:iso_code)
- return nil if supported_currencies.include?(new_currency.upcase)
-
- Spree.t(:currency_not_supported, currency: new_currency)
- end
-
def validate_address_ownership(address_type)
+ # Check nested address params (ship_address: { id: ... })
address_params = params[address_type]
- return nil unless address_params.is_a?(Hash)
+ if address_params.is_a?(Hash) && address_params[:id].present?
+ return ownership_error_for(address_params[:id])
+ end
- address_id = address_params[:id]
- return nil unless address_id.present?
+ # Check top-level address ID (ship_address_id: ...)
+ address_id = params[:"#{address_type}_id"]
+ if address_id.present?
+ return ownership_error_for(address_id)
+ end
+ nil
+ end
+
+ def ownership_error_for(address_id)
address = Spree::Address.find_by_prefix_id(address_id)
return nil unless address
@@ -128,60 +110,55 @@ module Spree
order.homogenize_line_item_currencies
end
- def update_email
- order.email = params[:email]
+ def update_order_attributes
+ order.email = params[:email] if params[:email].present?
+ order.special_instructions = params[:special_instructions] if params[:special_instructions].present?
end
- def update_special_instructions
- order.special_instructions = params[:special_instructions]
- end
-
- def update_ship_address
- address_params = normalize_address_params(params[:ship_address])
+ def update_address(address_type)
+ address_id_param = params[:"#{address_type}_id"]
+ address_params = params[address_type]
- if address_params[:id].present?
- # Using existing address - find by prefix_id
- existing = Spree::Address.find_by_prefix_id!(address_params[:id])
- order.ship_address_id = existing.id
- else
- # Creating/updating address
- revert_to_address_state if order.has_checkout_step?('address')
- order.ship_address = build_or_update_address(order.ship_address, address_params)
+ # Priority 1: Direct address ID reference (ship_address_id / bill_address_id)
+ if address_id_param.present?
+ assign_existing_address(address_type, address_id_param)
+ return
end
- end
- def update_bill_address
- address_params = normalize_address_params(params[:bill_address])
+ # Priority 2: Nested address params (ship_address / bill_address)
+ return unless address_params.is_a?(Hash)
if address_params[:id].present?
- # Using existing address - find by prefix_id
- existing = Spree::Address.find_by_prefix_id!(address_params[:id])
- order.bill_address_id = existing.id
+ # Using existing address by ID within nested params
+ assign_existing_address(address_type, address_params[:id])
else
- # Creating/updating address
- revert_to_address_state if order.has_checkout_step?('address')
- order.bill_address = build_or_update_address(order.bill_address, address_params)
+ # Creating/updating address with provided attributes
+ build_address(address_type, address_params)
end
end
- def update_address_ids
- if params[:ship_address_id].present?
- existing = Spree::Address.find_by_prefix_id!(params[:ship_address_id])
- # Use bracket notation to bypass Order::AddressBook custom setter
- # which requires address.user_id == order.user_id
- order[:ship_address_id] = existing.id
- end
- if params[:bill_address_id].present?
- existing = Spree::Address.find_by_prefix_id!(params[:bill_address_id])
- # Use bracket notation to bypass Order::AddressBook custom setter
- order[:bill_address_id] = existing.id
- end
+ def assign_existing_address(address_type, prefix_id)
+ existing = Spree::Address.find_by_prefix_id!(prefix_id)
+ # Use bracket notation to bypass Order::AddressBook custom setter
+ # which requires address.user_id == order.user_id
+ order[:"#{address_type}_id"] = existing.id
+ end
+
+ def build_address(address_type, address_params)
+ normalized = normalize_address_params(address_params)
+ revert_to_address_state if order.has_checkout_step?('address')
+
+ existing_address = order.public_send(address_type)
+ new_address = build_or_update_address(existing_address, normalized)
+ order.public_send(:"#{address_type}=", new_address)
end
def normalize_address_params(address_params)
- return {} unless address_params.is_a?(Hash)
+ permitted_keys = Spree::PermittedAttributes.address_attributes.select { |attr| attr.is_a?(Symbol) }
+ # Also permit state_abbr which is handled by the helper but not in permitted attributes
+ permitted_keys += [:state_abbr]
- normalized = address_params.slice(*PERMITTED_ADDRESS_PARAMS)
+ normalized = address_params.slice(*permitted_keys)
fill_country_and_state_ids(normalized)
normalized
end
diff --git a/api/spec/services/spree/api/v3/orders/update_spec.rb b/api/spec/services/spree/api/v3/orders/update_spec.rb
index 06af676c03..b62a314331 100644
--- a/api/spec/services/spree/api/v3/orders/update_spec.rb
+++ b/api/spec/services/spree/api/v3/orders/update_spec.rb
@@ -56,126 +56,88 @@ RSpec.describe Spree::Api::V3::Orders::Update do
end
end
- describe 'updating ship_address' do
+ describe 'updating addresses' do
let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
let!(:state) { country.states.find_by(abbr: 'NY') || create(:state, country: country, abbr: 'NY', name: 'New York') }
- context 'with new address attributes' do
- let(:params) do
- {
- ship_address: {
- firstname: 'John',
- lastname: 'Doe',
- address1: '123 Main St',
- city: 'New York',
- zipcode: '10001',
- country_iso: 'US',
- state_abbr: 'NY',
- phone: '555-1234'
+ shared_examples 'address update' do |address_type|
+ let(:address_key) { address_type } # :ship_address or :bill_address
+ let(:address_id_key) { :"#{address_type}_id" } # :ship_address_id or :bill_address_id
+
+ context 'with new address attributes' do
+ let(:params) do
+ {
+ address_key => {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '123 Main St',
+ city: 'New York',
+ zipcode: '10001',
+ country_iso: 'US',
+ state_abbr: 'NY',
+ phone: '555-1234'
+ }
}
- }
- end
-
- it 'creates a new shipping address' do
- expect(subject).to be_success
- address = order.reload.ship_address
- expect(address.firstname).to eq('John')
- expect(address.lastname).to eq('Doe')
- expect(address.address1).to eq('123 Main St')
- expect(address.city).to eq('New York')
- expect(address.zipcode).to eq('10001')
- expect(address.country.iso).to eq('US')
- expect(address.state.abbr).to eq('NY')
- end
-
- context 'when order has address checkout step' do
- let(:order) { create(:order_with_line_items, user: user, store: store, state: 'delivery') }
+ end
- it 'reverts order to address state' do
+ it 'creates a new address' do
expect(subject).to be_success
- expect(order.reload.state).to eq('address')
+ address = order.reload.public_send(address_key)
+ expect(address.firstname).to eq('John')
+ expect(address.lastname).to eq('Doe')
+ expect(address.address1).to eq('123 Main St')
+ expect(address.city).to eq('New York')
+ expect(address.zipcode).to eq('10001')
+ expect(address.country.iso).to eq('US')
+ expect(address.state.abbr).to eq('NY')
end
- end
- context 'when order is in cart state' do
- let(:order) { create(:order_with_line_items, user: user, store: store, state: 'cart') }
+ context 'when order has address checkout step and is past address state' do
+ let(:order) { create(:order_with_line_items, user: user, store: store, state: 'delivery') }
- it 'does not change order state' do
- expect(subject).to be_success
- expect(order.reload.state).to eq('cart')
+ it 'reverts order to address state' do
+ expect(subject).to be_success
+ expect(order.reload.state).to eq('address')
+ end
end
- end
- end
- context 'with existing address by prefix_id' do
- let(:existing_address) { create(:address, user: user) }
- let(:params) { { ship_address: { id: existing_address.prefix_id } } }
+ context 'when order is in cart state' do
+ let(:order) { create(:order_with_line_items, user: user, store: store, state: 'cart') }
- it 'uses the existing address' do
- expect(subject).to be_success
- expect(order.reload.ship_address_id).to eq(existing_address.id)
+ it 'does not change order state' do
+ expect(subject).to be_success
+ expect(order.reload.state).to eq('cart')
+ end
+ end
end
- end
- context 'with ship_address_id parameter' do
- let(:existing_address) { create(:address, user: user) }
- let(:params) { { ship_address_id: existing_address.prefix_id } }
+ context 'with existing address by nested id' do
+ let(:existing_address) { create(:address, user: user) }
+ let(:params) { { address_key => { id: existing_address.prefix_id } } }
- it 'uses the existing address' do
- expect(subject).to be_success
- expect(order.reload.ship_address_id).to eq(existing_address.id)
+ it 'uses the existing address' do
+ expect(subject).to be_success
+ expect(order.reload.public_send(address_id_key)).to eq(existing_address.id)
+ end
end
- end
- end
- describe 'updating bill_address' do
- let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
- let!(:state) { country.states.find_by(abbr: 'CA') || create(:state, country: country, abbr: 'CA', name: 'California') }
-
- context 'with new address attributes' do
- let(:params) do
- {
- bill_address: {
- firstname: 'Jane',
- lastname: 'Smith',
- address1: '456 Oak Ave',
- city: 'Los Angeles',
- zipcode: '90001',
- country_iso: 'US',
- state_abbr: 'CA',
- phone: '555-5678'
- }
- }
- end
+ context 'with top-level address_id parameter' do
+ let(:existing_address) { create(:address, user: user) }
+ let(:params) { { address_id_key => existing_address.prefix_id } }
- it 'creates a new billing address' do
- expect(subject).to be_success
- address = order.reload.bill_address
- expect(address.firstname).to eq('Jane')
- expect(address.lastname).to eq('Smith')
- expect(address.address1).to eq('456 Oak Ave')
- expect(address.city).to eq('Los Angeles')
+ it 'uses the existing address' do
+ expect(subject).to be_success
+ expect(order.reload.public_send(address_id_key)).to eq(existing_address.id)
+ end
end
end
- context 'with existing address by prefix_id' do
- let(:existing_address) { create(:address, user: user) }
- let(:params) { { bill_address: { id: existing_address.prefix_id } } }
-
- it 'uses the existing address' do
- expect(subject).to be_success
- expect(order.reload.bill_address_id).to eq(existing_address.id)
- end
+ describe 'ship_address' do
+ include_examples 'address update', :ship_address
end
- context 'with bill_address_id parameter' do
- let(:existing_address) { create(:address, user: user) }
- let(:params) { { bill_address_id: existing_address.prefix_id } }
-
- it 'uses the existing address' do
- expect(subject).to be_success
- expect(order.reload.bill_address_id).to eq(existing_address.id)
- end
+ describe 'bill_address' do
+ include_examples 'address update', :bill_address
end
end
@@ -183,24 +145,29 @@ RSpec.describe Spree::Api::V3::Orders::Update do
let(:other_user) { create(:user) }
let(:other_users_address) { create(:address, user: other_user) }
- context 'when using another users address for ship_address' do
- let(:params) { { ship_address: { id: other_users_address.prefix_id } } }
+ shared_examples 'rejects other users address' do |address_type|
+ context "when using another user's address for #{address_type}" do
+ let(:params) { { address_type => { id: other_users_address.prefix_id } } }
- it 'returns failure' do
- expect(subject).to be_failure
- expect(subject.error).to be_present
+ it 'returns failure' do
+ expect(subject).to be_failure
+ expect(subject.error).to be_present
+ end
end
- end
- context 'when using another users address for bill_address' do
- let(:params) { { bill_address: { id: other_users_address.prefix_id } } }
+ context "when using another user's address via #{address_type}_id" do
+ let(:params) { { :"#{address_type}_id" => other_users_address.prefix_id } }
- it 'returns failure' do
- expect(subject).to be_failure
- expect(subject.error).to be_present
+ it 'returns failure' do
+ expect(subject).to be_failure
+ expect(subject.error).to be_present
+ end
end
end
+ include_examples 'rejects other users address', :ship_address
+ include_examples 'rejects other users address', :bill_address
+
context 'when using guest address (no user)' do
let(:guest_address) { create(:address, user: nil) }
let(:params) { { ship_address_id: guest_address.prefix_id } }