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 class AnnuaireEducationUpdateJob < ApplicationJob
def perform(champ) def perform(champ)
search_term = champ.value external_id = champ.external_id
if search_term.present? if external_id.present?
data = ApiEducation::AnnuaireEducationAdapter.new(search_term).to_params data = ApiEducation::AnnuaireEducationAdapter.new(external_id).to_params
if data.present? if data.present?
champ.data = data champ.data = data
else else
champ.value = nil champ.external_id = nil
end end
champ.save! champ.save!
end end

View file

@ -7,8 +7,8 @@ class ApiEducation::AnnuaireEducationAdapter
end end
end end
def initialize(search_term) def initialize(id)
@search_term = search_term @id = id
end end
def to_params def to_params
@ -27,7 +27,7 @@ class ApiEducation::AnnuaireEducationAdapter
private private
def data_source 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 end
def schemer def schemer

View file

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

View file

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