demarches-normaliennes/app/services/siret_service.rb

34 lines
1.1 KiB
Ruby

class SIRETService
def self.fetch(siret, dossier = nil)
procedure_id = dossier&.procedure_id
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siren(siret), procedure_id).to_params
if etablissement_params.present? && entreprise_params.present?
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
exercices_array = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_array
params = etablissement_params
.merge(entreprise_params.transform_keys { |k| "entreprise_#{k}" })
.merge(association_params.transform_keys { |k| "association_#{k}" })
.merge(exercices_attributes: exercices_array)
# This is to fill legacy models and relationships
if dossier.present?
params[:entreprise_attributes] = entreprise_params.merge(
{
dossier: dossier,
rna_information_attributes: association_params.presence
}.compact
)
end
params
end
end
def self.siren(siret)
siret[0..8]
end
end