Load all shops that a user is associated with as a customer

Regardless of the presence of an order
This commit is contained in:
Rob Harrington
2018-06-22 15:34:17 +10:00
parent 5e6291bce3
commit ddb9ae1140
4 changed files with 20 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
module Api
class CustomersController < BaseController
def index
@customers = current_api_user.customers
@customers = current_api_user.customers.of_regular_shops
render json: @customers, each_serializer: CustomerSerializer
end

View File

@@ -69,7 +69,8 @@ module InjectionHelper
end
def inject_shops
shops = Enterprise.where(id: @orders.pluck(:distributor_id).uniq)
customers = spree_current_user.customers.of_regular_shops
shops = Enterprise.where(id: @orders.pluck(:distributor_id).uniq | customers.pluck(:enterprise_id))
inject_json_ams "shops", shops.all, Api::ShopForOrdersSerializer
end

View File

@@ -23,6 +23,11 @@ class Customer < ActiveRecord::Base
scope :of, ->(enterprise) { where(enterprise_id: enterprise) }
scope :of_regular_shops, lambda {
next scoped unless Spree::Config.accounts_distributor_id
where('enterprise_id <> ?', Spree::Config.accounts_distributor_id)
}
before_create :associate_user
private

View File

@@ -23,6 +23,18 @@ module Api
expect(json_response.length).to eq 1
expect(json_response.first[:id]).to eq customer1.id
end
context "when the accounts distributor id has been set" do
before do
Spree::Config.set(accounts_distributor_id: customer1.enterprise.id)
end
it "ignores the customer for that enterprise (if it exists)" do
spree_get :index
expect(response.status).to eq 200
expect(json_response.length).to eq 0
end
end
end
describe "#update" do