Merge pull request #1796 from tchak/api-entreprise-job
Job to fetch missing établissements
This commit is contained in:
commit
c7f5cab71a
2 changed files with 36 additions and 0 deletions
19
app/jobs/etablissement_update_job.rb
Normal file
19
app/jobs/etablissement_update_job.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
class EtablissementUpdateJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(dossier, siret)
|
||||
etablissement_attributes = SIRETService.fetch(siret, dossier.procedure_id, dossier)
|
||||
|
||||
if etablissement_attributes.present?
|
||||
if dossier.entreprise.present?
|
||||
dossier.entreprise.destroy
|
||||
end
|
||||
if dossier.etablissement.present?
|
||||
dossier.etablissement.destroy
|
||||
end
|
||||
etablissement_attributes = ActionController::Parameters.new(etablissement_attributes).permit!
|
||||
etablissement = dossier.build_etablissement(etablissement_attributes)
|
||||
etablissement.save
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,17 @@
|
|||
namespace :'2018_04_04_fetch_etablissement_with_no_entreprise' do
|
||||
task fetch: :environment do
|
||||
dossiers = Entreprise.joins('LEFT JOIN etablissements et ON entreprises.id = et.entreprise_id')
|
||||
.where('et.id IS NULL')
|
||||
.map(&:dossier_id).map { |id| Dossier.unscoped.find_by(id: id) }.compact
|
||||
|
||||
dossiers.each do |dossier|
|
||||
siret = dossier.entreprise.siret_siege_social
|
||||
|
||||
puts "Fetch siret: #{siret} for dossier: #{dossier.id}"
|
||||
|
||||
if siret
|
||||
EtablissementUpdateJob.perform_later(dossier, siret)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue