Use external_id to fetch annuaire_education data

This commit is contained in:
Paul Chavard 2021-01-14 23:55:34 +01:00
parent 9f691dce4c
commit 97ce8f312b
4 changed files with 11 additions and 11 deletions

View file

@ -1,14 +1,14 @@
class AnnuaireEducationUpdateJob < ApplicationJob
def perform(champ)
search_term = champ.value
external_id = champ.external_id
if search_term.present?
data = ApiEducation::AnnuaireEducationAdapter.new(search_term).to_params
if external_id.present?
data = ApiEducation::AnnuaireEducationAdapter.new(external_id).to_params
if data.present?
champ.data = data
else
champ.value = nil
champ.external_id = nil
end
champ.save!
end

View file

@ -7,8 +7,8 @@ class ApiEducation::AnnuaireEducationAdapter
end
end
def initialize(search_term)
@search_term = search_term
def initialize(id)
@id = id
end
def to_params
@ -27,7 +27,7 @@ class ApiEducation::AnnuaireEducationAdapter
private
def data_source
@data_source ||= JSON.parse(ApiEducation::API.search_annuaire_education(@search_term), symbolize_names: true)
@data_source ||= JSON.parse(ApiEducation::API.get_annuaire_education(@id), symbolize_names: true)
end
def schemer

View file

@ -2,8 +2,8 @@ class ApiEducation::API
class ResourceNotFound < StandardError
end
def self.search_annuaire_education(search_term)
call([API_EDUCATION_URL, 'search'].join('/'), 'fr-en-annuaire-education', { q: search_term })
def self.get_annuaire_education(id)
call([API_EDUCATION_URL, 'search'].join('/'), 'fr-en-annuaire-education', { 'refine.identifiant_de_l_etablissement': id })
end
private

View file

@ -23,13 +23,13 @@ class Champs::AnnuaireEducationChamp < Champs::TextChamp
private
def cleanup_if_empty
if value_changed?
if external_id_changed?
self.data = nil
end
end
def fetch_data
if value.present? && data.nil?
if external_id.present? && data.nil?
AnnuaireEducationUpdateJob.perform_later(self)
end
end