diff --git a/app/assets/javascripts/new-description.js b/app/assets/javascripts/new-description.js new file mode 100644 index 000000000..8de71855e --- /dev/null +++ b/app/assets/javascripts/new-description.js @@ -0,0 +1,36 @@ +(function() { + var showNotFound = function() { + $('.dossier-link .text-info').hide(); + $('.dossier-link .text-warning').show(); + }; + + var showData = function(data) { + $('.dossier-link .procedureLibelle').text(data.procedureLibelle); + $('.dossier-link .text-info').show(); + $('.dossier-link .text-warning').hide(); + }; + + var hideEverything = function() { + $('.dossier-link .text-info').hide(); + $('.dossier-link .text-warning').hide(); + }; + + var fetchProcedureLibelle = function(e) { + var dossierId = $(e.target).val(); + if(dossierId) { + $.get('/users/dossiers/' + dossierId + '/procedure_libelle') + .done(showData) + .fail(showNotFound); + } else { + hideEverything(); + } + }; + + var timeOut = null; + var debounceFetchProcedureLibelle = function(e) { + if(timeOut){ clearTimeout(timeOut); } + timeOut = setTimeout(function() { fetchProcedureLibelle(e); }, 300); + }; + + $(document).on('input', '[data-type=dossier-link]', debounceFetchProcedureLibelle); +})(); diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index fe4421dab..4eade942c 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -154,6 +154,13 @@ class Users::DossiersController < UsersController redirect_to url_for users_dossiers_path end + def procedure_libelle + dossier = Dossier.find(params[:dossier_id]) + render json: { procedureLibelle: dossier.procedure.libelle } + rescue ActiveRecord::RecordNotFound + render json: {}, status: 404 + end + private def check_siret diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 2dbfb2d90..dcdf0a102 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -18,7 +18,8 @@ class TypeDeChamp < ActiveRecord::Base departements: 'departements', engagement: 'engagement', header_section: 'header_section', - explication: 'explication' + explication: 'explication', + dossier_link: 'dossier_link' } belongs_to :procedure diff --git a/app/views/dossiers/_infos_dossier.html.haml b/app/views/dossiers/_infos_dossier.html.haml index 5aea3610b..954aa6856 100644 --- a/app/views/dossiers/_infos_dossier.html.haml +++ b/app/views/dossiers/_infos_dossier.html.haml @@ -34,11 +34,18 @@ - else .row .col-xs-6.depositaire-label= champ.libelle - .col-md-1.col-lg-1.col-sm-1.col-xs-1.comments-off + .col-xs-1.comments-off = "-" .col-xs-5.depositaire-info{ id: "champ-#{champ.id}-value" } - unless champ.decorate.value.blank? - = champ.decorate.value.html_safe + - if champ.type_champ == 'dossier_link' + - dossier = Dossier.find_by(id: champ.decorate.value) + - if dossier && gestionnaire_signed_in? + = link_to(dossier.procedure.libelle, backoffice_dossier_path(champ.decorate.value), target: '_blank') + - else + = dossier.nil? ? 'pas de dossier associé' : dossier.procedure.libelle + - else + = champ.decorate.value.html_safe - if @facade.dossier.mandataire_social && gestionnaire_signed_in? .mandataire_social.text-success.center diff --git a/app/views/users/description/champs/_dossier_link.html.haml b/app/views/users/description/champs/_dossier_link.html.haml new file mode 100644 index 000000000..41d6f0526 --- /dev/null +++ b/app/views/users/description/champs/_dossier_link.html.haml @@ -0,0 +1,21 @@ +- dossier = Dossier.find_by(id: champ.value) +- show_procedure_libelle = dossier ? true : false +- show_warning = !show_procedure_libelle && champ.value.present? +- procedure_libelle = dossier.nil? ? '' : dossier.procedure.libelle + +.dossier-link + %input.form-control{ name: "champs['#{ champ.id }']", + placeholder: champ.libelle, + id: "champs_#{ champ.id }", + value: champ.value, + type: 'number', + 'autocomplete' => 'off', + 'data-type' => 'dossier-link' } + + .help-block + %p.text-info{ style: show_procedure_libelle ? nil : 'display: none;' } + ce dossier répond à la procédure : + %br + %span.procedureLibelle= procedure_libelle + %p.text-warning{ style: show_warning ? nil : 'display: none;' } + ce dossier est inconnu diff --git a/app/views/users/description/champs/_render_list_champs.html.haml b/app/views/users/description/champs/_render_list_champs.html.haml index f24932516..0d1f91f76 100644 --- a/app/views/users/description/champs/_render_list_champs.html.haml +++ b/app/views/users/description/champs/_render_list_champs.html.haml @@ -41,6 +41,9 @@ - elsif champ.type_champ == 'departements' = render partial: 'users/description/champs/departements', locals: { champ: champ } + - elsif champ.type_champ == 'dossier_link' + = render partial: 'users/description/champs/dossier_link', locals: { champ: champ } + - elsif champ.type_champ == 'explication' - else diff --git a/config/locales/models/type_de_champ/fr.yml b/config/locales/models/type_de_champ/fr.yml index 06206a682..1e3414f1f 100644 --- a/config/locales/models/type_de_champ/fr.yml +++ b/config/locales/models/type_de_champ/fr.yml @@ -24,3 +24,4 @@ fr: header_section: 'Titre de section' explication: 'Explication' multiple_drop_down_list: 'Menu déroulant à choix multiples' + dossier_link: 'Lien vers un autre dossier' diff --git a/config/routes.rb b/config/routes.rb index 5811b4890..216951ace 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -94,6 +94,8 @@ Rails.application.routes.draw do post '/siret_informations' => 'dossiers#siret_informations' put '/change_siret' => 'dossiers#change_siret' + + get 'procedure_libelle' => 'dossiers#procedure_libelle' end resource :dossiers end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 6d77ce488..601b243bc 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -554,4 +554,28 @@ describe Users::DossiersController, type: :controller do end end + describe 'Get #procedure_libelle' do + let!(:dossier) { create(:dossier, procedure: procedure) } + + context 'when user is connected' do + before { sign_in user } + + context 'when the dossier exist' do + before { get :procedure_libelle, params: { dossier_id: dossier.id } } + it 'returns the procedure name' do + expect(JSON.parse(response.body)).to eq('procedureLibelle' => procedure.libelle) + end + end + + context 'when the dossier does not exist' do + before { get :procedure_libelle, params: { dossier_id: 666 } } + it { expect(response.code).to eq('404') } + end + end + + context 'when user is not connected' do + before { get :procedure_libelle, params: { dossier_id: dossier.id } } + it { expect(response.code).to eq('302') } + end + end end diff --git a/spec/views/users/description/champs/_dossier_link.html.haml_spec.rb b/spec/views/users/description/champs/_dossier_link.html.haml_spec.rb new file mode 100644 index 000000000..a61f09532 --- /dev/null +++ b/spec/views/users/description/champs/_dossier_link.html.haml_spec.rb @@ -0,0 +1,47 @@ +require 'spec_helper' + +describe 'users/description/champs/dossier_link.html.haml', type: :view do + let(:type_champ) { create(:type_de_champ_public, type_champ: :dossier_link) } + + before do + render 'users/description/champs/dossier_link.html.haml', champ: champ + end + + context 'When no dossier is provided' do + let!(:champ) { create(:champ, type_de_champ: type_champ, value: nil) } + + it 'should not display the procedure libelle' do + expect(rendered).to have_css('.text-info[style*="display: none"]') + end + + it 'should not display a warning' do + expect(rendered).to have_css('.text-warning[style*="display: none"]') + end + end + + context 'When a dossier whith a procedure is provided' do + let!(:procedure) { create(:procedure) } + let!(:dossier) { create(:dossier, procedure: procedure) } + let!(:champ) { create(:champ, type_de_champ: type_champ, value: dossier.id) } + + it 'should display the procedure libelle' do + expect(rendered).not_to have_css('.text-info[style*="display: none"]') + end + + it 'should not display a warning' do + expect(rendered).to have_css('.text-warning[style*="display: none"]') + end + end + + context 'When a unknown dossier id is provided' do + let!(:champ) { create(:champ, type_de_champ: type_champ, value: 666) } + + it 'should not display the procedure libelle' do + expect(rendered).to have_css('.text-info[style*="display: none"]') + end + + it 'should display a warning' do + expect(rendered).not_to have_css('.text-warning[style*="display: none"]') + end + end +end diff --git a/spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb b/spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb index a60d0e86b..17ca6d9d3 100644 --- a/spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb +++ b/spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb @@ -24,4 +24,18 @@ describe 'users/description/champs/render_list_champs.html.haml', type: :view do expect(rendered).to have_css('input[type=checkbox][checked]') end end + + context 'with a dossier_link' do + let(:type_champ) { create(:type_de_champ_public, type_champ: :dossier_link) } + let!(:champ) { create(:champ, type_de_champ: type_champ, value: nil) } + + before do + render 'users/description/champs/render_list_champs.html.haml', champs: Champ.all, order_place: 0 + end + + it 'should render a number input with the right data-attribute' do + expect(view).to render_template(partial: 'users/description/champs/_dossier_link', + locals: { champ: champ }) + end + end end