Allow customer's address update with country code

The customer endpoint now serves 2-letter ISO codes for countries and
accepts these for updates. It also serves and accepts region codes
(abbreviations) like VIC for Victoria.

Updates treat these fields as case-insensitive and either code or name
have to be present.

This commit also updates the Swagger documentation.
This commit is contained in:
Maikel Linke
2022-07-18 16:01:53 +10:00
parent cc135c8190
commit 6c0d4cffe5
5 changed files with 91 additions and 14 deletions

View File

@@ -107,17 +107,25 @@ module Api
street_address_1: :address1, street_address_2: :address2,
postal_code: :zipcode,
locality: :city,
region: :state_name,
region: :state,
country: :country,
}.with_indifferent_access[key]
end
if address[:state_name].present?
address[:state] = Spree::State.find_by(name: address[:state_name][:name])
if address[:state].present?
address[:state] = Spree::State.find_by(
"LOWER(abbr) = ? OR LOWER(name) = ?",
address.dig(:state, :code)&.downcase,
address.dig(:state, :name)&.downcase,
)
end
if address[:country].present?
address[:country] = Spree::Country.find_by(name: address[:country][:name])
address[:country] = Spree::Country.find_by(
"LOWER(iso) = ? OR LOWER(name) = ?",
address.dig(:country, :code)&.downcase,
address.dig(:country, :name)&.downcase,
)
end
attributes["#{to}_attributes"] = address

View File

@@ -42,7 +42,7 @@ class CustomerSchema < JsonApiSchema
postal_code: "1234",
locality: "Melbourne",
region: { code: "Vic", name: "Victoria" },
country: { code: "AUS", name: "Australia" },
country: { code: "AU", name: "Australia" },
}
end

View File

@@ -20,7 +20,7 @@ module Api
attribute :country do |object|
{
name: object.country.name,
code: object.country.iso3,
code: object.country.iso,
}
end
end

View File

@@ -302,6 +302,59 @@ describe "Customers", type: :request do
end
end
describe "address" do
it "matches by country and state code" do
put "/api/v1/customers/#{customer1.id}", params: {
customer: {
shipping_address: CustomerSchema.address_example.merge(
region: { code: "Vic" },
country: { code: "AU" },
),
}
}
expect(response).to have_http_status :ok
expect(json_response.dig(:data, :attributes, :shipping_address)).to include(
region: { code: "Vic", name: "Victoria" },
country: { code: "AU", name: "Australia" },
)
end
it "matches by country and state name" do
put "/api/v1/customers/#{customer1.id}", params: {
customer: {
shipping_address: CustomerSchema.address_example.merge(
region: { name: "Victoria" },
country: { name: "Australia" },
),
}
}
expect(response).to have_http_status :ok
expect(json_response.dig(:data, :attributes, :shipping_address)).to include(
region: { code: "Vic", name: "Victoria" },
country: { code: "AU", name: "Australia" },
)
end
it "matches country and state case-insensitive" do
put "/api/v1/customers/#{customer1.id}", params: {
customer: {
shipping_address: CustomerSchema.address_example.merge(
region: { code: "VIC" },
country: { name: "AUSTRALIA" },
),
}
}
expect(response).to have_http_status :ok
expect(json_response.dig(:data, :attributes, :shipping_address)).to include(
region: { code: "Vic", name: "Victoria" },
country: { code: "AU", name: "Australia" },
)
end
end
response "422", "Unprocessable entity" do
param(:id) { customer1.id }
param(:customer) { {} }

View File

@@ -91,8 +91,12 @@ components:
street_address_2: ''
postal_code: '1234'
locality: Melbourne
region: Victoria
country: Australia
region:
code: Vic
name: Victoria
country:
code: AU
name: Australia
required:
- id
- enterprise_id
@@ -200,8 +204,12 @@ components:
street_address_2: ''
postal_code: '1234'
locality: Melbourne
region: Victoria
country: Australia
region:
code: Vic
name: Victoria
country:
code: AU
name: Australia
required:
- id
- enterprise_id
@@ -372,8 +380,12 @@ paths:
street_address_2: ''
postal_code: '1234'
locality: Melbourne
region: Victoria
country: Australia
region:
code: Vic
name: Victoria
country:
code: AU
name: Australia
required:
- enterprise_id
- email
@@ -478,8 +490,12 @@ paths:
street_address_2: ''
postal_code: '1234'
locality: Melbourne
region: Victoria
country: Australia
region:
code: Vic
name: Victoria
country:
code: AU
name: Australia
required:
- enterprise_id
- email