Import DFC country by name or ISO code

This commit is contained in:
Maikel Linke
2025-12-10 16:52:16 +11:00
parent bf6176c883
commit fe8a0a908e
2 changed files with 38 additions and 4 deletions

View File

@@ -34,15 +34,15 @@ class EnterpriseImporter
def apply(enterprise)
address = @dfc_enterprise.localizations.first
state = Spree::State.find_by(name: address.region) || Spree::State.first
country = find_country(address)
enterprise.name = @dfc_enterprise.name
enterprise.address.assign_attributes(
address1: address.street,
city: address.city,
zipcode: address.postalCode,
state: state,
country: state.country,
state: find_state(country, address),
country:,
)
enterprise.email_address = @dfc_enterprise.emails.first
enterprise.description = @dfc_enterprise.description
@@ -81,4 +81,17 @@ class EnterpriseImporter
# at all. Maybe we'll add UX for error handling later.
nil
end
def find_country(address)
country = address.country
country = country[:path] if country.is_a?(Hash)
Spree::Country.find_by(iso3: country.to_s[-3..]) ||
Spree::Country.find_by(name: country) ||
Spree::Country.first
end
def find_state(country, address)
country.states.find_by(name: address.region) || country.states.first
end
end

View File

@@ -10,7 +10,15 @@ RSpec.describe EnterpriseImporter do
"litefarm.org",
name: "Test Farm",
localizations: [
DataFoodConsortium::Connector::Address.new(nil)
DataFoodConsortium::Connector::Address.new(
nil,
region: "Victoria",
country: {
scheme: "http",
host: "publications.europa.eu",
path: "/resource/authority/country/AUS",
}
)
],
)
}
@@ -21,6 +29,19 @@ RSpec.describe EnterpriseImporter do
expect(enterprise.id).to eq nil
expect(enterprise.semantic_link.semantic_id).to eq "litefarm.org"
expect(enterprise.name).to eq "Test Farm"
expect(enterprise.address.state.name).to eq "Victoria"
expect(enterprise.address.country.name).to eq "Australia"
end
it "understands old country names" do
dfc_enterprise.localizations[0].country = "France"
dfc_enterprise.localizations[0].region = "Aquitaine"
enterprise = subject.import
expect(enterprise.id).to eq nil
expect(enterprise.address.country.name).to eq "France"
expect(enterprise.address.state.name).to eq "Aquitaine"
end
it "ignores errors during image import" do