mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
42 lines
1.2 KiB
Ruby
42 lines
1.2 KiB
Ruby
module Admin
|
|
class CustomersController < ResourceController
|
|
before_filter :load_managed_shops, only: :index, if: :html_request?
|
|
respond_to :json
|
|
|
|
def index
|
|
respond_to do |format|
|
|
format.html
|
|
format.json do
|
|
serialised = ActiveModel::ArraySerializer.new(
|
|
@collection,
|
|
each_serializer: Api::Admin::CustomerSerializer,
|
|
spree_current_user: spree_current_user)
|
|
render json: serialised.to_json
|
|
end
|
|
end
|
|
end
|
|
|
|
def create
|
|
@customer = Customer.new(params[:customer])
|
|
if spree_current_user.enterprises.include? @customer.enterprise
|
|
@customer.save
|
|
render json: Api::Admin::CustomerSerializer.new(@customer).to_json
|
|
else
|
|
redirect_to '/unauthorized'
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def collection
|
|
return Customer.where("1=0") unless json_request? && params[:enterprise_id].present?
|
|
enterprise = Enterprise.managed_by(spree_current_user).find_by_id(params[:enterprise_id])
|
|
Customer.of(enterprise)
|
|
end
|
|
|
|
def load_managed_shops
|
|
@shops = Enterprise.managed_by(spree_current_user).is_distributor
|
|
end
|
|
end
|
|
end
|