refactor(etablissement): update champ value json address from model

This commit is contained in:
Colin Darie 2024-11-22 11:37:13 +01:00
parent 2e873254fe
commit a400045964
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
5 changed files with 43 additions and 9 deletions

View file

@ -5,5 +5,6 @@ class APIEntreprise::EntrepriseJob < APIEntreprise::Job
find_etablissement(etablissement_id)
etablissement_params = APIEntreprise::EntrepriseAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
etablissement.update_champ_value_json!
end
end

View file

@ -204,6 +204,16 @@ class Etablissement < ApplicationRecord
adresse.nil? # TOOD: maybe dedicated column or more robust way
end
def update_champ_value_json!
return if champ.nil?
champ.update!(value_json: champ_value_json)
end
def champ_value_json
APIGeoService.parse_etablissement_address(self)
end
private
def bilans_new_keys

View file

@ -22,10 +22,7 @@ class APIEntrepriseService
etablissement = dossier_or_champ.build_etablissement(etablissement_params)
etablissement.save!
if dossier_or_champ.is_a?(Champ)
dossier_or_champ.update!(value_json: APIGeoService.parse_etablissement_address(etablissement))
end
etablissement.update_champ_value_json!
perform_later_fetch_jobs(etablissement, procedure_id, user_id)
@ -49,10 +46,7 @@ class APIEntrepriseService
return nil if etablissement_params.empty?
etablissement.update!(etablissement_params)
if etablissement.champ.present?
etablissement.champ.update!(value_json: APIGeoService.parse_etablissement_address(etablissement))
end
etablissement.update_champ_value_json!
etablissement
end

View file

@ -12,7 +12,7 @@ module Maintenance
def process(champ)
return if champ.etablissement.blank?
champ.update!(value_json: APIGeoService.parse_etablissement_address(champ.etablissement))
champ.update!(value_json: champ.etablissement.champ_value_json)
rescue ActiveRecord::RecordInvalid
# noop, just a champ without dossier
end

View file

@ -130,6 +130,35 @@ describe Etablissement do
end
end
describe '#update_champ_value_json!' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :siret }]) }
let(:dossier) { create(:dossier, procedure:) }
let(:etablissement) { create(:etablissement) }
let(:champ) { dossier.champs[0] }
let(:address_data) do
{
"street_number" => "6",
"street_name" => "RAOUL NORDLING",
"postal_code" => "92270",
"city_name" => "BOIS COLOMBES"
}
end
before do
allow(APIGeoService).to receive(:parse_etablissement_address)
.with(etablissement)
.and_return(address_data.dup)
etablissement.champ = champ
end
it 'updates the associated champ value_json with geocoded address' do
etablissement.update_champ_value_json!
expect(champ.reload.value_json).to eq(address_data)
end
end
private
def csv_to_array_of_hash(lines)