diff --git a/app/models/types_de_champ/prefill_annuaire_education_type_de_champ.rb b/app/models/types_de_champ/prefill_annuaire_education_type_de_champ.rb new file mode 100644 index 000000000..afd256e05 --- /dev/null +++ b/app/models/types_de_champ/prefill_annuaire_education_type_de_champ.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class TypesDeChamp::PrefillAnnuaireEducationTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp + def to_assignable_attributes(champ, value) + return nil if value.blank? + return nil unless (data = APIEducation::AnnuaireEducationAdapter.new(value.presence).to_params) + + { + id: champ.id, + external_id: data['identifiant_de_l_etablissement'], + value: "#{data['nom_etablissement']}, #{data['nom_commune']} (#{data['identifiant_de_l_etablissement']})" + } + rescue APIEducation::AnnuaireEducationAdapter::InvalidSchemaError + nil + end +end diff --git a/app/models/types_de_champ/prefill_type_de_champ.rb b/app/models/types_de_champ/prefill_type_de_champ.rb index 34dee1206..0dbadbb3c 100644 --- a/app/models/types_de_champ/prefill_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_type_de_champ.rb @@ -27,6 +27,8 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator TypesDeChamp::PrefillCommuneTypeDeChamp.new(type_de_champ, revision) when TypeDeChamp.type_champs.fetch(:epci) TypesDeChamp::PrefillEpciTypeDeChamp.new(type_de_champ, revision) + when TypeDeChamp.type_champs.fetch(:annuaire_education) + TypesDeChamp::PrefillAnnuaireEducationTypeDeChamp.new(type_de_champ, revision) else new(type_de_champ, revision) end diff --git a/spec/fixtures/files/api_education/annuaire_education_empty.json b/spec/fixtures/files/api_education/annuaire_education_empty.json new file mode 100644 index 000000000..34e44e091 --- /dev/null +++ b/spec/fixtures/files/api_education/annuaire_education_empty.json @@ -0,0 +1,14 @@ +{ + "nhits": 0, + "parameters": { + "dataset": "fr-en-annuaire-education", + "rows": 1, + "start": 0, + "refine": { + "identifiant_de_l_etablissement": "0341247" + }, + "format": "json", + "timezone": "UTC" + }, + "records": [] +} diff --git a/spec/lib/api_education/annuaire_education_adapter_spec.rb b/spec/lib/api_education/annuaire_education_adapter_spec.rb index 2b29c9b7e..eff7bd789 100644 --- a/spec/lib/api_education/annuaire_education_adapter_spec.rb +++ b/spec/lib/api_education/annuaire_education_adapter_spec.rb @@ -38,4 +38,13 @@ describe APIEducation::AnnuaireEducationAdapter do expect { subject }.to raise_exception(APIEducation::AnnuaireEducationAdapter::InvalidSchemaError) end end + + context "when responds with empty schema" do + let(:body) { File.read('spec/fixtures/files/api_education/annuaire_education_empty.json') } + let(:status) { 200 } + + it '#to_params returns nil' do + expect(subject).to eq(nil) + end + end end diff --git a/spec/models/types_de_champ/prefill_annuaire_education_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_annuaire_education_type_de_champ_spec.rb new file mode 100644 index 000000000..e7bba2828 --- /dev/null +++ b/spec/models/types_de_champ/prefill_annuaire_education_type_de_champ_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +RSpec.describe TypesDeChamp::PrefillAnnuaireEducationTypeDeChamp do + let(:procedure) { create(:procedure) } + let(:type_de_champ) { build(:type_de_champ_annuaire_education, procedure: procedure) } + + describe 'ancestors' do + subject { described_class.new(type_de_champ, procedure.active_revision) } + + it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) } + end + + describe '#to_assignable_attributes' do + let(:champ) { create(:champ_annuaire_education, type_de_champ: type_de_champ) } + subject { described_class.build(type_de_champ, procedure.active_revision).to_assignable_attributes(champ, value) } + + context 'when the value is nil' do + let(:value) { nil } + + it { is_expected.to eq(nil) } + end + + context 'when the value is empty' do + let(:value) { '' } + + it { is_expected.to eq(nil) } + end + + context 'when the value is present' do + let(:value) { '0050009H' } + + before do + stub_request(:get, /https:\/\/data.education.gouv.fr\/api\/records\/1.0/) + .to_return(body: body, status: 200) + end + + context 'when the annuaire education api responds with a valid schema' do + let(:body) { File.read('spec/fixtures/files/api_education/annuaire_education.json') } + + it { is_expected.to match({ id: champ.id, external_id: '0050009H', value: 'Lycée professionnel Sévigné, Gap (0050009H)' }) } + end + + context "when the annuaire education api responds with invalid schema" do + let(:body) { File.read('spec/fixtures/files/api_education/annuaire_education_invalid.json') } + + it { is_expected.to eq(nil) } + end + + context 'when the annuaire education api responds with empty schema' do + let(:body) { File.read('spec/fixtures/files/api_education/annuaire_education_empty.json') } + + it { is_expected.to eq(nil) } + end + end + end +end diff --git a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb index 844115889..1d8e135e5 100644 --- a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb @@ -57,6 +57,12 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do it { expect(built).to be_kind_of(TypesDeChamp::PrefillEpciTypeDeChamp) } end + context 'when the type de champ is an annuaire_education' do + let(:type_de_champ) { build(:type_de_champ_annuaire_education) } + + it { expect(built).to be_kind_of(TypesDeChamp::PrefillAnnuaireEducationTypeDeChamp) } + end + context 'when any other type de champ' do let(:type_de_champ) { build(:type_de_champ_date, procedure: procedure) }