Skip to content

Commit

Permalink
add small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lovrocolic committed Jun 28, 2024
1 parent b7f0e03 commit 8088fa6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
11 changes: 0 additions & 11 deletions app/graphql/types/customers/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions app/serializers/v1/customer_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
4 changes: 4 additions & 0 deletions spec/serializers/v1/customer_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8088fa6

Please sign in to comment.