mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-03 06:59:14 +00:00
Load all shops that a user is associated with as a customer
Regardless of the presence of an order
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user