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.

SD-1357 move core create & update address services to frontend addresses controller (#11140)

Kacper Mękarskid3620d685dc3
diff --git a/api/spec/controllers/spree/api/v2/storefront/account/addresses_controller_spec.rb b/api/spec/controllers/spree/api/v2/storefront/account/addresses_controller_spec.rb
new file mode 100644
index 0000000000..bc91f0f74d
--- /dev/null
+++ b/api/spec/controllers/spree/api/v2/storefront/account/addresses_controller_spec.rb
@@ -0,0 +1,237 @@
+require 'spec_helper'
+
+describe 'Spree::Api::V2::Storefront::Account::AddressesController', type: :request do
+ let!(:user) { create(:user_with_addresses) }
+ let!(:addresses) { create_list(:address, 1, user_id: user.id, country: country, state: state) }
+ let!(:country) { create(:country) }
+ let!(:state) { create(:state, country: country) }
+ let(:params) { { address: new_attributes } }
+
+ include_context 'API v2 tokens'
+
+ describe 'addresses#create' do
+ subject :post_create do
+ post '/api/v2/storefront/account/addresses', params: params, headers: headers_bearer
+ end
+
+ context 'valid request' do
+ let(:new_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor',
+ city: 'London',
+ phone: '079 4721 9458',
+ zipcode: 'SE25 3FZ',
+ state_name: 'England',
+ country_id: country.id
+ }
+ end
+
+ it 'returns created address attributes' do
+ post_create
+
+ expect(json_response['data']).to have_attribute(:firstname).with_value(new_attributes[:firstname])
+ expect(json_response['data']).to have_attribute(:lastname).with_value(new_attributes[:lastname])
+ expect(json_response['data']).to have_attribute(:address1).with_value(new_attributes[:address1])
+ expect(json_response['data']).to have_attribute(:address2).with_value(new_attributes[:address2])
+ expect(json_response['data']).to have_attribute(:city).with_value(new_attributes[:city])
+ expect(json_response['data']).to have_attribute(:phone).with_value(new_attributes[:phone])
+ expect(json_response['data']).to have_attribute(:zipcode).with_value(new_attributes[:zipcode])
+ expect(json_response['data']).to have_attribute(:state_name).with_value(new_attributes[:state_name])
+ expect(json_response.size).to eq(1)
+ end
+
+ it 'creates address' do
+ expect{ post_create }.to change { Spree::Address.count }.by(1)
+ end
+ end
+
+ context 'invalid request' do
+ let(:new_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor'
+ }
+ end
+
+ it 'returns errors' do
+ post_create
+
+ expect(json_response['error']).to eq("City can't be blank, Country can't be blank, Zip Code can't be blank, Phone can't be blank")
+ expect(json_response['errors']).to eq(
+ 'city' => ["can't be blank"],
+ 'zipcode' => ["can't be blank"],
+ 'country' => ["can't be blank"],
+ 'phone' => ["can't be blank"]
+ )
+ end
+
+ it 'does not create address' do
+ expect{ post_create }.to change { Spree::Address.count }.by(0)
+ end
+ end
+
+ context 'with missing authorization token' do
+ before { post '/api/v2/storefront/account/addresses' }
+
+ it_behaves_like 'returns 403 HTTP status'
+ end
+ end
+
+ describe 'addresses#update' do
+ subject :patch_update do
+ patch "/api/v2/storefront/account/addresses/#{address.id}", params: params, headers: headers_bearer
+ end
+
+ let(:address) { addresses.last }
+
+ context 'when address is editable' do
+ context 'valid request' do
+ let(:new_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor',
+ city: 'London',
+ phone: '079 4721 9458',
+ zipcode: 'SE25 3FZ',
+ state_name: state.name,
+ }
+ end
+
+ it 'updates and returns address' do
+ patch_update
+
+ expect(json_response['data']).to have_id(address.id.to_s)
+ expect(json_response['data']).to have_attribute(:firstname).with_value(new_attributes[:firstname])
+ expect(json_response['data']).to have_attribute(:lastname).with_value(new_attributes[:lastname])
+ expect(json_response['data']).to have_attribute(:address1).with_value(new_attributes[:address1])
+ expect(json_response['data']).to have_attribute(:address2).with_value(new_attributes[:address2])
+ expect(json_response['data']).to have_attribute(:city).with_value(new_attributes[:city])
+ expect(json_response['data']).to have_attribute(:phone).with_value(new_attributes[:phone])
+ expect(json_response['data']).to have_attribute(:zipcode).with_value(new_attributes[:zipcode])
+ expect(json_response['data']).to have_attribute(:state_name).with_value(new_attributes[:state_name])
+ expect(json_response.size).to eq(1)
+ end
+
+ it 'does not create new address' do
+ expect{ patch_update }.to change { Spree::Address.count }.by(0)
+ end
+ end
+
+ context 'invalid request' do
+ let(:new_attributes) do
+ {
+ firstname: '',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor',
+ city: 'London',
+ phone: '079 4721 9458',
+ zipcode: 'SE25 3FZ',
+ state_name: state.name,
+ }
+ end
+
+ it 'returns errors' do
+ patch_update
+
+ expect(json_response['error']).to eq("First Name can't be blank")
+ expect(json_response['errors']).to eq(
+ 'firstname' => ["can't be blank"]
+ )
+ end
+
+ it 'does not update address' do
+ expect { patch_update }.not_to change { address }
+ end
+ end
+ end
+
+ context 'when address is not editable' do
+ let!(:shipment) { create(:shipment, address: address) }
+
+ context 'valid request' do
+ let(:new_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor',
+ city: 'London',
+ phone: '079 4721 9458',
+ zipcode: 'SE25 3FZ',
+ state_name: state.name,
+ }
+ end
+
+ it 'returns created address attributes' do
+ patch_update
+
+ expect(json_response['data']).to have_attribute(:firstname).with_value(new_attributes[:firstname])
+ expect(json_response['data']).to have_attribute(:lastname).with_value(new_attributes[:lastname])
+ expect(json_response['data']).to have_attribute(:address1).with_value(new_attributes[:address1])
+ expect(json_response['data']).to have_attribute(:address2).with_value(new_attributes[:address2])
+ expect(json_response['data']).to have_attribute(:city).with_value(new_attributes[:city])
+ expect(json_response['data']).to have_attribute(:phone).with_value(new_attributes[:phone])
+ expect(json_response['data']).to have_attribute(:zipcode).with_value(new_attributes[:zipcode])
+ expect(json_response['data']).to have_attribute(:state_name).with_value(new_attributes[:state_name])
+ expect(json_response.size).to eq(1)
+ end
+
+ it 'creates address' do
+ expect{ patch_update }.to change { Spree::Address.count }.by(1)
+ end
+
+ it 'sets deleted_at attribute of original address' do
+ Timecop.freeze(Time.current) do
+ expect(address.deleted_at).to be_nil
+
+ patch_update
+
+ expect(address.reload.deleted_at).not_to be nil
+ end
+ end
+ end
+
+ context 'invalid request' do
+ let(:new_attributes) do
+ {
+ firstname: '',
+ lastname: 'Doe',
+ address1: '51 Guild Street',
+ address2: '2nd floor',
+ city: 'London',
+ phone: '079 4721 9458',
+ zipcode: 'SE25 3FZ',
+ state_name: state.name,
+ }
+ end
+
+ it 'returns errors' do
+ patch_update
+
+ expect(json_response['error']).to eq("First Name can't be blank")
+ expect(json_response['errors']).to eq(
+ 'firstname' => ["can't be blank"]
+ )
+ end
+
+ it 'does not update address' do
+ expect { patch_update }.not_to change { address }
+ end
+ end
+ end
+
+ context 'with missing authorization token' do
+ before { patch "/api/v2/storefront/account/addresses/#{address.id}" }
+
+ it_behaves_like 'returns 403 HTTP status'
+ end
+ end
+end
diff --git a/core/app/services/spree/account/addresses/update.rb b/core/app/services/spree/account/addresses/update.rb
index 501a9335ae..a404bb9cd4 100644
--- a/core/app/services/spree/account/addresses/update.rb
+++ b/core/app/services/spree/account/addresses/update.rb
@@ -11,12 +11,18 @@ module Spree
address_params[:country_id] ||= address.country_id
fill_country_and_state_ids(address_params)
- if address.update(address_params)
- success(address)
+ if address&.editable?
+ address.update(address_params) ? success(address) : failure(address)
else
- failure(address)
+ new_address(address_params).valid? ? address.destroy && success(new_address) : failure(new_address)
end
end
+
+ private
+
+ def new_address(address_params = {})
+ @new_address ||= ::Spree::Address.find_or_create_by(address_params.except(:id, :updated_at, :created_at))
+ end
end
end
end
diff --git a/frontend/app/controllers/spree/addresses_controller.rb b/frontend/app/controllers/spree/addresses_controller.rb
index 30694f1fc4..65d9814311 100644
--- a/frontend/app/controllers/spree/addresses_controller.rb
+++ b/frontend/app/controllers/spree/addresses_controller.rb
@@ -10,7 +10,7 @@ module Spree
def create
@address = try_spree_current_user.addresses.build(address_params)
- if @address.save
+ if create_service.call(user: try_spree_current_user, address_params: @address.attributes).success?
flash[:notice] = I18n.t(:successfully_created, scope: :address_book)
redirect_to spree.account_path
else
@@ -27,24 +27,11 @@ module Spree
end
def update
- if @address.editable?
- if @address.update(address_params)
- flash[:notice] = Spree.t(:successfully_updated, scope: :address_book)
- redirect_back_or_default(addresses_path)
- else
- render :edit
- end
+ if update_service.call(address: @address, address_params: address_params).success?
+ flash[:notice] = Spree.t(:successfully_updated, scope: :address_book)
+ redirect_back_or_default(addresses_path)
else
- new_address = @address.clone
- new_address.attributes = address_params
- new_address.user_id = @address.user_id
- @address.update_attribute(:deleted_at, Time.current)
- if new_address.save
- flash[:notice] = Spree.t(:successfully_updated, scope: :address_book)
- redirect_back_or_default(addresses_path)
- else
- render :edit
- end
+ render :edit
end
end
@@ -60,5 +47,13 @@ module Spree
def address_params
params.require(:address).permit(permitted_address_attributes)
end
+
+ def create_service
+ Spree::Dependencies.account_create_address_service.constantize
+ end
+
+ def update_service
+ Spree::Dependencies.account_update_address_service.constantize
+ end
end
end
diff --git a/frontend/spec/controllers/spree/addresses_controller_spec.rb b/frontend/spec/controllers/spree/addresses_controller_spec.rb
new file mode 100644
index 0000000000..691b8c49fc
--- /dev/null
+++ b/frontend/spec/controllers/spree/addresses_controller_spec.rb
@@ -0,0 +1,183 @@
+require 'spec_helper'
+
+describe Spree::AddressesController, type: :controller do
+ let(:store) { Spree::Store.default }
+ let(:country) { store.default_country }
+ let(:state) { country.states.first }
+ let(:token) { 'some_token' }
+ let(:user) { create(:user) }
+ let(:order) { create(:order_with_totals, store: store) }
+
+ before do
+ allow(controller).to receive_messages try_spree_current_user: user
+ allow(controller).to receive_messages spree_current_user: user
+ allow(controller).to receive_messages current_order: order
+ allow_any_instance_of(ActionDispatch::Routing::RoutesProxy).to receive(:account_path).and_return('/account')
+ allow_any_instance_of(CanCan::ControllerResource).to receive(:load_resource)
+ allow_any_instance_of(CanCan::ControllerResource).to receive(:authorize_resource)
+ end
+
+ describe '#create' do
+ subject :post_create do
+ post :create, params: { address: address_params }
+ end
+
+ context 'when data is valid' do
+ let(:address_params) do
+ address = build(:address, country: country, state: state)
+ address.attributes.except('created_at', 'updated_at')
+ end
+
+ it 'returns 302 status code' do
+ post_create
+
+ expect(response.status).to eq(302)
+ end
+
+ it 'redirects to /account' do
+ post_create
+
+ expect(response).to redirect_to('/account')
+ end
+
+ it 'creates address' do
+ expect{ post_create }.to change { Spree::Address.count }.by(1)
+ end
+
+ it 'sets flash message' do
+ post_create
+
+ expect(flash[:notice]).to eq I18n.t(:successfully_created, scope: :address_book)
+ end
+ end
+
+ context 'when data is not valid' do
+ let(:address_params) do
+ address = build(:address, country: country, state: state, first_name: nil)
+ address.attributes.except('created_at', 'updated_at')
+ end
+
+ it 'does not create address' do
+ expect{ post_create }.to change { Spree::Address.count }.by(0)
+ end
+
+ it 'returns 200 status code' do
+ post_create
+
+ expect(response.status).to eq(200)
+ end
+
+ it 'renders address form template' do
+ expect(post_create).to render_template(:new)
+ end
+ end
+ end
+
+ describe '#update' do
+ subject :put_update do
+ put :update, params: { address: address_params, id: address.id }
+ end
+
+ before { controller.instance_variable_set(:@address, address) }
+
+ context 'when address is editable' do
+ let(:address) { create(:address, country: country, state: state, label: 'label', user: user) }
+
+ context 'with data is valid' do
+ let(:address_params) { address.attributes.symbolize_keys.merge(firstname: 'test') }
+
+ it 'updates address' do
+ expect(address.attributes.symbolize_keys.except(:created_at, :updated_at)).not_to match(address_params.except(:created_at, :updated_at))
+
+ put_update
+
+ expect(address.reload.attributes.symbolize_keys.except(:created_at, :updated_at)).to match(address_params.except(:created_at, :updated_at))
+ end
+
+ it 'sets flash message' do
+ put_update
+
+ expect(flash[:notice]).to eq Spree.t(:successfully_updated, scope: :address_book)
+ end
+
+ it 'returns 302 status code' do
+ put_update
+
+ expect(response.status).to eq(302)
+ end
+
+ it 'redirects to /account' do
+ put_update
+
+ expect(response).to redirect_to(spree.addresses_path)
+ end
+ end
+
+ context 'when data is not valid' do
+ let(:address_params) { address.attributes.symbolize_keys.merge(firstname: nil) }
+
+ it 'does not update address' do
+ expect { put_update }.not_to change { address }
+ end
+
+ it 'renders address form template' do
+ expect(put_update).to render_template(:edit)
+ end
+ end
+ end
+
+
+ context 'when address is not editable' do
+ let(:address) { create(:address, country: country, state: state, label: 'label', user: user) }
+ let!(:shipment) { create(:shipment, address: address) }
+
+ context 'with data is valid' do
+ let(:address_params) { address.attributes.symbolize_keys.except(:id).merge(firstname: 'test', label: '') }
+
+ it 'creates address' do
+ expect{ put_update }.to change { Spree::Address.count }.by(1)
+ end
+
+ it 'sets deleted_at attribute of original address' do
+ Timecop.freeze(Time.current) do
+ expect(address.deleted_at).to be_nil
+
+ put_update
+
+ expect(address.deleted_at).not_to be nil
+ end
+ end
+
+ it 'sets flash message' do
+ put_update
+
+ expect(flash[:notice]).to eq Spree.t(:successfully_updated, scope: :address_book)
+ end
+
+ it 'returns 302 status code' do
+ put_update
+
+ expect(response.status).to eq(302)
+ end
+
+ it 'redirects to /account' do
+ put_update
+
+ expect(response).to redirect_to(spree.addresses_path)
+ end
+ end
+
+ context 'when data is not valid' do
+ let(:address_params) { address.attributes.symbolize_keys.merge(firstname: nil) }
+
+ it 'does not create address' do
+ expect{ put_update }.to change { Spree::Address.count }.by(0)
+ end
+
+ it 'renders address form template' do
+ expect(put_update).to render_template(:edit)
+ end
+ end
+ end
+ end
+end