diff --git a/app/graphql/api/v2/stored_query.rb b/app/graphql/api/v2/stored_query.rb index ae1fe7c92..4ade362d6 100644 --- a/app/graphql/api/v2/stored_query.rb +++ b/app/graphql/api/v2/stored_query.rb @@ -573,6 +573,12 @@ class API::V2::StoredQuery rnf { ...RNFFragment } + commune { + ...CommuneFragment + } + departement { + ...DepartementFragment + } } } diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index b85bcd664..33acfa3b1 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -3626,6 +3626,8 @@ type RNF { } type RNFChamp implements Champ { + commune: Commune + departement: Departement id: ID! """ diff --git a/app/graphql/types/champs/rnf_champ_type.rb b/app/graphql/types/champs/rnf_champ_type.rb index 13f39fed9..32e638e5e 100644 --- a/app/graphql/types/champs/rnf_champ_type.rb +++ b/app/graphql/types/champs/rnf_champ_type.rb @@ -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? diff --git a/app/models/champs/address_champ.rb b/app/models/champs/address_champ.rb index a9177b0c3..4031cf117 100644 --- a/app/models/champs/address_champ.rb +++ b/app/models/champs/address_champ.rb @@ -48,7 +48,9 @@ class Champs::AddressChamp < Champs::TextChamp end def departement_code_and_name - "#{address.fetch('department_code')} – #{departement_name}" + if full_address? + "#{address.fetch('department_code')} – #{departement_name}" + end end def departement @@ -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') diff --git a/app/models/champs/commune_champ.rb b/app/models/champs/commune_champ.rb index 290ff64d3..f542fc49f 100644 --- a/app/models/champs/commune_champ.rb +++ b/app/models/champs/commune_champ.rb @@ -11,7 +11,9 @@ class Champs::CommuneChamp < Champs::TextChamp end def departement_code_and_name - "#{code_departement} – #{departement_name}" + if departement? + "#{code_departement} – #{departement_name}" + end end def departement diff --git a/app/models/champs/rnf_champ.rb b/app/models/champs/rnf_champ.rb index 13779a225..b487e78f6 100644 --- a/app/models/champs/rnf_champ.rb +++ b/app/models/champs/rnf_champ.rb @@ -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 diff --git a/app/models/types_de_champ/address_type_de_champ.rb b/app/models/types_de_champ/address_type_de_champ.rb index d1d82c0d9..d5b2e614e 100644 --- a/app/models/types_de_champ/address_type_de_champ.rb +++ b/app/models/types_de_champ/address_type_de_champ.rb @@ -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 diff --git a/app/models/types_de_champ/commune_type_de_champ.rb b/app/models/types_de_champ/commune_type_de_champ.rb index 6ead9e2b7..f6983ee85 100644 --- a/app/models/types_de_champ/commune_type_de_champ.rb +++ b/app/models/types_de_champ/commune_type_de_champ.rb @@ -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 diff --git a/app/models/types_de_champ/rnf_type_de_champ.rb b/app/models/types_de_champ/rnf_type_de_champ.rb index a5a796342..05a466f08 100644 --- a/app/models/types_de_champ/rnf_type_de_champ.rb +++ b/app/models/types_de_champ/rnf_type_de_champ.rb @@ -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 diff --git a/spec/models/champs/rnf_champ_spec.rb b/spec/models/champs/rnf_champ_spec.rb index 865fec322..0a1d1537f 100644 --- a/spec/models/champs/rnf_champ_spec.rb +++ b/spec/models/champs/rnf_champ_spec.rb @@ -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