2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
class APIEntrepriseService
|
2022-09-16 00:32:17 +02:00
|
|
|
class << self
|
|
|
|
# create etablissement with EtablissementAdapter
|
|
|
|
# enqueue api_entreprise jobs to retrieve
|
|
|
|
# all informations we can get about a SIRET.
|
|
|
|
#
|
|
|
|
# Returns nil if the SIRET is unknown
|
|
|
|
#
|
|
|
|
# Raises a APIEntreprise::API::Error::RequestFailed exception on transient errors
|
|
|
|
# (timeout, 5XX HTTP error code, etc.)
|
|
|
|
#
|
|
|
|
def create_etablissement(dossier_or_champ, siret, user_id = nil)
|
|
|
|
procedure_id = dossier_or_champ.procedure.id
|
|
|
|
|
|
|
|
etablissement_params = APIEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
|
|
|
|
return nil if etablissement_params.empty?
|
|
|
|
|
2023-02-02 09:59:59 +01:00
|
|
|
entreprise_params = APIEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params
|
|
|
|
etablissement_params.merge!(entreprise_params) if entreprise_params.any?
|
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
etablissement = dossier_or_champ.build_etablissement(etablissement_params)
|
|
|
|
etablissement.save!
|
2024-11-22 11:37:13 +01:00
|
|
|
etablissement.update_champ_value_json!
|
2024-10-14 15:58:32 +02:00
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
perform_later_fetch_jobs(etablissement, procedure_id, user_id)
|
|
|
|
|
|
|
|
etablissement
|
2018-02-22 16:53:29 +01:00
|
|
|
end
|
2020-04-07 19:29:14 +02:00
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
def create_etablissement_as_degraded_mode(dossier_or_champ, siret, user_id = nil)
|
|
|
|
etablissement = dossier_or_champ.build_etablissement(siret: siret)
|
|
|
|
etablissement.save!
|
|
|
|
|
|
|
|
procedure_id = dossier_or_champ.procedure.id
|
2022-09-15 17:49:57 +02:00
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
perform_later_fetch_jobs(etablissement, procedure_id, user_id, wait: 30.minutes)
|
2022-09-15 17:49:57 +02:00
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
etablissement
|
|
|
|
end
|
2022-09-15 17:49:57 +02:00
|
|
|
|
2022-09-23 15:12:25 +02:00
|
|
|
def update_etablissement_from_degraded_mode(etablissement, procedure_id)
|
2022-09-22 18:02:47 +02:00
|
|
|
siret = etablissement.siret
|
|
|
|
etablissement_params = APIEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
|
|
|
|
return nil if etablissement_params.empty?
|
|
|
|
|
|
|
|
etablissement.update!(etablissement_params)
|
2024-11-22 11:37:13 +01:00
|
|
|
etablissement.update_champ_value_json!
|
2024-10-14 15:58:32 +02:00
|
|
|
|
|
|
|
etablissement
|
2022-09-22 18:02:47 +02:00
|
|
|
end
|
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
def perform_later_fetch_jobs(etablissement, procedure_id, user_id, wait: nil)
|
2024-10-14 15:58:32 +02:00
|
|
|
jobs = [
|
2023-05-25 15:29:16 +02:00
|
|
|
APIEntreprise::EntrepriseJob, APIEntreprise::ExtraitKbisJob, APIEntreprise::TvaJob,
|
2023-05-25 14:46:34 +02:00
|
|
|
APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob,
|
2022-09-16 00:32:17 +02:00
|
|
|
APIEntreprise::EffectifsJob, APIEntreprise::EffectifsAnnuelsJob, APIEntreprise::AttestationSocialeJob,
|
|
|
|
APIEntreprise::BilansBdfJob
|
2024-10-14 15:58:32 +02:00
|
|
|
]
|
|
|
|
if etablissement.as_degraded_mode?
|
|
|
|
jobs << APIEntreprise::EtablissementJob
|
|
|
|
end
|
|
|
|
jobs.each do |job|
|
2022-09-16 00:32:17 +02:00
|
|
|
job.set(wait:).perform_later(etablissement.id, procedure_id)
|
|
|
|
end
|
2022-09-15 17:49:57 +02:00
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
APIEntreprise::AttestationFiscaleJob.set(wait:).perform_later(etablissement.id, procedure_id, user_id)
|
|
|
|
end
|
|
|
|
|
2024-04-05 12:07:40 +02:00
|
|
|
# See: https://entreprise.api.gouv.fr/developpeurs#surveillance-etat-fournisseurs
|
|
|
|
def api_insee_up?
|
2024-04-05 12:26:32 +02:00
|
|
|
api_up?("https://entreprise.api.gouv.fr/ping/insee/sirene")
|
2024-04-05 12:07:40 +02:00
|
|
|
end
|
2024-04-05 12:08:40 +02:00
|
|
|
|
|
|
|
def api_djepva_up?
|
2024-04-05 12:26:32 +02:00
|
|
|
api_up?("https://entreprise.api.gouv.fr/ping/djepva/api-association")
|
|
|
|
end
|
|
|
|
|
2024-10-14 15:58:32 +02:00
|
|
|
def service_unavailable_error?(error, target:)
|
|
|
|
return false if !error.try(:network_error?)
|
|
|
|
return true if target == :insee && !APIEntrepriseService.api_insee_up?
|
|
|
|
return true if target == :djepva && !APIEntrepriseService.api_djepva_up?
|
|
|
|
error.is_a?(APIEntreprise::API::Error::ServiceUnavailable)
|
|
|
|
end
|
|
|
|
|
2024-04-05 12:26:32 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def api_up?(url)
|
|
|
|
response = Typhoeus.get(url, timeout: 1)
|
2024-04-05 12:08:40 +02:00
|
|
|
if response.success?
|
|
|
|
JSON.parse(response.body).fetch('status') == 'ok'
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2024-04-05 12:26:32 +02:00
|
|
|
rescue => e
|
|
|
|
Sentry.capture_exception(e)
|
|
|
|
false
|
2024-04-05 12:08:40 +02:00
|
|
|
end
|
2022-09-15 17:49:57 +02:00
|
|
|
end
|
2018-02-22 16:53:29 +01:00
|
|
|
end
|