Add new enterprise customers search API

This commit is contained in:
Bing Xie
2016-08-31 14:25:30 +10:00
parent fd32152e88
commit bc798504e0
5 changed files with 39 additions and 9 deletions

View File

@@ -16,14 +16,18 @@ Spree::Admin::SearchController.class_eval do
render :users
end
def users_with_customers_ams
enterprise_ids = spree_current_user.enterprises.map &:id
customers = Customer.ransack({m: 'or', email_start: params[:q], name_start: params[:q]})
def customers
@customers = Customer.ransack({m: 'or', email_start: params[:q], name_start: params[:q]})
.result(distinct: true)
.where(enterprise_id: enterprise_ids)
.where(enterprise_id: params[:distributor_id])
render json: customers, each_serializer: Api::Admin::CustomerSerializer
render json: @customers, each_serializer: Api::Admin::CustomerSerializer
end
alias_method_chain :users, :customers_ams
def users_with_ams
users_without_ams
render json: @users, each_serializer: Api::Admin::UserSerializer
end
alias_method_chain :users, :ams
end

View File

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

View File

@@ -234,6 +234,8 @@ 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
resources :products do
get :product_distributions, on: :member

View File

@@ -31,5 +31,29 @@ describe Spree::Admin::SearchController do
end
end
end
describe 'searching for customers' do
let!(:customer_1) { create(:customer, enterprise: enterprise, email: 'test1@email.com') }
let!(:customer_2) { create(:customer, enterprise: enterprise, name: 'test2') }
let!(:customer_3) { create(:customer, email: 'test3@email.com') }
before do
spree_get :customers, q: "test", distributor_id: enterprise.id
@results = JSON.parse(response.body)
end
describe 'when search query matches the email or name' do
it 'returns a list of customers of the enterprise' do
expect(@results.size).to eq 2
expect(@results.find { |c| c['id'] == customer_1.id}).to be_true
expect(@results.find { |c| c['id'] == customer_2.id}).to be_true
end
it 'does not return the customer of other enterprises' do
expect(@results.find { |c| c['id'] == customer_3.id}).to be_false
end
end
end
end
end

View File

@@ -498,7 +498,7 @@ module Spree
end
it "should have the ability to search for users which share management of its enterprises" do
should have_ability([:admin, :known_users, :users], for: :search)
should have_ability([:admin, :known_users, :customers], for: :search)
should_not have_ability([:users], for: :search)
end
end