2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
class APIEntreprise::EntrepriseAdapter < APIEntreprise::Adapter
|
2023-05-26 10:06:01 +02:00
|
|
|
# Doc métier : https://entreprise.api.gouv.fr/catalogue/insee/unites_legales
|
|
|
|
# Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Informations-generales/paths/~1v3~1insee~1sirene~1unites_legales~1%7Bsiren%7D/get
|
|
|
|
|
2018-02-21 16:29:22 +01:00
|
|
|
private
|
|
|
|
|
2018-03-19 15:05:56 +01:00
|
|
|
def get_resource
|
2022-07-20 14:52:17 +02:00
|
|
|
api(@procedure_id).entreprise(siren)
|
2018-03-15 14:30:10 +01:00
|
|
|
end
|
|
|
|
|
2018-03-19 15:45:55 +01:00
|
|
|
def process_params
|
2023-05-25 11:15:26 +02:00
|
|
|
params = data_source[:data]
|
2023-04-05 18:38:13 +02:00
|
|
|
return {} if params.nil?
|
2018-10-04 16:22:19 +02:00
|
|
|
|
2023-04-05 18:38:13 +02:00
|
|
|
Sentry.with_scope do |scope|
|
|
|
|
scope.set_tags(siret: @siret)
|
|
|
|
scope.set_extras(source: params)
|
|
|
|
|
|
|
|
params = params.slice(*attr_to_fetch)
|
|
|
|
params[:etat_administratif] = map_etat_administratif(data_source)
|
|
|
|
|
|
|
|
if params.present? && valid_params?(params)
|
|
|
|
params[:date_creation] = Time.zone.at(params[:date_creation]).to_datetime if params[:date_creation].present?
|
2023-05-25 11:15:26 +02:00
|
|
|
|
|
|
|
forme_juridique = params.extract! :forme_juridique
|
|
|
|
if forme_juridique.present?
|
|
|
|
params[:forme_juridique] = forme_juridique[:forme_juridique][:libelle]
|
|
|
|
params[:forme_juridique_code] = forme_juridique[:forme_juridique][:code]
|
|
|
|
end
|
|
|
|
|
|
|
|
personne_morale_attributs = params.extract! :personne_morale_attributs
|
|
|
|
if personne_morale_attributs.present?
|
|
|
|
params[:raison_sociale] = personne_morale_attributs[:personne_morale_attributs][:raison_sociale]
|
|
|
|
end
|
|
|
|
|
|
|
|
personne_physique_attributs = params.extract! :personne_physique_attributs
|
|
|
|
if personne_physique_attributs.present?
|
|
|
|
params[:nom] = build_nom(personne_physique_attributs)
|
|
|
|
params[:prenom] = personne_physique_attributs[:personne_physique_attributs][:prenom_usuel]
|
2023-04-05 18:38:13 +02:00
|
|
|
end
|
2023-05-25 11:15:26 +02:00
|
|
|
|
|
|
|
tranche_effectif = params.extract! :tranche_effectif_salarie
|
|
|
|
if tranche_effectif.present?
|
|
|
|
params[:code_effectif_entreprise] = tranche_effectif[:tranche_effectif_salarie][:code]
|
|
|
|
end
|
|
|
|
|
2023-04-05 18:38:13 +02:00
|
|
|
params.transform_keys { |k| :"entreprise_#{k}" }
|
|
|
|
else
|
|
|
|
{}
|
2022-12-06 17:35:20 +01:00
|
|
|
end
|
2018-10-04 16:22:19 +02:00
|
|
|
end
|
2018-03-19 15:45:55 +01:00
|
|
|
end
|
|
|
|
|
2023-05-25 11:15:26 +02:00
|
|
|
def build_nom(personne_physique_attributs)
|
|
|
|
nom_usage = personne_physique_attributs[:personne_physique_attributs][:nom_usage]&.strip
|
|
|
|
nom_naissance = personne_physique_attributs[:personne_physique_attributs][:nom_naissance]&.strip
|
|
|
|
|
|
|
|
return nom_usage if nom_naissance.blank? || nom_usage == nom_naissance
|
|
|
|
return nom_naissance if nom_usage.blank?
|
|
|
|
"#{nom_usage} (#{nom_naissance})"
|
|
|
|
end
|
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
def attr_to_fetch
|
2018-01-15 14:42:48 +01:00
|
|
|
[
|
|
|
|
:siren,
|
|
|
|
:forme_juridique,
|
2023-05-25 11:15:26 +02:00
|
|
|
:personne_morale_attributs,
|
|
|
|
:personne_physique_attributs,
|
2018-01-15 14:42:48 +01:00
|
|
|
:raison_sociale,
|
|
|
|
:siret_siege_social,
|
2023-05-25 11:15:26 +02:00
|
|
|
:tranche_effectif_salarie,
|
|
|
|
:date_creation
|
2018-01-15 14:42:48 +01:00
|
|
|
]
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
2022-07-18 12:24:41 +02:00
|
|
|
|
|
|
|
def map_etat_administratif(data_source)
|
2023-05-25 11:15:26 +02:00
|
|
|
raw_value = data_source.dig(:data, :etat_administratif) # data structure will change in v3
|
2022-07-18 12:24:41 +02:00
|
|
|
|
|
|
|
case raw_value
|
|
|
|
when 'A' then 'actif'
|
2023-05-10 18:08:23 +02:00
|
|
|
when 'F', 'C' then 'fermé'
|
2022-07-18 12:24:41 +02:00
|
|
|
end
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|