Add new serializer to allow search for customer addresses

This commit is contained in:
Rob Harrington
2018-05-10 13:08:03 +10:00
committed by Maikel Linke
parent c71a5ec0df
commit e0d46aa105
4 changed files with 44 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ angular.module("admin.subscriptions").controller "DetailsController", ($scope, $
$scope.loadCustomer = ->
params = { id: $scope.subscription.customer_id }
params.ams_prefix = 'subscription' unless $scope.subscription.id
$scope.customer = CustomerResource.get params, (response) ->
for address in ['bill_address','ship_address']
return unless response[address]

View File

@@ -24,7 +24,7 @@ module Admin
end
def show
render_as_json @customer
render_as_json @customer, ams_prefix: params[:ams_prefix]
end
def create
@@ -91,5 +91,9 @@ module Admin
spree_current_user.admin? ||
spree_current_user.enterprises.include?(@customer.enterprise)
end
def ams_prefix_whitelist
[:subscription]
end
end
end

View File

@@ -0,0 +1,20 @@
module Api
module Admin
# Used by admin subscription form
# Searches for a ship and bill addresses for the customer
# where they are not already explicitly set
class SubscriptionCustomerSerializer < CustomerSerializer
def bill_address
finder.bill_address
end
def ship_address
finder.ship_address
end
def finder
@finder ||= OpenFoodNetwork::AddressFinder.new(object, object.email)
end
end
end
end

View File

@@ -0,0 +1,18 @@
describe Api::Admin::SubscriptionCustomerSerializer do
let(:address) { build(:address) }
let(:customer) { build(:customer) }
let(:serializer) { Api::Admin::SubscriptionCustomerSerializer.new(customer) }
let(:finder_mock) { instance_double(OpenFoodNetwork::AddressFinder, bill_address: address, ship_address: address) }
before do
allow(serializer).to receive(:finder) { finder_mock }
end
it "serializes a customer " do
result = JSON.parse(serializer.to_json)
expect(result['email']).to eq customer.email
expect(result['ship_address']['id']).to be nil
expect(result['ship_address']['address1']).to eq address.address1
expect(result['ship_address']['firstname']).to eq address.firstname
end
end