Move address and card loading logic down into DetailsController

Also switch to using CreditCardResource to load the card
This commit is contained in:
Rob Harrington
2017-11-10 18:09:24 +11:00
parent a5ae4c801c
commit bdac68900f
5 changed files with 30 additions and 21 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -0,0 +1,5 @@
angular.module("admin.standingOrders").factory 'CreditCardResource', ($resource) ->
resource = $resource '/admin/customers/:customer_id/cards.json', {},
'index':
method: 'GET'
isArray: true

View File

@@ -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

View File

@@ -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