dossier: add action to save SIRET number

This commit is contained in:
Pierre de La Morinerie 2018-10-15 16:41:53 +00:00 committed by gregoirenovel
parent 54ba0f8e50
commit 310f4b92d1
6 changed files with 176 additions and 1 deletions

View file

@ -73,6 +73,36 @@ module NewUser
@dossier = dossier
end
def update_siret
@dossier = dossier
# We use the user as the holder model object for the siret value
# (so that we can restore it on the form in case of error).
#
# This is the only remaining use of User#siret: it could be refactored away.
# However some existing users have a siret but no associated etablissement,
# so we would need to analyze the legacy data and decide what to do with it.
current_user.siret = siret_params[:siret]
siret_model = Siret.new(siret: siret_params[:siret])
if !siret_model.valid?
return render_siret_error(siret_model.errors.full_messages)
end
sanitized_siret = siret_model.siret
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(sanitized_siret, @dossier.procedure.id)
if etablissement_attributes.blank?
return render_siret_error(t('errors.messages.siret_unknown'))
end
etablissement = @dossier.build_etablissement(etablissement_attributes)
etablissement.save!
current_user.update!(siret: sanitized_siret)
@dossier.update!(autorisation_donnees: true)
redirect_to etablissement_dossier_path
end
def brouillon
@dossier = dossier_with_champs
@ -255,10 +285,19 @@ module NewUser
redirect_to root_path
end
def render_siret_error(error_message)
flash.alert = error_message
render :siret
end
def individual_params
params.require(:individual).permit(:gender, :nom, :prenom, :birthdate)
end
def siret_params
params.require(:user).permit(:siret)
end
def commentaire_params
params.require(:commentaire).permit(:body, :file)
end

View file

@ -12,7 +12,7 @@
= render partial: "shared/dossiers/submit_is_over", locals: { dossier: @dossier }
- if !dossier_submission_is_closed?(@dossier)
= form_for @dossier, url: users_dossier_siret_informations_path(@dossier), html: { class: "form" } do |f|
= form_for current_user, url: siret_dossier_path(@dossier), html: { class: 'form', method: 'post' } do |f|
%h1 Identifier votre établissement
%p.mb-1 Merci de remplir le numéro de SIRET de votre entreprise, administration ou association pour commencer la démarche.

View file

@ -171,6 +171,8 @@ fr:
dossier_map_not_activated: "Le dossier n'a pas accès à la cartographie."
invalid_siret: "Le siret est incorrect"
procedure_not_found: "La démarche n'existe pas"
siret_unknown: 'Désolé, nous navons pas trouvé détablissement enregistré correspondant à ce numéro SIRET'
etablissement_fail: 'Désolé, nous navons pas réussi à enregistrer létablissement correspondant à ce numéro SIRET'
france_connect:
connexion: "Erreur lors de la connexion à France Connect."
extension_white_list_error: "Le format de fichier de la pièce jointe n'est pas valide."

View file

@ -0,0 +1,12 @@
fr:
activemodel:
models:
siret: 'SIRET'
errors:
models:
siret:
attributes:
siret:
format: 'Le numéro SIRET doit comporter 14 chiffres'
checksum: 'Le numéro SIRET comporte une erreur, vérifiez les chiffres composant le numéro'
invalid: 'Le numéro SIRET ne correspond pas à un établissement existant'

View file

@ -279,6 +279,7 @@ Rails.application.routes.draw do
get 'identite'
patch 'update_identite'
get 'siret'
post 'siret', to: 'dossiers#update_siret'
get 'brouillon'
patch 'brouillon', to: 'dossiers#update_brouillon'
get 'modifier', to: 'dossiers#modifier'

View file

@ -214,6 +214,127 @@ describe NewUser::DossiersController, type: :controller do
it { is_expected.to render_template(:siret) }
end
describe '#update_siret' do
let(:dossier) { create(:dossier, user: user) }
let(:siret) { params_siret.delete(' ') }
let(:siren) { siret[0..8] }
let(:api_etablissement_status) { 200 }
let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
let(:api_entreprise_status) { 200 }
let(:api_entreprise_body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
let(:api_exercices_status) { 200 }
let(:api_exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') }
let(:api_association_status) { 200 }
let(:api_association_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
def stub_api_entreprise_requests
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
.to_return(status: api_etablissement_status, body: api_etablissement_body)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
.to_return(status: api_entreprise_status, body: api_entreprise_body)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/#{siret}?.*token=/)
.to_return(status: api_exercices_status, body: api_exercices_body)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/#{siret}?.*token=/)
.to_return(status: api_association_status, body: api_association_body)
end
before do
sign_in(user)
stub_api_entreprise_requests
end
subject! { post :update_siret, params: { id: dossier.id, user: { siret: params_siret } } }
shared_examples 'SIRET informations are successfully saved' do
it do
dossier.reload
user.reload
expect(dossier.etablissement).to be_present
expect(dossier.autorisation_donnees).to be(true)
expect(user.siret).to eq(siret)
expect(response).to redirect_to(etablissement_dossier_path)
end
end
shared_examples 'the request fails with an error' do |error|
it 'doesnt save an etablissement' do
expect(dossier.reload.etablissement).to be_nil
end
it 'displays the SIRET that was sent by the user in the form' do
expect(controller.current_user.siret).to eq(siret)
end
it 'renders an error' do
expect(flash.alert).to eq(error)
expect(response).to render_template(:siret)
end
end
context 'with an invalid SIRET' do
let(:params_siret) { '000 000' }
it_behaves_like 'the request fails with an error', ['Siret Le numéro SIRET doit comporter 14 chiffres']
end
context 'with a valid SIRET' do
let(:params_siret) { '440 117 620 01530' }
context 'when API-Entreprise doesnt know this SIRET' do
let(:api_etablissement_status) { 404 }
let(:api_body_status) { '' }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end
context 'when the API returns no Entreprise' do
let(:api_entreprise_status) { 404 }
let(:api_entreprise_body) { '' }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end
context 'when the API returns no Exercices' do
let(:api_exercices_status) { 404 }
let(:api_exercices_body) { '' }
it_behaves_like 'SIRET informations are successfully saved'
it 'doesnt save the etablissement exercices' do
expect(dossier.reload.etablissement.exercices).to be_empty
end
end
context 'when the RNA doesnt have informations on the SIRET' do
let(:api_association_status) { 404 }
let(:api_association_body) { '' }
it_behaves_like 'SIRET informations are successfully saved'
it 'doesnt save the RNA informations' do
expect(dossier.reload.etablissement.association?).to be(false)
end
end
context 'when all API informations available' do
it_behaves_like 'SIRET informations are successfully saved'
it 'saves the associated informations on the etablissement' do
dossier.reload
expect(dossier.etablissement.entreprise).to be_present
expect(dossier.etablissement.exercices).to be_present
expect(dossier.etablissement.association?).to be(true)
end
end
end
end
describe '#brouillon' do
before { sign_in(user) }
let!(:dossier) { create(:dossier, user: user, autorisation_donnees: true) }