Refactor function create in user dossier controller

This commit is contained in:
Xavier J 2016-01-20 10:34:22 +01:00
parent 5beade4735
commit 65ba614be2
7 changed files with 27 additions and 33 deletions

View file

@ -44,24 +44,14 @@ class Users::DossiersController < UsersController
procedure_id: create_params[:procedure_id], procedure_id: create_params[:procedure_id],
mandataire_social: mandataire_social?(entreprise_adapter.mandataires_sociaux)) mandataire_social: mandataire_social?(entreprise_adapter.mandataires_sociaux))
entreprise = Entreprise.create entreprise_adapter.to_params entreprise = dossier.create_entreprise(entreprise_adapter.to_params)
.merge({dossier_id: dossier.id})
etablissement = Etablissement.create SIADE::EtablissementAdapter.new(siret).to_params entreprise.create_rna_information(SIADE::RNAAdapter.new(siret).to_params)
.merge({dossier_id: dossier.id,
entreprise_id: entreprise.id})
rna_information = SIADE::RNAAdapter.new(siret).to_params etablissement = dossier.create_etablissement(SIADE::EtablissementAdapter.new(siret).to_params
unless rna_information.nil? .merge({entreprise_id: entreprise.id}))
RNAInformation.create rna_information.merge({entreprise_id: entreprise.id})
end
exercices = SIADE::ExercicesAdapter.new(siret).to_params etablissement.exercices.create(SIADE::ExercicesAdapter.new(siret).to_params)
unless exercices.nil?
exercices.each_value do |exercice|
Exercice.create(exercice.merge({etablissement_id: etablissement.id}))
end
end
redirect_to url_for(controller: :dossiers, action: :show, id: dossier.id) redirect_to url_for(controller: :dossiers, action: :show, id: dossier.id)
@ -167,15 +157,13 @@ class Users::DossiersController < UsersController
end end
def mandataire_social? mandataires_list def mandataire_social? mandataires_list
mandataire_social = false
mandataires_list.each do |mandataire| mandataires_list.each do |mandataire|
break mandataire_social = true if !current_user.france_connect_particulier_id.nil? && return true if !current_user.france_connect_particulier_id.nil? &&
mandataire[:nom].upcase == current_user.family_name.upcase && mandataire[:nom].upcase == current_user.family_name.upcase &&
mandataire[:prenom].upcase == current_user.given_name.upcase && mandataire[:prenom].upcase == current_user.given_name.upcase &&
mandataire[:date_naissance_timestamp] == current_user.birthdate.to_time.to_i mandataire[:date_naissance_timestamp] == current_user.birthdate.to_time.to_i
end end
mandataire_social false
end end
end end

View file

@ -1,3 +1,6 @@
class Exercice < ActiveRecord::Base class Exercice < ActiveRecord::Base
belongs_to :etablissement belongs_to :etablissement
validates :ca, presence: true, allow_blank: false, allow_nil: false
end end

View file

@ -1,3 +1,5 @@
class RNAInformation < ActiveRecord::Base class RNAInformation < ActiveRecord::Base
belongs_to :entreprise belongs_to :entreprise
validates :association_id, presence: true, allow_blank: false, allow_nil: false
end end

View file

@ -10,16 +10,7 @@ class SIADE::ExercicesAdapter
end end
def to_params def to_params
params = {} data_source[:exercices]
data_source[:exercices].each_with_index do |values, i|
params[i] = {}
values.each do |index, value|
params[i][index] = value if attr_to_fetch.include?(index)
end
end
params
rescue rescue
nil nil
end end

View file

@ -1,6 +1,6 @@
class SIADE::RNAAdapter class SIADE::RNAAdapter
def initialize(siren) def initialize(siret)
@siret = siren @siret = siret
end end
def data_source def data_source

View file

@ -12,6 +12,9 @@ describe Users::DossiersController, type: :controller do
let(:rna_status) { 404 } let(:rna_status) { 404 }
let(:rna_body) { '' } let(:rna_body) { '' }
let(:exercices_status) { 200 }
let(:exercices_body) { File.read('spec/support/files/exercices.json') }
let(:siren) { dossier.siren } let(:siren) { dossier.siren }
let(:siret) { dossier.siret } let(:siret) { dossier.siret }
let(:bad_siret) { 1 } let(:bad_siret) { 1 }
@ -78,7 +81,7 @@ describe Users::DossiersController, type: :controller do
.to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) .to_return(status: 200, body: File.read('spec/support/files/entreprise.json'))
stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}")
.to_return(status: 200, body: File.read('spec/support/files/exercices.json')) .to_return(status: exercices_status, body: exercices_body)
stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/associations/#{siret}?token=#{SIADETOKEN}") stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/associations/#{siret}?token=#{SIADETOKEN}")
.to_return(status: rna_status, body: rna_body) .to_return(status: rna_status, body: rna_body)
@ -131,6 +134,13 @@ describe Users::DossiersController, type: :controller do
expect(Exercice.last.etablissement).to eq(Dossier.last.etablissement) expect(Exercice.last.etablissement).to eq(Dossier.last.etablissement)
end end
context 'when siret have no exercices' do
let(:exercices_status) { 404 }
let(:exercices_body) { '' }
it { expect { subject }.not_to change { Exercice.count } }
end
it 'links procedure to dossier' do it 'links procedure to dossier' do
subject subject
expect(Dossier.last.procedure).to eq(Procedure.last) expect(Dossier.last.procedure).to eq(Procedure.last)

View file

@ -10,7 +10,7 @@ describe SIADE::ExercicesAdapter do
end end
it '#to_params class est une Hash ?' do it '#to_params class est une Hash ?' do
expect(subject).to be_an_instance_of(Hash) expect(subject).to be_an_instance_of(Array)
end end
it 'have 3 exercices' do it 'have 3 exercices' do