diff --git a/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee b/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee index dee6ecedef..ee6c9b0151 100644 --- a/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/controllers/standing_order_controller.js.coffee @@ -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 diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index c1f5f6bf9a..0144d7ac9a 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -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 diff --git a/app/controllers/spree/admin/search_controller_decorator.rb b/app/controllers/spree/admin/search_controller_decorator.rb index 44cdce7aba..d0069fd2ae 100644 --- a/app/controllers/spree/admin/search_controller_decorator.rb +++ b/app/controllers/spree/admin/search_controller_decorator.rb @@ -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 diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 97c963bde1..7847e21d87 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -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) diff --git a/config/routes.rb b/config/routes.rb index 811fb8992e..20d25ee35b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/admin/customers_controller_spec.rb b/spec/controllers/admin/customers_controller_spec.rb index 3c75929972..b7c58a1b9a 100644 --- a/spec/controllers/admin/customers_controller_spec.rb +++ b/spec/controllers/admin/customers_controller_spec.rb @@ -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 diff --git a/spec/controllers/spree/admin/search_controller_spec.rb b/spec/controllers/spree/admin/search_controller_spec.rb index c0688e3349..59490cec5b 100644 --- a/spec/controllers/spree/admin/search_controller_spec.rb +++ b/spec/controllers/spree/admin/search_controller_spec.rb @@ -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