Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(anrok-integration): add support for customer shipping address on the API side #2217

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/api/v1/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ def create_params
:value,
:display_in_invoice
],
shipping_address: [
:address_line1,
:address_line2,
:city,
:zipcode,
:state,
:country
],
tax_codes: []
)
end
Expand Down
16 changes: 16 additions & 0 deletions app/graphql/types/customers/address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Types
module Customers
class Address < Types::BaseObject
graphql_name 'CustomerAddress'

field :address_line1, String, null: true
field :address_line2, String, null: true
field :city, String, null: true
field :country, Types::CountryCodeEnum, null: true
field :state, String, null: true
field :zipcode, String, null: true
end
end
end
16 changes: 16 additions & 0 deletions app/graphql/types/customers/address_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Types
module Customers
class AddressInput < BaseInputObject
graphql_name 'CustomerAddressInput'

argument :address_line1, String, required: false
argument :address_line2, String, required: false
argument :city, String, required: false
argument :country, Types::CountryCodeEnum, required: false
argument :state, String, required: false
argument :zipcode, String, required: false
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/customers/create_customer_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class CreateCustomerInput < BaseInputObject
argument :url, String, required: false
argument :zipcode, String, required: false

argument :shipping_address, Types::Customers::AddressInput, required: false

argument :metadata, [Types::Customers::Metadata::Input], required: false

argument :payment_provider, Types::PaymentProviders::ProviderTypeEnum, required: false
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/customers/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Object < Types::BaseObject
field :url, String, null: true
field :zipcode, String, null: true

field :shipping_address, Types::Customers::Address, null: true

field :metadata, [Types::Customers::Metadata::Object], null: true

field :billing_configuration, Types::Customers::BillingConfiguration, null: true
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/customers/update_customer_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class UpdateCustomerInput < BaseInputObject
argument :url, String, required: false, permission: 'customers:update'
argument :zipcode, String, required: false, permission: 'customers:update'

argument :shipping_address, Types::Customers::AddressInput, required: false, permission: 'customers:update'

argument :metadata, [Types::Customers::Metadata::Input], required: false, permission: 'customers:update'

argument :payment_provider, Types::PaymentProviders::ProviderTypeEnum, required: false, permission: 'customers:update'
Expand Down
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
3 changes: 2 additions & 1 deletion app/serializers/v1/customer_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def serialize
applicable_timezone: model.applicable_timezone,
net_payment_term: model.net_payment_term,
external_salesforce_id: model.external_salesforce_id,
billing_configuration:
billing_configuration:,
shipping_address: model.shipping_address
}.merge(legacy_values.except(:billing_configuration))

payload = payload.merge(metadata)
Expand Down
14 changes: 14 additions & 0 deletions app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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[:shipping_address] ||= {}

unless valid_metadata_count?(metadata: params[:metadata])
return result.single_validation_failure!(
Expand All @@ -31,6 +32,12 @@ def create_from_api(organization:, params:)
customer.zipcode = params[:zipcode] if params.key?(:zipcode)
customer.email = params[:email] if params.key?(:email)
customer.city = params[:city] if params.key?(:city)
customer.shipping_address_line1 = shipping_address[:address_line1] if shipping_address.key?(:address_line1)
customer.shipping_address_line2 = shipping_address[:address_line2] if shipping_address.key?(:address_line2)
customer.shipping_city = shipping_address[:city] if shipping_address.key?(:city)
customer.shipping_zipcode = shipping_address[:zipcode] if shipping_address.key?(:zipcode)
customer.shipping_state = shipping_address[:state] if shipping_address.key?(:state)
customer.shipping_country = shipping_address[:country]&.upcase if shipping_address.key?(:country)
customer.url = params[:url] if params.key?(:url)
customer.phone = params[:phone] if params.key?(:phone)
customer.logo_url = params[:logo_url] if params.key?(:logo_url)
Expand Down Expand Up @@ -97,6 +104,7 @@ def create_from_api(organization:, params:)

def create(**args)
billing_configuration = args[:billing_configuration]&.to_h || {}
shipping_address = args[:shipping_address]&.to_h || {}

unless valid_metadata_count?(metadata: args[:metadata])
return result.single_validation_failure!(
Expand All @@ -114,6 +122,12 @@ def create(**args)
address_line2: args[:address_line2],
state: args[:state],
zipcode: args[:zipcode],
shipping_address_line1: shipping_address[:address_line1],
shipping_address_line2: shipping_address[:address_line2],
shipping_country: shipping_address[:country]&.upcase,
shipping_state: shipping_address[:state],
shipping_zipcode: shipping_address[:zipcode],
shipping_city: shipping_address[:city],
email: args[:email],
city: args[:city],
url: args[:url],
Expand Down
8 changes: 8 additions & 0 deletions app/services/customers/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def update(**args)
old_provider_customer = customer.provider_customer
ActiveRecord::Base.transaction do
billing_configuration = args[:billing_configuration]&.to_h || {}
shipping_address = args[:shipping_address]&.to_h || {}

if args.key?(:currency)
update_currency(customer:, currency: args[:currency], customer_update: true)
result.raise_if_error!
Expand All @@ -40,6 +42,12 @@ def update(**args)
customer.legal_number = args[:legal_number] if args.key?(:legal_number)
customer.net_payment_term = args[:net_payment_term] if args.key?(:net_payment_term)
customer.external_salesforce_id = args[:external_salesforce_id] if args.key?(:external_salesforce_id)
customer.shipping_address_line1 = shipping_address[:address_line1] if shipping_address.key?(:address_line1)
customer.shipping_address_line2 = shipping_address[:address_line2] if shipping_address.key?(:address_line2)
customer.shipping_city = shipping_address[:city] if shipping_address.key?(:city)
customer.shipping_zipcode = shipping_address[:zipcode] if shipping_address.key?(:zipcode)
customer.shipping_state = shipping_address[:state] if shipping_address.key?(:state)
customer.shipping_country = shipping_address[:country]&.upcase if shipping_address.key?(:country)

assign_premium_attributes(customer, args)

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20240626094521_add_shipping_address_to_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AddShippingAddressToCustomers < ActiveRecord::Migration[7.1]
change_table :customers, bulk: true do |t|
lovrocolic marked this conversation as resolved.
Show resolved Hide resolved
t.string :shipping_address_line1
t.string :shipping_address_line2
t.string :shipping_city
t.string :shipping_zipcode
t.string :shipping_state
t.string :shipping_country
end
end
8 changes: 7 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading