enrich value from education api

This commit is contained in:
sebastiencarceles 2023-02-15 15:25:15 +01:00
parent 4f4f4baffd
commit d7bfb9dc18
6 changed files with 103 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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": []
}

View file

@ -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

View file

@ -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

View file

@ -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) }