From a4000459640c5f330a69bad532aef1cf02370016 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Fri, 22 Nov 2024 11:37:13 +0100 Subject: [PATCH] refactor(etablissement): update champ value json address from model --- app/jobs/api_entreprise/entreprise_job.rb | 1 + app/models/etablissement.rb | 10 +++++++ app/services/api_entreprise_service.rb | 10 ++----- .../populate_siret_value_json_task.rb | 2 +- spec/models/etablissement_spec.rb | 29 +++++++++++++++++++ 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/app/jobs/api_entreprise/entreprise_job.rb b/app/jobs/api_entreprise/entreprise_job.rb index 1fe7c37fa..403e9aaa7 100644 --- a/app/jobs/api_entreprise/entreprise_job.rb +++ b/app/jobs/api_entreprise/entreprise_job.rb @@ -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 diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 54e9c7cb7..d348ec530 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -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 diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index f73ccbcb0..276efc3a7 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -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 diff --git a/app/tasks/maintenance/populate_siret_value_json_task.rb b/app/tasks/maintenance/populate_siret_value_json_task.rb index 3b43ebb73..78a5d8757 100644 --- a/app/tasks/maintenance/populate_siret_value_json_task.rb +++ b/app/tasks/maintenance/populate_siret_value_json_task.rb @@ -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 diff --git a/spec/models/etablissement_spec.rb b/spec/models/etablissement_spec.rb index 58e122167..5cf5cb15a 100644 --- a/spec/models/etablissement_spec.rb +++ b/spec/models/etablissement_spec.rb @@ -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)