Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for prefill user address in new order #3558

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/app/controllers/spree/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def index

def new
user = Spree.user_class.find_by(id: params[:user_id]) if params[:user_id]
@order = Spree::Core::Importer::Order.import(user, order_params)
order_importer_params = order_params
order_importer_params[:bill_address] = user&.bill_address
order_importer_params[:ship_address] = user&.ship_address

@order = Spree::Core::Importer::Order.import(user, order_importer_params)
redirect_to cart_admin_order_url(@order)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
end

context "when a user_id is passed as a parameter" do
let(:user) { mock_model(Spree.user_class) }
let(:user) { mock_model(Spree.user_class, ship_address: mock_model(Spree::Address), bill_address: nil) }
before { allow(Spree.user_class).to receive_messages find_by: user }

it "imports a new order and assigns the user to the order" do
Expand Down
33 changes: 33 additions & 0 deletions backend/spec/features/admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
let!(:user_b) { create(:user_with_addresses, email: '[email protected]') }
let!(:admin_role) { create(:role, name: 'admin') }
let!(:user_role) { create(:role, name: 'user') }
let!(:store) { create(:store) }

let(:order) { create(:completed_order_with_totals, user: user_a, number: "R123") }

Expand Down Expand Up @@ -409,4 +410,36 @@ def always_invalid_email
end
end
end

context 'create new order' do
before do
allow(Spree.user_class).to receive(:find_by).with(id: user_a.id.to_s) { user_a }
click_link user_a.email
click_link 'Create order for this user'
end

it 'prefills the customer addresses with the user addresses' do
click_link 'Customer'

within '.js-billing-address' do
expect(page).to have_field('Street Address', with: user_a.bill_address.address1)
expect(page).to have_field("Street Address (cont'd)", with: user_a.bill_address.address2)
expect(page).to have_field('City', with: user_a.bill_address.city)
expect(page).to have_field('Zip Code', with: user_a.bill_address.zipcode)
expect(page).to have_select('Country', selected: "#{user_a.bill_address.country} of America")
expect(page).to have_select('State', selected: user_a.bill_address.state.name)
expect(page).to have_field('Phone', with: user_a.bill_address.phone)
end

within '.js-shipping-address' do
expect(page).to have_field('Street Address', with: user_a.ship_address.address1)
expect(page).to have_field("Street Address (cont'd)", with: user_a.ship_address.address2)
expect(page).to have_field('City', with: user_a.ship_address.city)
expect(page).to have_field('Zip Code', with: user_a.ship_address.zipcode)
expect(page).to have_select('Country', selected: "#{user_a.ship_address.country} of America")
expect(page).to have_select('State', selected: user_a.ship_address.state.name)
expect(page).to have_field('Phone', with: user_a.ship_address.phone)
end
end
end
end