diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 4dcd7f287..fa2d26f8d 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -17,20 +17,21 @@ class Users::DescriptionController < UsersController acc end + unless @dossier.can_be_initiated? + flash[:alert] = t('errors.messages.procedure_archived') + end + rescue ActiveRecord::RecordNotFound flash.alert = t('errors.messages.dossier_not_found') redirect_to url_for(root_path) end - # def error - # show - # flash.now.alert = 'Un ou plusieurs attributs obligatoires sont manquants ou incorrects.' - # render 'show' - # end - - def create + def update @dossier = current_user_dossier @procedure = @dossier.procedure + + return head :forbidden unless @dossier.can_be_initiated? + @champs = @dossier.ordered_champs mandatory = true diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 5877e87fd..8031da67b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -304,4 +304,8 @@ class Dossier < ActiveRecord::Base def invite_by_user? email (invites_user.pluck :email).include? email end + + def can_be_initiated? + !(procedure.archived && draft?) + end end diff --git a/app/views/users/description/_show.html.haml b/app/views/users/description/_show.html.haml index f216b3d7a..14b17b160 100644 --- a/app/views/users/description/_show.html.haml +++ b/app/views/users/description/_show.html.haml @@ -15,7 +15,7 @@ = @dossier.procedure.libelle -#TODO use form_for - = form_tag(url_for({controller: 'users/description', action: :create, dossier_id: @dossier.id}), class: 'form', method: 'POST', multipart: true) do + = form_tag(url_for({controller: 'users/description', action: :update, dossier_id: @dossier.id}), class: 'form', method: 'POST', multipart: true) do -unless @champs.nil? #liste_champs =render partial: 'users/description/champs', locals:{private: false} @@ -41,16 +41,16 @@ Vous pourrez dans tous les cas les compléter plus tard si vous ne les possédez pas de suite. - -route = Rails.application.routes.recognize_path(request.referrer) + -route = Rails.application.routes.recognize_path(request.referrer) # WTF ? - unless route[:controller].match('admin') %div{style: 'text-align:right'} %h6 Tous les champs portant un * sont obligatoires. - -if !@dossier.draft? - =render partial: '/layouts/modifications_terminees' - -else - = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: %w(btn btn btn-success), style: 'float:right', data: { disable_with: 'Soumettre votre dossier', submit: true} - = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: %w(btn btn-xs btn-default), style: 'float:right; margin-right: 10px; margin-top: 6px', data: {disable_with: 'Enregistrer un brouillon', submit: true} - - %br - %br + - if !@dossier.can_be_initiated? + .alert.alert-danger + = t('errors.messages.procedure_archived') + - elsif !@dossier.draft? + = render partial: '/layouts/modifications_terminees' + - else + = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: 'btn btn btn-success', style: 'float:right', disabled: @procedure.archived?, data: { disable_with: 'Soumettre votre dossier', submit: true} + = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: 'btn btn-xs btn-default', style: 'float:right; margin-right: 10px; margin-top: 6px', disabled: @procedure.archived?, data: {disable_with: 'Enregistrer un brouillon', submit: true} diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 795c51d50..5f8a4ad9e 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -147,6 +147,7 @@ fr: 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." + procedure_archived: "Cette démarche en ligne a été fermée, il n'est plus possible de déposer de dossier." datetime: distance_in_words: diff --git a/config/routes.rb b/config/routes.rb index 9735cadae..2f96e0031 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,9 +63,8 @@ Rails.application.routes.draw do resources :dossiers do get '/add_siret' => 'dossiers/add_siret#show' - get '/description' => 'description#show' - # get '/description/error' => 'description#error' - post 'description' => 'description#create' + get 'description' => 'description#show' + post 'description' => 'description#update' patch 'pieces_justificatives' => 'description#pieces_justificatives' diff --git a/spec/controllers/users/description_controller_shared_example.rb b/spec/controllers/users/description_controller_shared_example.rb index fa90d0824..f74eb29b1 100644 --- a/spec/controllers/users/description_controller_shared_example.rb +++ b/spec/controllers/users/description_controller_shared_example.rb @@ -17,12 +17,27 @@ shared_examples 'description_controller_spec' do context 'when all is ok' do before do dossier.entreprise = create :entreprise + get :show, params: {dossier_id: dossier_id} end it 'returns http success' do - get :show, params: {dossier_id: dossier_id} expect(response).to have_http_status(:success) end + + context 'procedure is archived' do + render_views + let(:archived) { true } + + it { expect(response).to have_http_status(:success) } + it { expect(response.body).to_not have_content(I18n.t('errors.messages.procedure_archived')) } + + context 'dossier is a draft' do + let(:state) { 'draft' } + + it { expect(response).to have_http_status(:success) } + it { expect(response.body).to have_content(I18n.t('errors.messages.procedure_archived')) } + end + end end it 'redirection vers start si mauvais dossier ID' do @@ -90,7 +105,7 @@ shared_examples 'description_controller_spec' do end end - describe 'POST #create' do + describe 'POST #update' do let(:timestamp) { Time.now } let(:description) { 'Description de test Coucou, je suis un saut à la ligne Je suis un double saut la ligne.' } @@ -98,7 +113,7 @@ shared_examples 'description_controller_spec' do describe 'Premier enregistrement des données' do let(:submit) { {nouveaux: 'nouveaux'} } - subject { post :create, params: {dossier_id: dossier_id, submit: submit} } + subject { post :update, params: {dossier_id: dossier_id, submit: submit} } before do dossier.draft! @@ -130,7 +145,7 @@ shared_examples 'description_controller_spec' do context 'En train de manipuler un dossier non brouillon' do before do dossier.initiated! - post :create, params: {dossier_id: dossier_id} + post :update, params: {dossier_id: dossier_id} dossier.reload end @@ -145,7 +160,7 @@ shared_examples 'description_controller_spec' do end context 'Quand la procédure accepte les CERFA' do - subject { post :create, params: {dossier_id: dossier_id, + subject { post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} } it 'Notification interne is create' do @@ -154,7 +169,7 @@ shared_examples 'description_controller_spec' do context 'Sauvegarde du CERFA PDF', vcr: {cassette_name: 'controllers_users_description_controller_save_cerfa'} do before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} dossier.reload end @@ -181,7 +196,7 @@ shared_examples 'description_controller_spec' do let(:cerfas) { Cerfa.where(dossier_id: dossier_id) } before do - post :create, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} + post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} end it "il y a deux CERFA PDF pour ce dossier" do @@ -195,7 +210,7 @@ shared_examples 'description_controller_spec' do context 'Sauvegarde du CERFA PDF' do let!(:procedure) { create(:procedure) } before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} dossier.reload end @@ -214,7 +229,7 @@ shared_examples 'description_controller_spec' do let(:dossier_minute_value) { '00' } before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, champs: { "'#{dossier.champs.first.id}'" => dossier_champs_first, "'#{dossier.champs.second.id}'" => dossier_date_value @@ -254,7 +269,7 @@ shared_examples 'description_controller_spec' do context 'Sauvegarde des pièces justificatives', vcr: {cassette_name: 'controllers_users_description_controller_sauvegarde_pj'} do let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids } before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, 'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0, 'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1} dossier.reload @@ -264,7 +279,7 @@ shared_examples 'description_controller_spec' do it 'ClamavService safe_file? is call' do expect(ClamavService).to receive(:safe_file?).twice - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, 'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0, 'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1} end @@ -281,6 +296,22 @@ shared_examples 'description_controller_spec' do it { expect(subject.user).to eq user } end end + + context 'La procédure est archivée' do + let(:archived) { true } + + before do + post :update, params: { dossier_id: dossier.id } + end + + it { expect(response.status).to eq(302) } + + context 'Le dossier est en brouillon' do + let(:state) { 'draft' } + + it { expect(response.status).to eq(403) } + end + end end describe 'POST #pieces_justificatives', vcr: {cassette_name: 'controllers_users_description_controller_pieces_justificatives'} do diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index ace2b9f1b..0cd1c0414 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -5,9 +5,11 @@ require 'controllers/users/description_controller_shared_example' describe Users::DescriptionController, type: :controller, vcr: {cassette_name: 'controllers_users_description_controller'} do let(:owner_user) { create(:user) } let(:invite_by_user) { create :user, email: 'invite@plop.com' } + let(:archived) { false } + let(:state) { 'initiated' } - let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true) } - let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: 'initiated') } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true, archived: archived) } + let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: state) } let(:dossier_id) { dossier.id } let(:bad_dossier_id) { Dossier.count + 10000 }