Merge pull request #7799 from mfo/US/backfill_dossier_and_champs_as_degraded_mode

fix(etablissement.as_degraded_mode): backfill missing data via a cron
This commit is contained in:
mfo 2022-09-23 15:25:10 +02:00 committed by GitHub
commit 84c52099fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 3 deletions

View file

@ -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

View file

@ -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?

View file

@ -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

View file

@ -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