From e8fd212d13ca3e2c6d8fabea04c1c4798d7410d5 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 19 Jan 2016 17:19:38 +0100 Subject: [PATCH] - Code review - Delete Mandataires sociaux adapter - Add function to get mandataires sociaux in Enterprise Adapter --- .../admin/procedures_controller.rb | 6 +- .../france_connect/particulier_controller.rb | 9 +-- app/controllers/users/dossiers_controller.rb | 65 ++++++++++--------- app/models/etablissement.rb | 2 +- .../particulier/check_email.html.haml | 4 +- lib/siade/entreprise_adapter.rb | 6 ++ lib/siade/mandataires_sociaux_adapter.rb | 35 ---------- .../particulier_controller_spec.rb | 16 ++--- .../users/registrations_controller_spec.rb | 4 +- .../france_connect_particulier_spec.rb | 16 ++++- spec/lib/siade/entreprise_adapter_spec.rb | 33 +++++++++- .../siade/mandataires_sociaux_adapter_spec.rb | 43 ------------ spec/models/etablissement_spec.rb | 9 +++ .../users/dossiers/index_html.haml_spec.rb | 11 ++++ 14 files changed, 121 insertions(+), 138 deletions(-) delete mode 100644 lib/siade/mandataires_sociaux_adapter.rb delete mode 100644 spec/lib/siade/mandataires_sociaux_adapter_spec.rb diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 5677dbbaf..a8865aaf4 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -11,7 +11,6 @@ class Admin::ProceduresController < AdminController partial: "admin/procedures/list", array: true - @page = 'active' active_class end @@ -21,7 +20,6 @@ class Admin::ProceduresController < AdminController partial: "admin/procedures/list", array: true - @page = 'archived' archived_class render 'index' @@ -78,11 +76,11 @@ class Admin::ProceduresController < AdminController end def active_class - @active_class = 'active' if @page == 'active' + @active_class = 'active' end def archived_class - @archived_class = 'active' if @page == 'archived' + @archived_class = 'active' end private diff --git a/app/controllers/france_connect/particulier_controller.rb b/app/controllers/france_connect/particulier_controller.rb index c877ed2bf..5fccd6548 100644 --- a/app/controllers/france_connect/particulier_controller.rb +++ b/app/controllers/france_connect/particulier_controller.rb @@ -33,20 +33,21 @@ class FranceConnect::ParticulierController < ApplicationController end def check_email - return create if User.find_by_email(params[:user][:email]).nil? + user = User.find_by_email(params[:user][:email]) + + return create if user.nil? return redirect_to root_path if france_connect_particulier_id_blank? unless params[:user][:password].nil? - user = User.find_by_email(params[:user][:email]) - valid_password = user.valid_password?(params[:user][:password]) - if valid_password + if user.valid_password?(params[:user][:password]) user.update_attributes create_user_params return connect_france_connect_particulier user else flash.now.alert = 'Mot de passe invalide' end end + @user = (User.new create_user_params).decorate end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 09fea9acd..ed2415cf6 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -37,43 +37,31 @@ class Users::DossiersController < UsersController end def create - etablissement = Etablissement.new(SIADE::EtablissementAdapter.new(siret).to_params) - entreprise = Entreprise.new(SIADE::EntrepriseAdapter.new(siren).to_params) - rna_information = SIADE::RNAAdapter.new(siret).to_params - exercices = SIADE::ExercicesAdapter.new(siret).to_params - mandataires_sociaux = SIADE::MandatairesSociauxAdapter.new(siren).to_params + entreprise_adapter = SIADE::EntrepriseAdapter.new(siren) + dossier = Dossier.create(user: current_user, + state: 'draft', + procedure_id: create_params[:procedure_id], + mandataire_social: mandataire_social?(entreprise_adapter.mandataires_sociaux)) + + entreprise = Entreprise.create entreprise_adapter.to_params + .merge({dossier_id: dossier.id}) + + etablissement = Etablissement.create SIADE::EtablissementAdapter.new(siret).to_params + .merge({dossier_id: dossier.id, + entreprise_id: entreprise.id}) + + rna_information = SIADE::RNAAdapter.new(siret).to_params + unless rna_information.nil? + RNAInformation.create rna_information.merge({entreprise_id: entreprise.id}) + end + + exercices = SIADE::ExercicesAdapter.new(siret).to_params unless exercices.nil? exercices.each_value do |exercice| - exercice = Exercice.new(exercice) - exercice.etablissement = etablissement - exercice.save + Exercice.create(exercice.merge({etablissement_id: etablissement.id})) end end - mandataire_social = false - - mandataires_sociaux.each do |k, mandataire| - break mandataire_social = true if !current_user.france_connect_particulier_id.nil? && - mandataire[:nom] == current_user.family_name && - mandataire[:prenom] == current_user.given_name && - mandataire[:date_naissance_timestamp] == current_user.birthdate.to_time.to_i - - end - - dossier = Dossier.create(user: current_user, state: 'draft', procedure_id: create_params[:procedure_id], mandataire_social: mandataire_social) - - entreprise.dossier = dossier - entreprise.save - - unless rna_information.nil? - rna_information = RNAInformation.new(rna_information) - rna_information.entreprise = entreprise - rna_information.save - end - - etablissement.dossier = dossier - etablissement.entreprise = entreprise - etablissement.save redirect_to url_for(controller: :dossiers, action: :show, id: dossier.id) @@ -177,4 +165,17 @@ class Users::DossiersController < UsersController redirect_to url_for users_dossiers_path end + + def mandataire_social? mandataires_list + mandataire_social = false + + mandataires_list.each do |mandataire| + break mandataire_social = true if !current_user.france_connect_particulier_id.nil? && + mandataire[:nom].upcase == current_user.family_name.upcase && + mandataire[:prenom].upcase == current_user.given_name.upcase && + mandataire[:date_naissance_timestamp] == current_user.birthdate.to_time.to_i + end + + mandataire_social + end end diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 627a4fa01..9fee925cd 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -5,6 +5,6 @@ class Etablissement < ActiveRecord::Base has_many :exercices def geo_adresse - numero_voie.to_s << ' ' << type_voie.to_s << ' ' << nom_voie.to_s << ' ' << complement_adresse.to_s << ' ' << code_postal.to_s << ' ' << localite.to_s + [numero_voie, type_voie, nom_voie, complement_adresse, code_postal, localite].join(' ') end end diff --git a/app/views/france_connect/particulier/check_email.html.haml b/app/views/france_connect/particulier/check_email.html.haml index 48642caf0..1ce8363af 100644 --- a/app/views/france_connect/particulier/check_email.html.haml +++ b/app/views/france_connect/particulier/check_email.html.haml @@ -7,7 +7,7 @@ %br %p - %h4.center Nous vous avons trouvé un compte qui utilise déjà cette adresse email. + %h4.center Nous avons trouvé un compte qui utilise déjà cette adresse email. %p.center Afin d'associer ce compte à votre identifiant France Connect, merci de saisir votre mot de passe TPS. %br @@ -15,7 +15,7 @@ #france_connect_particulier_email = form_for @user, url: {controller: 'france_connect/particulier', action: :check_email}, method: :post do |f| .form-group.form-group-lg - = f.text_field :email, class: "form-control", placeholder: "Entrez votre email", readonly: 'readonly' + = f.text_field :email, class: "form-control", readonly: 'readonly' %br = f.password_field :password, class: "form-control", placeholder: "Entrez votre mot de passe" = f.hidden_field :email diff --git a/lib/siade/entreprise_adapter.rb b/lib/siade/entreprise_adapter.rb index 06a6344a0..de4bae256 100644 --- a/lib/siade/entreprise_adapter.rb +++ b/lib/siade/entreprise_adapter.rb @@ -34,4 +34,10 @@ class SIADE::EntrepriseAdapter :nom, :prenom] end + + def mandataires_sociaux + data_source[:entreprise][:mandataires_sociaux] + rescue + nil + end end diff --git a/lib/siade/mandataires_sociaux_adapter.rb b/lib/siade/mandataires_sociaux_adapter.rb deleted file mode 100644 index 0de39a041..000000000 --- a/lib/siade/mandataires_sociaux_adapter.rb +++ /dev/null @@ -1,35 +0,0 @@ -class SIADE::MandatairesSociauxAdapter - def initialize(siren) - @siren = siren - end - - def data_source - @data_source ||= JSON.parse(SIADE::API.entreprise(@siren), symbolize_names: true) - rescue - @data_source = nil - end - - def to_params - params = {} - - data_source[:entreprise][:mandataires_sociaux].each_with_index do |mandataire, i| - params[i] = {} - - mandataire.each do |k, v| - params[i][k] = v if attr_to_fetch.include?(k) - end - end - - params - rescue - nil - end - - def attr_to_fetch - [:nom, - :prenom, - :fonction, - :date_naissance, - :date_naissance_timestamp] - end -end diff --git a/spec/controllers/france_connect/particulier_controller_spec.rb b/spec/controllers/france_connect/particulier_controller_spec.rb index c6a12c2e8..f5bacba6f 100644 --- a/spec/controllers/france_connect/particulier_controller_spec.rb +++ b/spec/controllers/france_connect/particulier_controller_spec.rb @@ -81,28 +81,20 @@ describe FranceConnect::ParticulierController, type: :controller do end describe 'POST #create' do - let(:email) { 'plop@gmail.com' } - subject { post :create, user: user_info } context 'when email is filled' do - it { expect { subject }.to change { User.count }.by(1) } + let(:email) { 'plop@gmail.com' } - it 'redirects user root page' do - subject - expect(response).to redirect_to(root_path) - end + it { expect { subject }.to change { User.count }.by(1) } + it { expect(subject).to redirect_to(root_path) } end context 'when email is incorrect' do let(:email) { '' } it { expect { subject }.to change { User.count }.by(0) } - - it 'redirect to check email FC page' do - subject - expect(response).to redirect_to(france_connect_particulier_new_path(user: user_info)) - end + it { expect(subject).to redirect_to(france_connect_particulier_new_path(user: user_info)) } end end diff --git a/spec/controllers/users/registrations_controller_spec.rb b/spec/controllers/users/registrations_controller_spec.rb index cca9d229e..2d752789b 100644 --- a/spec/controllers/users/registrations_controller_spec.rb +++ b/spec/controllers/users/registrations_controller_spec.rb @@ -17,7 +17,7 @@ describe Users::RegistrationsController, type: :controller do context 'when user is correct' do it { expect(described_class).to be < Devise::RegistrationsController } - it 'welcome email is send' do + it 'sends welcome email' do expect(WelcomeMailer).to receive(:welcome_email).and_return(WelcomeMailer) expect(WelcomeMailer).to receive(:deliver_now!) @@ -28,7 +28,7 @@ describe Users::RegistrationsController, type: :controller do context 'when user is not correct' do let(:user) { {email: '', password: password, password_confirmation: password} } - it 'welcome email is not send' do + it 'not sends welcome email' do expect(WelcomeMailer).not_to receive(:welcome_email) subject diff --git a/spec/features/france_connect/france_connect_particulier_spec.rb b/spec/features/france_connect/france_connect_particulier_spec.rb index 267e12d7b..4876d5956 100644 --- a/spec/features/france_connect/france_connect_particulier_spec.rb +++ b/spec/features/france_connect/france_connect_particulier_spec.rb @@ -12,7 +12,13 @@ feature 'France Connect Particulier Connexion' do let(:know_france_connect_particulier_id) { 'blabla' } let(:unknow_france_connect_particulier_id) { 'titi' } - let(:user_info) { Hashie::Mash.new(france_connect_particulier_id: france_connect_particulier_id, given_name: given_name, family_name: family_name, birthdate: birthdate, birthplace: birthplace, gender: gender, email: email) } + let(:user_info) { Hashie::Mash.new(france_connect_particulier_id: france_connect_particulier_id, + given_name: given_name, + family_name: family_name, + birthdate: birthdate, + birthplace: birthplace, + gender: gender, + email: email) } context 'when user is on login page' do @@ -28,7 +34,13 @@ feature 'France Connect Particulier Connexion' do let(:code) { 'plop' } context 'when authentification is ok' do - let!(:user) { create(:user, france_connect_particulier_id: know_france_connect_particulier_id, given_name: given_name, family_name: family_name, birthdate: birthdate, birthplace: birthplace, gender: gender) } + let!(:user) { create(:user, + france_connect_particulier_id: know_france_connect_particulier_id, + given_name: given_name, + family_name: family_name, + birthdate: birthdate, + birthplace: birthplace, + gender: gender) } before do allow_any_instance_of(FranceConnectParticulierClient).to receive(:authorization_uri).and_return(france_connect_particulier_callback_path(code: code)) diff --git a/spec/lib/siade/entreprise_adapter_spec.rb b/spec/lib/siade/entreprise_adapter_spec.rb index f17629e0d..2eee6ba1b 100644 --- a/spec/lib/siade/entreprise_adapter_spec.rb +++ b/spec/lib/siade/entreprise_adapter_spec.rb @@ -5,7 +5,7 @@ describe SIADE::EntrepriseAdapter do before do stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/418166096?token=#{SIADETOKEN}") - .to_return(body: File.read('spec/support/files/entreprise.json', status: 200)) + .to_return(body: File.read('spec/support/files/entreprise.json', status: 200)) end it '#to_params class est une Hash ?' do @@ -61,4 +61,35 @@ describe SIADE::EntrepriseAdapter do expect(subject[:prenom]).to eq('test_prenom') end end + + context 'Mandataire sociaux' do + subject { described_class.new('418166096').mandataires_sociaux } + + it '#to_params class est une Hash ?' do + expect(subject).to be_an_instance_of(Array) + end + + it { expect(subject.size).to eq(8) } + + describe 'Attributs' do + it 'Un mandataire social possède bien un nom' do + expect(subject[0][:nom]).to eq('HISQUIN') + end + it 'Un mandataire social possède bien un prenom' do + expect(subject[0][:prenom]).to eq('FRANCOIS') + end + + it 'Un mandataire social possède bien une fonction' do + expect(subject[0][:fonction]).to eq('PRESIDENT DU DIRECTOIRE') + end + + it 'Un mandataire social possède bien une date de naissance' do + expect(subject[0][:date_naissance]).to eq('1965-01-27') + end + + it 'Un mandataire social possède bien une date de naissance au format timestamp' do + expect(subject[0][:date_naissance_timestamp]).to eq(-155523600) + end + end + end end diff --git a/spec/lib/siade/mandataires_sociaux_adapter_spec.rb b/spec/lib/siade/mandataires_sociaux_adapter_spec.rb deleted file mode 100644 index 24e78fb34..000000000 --- a/spec/lib/siade/mandataires_sociaux_adapter_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'spec_helper' - -describe SIADE::MandatairesSociauxAdapter do - subject { described_class.new('418166096').to_params } - - before do - stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/418166096?token=#{SIADETOKEN}") - .to_return(body: File.read('spec/support/files/entreprise.json', status: 200)) - end - - it '#to_params class est une Hash ?' do - expect(subject).to be_an_instance_of(Hash) - end - - describe 'Mandataires Sociaux' do - - it { expect(subject.size).to eq(8) } - - describe 'Attributs' do - - it 'Un mandataire social possède bien un nom' do - expect(subject[0][:nom]).to eq('HISQUIN') - end - it 'Un mandataire social possède bien un prenom' do - expect(subject[0][:prenom]).to eq('FRANCOIS') - end - - it 'Un mandataire social possède bien une fonction' do - expect(subject[0][:fonction]).to eq('PRESIDENT DU DIRECTOIRE') - end - - it 'Un mandataire social possède bien une date de naissance' do - expect(subject[0][:date_naissance]).to eq('1965-01-27') - end - - it 'Un mandataire social possède bien une date de naissance au format timestamp' do - expect(subject[0][:date_naissance_timestamp]).to eq(-155523600) - end - - - end - end -end diff --git a/spec/models/etablissement_spec.rb b/spec/models/etablissement_spec.rb index 376886794..6097cac9c 100644 --- a/spec/models/etablissement_spec.rb +++ b/spec/models/etablissement_spec.rb @@ -21,4 +21,13 @@ describe Etablissement do it { is_expected.to belong_to(:entreprise) } it { is_expected.to have_many(:exercices) } end + + describe '#geo_adresse' do + + let(:etablissement) { create(:etablissement) } + + subject { etablissement.geo_adresse } + + it { is_expected.to eq '6 RUE RAOUL NORDLING IMMEUBLE BORA 92270 BOIS COLOMBES' } + end end diff --git a/spec/views/users/dossiers/index_html.haml_spec.rb b/spec/views/users/dossiers/index_html.haml_spec.rb index 7276a53be..7c91514ac 100644 --- a/spec/views/users/dossiers/index_html.haml_spec.rb +++ b/spec/views/users/dossiers/index_html.haml_spec.rb @@ -5,6 +5,7 @@ describe 'users/dossiers/index.html.haml', type: :view do let!(:dossier) { create(:dossier, :with_procedure, user: user, state: 'initiated', nom_projet: 'projet de test').decorate } let!(:dossier_2) { create(:dossier, :with_procedure, user: user, state: 'replied', nom_projet: 'projet répondu').decorate } + let!(:dossier_3) { create(:dossier, :with_procedure, user: user, state: 'replied', nom_projet: 'projet répondu 2').decorate } let!(:dossier_termine) { create(:dossier, :with_procedure, user: user, state: 'closed').decorate } describe 'params liste is a_traiter' do @@ -18,6 +19,10 @@ describe 'users/dossiers/index.html.haml', type: :view do partial: "users/dossiers/list", array: true)) assign(:liste, 'a_traiter') + assign(:dossiers_a_traiter_total, '1') + assign(:dossiers_en_attente_total, '2') + assign(:dossiers_termine_total, '1') + render end @@ -36,6 +41,12 @@ describe 'users/dossiers/index.html.haml', type: :view do it { is_expected.not_to have_content(dossier.nom_projet) } it { is_expected.not_to have_content(dossier_termine.nom_projet) } end + + describe 'badges on tabs' do + it { is_expected.to have_content('À traiter 1') } + it { is_expected.to have_content('En attente 2') } + it { is_expected.to have_content('Terminé 1') } + end end describe 'params liste is en_attente' do