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:
commit
84c52099fb
4 changed files with 63 additions and 3 deletions
32
app/jobs/cron/backfill_siret_degraded_mode_job.rb
Normal file
32
app/jobs/cron/backfill_siret_degraded_mode_job.rb
Normal 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
|
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
28
spec/jobs/cron/backfill_siret_degraded_mode_job_spec.rb
Normal file
28
spec/jobs/cron/backfill_siret_degraded_mode_job_spec.rb
Normal 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
|
Loading…
Reference in a new issue