From 6044c120c580a46e41752101b63bfe219c25df95 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Sun, 30 Oct 2016 22:59:57 +0100 Subject: [PATCH] Active degraded mode if insee is disabled --- app/controllers/users/dossiers_controller.rb | 3 ++- app/services/dossier_service.rb | 9 +++++--- app/views/dossiers/degraded.js.erb | 13 +++++++++++ .../etapes/_etape2_degraded.html.haml | 1 + .../etapes/etape_2/_degraded.html.haml | 23 +++++++++++++++++++ lib/siade/entreprise_adapter.rb | 18 +++++++++++---- spec/lib/siade/api_spec.rb | 3 +++ spec/lib/siade/entreprise_adapter_spec.rb | 2 ++ 8 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 app/views/dossiers/degraded.js.erb create mode 100644 app/views/dossiers/etapes/_etape2_degraded.html.haml create mode 100644 app/views/dossiers/etapes/etape_2/_degraded.html.haml diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 8e175d8b2..23ca45641 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -86,7 +86,8 @@ class Users::DossiersController < UsersController end rescue RestClient::ResourceNotFound, RestClient::BadRequest errors_valid_siret - + rescue RestClient::ServiceUnavailable + render '/dossiers/degraded', formats: 'js' rescue ActiveRecord::RecordNotFound flash.alert = t('errors.messages.dossier_not_found') redirect_to url_for users_dossiers_path diff --git a/app/services/dossier_service.rb b/app/services/dossier_service.rb index 01c1fc6a0..e66b0d814 100644 --- a/app/services/dossier_service.rb +++ b/app/services/dossier_service.rb @@ -7,10 +7,14 @@ class DossierService end def dossier_informations! - @entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret) + @entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret).to_params - if @entreprise_adapter.to_params.nil? + if @entreprise_adapter[:http_code] == 404 raise RestClient::ResourceNotFound + elsif @entreprise_adapter[:http_code] == 503 || + @entreprise_adapter[:http_code] == 500 || + @entreprise_adapter[:http_code] == 400 + raise RestClient::ServiceUnavailable end @etablissement_adapter = SIADE::EtablissementAdapter.new(@siret) @@ -34,7 +38,6 @@ class DossierService @dossier end - def self.siren siret siret[0..8] end diff --git a/app/views/dossiers/degraded.js.erb b/app/views/dossiers/degraded.js.erb new file mode 100644 index 000000000..72bc9737a --- /dev/null +++ b/app/views/dossiers/degraded.js.erb @@ -0,0 +1,13 @@ +<% if flash.empty? %> +$('.row.etape.etape_2').hide(300, render_new_siret); +$('.row.etape.etape_2').slideDown(400, the_terms); +toggle_etape_1(); +<% else %> +error_form_siret('<%= invalid_siret %>'); +<% end %> + +<% flash.clear %> + +function render_new_siret(){ + $('.row.etape.etape_2').html("<%= escape_javascript(render partial: '/dossiers/etapes/etape2_degraded', locals: { facade: @facade } ) %>"); +} \ No newline at end of file diff --git a/app/views/dossiers/etapes/_etape2_degraded.html.haml b/app/views/dossiers/etapes/_etape2_degraded.html.haml new file mode 100644 index 000000000..df0db13b6 --- /dev/null +++ b/app/views/dossiers/etapes/_etape2_degraded.html.haml @@ -0,0 +1 @@ += render partial: 'dossiers/etapes/etape_2/degraded' \ No newline at end of file diff --git a/app/views/dossiers/etapes/etape_2/_degraded.html.haml b/app/views/dossiers/etapes/etape_2/_degraded.html.haml new file mode 100644 index 000000000..eb8324425 --- /dev/null +++ b/app/views/dossiers/etapes/etape_2/_degraded.html.haml @@ -0,0 +1,23 @@ +.etape.etapes_menu.col-md-3.col-lg-3 + %h3 + Mes informations + %br + +.etape.etapes_informations.col-md-9.col-lg-9 + .row + #recap_info_entreprise + %h3.text-warning + INSEE Indisponible + + %p + %b + Notre partenaire l'INSEE est actuellement indisponible. Nous ne pouvons donc pas récupérer les informations de votre entreprise. Une fois de retour, vos informations seront automatiquement ajoutées à votre dossier. + + %br + = form_for @facade.dossier, url: { controller: '/users/dossiers', action: :update } do |f| + = f.hidden_field :id + %label{ style:'font-weight:normal' } + = f.check_box :autorisation_donnees + J'autorise les décideurs publics à vérifier les informations de mon organisation auprès des administrations concernées. Ces informations resteront strictement confidentielles. + %br + = f.submit 'Etape suivante', class: "btn btn btn-info", style: 'float:right', id: 'etape_suivante', disabled: :disabled diff --git a/lib/siade/entreprise_adapter.rb b/lib/siade/entreprise_adapter.rb index d926ae189..50c5e1774 100644 --- a/lib/siade/entreprise_adapter.rb +++ b/lib/siade/entreprise_adapter.rb @@ -4,9 +4,19 @@ class SIADE::EntrepriseAdapter end def data_source - @data_source ||= JSON.parse(SIADE::API.entreprise(@siren), symbolize_names: true) - rescue - @data_source = nil + @data_source ||= JSON.parse(SIADE::API.entreprise(@siren), symbolize_names: true).merge(http_code: 200) + rescue RestClient::ServiceUnavailable + @data_source = {http_code: 503} + rescue RestClient::ResourceNotFound + @data_source = {http_code: 404} + rescue RestClient::InternalServerError + @data_source = {http_code: 500} + rescue => e + if e.http_code == 400 + @data_source = {http_code: 400} + else + @data_source = nil + end end def to_params @@ -19,7 +29,7 @@ class SIADE::EntrepriseAdapter params rescue - nil + data_source end def attr_to_fetch diff --git a/spec/lib/siade/api_spec.rb b/spec/lib/siade/api_spec.rb index a82a3e4d7..747da2e03 100644 --- a/spec/lib/siade/api_spec.rb +++ b/spec/lib/siade/api_spec.rb @@ -15,6 +15,9 @@ describe SIADE::API do it 'raises RestClient::ResourceNotFound' do expect { subject }.to raise_error(RestClient::ResourceNotFound) end + + it { expect(subject.http_code).to eq status } + end context 'when siret exist' do let(:siren) { '418166096' } diff --git a/spec/lib/siade/entreprise_adapter_spec.rb b/spec/lib/siade/entreprise_adapter_spec.rb index 3cfd96673..55f1bf597 100644 --- a/spec/lib/siade/entreprise_adapter_spec.rb +++ b/spec/lib/siade/entreprise_adapter_spec.rb @@ -12,6 +12,8 @@ describe SIADE::EntrepriseAdapter do expect(subject).to be_an_instance_of(Hash) end + it { expect(described_class.new('418166096').data_source[:http_code]).to eq 200 } + context 'Attributs Entreprises' do it 'L\'entreprise contient bien un siren' do expect(subject[:siren]).to eq('418166096')