2020-08-05 18:40:47 +02:00
|
|
|
class APIEntrepriseService
|
2020-05-15 17:15:52 +02:00
|
|
|
# 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
|
|
|
#
|
2020-05-15 17:15:52 +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.)
|
2020-05-26 17:55:45 +02:00
|
|
|
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
|
2020-05-15 17:15:52 +02:00
|
|
|
return nil if etablissement_params.empty?
|
|
|
|
|
2020-05-26 17:55:45 +02:00
|
|
|
etablissement = dossier_or_champ.build_etablissement(etablissement_params)
|
2020-08-13 11:53:32 +02:00
|
|
|
etablissement.save!
|
2020-05-15 17:15:52 +02:00
|
|
|
|
|
|
|
[
|
2020-08-05 18:40:47 +02:00
|
|
|
APIEntreprise::EntrepriseJob, APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob,
|
|
|
|
APIEntreprise::EffectifsJob, APIEntreprise::EffectifsAnnuelsJob, APIEntreprise::AttestationSocialeJob,
|
|
|
|
APIEntreprise::BilansBdfJob
|
2020-05-15 17:15:52 +02:00
|
|
|
].each do |job|
|
2020-05-26 17:55:45 +02:00
|
|
|
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)
|
2020-04-07 19:29:14 +02:00
|
|
|
|
2020-05-15 17:15:52 +02:00
|
|
|
etablissement
|
2020-04-07 19:29:14 +02:00
|
|
|
end
|
2022-09-15 17:49:57 +02:00
|
|
|
|
|
|
|
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
|