Adding route and controller action for searching for customer addresses using AddressFinder

This commit is contained in:
Rob Harrington
2017-01-20 15:52:27 +11:00
parent c5066bb613
commit 6ac49a7694
4 changed files with 45 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
require 'open_food_network/address_finder'
Spree::Admin::SearchController.class_eval do
def known_users
if exact_match = Spree.user_class.find_by_email(params[:q])
@@ -33,4 +35,14 @@ Spree::Admin::SearchController.class_eval do
end
alias_method_chain :users, :ams
def customer_addresses
customer = Customer.of(spree_current_user.enterprises).find(params[:customer_id])
redirect_to :unauthorised unless customer.present?
finder = OpenFoodNetwork::AddressFinder.new(customer, customer.email)
bill_address = Api::AddressSerializer.new(finder.bill_address).serializable_hash
ship_address = Api::AddressSerializer.new(finder.ship_address).serializable_hash
render json: { bill_address: bill_address, ship_address: ship_address }
end
end

View File

@@ -111,7 +111,7 @@ class AbilityDecorator
user.enterprises.include? enterprise_fee.enterprise
end
can [:admin, :known_users, :customers], :search
can [:admin, :known_users, :customers, :customer_addresses], :search
can [:admin, :show], :account

View File

@@ -288,6 +288,7 @@ Spree::Core::Engine.routes.prepend do
namespace :admin do
get '/search/known_users' => "search#known_users", :as => :search_known_users
get '/search/customers' => 'search#customers', :as => :search_customers
get '/search/customer_addresses' => 'search#customer_addresses', :as => :search_customer_addresses
resources :products do
get :product_distributions, on: :member

View File

@@ -70,5 +70,36 @@ describe Spree::Admin::SearchController, type: :controller do
end
end
end
describe "searching for customer addresses" do
let(:bill_address) { create(:address, firstname: "Dominic", address1: "123 Lala Street" ) }
let(:ship_address) { create(:address, firstname: "Dom", address1: "123 Sesame Street") }
let(:managed_customer) { create(:customer, enterprise: enterprise, bill_address: bill_address, ship_address: ship_address) }
let(:unmanaged_customer) { create(:customer) }
let(:params) { { format: :json } }
context "when I manage the customer" do
before { params.merge!({customer_id: managed_customer.id}) }
it "returns with serialized addresses for the customer" do
spree_get :customer_addresses, params
json_response = JSON.parse(response.body)
expect(json_response.keys).to include "bill_address", "ship_address"
expect(json_response["bill_address"]["firstname"]).to eq "Dominic"
expect(json_response["bill_address"]["address1"]).to eq "123 Lala Street"
expect(json_response["ship_address"]["firstname"]).to eq "Dom"
expect(json_response["ship_address"]["address1"]).to eq "123 Sesame Street"
end
end
context "when I don't manage the customer" do
before { params.merge!({customer_id: unmanaged_customer.id}) }
it "redirects to unauthorised" do
spree_get :customer_addresses, params
expect(response).to redirect_to spree.unauthorized_path
end
end
end
end
end