diff --git a/app/jobs/cron/backfill_siret_degraded_mode_job.rb b/app/jobs/cron/backfill_siret_degraded_mode_job.rb new file mode 100644 index 000000000..11ac5d0fb --- /dev/null +++ b/app/jobs/cron/backfill_siret_degraded_mode_job.rb @@ -0,0 +1,32 @@ +class Cron::BackfillSiretDegradedModeJob < Cron::CronJob + self.schedule_expression = "every 2 hour" + + def perform(*args) + fix_etablissement_with_dossier + fix_etablissement_with_champs + end + + def fix_etablissement_with_dossier + Etablissement.joins(:dossier).where(adresse: nil).find_each do |etablissement| + begin + procedure_id = etablissement.dossier.procedure.id + + APIEntrepriseService.update_etablissement_from_degraded_mode(etablissement, procedure_id) + rescue => e + Sentry.capture_exception(e) + end + end + end + + def fix_etablissement_with_champs + Etablissement.joins(:champ).where(adresse: nil).find_each do |etablissement| + begin + procedure_id = etablissement.champ.procedure.id + + APIEntrepriseService.update_etablissement_from_degraded_mode(etablissement, procedure_id) + rescue => e + Sentry.capture_exception(e) + end + end + end +end diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index c83bcbde1..8eb5f4ed8 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -34,8 +34,7 @@ class APIEntrepriseService etablissement end - def update_etablissement_from_degraded_mode(etablissement) - procedure_id = etablissement.dossier.procedure.id + def update_etablissement_from_degraded_mode(etablissement, procedure_id) siret = etablissement.siret etablissement_params = APIEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params return nil if etablissement_params.empty? diff --git a/lib/tasks/deployment/20220922154831_backfill_etablissement_as_degraded_mode.rake b/lib/tasks/deployment/20220922154831_backfill_etablissement_as_degraded_mode.rake index 3d7aba523..abb6f97a5 100644 --- a/lib/tasks/deployment/20220922154831_backfill_etablissement_as_degraded_mode.rake +++ b/lib/tasks/deployment/20220922154831_backfill_etablissement_as_degraded_mode.rake @@ -5,7 +5,8 @@ namespace :after_party do Etablissement.joins(:dossier).where(adresse: nil).find_each do |etablissement| begin - APIEntrepriseService.update_etablissement_from_degraded_mode(etablissement) + procedure_id = etablissement.dossier.procedure.id + APIEntrepriseService.update_etablissement_from_degraded_mode(etablissement, procedure_id) rescue => e Sentry.capture_exception(e) end diff --git a/spec/jobs/cron/backfill_siret_degraded_mode_job_spec.rb b/spec/jobs/cron/backfill_siret_degraded_mode_job_spec.rb new file mode 100644 index 000000000..74a9c4468 --- /dev/null +++ b/spec/jobs/cron/backfill_siret_degraded_mode_job_spec.rb @@ -0,0 +1,28 @@ +RSpec.describe Cron::BackfillSiretDegradedModeJob, type: :job do + describe '.perform' do + let(:etablissement) { create(:etablissement, adresse: nil, siret: '01234567891011') } + let(:new_adresse) { '7 rue du puits, coye la foret' } + + context 'fix etablissement with dossier with adresse nil' do + let(:dossier) { create(:dossier, :en_construction, etablissement: etablissement) } + before do + dossier + end + it 'works' do + allow_any_instance_of(APIEntreprise::EtablissementAdapter).to receive(:to_params).and_return({ adresse: new_adresse }) + expect { Cron::BackfillSiretDegradedModeJob.perform_now }.to change { etablissement.reload.adresse }.from(nil).to(new_adresse) + end + end + + context 'fix etablisEtablissementAdapter.newsement with champs with adresse nil' do + let(:champ_siret) { create(:champ_siret, etablissement: etablissement) } + before do + champ_siret + end + it 'works' do + allow_any_instance_of(APIEntreprise::EtablissementAdapter).to receive(:to_params).and_return({ adresse: new_adresse }) + expect { Cron::BackfillSiretDegradedModeJob.perform_now }.to change { etablissement.reload.adresse }.from(nil).to(new_adresse) + end + end + end +end