add SIRETService

This commit is contained in:
Paul Chavard 2018-02-22 16:53:29 +01:00
parent da14cceadc
commit 896518f3d5
8 changed files with 68 additions and 64 deletions

View file

@ -103,11 +103,13 @@ class Users::DossiersController < UsersController
def siret_informations
@facade = facade params[:dossier_id]
update_current_user_siret! siret
update_current_user_siret!(siret)
dossier = DossierService.new(@facade.dossier, siret, current_user.france_connect_information).dossier_informations!
etablissement_attributes = SIRETService.fetch(siret, @facade.dossier)
if dossier.entreprise.nil? || dossier.etablissement.nil?
if etablissement_attributes.present? && @facade.dossier.create_etablissement(etablissement_attributes)
@facade.dossier.mandataire_social!(current_user.france_connect_information)
else
return errors_valid_siret
end

View file

@ -23,13 +23,6 @@ class SIADE::EntrepriseAdapter
nil
end
def mandataires_sociaux
data_source[:entreprise].fetch(:mandataires_sociaux)
rescue
nil
end
private
def attr_to_fetch
@ -39,6 +32,7 @@ class SIADE::EntrepriseAdapter
:numero_tva_intracommunautaire,
:forme_juridique,
:forme_juridique_code,
:mandataires_sociaux,
:nom_commercial,
:raison_sociale,
:siret_siege_social,

View file

@ -293,6 +293,12 @@ class Dossier < ActiveRecord::Base
end
end
def mandataire_social!(france_connect_information)
if etablissement.mandataire_social?(france_connect_information)
update_column(:mandataire_social, true)
end
end
private
def update_state_dates

View file

@ -6,9 +6,13 @@ class Entreprise < ActiveRecord::Base
validates_presence_of :siren
validates_uniqueness_of :dossier_id
accepts_nested_attributes_for :rna_information
before_save :default_values
def default_values
self.raison_sociale ||= ''
end
attr_writer :mandataires_sociaux
end

View file

@ -4,6 +4,9 @@ class Etablissement < ActiveRecord::Base
has_many :exercices, dependent: :destroy
accepts_nested_attributes_for :exercices
accepts_nested_attributes_for :entreprise
validates_uniqueness_of :dossier_id
def geo_adresse
@ -14,4 +17,14 @@ class Etablissement < ActiveRecord::Base
# squeeze needed because of space in excess in the data
"#{numero_voie} #{type_voie} #{nom_voie}, #{complement_adresse}, #{code_postal} #{localite}".squeeze(' ')
end
attr_accessor :entreprise_mandataires_sociaux
def mandataire_social?(france_connect_information)
if france_connect_information.present?
entreprise_mandataires_sociaux&.find do |mandataire|
france_connect_information.mandataire_social?(mandataire)
end
end
end
end

View file

@ -2,4 +2,10 @@ class FranceConnectInformation < ActiveRecord::Base
belongs_to :user
validates :france_connect_particulier_id, presence: true, allow_blank: false, allow_nil: false
def mandataire_social?(params)
params[:nom].casecmp(family_name).zero? &&
params[:prenom].casecmp(given_name).zero? &&
params[:date_naissance_timestamp] == birthdate.to_time.to_i
end
end

View file

@ -1,54 +0,0 @@
class DossierService
def initialize dossier, siret, france_connect_information
@dossier = dossier
@siret = siret
@france_connect_information = france_connect_information
end
def dossier_informations!
@entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret)
if @entreprise_adapter.to_params.nil?
raise RestClient::ResourceNotFound
end
@etablissement_adapter = SIADE::EtablissementAdapter.new(@siret)
if @etablissement_adapter.to_params.nil?
raise RestClient::ResourceNotFound
end
@dossier.create_entreprise(@entreprise_adapter.to_params)
@dossier.create_etablissement(@etablissement_adapter.to_params)
@rna_adapter = SIADE::RNAAdapter.new(@siret)
@dossier.entreprise.create_rna_information(@rna_adapter.to_params)
@exercices_adapter = SIADE::ExercicesAdapter.new(@siret)
@dossier.etablissement.exercices.create(@exercices_adapter.to_params)
@dossier.update_attributes(mandataire_social: mandataire_social?(@entreprise_adapter.mandataires_sociaux))
@dossier.etablissement.update_attributes(entreprise: @dossier.entreprise)
@dossier
end
def self.siren siret
siret[0..8]
end
private
def mandataire_social? mandataires_list
if @france_connect_information.present?
mandataires_list.each do |mandataire|
return true if mandataire[:nom].casecmp(@france_connect_information.family_name).zero? &&
mandataire[:prenom].casecmp(@france_connect_information.given_name).zero? &&
mandataire[:date_naissance_timestamp] == @france_connect_information.birthdate.to_time.to_i
end
end
false
end
end

View file

@ -0,0 +1,33 @@
class SIRETService
def self.fetch(siret, dossier = nil)
etablissement = SIADE::EtablissementAdapter.new(siret)
entreprise = SIADE::EntrepriseAdapter.new(siren(siret))
if etablissement.success? && entreprise.success?
association = SIADE::RNAAdapter.new(siret)
exercices = SIADE::ExercicesAdapter.new(siret)
params = etablissement.to_params
.merge(entreprise.to_params.map { |k,v| ["entreprise_#{k}", v] }.to_h)
.merge(association.to_params&.map { |k,v| ["association_#{k}", v] }.to_h)
.merge(exercices_attributes: exercices.to_params)
# This is to fill legacy models and relationships
if dossier.present?
return params.merge(
entreprise_attributes: entreprise.to_params
.merge({
dossier: dossier,
rna_information_attributes: association.to_params
}.compact)
)
else
return params
end
end
end
def self.siren(siret)
siret[0..8]
end
end