demarches-normaliennes/app/services/api_entreprise_service.rb

42 lines
1.5 KiB
Ruby
Raw Normal View History

2020-08-05 18:40:47 +02:00
class APIEntrepriseService
# create etablissement with EtablissementAdapter
# enqueue api_entreprise jobs to retrieve
# all informations we can get about a SIRET.
2019-04-30 16:36:40 +02:00
#
# Returns nil if the SIRET is unknown
2019-04-30 16:36:40 +02:00
#
2020-08-05 18:40:47 +02:00
# Raises a APIEntreprise::API::Error::RequestFailed exception on transient errors
2019-04-30 16:36:40 +02:00
# (timeout, 5XX HTTP error code, etc.)
def self.create_etablissement(dossier_or_champ, siret, user_id = nil)
2020-08-05 18:40:47 +02:00
etablissement_params = APIEntreprise::EtablissementAdapter.new(siret, dossier_or_champ.procedure.id).to_params
return nil if etablissement_params.empty?
etablissement = dossier_or_champ.build_etablissement(etablissement_params)
etablissement.save!
[
2020-08-05 18:40:47 +02:00
APIEntreprise::EntrepriseJob, APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob,
APIEntreprise::EffectifsJob, APIEntreprise::EffectifsAnnuelsJob, APIEntreprise::AttestationSocialeJob,
APIEntreprise::BilansBdfJob
].each do |job|
job.perform_later(etablissement.id, dossier_or_champ.procedure.id)
2018-02-22 16:53:29 +01:00
end
2020-08-05 18:40:47 +02:00
APIEntreprise::AttestationFiscaleJob.perform_later(etablissement.id, dossier_or_champ.procedure.id, user_id)
etablissement
end
def self.api_up?(uname = "apie_2_etablissements")
statuses = APIEntreprise::API.new.current_status.fetch(:results)
# find results having uname = apie_2_etablissements
status = statuses.find { |result| result[:uname] == uname }
status.fetch(:code) == 200
rescue => e
Sentry.capture_exception(e, extra: { uname: uname })
nil
end
2018-02-22 16:53:29 +01:00
end