diff --git a/app/graphql/types/customers/object.rb b/app/graphql/types/customers/object.rb index c680a026c6b4..8e170a7e9567 100644 --- a/app/graphql/types/customers/object.rb +++ b/app/graphql/types/customers/object.rb @@ -126,17 +126,6 @@ def billing_configuration document_locale: object&.document_locale } end - - def shipping_address - { - address_line1: object.shipping_address_line1, - address_line2: object.shipping_address_line2, - city: object.shipping_city, - zipcode: object.shipping_zipcode, - state: object.shipping_state, - country: object.shipping_country - } - end end end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 63cbd1ea380f..b2f055e17914 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -116,6 +116,17 @@ def provider_customer end end + def shipping_address + { + address_line1: shipping_address_line1, + address_line2: shipping_address_line2, + city: shipping_city, + zipcode: shipping_zipcode, + state: shipping_state, + country: shipping_country + } + end + private def ensure_slug diff --git a/app/serializers/v1/customer_serializer.rb b/app/serializers/v1/customer_serializer.rb index 18d267ef6505..9e34664fd55a 100644 --- a/app/serializers/v1/customer_serializer.rb +++ b/app/serializers/v1/customer_serializer.rb @@ -30,7 +30,7 @@ def serialize net_payment_term: model.net_payment_term, external_salesforce_id: model.external_salesforce_id, billing_configuration:, - shipping_address: + shipping_address: model.shipping_address }.merge(legacy_values.except(:billing_configuration)) payload = payload.merge(metadata) @@ -76,17 +76,6 @@ def billing_configuration configuration end - def shipping_address - { - address_line1: model.shipping_address_line1, - address_line2: model.shipping_address_line2, - city: model.shipping_city, - zipcode: model.shipping_zipcode, - state: model.shipping_state, - country: model.shipping_country - } - end - def legacy_values @legacy_values ||= ::V1::Legacy::CustomerSerializer.new(model).serialize end diff --git a/app/services/customers/create_service.rb b/app/services/customers/create_service.rb index 9bd08346fb53..3e119a1d3b09 100644 --- a/app/services/customers/create_service.rb +++ b/app/services/customers/create_service.rb @@ -7,7 +7,7 @@ class CreateService < BaseService def create_from_api(organization:, params:) customer = organization.customers.find_or_initialize_by(external_id: params[:external_id]) new_customer = customer.new_record? - shipping_address = params.key?(:shipping_address) ? params[:shipping_address] : {} + shipping_address = params[:shipping_address] ||= {} unless valid_metadata_count?(metadata: params[:metadata]) return result.single_validation_failure!( diff --git a/spec/serializers/v1/customer_serializer_spec.rb b/spec/serializers/v1/customer_serializer_spec.rb index 80d558be20f0..862cac8e7d05 100644 --- a/spec/serializers/v1/customer_serializer_spec.rb +++ b/spec/serializers/v1/customer_serializer_spec.rb @@ -15,6 +15,7 @@ before do metadata customer_applied_tax + customer.update!(shipping_city: 'Paris', shipping_address_line1: 'test1', shipping_zipcode: '002') end it 'serializes the object' do @@ -49,6 +50,9 @@ expect(result['customer']['billing_configuration']['invoice_grace_period']).to eq(customer.invoice_grace_period) expect(result['customer']['billing_configuration']['vat_rate']).to eq(customer.vat_rate) expect(result['customer']['billing_configuration']['document_locale']).to eq(customer.document_locale) + expect(result['customer']['shipping_address']['address_line1']).to eq('test1') + expect(result['customer']['shipping_address']['city']).to eq('Paris') + expect(result['customer']['shipping_address']['zipcode']).to eq('002') expect(result['customer']['metadata'].first['lago_id']).to eq(metadata.id) expect(result['customer']['metadata'].first['key']).to eq(metadata.key) expect(result['customer']['metadata'].first['value']).to eq(metadata.value)