API V1, Customer endpoint: region and country as object

This commit is contained in:
Jean-Baptiste Bellet
2022-07-07 10:58:45 +02:00
parent 3d6842db80
commit cc135c8190
3 changed files with 18 additions and 7 deletions

View File

@@ -77,7 +77,8 @@ module Api
:phone, :latitude, :longitude,
:first_name, :last_name,
:street_address_1, :street_address_2,
:postal_code, :locality, :region, :country,
:postal_code, :locality,
{ region: [:name, :code], country: [:name, :code] },
]
).to_h
@@ -112,11 +113,11 @@ module Api
end
if address[:state_name].present?
address[:state] = Spree::State.find_by(name: address[:state_name])
address[:state] = Spree::State.find_by(name: address[:state_name][:name])
end
if address[:country].present?
address[:country] = Spree::Country.find_by(name: address[:country])
address[:country] = Spree::Country.find_by(name: address[:country][:name])
end
attributes["#{to}_attributes"] = address

View File

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

View File

@@ -11,8 +11,18 @@ module Api
attribute :street_address_2, &:address2
attribute :postal_code, &:zipcode
attribute :locality, &:city
attribute :region, &:state_name
attribute :country, ->(object, _) { object.country.name }
attribute :region do |object|
{
name: object.state.name,
code: object.state.abbr,
}
end
attribute :country do |object|
{
name: object.country.name,
code: object.country.iso3,
}
end
end
end
end