Merge pull request #1796 from tchak/api-entreprise-job

Job to fetch missing établissements
This commit is contained in:
Paul Chavard 2018-04-05 18:55:40 +02:00 committed by GitHub
commit c7f5cab71a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View 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

View file

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