mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Move customer address lookup action to CustomersController
This commit is contained in:
@@ -47,7 +47,7 @@ angular.module("admin.standingOrders").controller "StandingOrderController", ($s
|
||||
|
||||
$scope.$watch "standingOrder.customer_id", (newValue, oldValue) ->
|
||||
return if !newValue? || newValue == oldValue
|
||||
$http.get("/admin/search/customer_addresses", params: { customer_id: newValue })
|
||||
$http.get("/admin/customers/#{newValue}/addresses")
|
||||
.success (response) =>
|
||||
delete response.bill_address.id
|
||||
delete response.ship_address.id
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require 'open_food_network/address_finder'
|
||||
|
||||
module Admin
|
||||
class CustomersController < ResourceController
|
||||
before_filter :load_managed_shops, only: :index, if: :html_request?
|
||||
@@ -53,6 +55,15 @@ module Admin
|
||||
end
|
||||
end
|
||||
|
||||
# GET /admin/customers/:id/addresses
|
||||
# Used by standing orders form to load details for selected customer
|
||||
def addresses
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def collection
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
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])
|
||||
@@ -35,14 +33,4 @@ Spree::Admin::SearchController.class_eval do
|
||||
end
|
||||
|
||||
alias_method_chain :users, :ams
|
||||
|
||||
def customer_addresses
|
||||
customer = Customer.of(spree_current_user.enterprises).find_by_id(params[:customer_id])
|
||||
return redirect_to :unauthorized 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
|
||||
|
||||
@@ -111,7 +111,7 @@ class AbilityDecorator
|
||||
user.enterprises.include? enterprise_fee.enterprise
|
||||
end
|
||||
|
||||
can [:admin, :known_users, :customers, :customer_addresses], :search
|
||||
can [:admin, :known_users, :customers], :search
|
||||
|
||||
can [:admin, :show], :account
|
||||
|
||||
@@ -251,7 +251,7 @@ class AbilityDecorator
|
||||
can [:admin, :index, :customers, :group_buys, :bulk_coop, :sales_tax, :payments, :orders_and_distributors, :orders_and_fulfillment, :products_and_inventory, :order_cycle_management, :xero_invoices], :report
|
||||
|
||||
can [:create], Customer
|
||||
can [:admin, :index, :update, :destroy], Customer, enterprise_id: Enterprise.managed_by(user).pluck(:id)
|
||||
can [:admin, :index, :update, :destroy, :addresses], Customer, enterprise_id: Enterprise.managed_by(user).pluck(:id)
|
||||
can [:admin, :new, :index], StandingOrder
|
||||
can [:create, :edit, :update, :cancel, :pause, :unpause], StandingOrder do |standing_order|
|
||||
user.enterprises.include?(standing_order.shop)
|
||||
|
||||
@@ -141,7 +141,9 @@ Openfoodnetwork::Application.routes.draw do
|
||||
|
||||
resources :inventory_items, only: [:create, :update]
|
||||
|
||||
resources :customers, only: [:index, :create, :update, :destroy]
|
||||
resources :customers, only: [:index, :create, :update, :destroy] do
|
||||
get :addresses, on: :member
|
||||
end
|
||||
|
||||
resources :tag_rules, only: [], format: :json do
|
||||
get :map_by_tag, on: :collection
|
||||
|
||||
@@ -138,4 +138,47 @@ describe Admin::CustomersController, type: :controller do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#addresses" do
|
||||
let!(:enterprise) { create(:enterprise) }
|
||||
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 } }
|
||||
|
||||
before { login_as_enterprise_user [enterprise] }
|
||||
|
||||
context "when I manage the customer" do
|
||||
before { params.merge!(id: managed_customer.id) }
|
||||
|
||||
it "returns with serialized addresses for the customer" do
|
||||
spree_get :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 :addresses, params
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
|
||||
context "when no customer with a matching id exists" do
|
||||
before { params.merge!({customer_id: 1}) }
|
||||
|
||||
it "redirects to unauthorised" do
|
||||
spree_get :addresses, params
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -70,45 +70,5 @@ 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
|
||||
|
||||
context "when no customer with a matching id exists" do
|
||||
before { params.merge!({customer_id: 1}) }
|
||||
|
||||
it "redirects to unauthorised" do
|
||||
spree_get :customer_addresses, params
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user