Merge pull request #9751 from tchak/departement-tags

feat(champs): expose more tags and api fields on rnf, address and commune
This commit is contained in:
Colin Darie 2023-12-21 08:45:01 +00:00 committed by GitHub
commit 29df9ad09d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 158 additions and 36 deletions

View file

@ -573,6 +573,12 @@ class API::V2::StoredQuery
rnf {
...RNFFragment
}
commune {
...CommuneFragment
}
departement {
...DepartementFragment
}
}
}

View file

@ -3626,6 +3626,8 @@ type RNF {
}
type RNFChamp implements Champ {
commune: Commune
departement: Departement
id: ID!
"""

View file

@ -3,40 +3,14 @@ module Types::Champs
implements Types::ChampType
class RNFType < Types::BaseObject
field :id, String, null: false
field :title, String, null: true
field :address, Types::AddressType, null: true
def id
object.value
end
def title
object.data["title"]
end
def address
address = object.data["address"]
if address
{
label: address["label"],
type: address["type"],
street_address: address["streetAddress"],
street_number: address["streetNumber"],
street_name: address["streetName"],
postal_code: address["postalCode"],
city_name: address["cityName"],
city_code: address["cityCode"],
department_name: address["departmentName"],
department_code: address["departmentCode"],
region_name: address["regionName"],
region_code: address["regionCode"]
}
end
end
field :id, String, null: false, method: :external_id
field :title, String, null: true, method: :title
field :address, Types::AddressType, null: true, method: :rnf_address
end
field :rnf, RNFType, null: true
field :commune, Types::Champs::CommuneChampType::CommuneType, null: true
field :departement, Types::Champs::DepartementChampType::DepartementType, null: true
def rnf
object if object.external_id.present?

View file

@ -48,8 +48,10 @@ class Champs::AddressChamp < Champs::TextChamp
end
def departement_code_and_name
if full_address?
"#{address.fetch('department_code')} #{departement_name}"
end
end
def departement
if full_address?
@ -57,6 +59,12 @@ class Champs::AddressChamp < Champs::TextChamp
end
end
def commune_name
if full_address?
"#{APIGeoService.commune_name(department_code, address['city_code'])} (#{address['postal_code']})"
end
end
def commune
if full_address?
department_code = address.fetch('department_code')

View file

@ -11,8 +11,10 @@ class Champs::CommuneChamp < Champs::TextChamp
end
def departement_code_and_name
if departement?
"#{code_departement} #{departement_name}"
end
end
def departement
{ code: code_departement, name: departement_name }

View file

@ -26,10 +26,87 @@ class Champs::RNFChamp < Champ
end
def for_export
if data
[rnf_id, data['title'], data.dig('address', 'label'), data.dig('address', 'cityCode'), "#{data['department']} - #{APIGeoService.departement_name(data['department'])}"]
if address.present?
[rnf_id, title, address['label'], address['cityCode'], departement_code_and_name]
else
[rnf_id, nil, nil, nil, nil]
end
end
def code_departement
address.present? && address['departmentCode']
end
def departement?
code_departement.present?
end
def departement
if departement?
{ code: code_departement, name: departement_name }
end
end
def departement_name
APIGeoService.departement_name(code_departement)
end
def departement_code_and_name
if departement?
"#{code_departement} #{departement_name}"
end
end
def commune_name
if departement?
"#{APIGeoService.commune_name(department_code, address['cityCode'])} (#{address['postalCode']})"
end
end
def commune
if departement?
city_code = address['cityCode']
city_name = address['cityName']
postal_code = address['postalCode']
commune_name = APIGeoService.commune_name(department_code, city_code)
commune_code = APIGeoService.commune_code(department_code, city_name)
if commune_name.present?
{
code: city_code,
name: commune_name
}
elsif commune_code.present?
{
code: commune_code,
name: city_name
}
else
{
code: city_code,
name: city_name
}
end.merge(postal_code:)
end
end
def rnf_address
if departement?
{
label: address["label"],
type: address["type"],
street_address: address["streetAddress"],
street_number: address["streetNumber"],
street_name: address["streetName"],
postal_code: address["postalCode"],
city_name: address["cityName"],
city_code: address["cityCode"],
department_name: address["departmentName"],
department_code: address["departmentCode"],
region_name: address["regionName"],
region_code: address["regionCode"]
}
end
end
end

View file

@ -1,2 +1,21 @@
class TypesDeChamp::AddressTypeDeChamp < TypesDeChamp::TextTypeDeChamp
def tags_for_template
tags = super
stable_id = @type_de_champ.stable_id
tags.push(
{
libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Département)",
id: "tdc#{stable_id}/departement",
description: "#{description} (Département)",
lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.departement_code_and_name }
},
{
libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Commune)",
id: "tdc#{stable_id}/commune",
description: "#{description} (Commune)",
lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.commune_name }
}
)
tags
end
end

View file

@ -2,4 +2,18 @@ class TypesDeChamp::CommuneTypeDeChamp < TypesDeChamp::TypeDeChampBase
def libelle_for_export(index)
[libelle, "#{libelle} (Code insee)", "#{libelle} (Département)"][index]
end
def tags_for_template
tags = super
stable_id = @type_de_champ.stable_id
tags.push(
{
libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Département)",
id: "tdc#{stable_id}/departement",
description: "#{description} (Département)",
lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.departement_code_and_name }
}
)
tags
end
end

View file

@ -2,4 +2,24 @@ class TypesDeChamp::RNFTypeDeChamp < TypesDeChamp::TextTypeDeChamp
def libelle_for_export(index)
[libelle, "#{libelle} (Nom)", "#{libelle} (Adresse)", "#{libelle} (Code insee Ville)", "#{libelle} (Département)"][index]
end
def tags_for_template
tags = super
stable_id = @type_de_champ.stable_id
tags.push(
{
libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Département)",
id: "tdc#{stable_id}/departement",
description: "#{description} (Département)",
lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.departement_code_and_name }
},
{
libelle: "#{TagsSubstitutionConcern::TagsParser.normalize(libelle)} (Commune)",
id: "tdc#{stable_id}/commune",
description: "#{description} (Commune)",
lambda: -> (champs) { champs.find { _1.stable_id == stable_id }&.commune_name }
}
)
tags
end
end

View file

@ -77,7 +77,7 @@ describe Champs::RNFChamp, type: :model do
describe 'for_export' do
let(:champ) { build(:champ_rnf, external_id:, data: JSON.parse(body)) }
it do
expect(champ.for_export).to eq(['075-FDD-00003-01', 'Fondation SFR', '16 Rue du Général de Boissieu 75015 Paris', '75115', '75 - Paris'])
expect(champ.for_export).to eq(['075-FDD-00003-01', 'Fondation SFR', '16 Rue du Général de Boissieu 75015 Paris', '75115', '75 Paris'])
end
end
end