Merge pull request #1460 from tchak/cleanup-etablissement

Inline entereprise and association on etablissement
This commit is contained in:
Paul Chavard 2018-02-27 16:31:59 +01:00 committed by GitHub
commit cbdde36873
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 180 additions and 113 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

@ -9,19 +9,22 @@ class SIADE::EntrepriseAdapter
@data_source = nil
end
def to_params
params = {}
data_source[:entreprise].each do |k, v|
params[k] = v if attr_to_fetch.include?(k)
def success?
data_source
rescue
false
end
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
def to_params
params = data_source[:entreprise].slice(*attr_to_fetch)
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
params
rescue
nil
end
private
def attr_to_fetch
[
:siren,
@ -29,6 +32,7 @@ class SIADE::EntrepriseAdapter
:numero_tva_intracommunautaire,
:forme_juridique,
:forme_juridique_code,
:mandataires_sociaux,
:nom_commercial,
:raison_sociale,
:siret_siege_social,
@ -38,10 +42,4 @@ class SIADE::EntrepriseAdapter
:prenom
]
end
def mandataires_sociaux
data_source[:entreprise][:mandataires_sociaux]
rescue
nil
end
end

View file

@ -7,23 +7,27 @@ class SIADE::EtablissementAdapter
@data_source ||= JSON.parse(SIADE::API.etablissement(@siret), symbolize_names: true)
end
def to_params
params = {}
def success?
data_source
rescue => e
false
end
data_source[:etablissement].each do |k, v|
params[k] = v if attr_to_fetch.include?(k)
end
params[:adresse] = adresse
data_source[:etablissement][:adresse].each do |k, v|
params[k] = v if address_attribut_to_fetch.include?(k)
end
def to_params
params = data_source[:etablissement].slice(*attr_to_fetch)
adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n")
params.merge!(params[:adresse].slice(*address_attr_to_fetch))
params[:adresse] = adresse_line
params
rescue
nil
end
private
def attr_to_fetch
[
:adresse,
:siret,
:siege_social,
:naf,
@ -31,13 +35,7 @@ class SIADE::EtablissementAdapter
]
end
def adresse
[:l1, :l2, :l3, :l4, :l5, :l6, :l7].map do |line|
data_source[:etablissement][:adresse][line]
end.compact.join("\r\n")
end
def address_attribut_to_fetch
def address_attr_to_fetch
[
:numero_voie,
:type_voie,
@ -48,4 +46,8 @@ class SIADE::EtablissementAdapter
:code_insee_localite
]
end
def address_lines_to_fetch
[:l1, :l2, :l3, :l4, :l5, :l6, :l7]
end
end

View file

@ -11,13 +11,15 @@ class SIADE::ExercicesAdapter
def to_params
data_source[:exercices].map do |exercice|
{
ca: exercice[:ca],
dateFinExercice: exercice[:date_fin_exercice],
date_fin_exercice_timestamp: exercice[:date_fin_exercice_timestamp]
}
exercice.slice(*attr_to_fetch)
end
rescue
nil
[]
end
private
def attr_to_fetch
[:ca, :date_fin_exercice, :date_fin_exercice_timestamp]
end
end

View file

@ -8,23 +8,17 @@ class SIADE::RNAAdapter
end
def to_params
params = {}
data_source[:association].each do |k, v|
params[k] = v if attr_to_fetch.include?(k)
end
params[:association_id] = params[:id]
params.delete(:id)
params = data_source[:association].slice(*attr_to_fetch)
params[:rna] = data_source[:association][:id]
params
rescue
nil
end
private
def attr_to_fetch
[
:id,
:titre,
:objet,
:date_creation,

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,8 @@ class Exercice < ActiveRecord::Base
belongs_to :etablissement
validates :ca, presence: true, allow_blank: false, allow_nil: false
def date_fin_exercice
super || dateFinExercice
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

@ -2,4 +2,8 @@ class RNAInformation < ActiveRecord::Base
belongs_to :entreprise
validates :association_id, presence: true, allow_blank: false, allow_nil: false
def rna=(id)
write_attribute(:association_id, id)
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

View file

@ -49,7 +49,7 @@
.col-xs-8.entreprise-info
- @facade.etablissement.exercices.each_with_index do |exercice, index|
%strong
= "#{exercice.dateFinExercice.year} : "
= "#{exercice.date_fin_exercice.year} : "
= number_to_currency(exercice.ca)
%br

View file

@ -44,7 +44,7 @@
%th Exercices :
%td
- etablissement.exercices.each_with_index do |exercice, index|
= "#{exercice.dateFinExercice.year} : "
= "#{exercice.date_fin_exercice.year} : "
= number_to_currency(exercice.ca)
%br
- if entreprise.rna_information.present?

View file

@ -0,0 +1,26 @@
class AddEntrepriseToEtablissement < ActiveRecord::Migration[5.2]
def change
add_column :etablissements, :entreprise_siren, :string
add_column :etablissements, :entreprise_capital_social, :integer
add_column :etablissements, :entreprise_numero_tva_intracommunautaire, :string
add_column :etablissements, :entreprise_forme_juridique, :string
add_column :etablissements, :entreprise_forme_juridique_code, :string
add_column :etablissements, :entreprise_nom_commercial, :string
add_column :etablissements, :entreprise_raison_sociale, :string
add_column :etablissements, :entreprise_siret_siege_social, :string
add_column :etablissements, :entreprise_code_effectif_entreprise, :string
add_column :etablissements, :entreprise_date_creation, :date
add_column :etablissements, :entreprise_nom, :string
add_column :etablissements, :entreprise_prenom, :string
add_column :etablissements, :association_rna, :string
add_column :etablissements, :association_titre, :string
add_column :etablissements, :association_objet, :text
add_column :etablissements, :association_date_creation, :date
add_column :etablissements, :association_date_declaration, :date
add_column :etablissements, :association_date_publication, :date
add_column :champs, :etablissement_id, :integer, index: true
add_column :exercices, :date_fin_exercice, :datetime
end
end

View file

@ -161,6 +161,7 @@ ActiveRecord::Schema.define(version: 2018_02_26_162351) do
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "private"
t.integer "etablissement_id"
t.index ["dossier_id"], name: "index_champs_on_dossier_id"
t.index ["private"], name: "index_champs_on_private"
t.index ["type_de_champ_id"], name: "index_champs_on_type_de_champ_id"
@ -264,6 +265,24 @@ ActiveRecord::Schema.define(version: 2018_02_26_162351) do
t.string "code_insee_localite"
t.integer "dossier_id"
t.integer "entreprise_id"
t.string "entreprise_siren"
t.integer "entreprise_capital_social"
t.string "entreprise_numero_tva_intracommunautaire"
t.string "entreprise_forme_juridique"
t.string "entreprise_forme_juridique_code"
t.string "entreprise_nom_commercial"
t.string "entreprise_raison_sociale"
t.string "entreprise_siret_siege_social"
t.string "entreprise_code_effectif_entreprise"
t.date "entreprise_date_creation"
t.string "entreprise_nom"
t.string "entreprise_prenom"
t.string "association_rna"
t.string "association_titre"
t.text "association_objet"
t.date "association_date_creation"
t.date "association_date_declaration"
t.date "association_date_publication"
t.index ["dossier_id"], name: "index_etablissements_on_dossier_id"
end
@ -272,6 +291,7 @@ ActiveRecord::Schema.define(version: 2018_02_26_162351) do
t.datetime "dateFinExercice"
t.integer "date_fin_exercice_timestamp"
t.integer "etablissement_id"
t.datetime "date_fin_exercice"
end
create_table "follows", id: :serial, force: :cascade do |t|

View file

@ -1,7 +1,7 @@
FactoryBot.define do
factory :exercice do
ca '12345678'
dateFinExercice "2014-12-30 23:00:00"
date_fin_exercice "2014-12-30 23:00:00"
date_fin_exercice_timestamp 1419980400
association :etablissement, factory: [:etablissement]
end

View file

@ -1,10 +1,12 @@
require 'spec_helper'
describe SIADE::EntrepriseAdapter do
subject { described_class.new('418166096').to_params }
let(:siren) { '418166096' }
let(:adapter) { described_class.new(siren) }
subject { adapter.to_params }
before do
stub_request(:get, "https://staging.entreprise.api.gouv.fr/v2/entreprises/418166096?token=#{SIADETOKEN}")
stub_request(:get, "https://staging.entreprise.api.gouv.fr/v2/entreprises/#{siren}?token=#{SIADETOKEN}")
.to_return(body: File.read('spec/support/files/entreprise.json', status: 200))
end
@ -14,7 +16,7 @@ describe SIADE::EntrepriseAdapter do
context 'Attributs Entreprises' do
it 'L\'entreprise contient bien un siren' do
expect(subject[:siren]).to eq('418166096')
expect(subject[:siren]).to eq(siren)
end
it 'L\'entreprise contient bien un capital_social' do
@ -60,10 +62,14 @@ describe SIADE::EntrepriseAdapter do
it 'L\'entreprise contient bien un prenom' do
expect(subject[:prenom]).to eq('test_prenom')
end
it 'L\'entreprise contient bien les mandataires_sociaux' do
expect(subject[:mandataires_sociaux]).to be_an_instance_of(Array)
end
end
context 'Mandataire sociaux' do
subject { described_class.new('418166096').mandataires_sociaux }
subject { described_class.new(siren).to_params[:mandataires_sociaux] }
it '#to_params class est une Hash ?' do
expect(subject).to be_an_instance_of(Array)

View file

@ -2,7 +2,7 @@ require 'spec_helper'
describe SIADE::EtablissementAdapter do
context 'SIRET valide' do
let(:siret) { 41_816_609_600_051 }
let(:siret) { '41816609600051' }
subject { described_class.new(siret).to_params }
before do
@ -16,7 +16,7 @@ describe SIADE::EtablissementAdapter do
context 'Attributs Etablissements' do
it 'L\'entreprise contient bien un siret' do
expect(subject[:siret]).to eq('41816609600051')
expect(subject[:siret]).to eq(siret)
end
it 'L\'entreprise contient bien un siege_social' do

View file

@ -23,7 +23,7 @@ describe SIADE::ExercicesAdapter do
end
it 'L\'exercice contient bien une date de fin d\'exercice' do
expect(subject[0][:dateFinExercice]).to eq("2013-12-31T00:00:00+01:00")
expect(subject[0][:date_fin_exercice]).to eq("2013-12-31T00:00:00+01:00")
end
it 'L\'exercice contient bien une date_fin_exercice_timestamp' do

View file

@ -4,8 +4,9 @@ describe SIADE::RNAAdapter do
let(:siret) { '50480511000013' }
let(:body) { File.read('spec/support/files/rna.json') }
let(:status) { 200 }
let(:adapter) { described_class.new(siret) }
subject { described_class.new(siret).to_params }
subject { adapter.to_params }
before do
stub_request(:get, /https:\/\/staging.entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
@ -23,7 +24,7 @@ describe SIADE::RNAAdapter do
it { expect(subject).to be_an_instance_of(Hash) }
describe 'Attributs Associations' do
it { expect(subject[:association_id]).to eq('W595001988') }
it { expect(subject[:rna]).to eq('W595001988') }
it { expect(subject[:titre]).to eq('UN SUR QUATRE') }