diff --git a/app/assets/javascripts/admin/standing_orders/controllers/details_controller.js.coffee b/app/assets/javascripts/admin/standing_orders/controllers/details_controller.js.coffee index db265cd972..3f3764d0f8 100644 --- a/app/assets/javascripts/admin/standing_orders/controllers/details_controller.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/controllers/details_controller.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.standingOrders").controller "DetailsController", ($scope, StatusMessage) -> +angular.module("admin.standingOrders").controller "DetailsController", ($scope, $http, CreditCardResource, StatusMessage) -> $scope.cardRequired = false $scope.registerNextCallback 'details', -> @@ -10,8 +10,13 @@ angular.module("admin.standingOrders").controller "DetailsController", ($scope, else StatusMessage.display 'failure', t('admin.standing_orders.details.invalid_error') + $scope.$watch "standingOrder.customer_id", (newValue, oldValue) -> + return if !newValue? + $scope.loadAddresses(newValue) unless $scope.standingOrder.id? + $scope.loadCreditCards(newValue) + $scope.$watch "standingOrder.payment_method_id", (newValue, oldValue) -> - return if !newValue? || newValue == oldValue + return if !newValue? paymentMethod = ($scope.paymentMethods.filter (pm) -> pm.id == newValue)[0] return unless paymentMethod? if paymentMethod.type == "Spree::Gateway::StripeConnect" @@ -19,3 +24,15 @@ angular.module("admin.standingOrders").controller "DetailsController", ($scope, else $scope.cardRequired = false $scope.standingOrder.credit_card_id = null + + $scope.loadAddresses = (customer_id) -> + $http.get("/admin/customers/#{customer_id}/addresses") + .success (response) => + delete response.bill_address.id + delete response.ship_address.id + angular.extend($scope.standingOrder.bill_address, response.bill_address) + angular.extend($scope.standingOrder.ship_address, response.ship_address) + $scope.shipAddressFromBilling() unless response.ship_address.address1? + + $scope.loadCreditCards = (customer_id) -> + $scope.creditCards = CreditCardResource.index(customer_id: customer_id) 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 87435d099f..00870951a5 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 @@ -1,4 +1,4 @@ -angular.module("admin.standingOrders").controller "StandingOrderController", ($scope, $http, StandingOrder, StandingOrderForm, customers, schedules, paymentMethods, shippingMethods) -> +angular.module("admin.standingOrders").controller "StandingOrderController", ($scope, StandingOrder, StandingOrderForm, customers, schedules, paymentMethods, shippingMethods) -> $scope.standingOrder = new StandingOrder() $scope.errors = null $scope.save = null @@ -18,18 +18,6 @@ angular.module("admin.standingOrders").controller "StandingOrderController", ($s $scope.next = -> $scope.nextCallbacks[$scope.view]() $scope.back = -> $scope.backCallbacks[$scope.view]() - $scope.$watch "standingOrder.customer_id", (newValue, oldValue) -> - return if !newValue? || newValue == oldValue - $http.get("/admin/customers/#{newValue}/addresses") - .success (response) => - delete response.bill_address.id - delete response.ship_address.id - angular.extend($scope.standingOrder.bill_address, response.bill_address) - angular.extend($scope.standingOrder.ship_address, response.ship_address) - $scope.shipAddressFromBilling() unless response.ship_address.address1? - $http.get("/admin/customers/#{newValue}/cards") - .success (response) => $scope.creditCards = response.cards - $scope.shipAddressFromBilling = => angular.extend($scope.standingOrder.ship_address, $scope.standingOrder.bill_address) diff --git a/app/assets/javascripts/admin/standing_orders/services/credit_card_resource.js.coffee b/app/assets/javascripts/admin/standing_orders/services/credit_card_resource.js.coffee new file mode 100644 index 0000000000..6d4336730a --- /dev/null +++ b/app/assets/javascripts/admin/standing_orders/services/credit_card_resource.js.coffee @@ -0,0 +1,5 @@ +angular.module("admin.standingOrders").factory 'CreditCardResource', ($resource) -> + resource = $resource '/admin/customers/:customer_id/cards.json', {}, + 'index': + method: 'GET' + isArray: true diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index 355d71cfa2..c1da99ceab 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -68,7 +68,7 @@ module Admin # Used by standing orders form to load details for selected customer def cards cards = Spree::CreditCard.where(user_id: @customer.user_id) - render json: { cards: ActiveModel::ArraySerializer.new(cards, each_serializer: Api::CreditCardSerializer) } + render json: ActiveModel::ArraySerializer.new(cards, each_serializer: Api::CreditCardSerializer) end private diff --git a/spec/controllers/admin/customers_controller_spec.rb b/spec/controllers/admin/customers_controller_spec.rb index b21e24c9d0..189d862d31 100644 --- a/spec/controllers/admin/customers_controller_spec.rb +++ b/spec/controllers/admin/customers_controller_spec.rb @@ -200,8 +200,7 @@ describe Admin::CustomersController, type: :controller do it "returns with an empty array" do spree_get :cards, params json_response = JSON.parse(response.body) - expect(json_response.keys).to include "cards" - expect(json_response["cards"]).to eq [] + expect(json_response).to eq [] end end @@ -211,9 +210,9 @@ describe Admin::CustomersController, type: :controller do it "returns with serialized cards for the customer" do spree_get :cards, params json_response = JSON.parse(response.body) - expect(json_response.keys).to include "cards" - expect(json_response["cards"].length).to be 1 - expect(json_response["cards"].first["id"]).to eq credit_card1.id + expect(json_response).to be_an Array + expect(json_response.length).to be 1 + expect(json_response.first["id"]).to eq credit_card1.id end end end