mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Add new serializer to allow search for customer addresses
This commit is contained in:
committed by
Maikel Linke
parent
c71a5ec0df
commit
e0d46aa105
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user