From 6de1e2ec586774895563297e9d0fa54a5749e118 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 11 Jul 2019 11:37:59 +0200 Subject: [PATCH 1/3] Fix bug when an instructeur closes a dossier already closed --- .../gestionnaires/dossiers_controller.rb | 25 +++++++++++-------- .../gestionnaires/dossiers_controller_spec.rb | 15 +++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/controllers/gestionnaires/dossiers_controller.rb b/app/controllers/gestionnaires/dossiers_controller.rb index 764f24b6c..f8d02a855 100644 --- a/app/controllers/gestionnaires/dossiers_controller.rb +++ b/app/controllers/gestionnaires/dossiers_controller.rb @@ -3,6 +3,7 @@ module Gestionnaires include ActionView::Helpers::NumberHelper include ActionView::Helpers::TextHelper include CreateAvisConcern + include DossierHelper after_action :mark_demande_as_read, only: :show after_action :mark_messagerie_as_read, only: [:messagerie, :create_commentaire] @@ -108,16 +109,20 @@ module Gestionnaires motivation = params[:dossier] && params[:dossier][:motivation] justificatif = params[:dossier] && params[:dossier][:justificatif_motivation] - case params[:process_action] - when "refuser" - dossier.refuser!(current_gestionnaire, motivation, justificatif) - flash.notice = "Dossier considéré comme refusé." - when "classer_sans_suite" - dossier.classer_sans_suite!(current_gestionnaire, motivation, justificatif) - flash.notice = "Dossier considéré comme sans suite." - when "accepter" - dossier.accepter!(current_gestionnaire, motivation, justificatif) - flash.notice = "Dossier traité avec succès." + if dossier.termine? + flash.notice = "Le dossier est déjà #{dossier_display_state(dossier, lower: true)}" + else + case params[:process_action] + when "refuser" + dossier.refuser!(current_gestionnaire, motivation, justificatif) + flash.notice = "Dossier considéré comme refusé." + when "classer_sans_suite" + dossier.classer_sans_suite!(current_gestionnaire, motivation, justificatif) + flash.notice = "Dossier considéré comme sans suite." + when "accepter" + dossier.accepter!(current_gestionnaire, motivation, justificatif) + flash.notice = "Dossier traité avec succès." + end end render partial: 'state_button_refresh', locals: { dossier: dossier } diff --git a/spec/controllers/gestionnaires/dossiers_controller_spec.rb b/spec/controllers/gestionnaires/dossiers_controller_spec.rb index 532913960..26365a6c7 100644 --- a/spec/controllers/gestionnaires/dossiers_controller_spec.rb +++ b/spec/controllers/gestionnaires/dossiers_controller_spec.rb @@ -319,6 +319,21 @@ describe Gestionnaires::DossiersController, type: :controller do it { expect(subject.body).to include('.state-button') } end end + + context 'when a dossier is already closed' do + let(:dossier) { create(:dossier, :accepte, procedure: procedure) } + + before { allow(dossier).to receive(:accepter!) } + + subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: 'js' } + + it 'does not close it again' do + subject + + expect(dossier).not_to have_received(:accepter!) + expect(response).to have_http_status(:ok) + end + end end describe "#create_commentaire" do From 5f39d3eac8c156d72796a19d2c8bf36ab0b6c629 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 11 Jul 2019 11:47:37 +0200 Subject: [PATCH 2/3] Fix bug when instruction repasse en construction twice --- .../gestionnaires/dossiers_controller.rb | 8 +++++-- .../gestionnaires/dossiers_controller_spec.rb | 22 ++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/controllers/gestionnaires/dossiers_controller.rb b/app/controllers/gestionnaires/dossiers_controller.rb index f8d02a855..6584b3af3 100644 --- a/app/controllers/gestionnaires/dossiers_controller.rb +++ b/app/controllers/gestionnaires/dossiers_controller.rb @@ -92,8 +92,12 @@ module Gestionnaires end def repasser_en_construction - dossier.repasser_en_construction!(current_gestionnaire) - flash.notice = 'Dossier repassé en construction.' + if dossier.en_construction? + flash.notice = 'Le dossier est déjà en construction.' + else + dossier.repasser_en_construction!(current_gestionnaire) + flash.notice = 'Dossier repassé en construction.' + end render partial: 'state_button_refresh', locals: { dossier: dossier } end diff --git a/spec/controllers/gestionnaires/dossiers_controller_spec.rb b/spec/controllers/gestionnaires/dossiers_controller_spec.rb index 26365a6c7..eeee2ee5d 100644 --- a/spec/controllers/gestionnaires/dossiers_controller_spec.rb +++ b/spec/controllers/gestionnaires/dossiers_controller_spec.rb @@ -128,21 +128,27 @@ describe Gestionnaires::DossiersController, type: :controller do end describe '#repasser_en_construction' do + let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) } + before do - dossier.en_instruction! sign_in gestionnaire + post :repasser_en_construction, + params: { procedure_id: procedure.id, dossier_id: dossier.id }, + format: 'js' end - subject { post :repasser_en_construction, params: { procedure_id: procedure.id, dossier_id: dossier.id }, format: 'js' } + it { expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) } + it { expect(response).to have_http_status(:ok) } + it { expect(response.body).to include('.state-button') } - it 'change state to en_construction' do - subject + context 'when the dossier has already been put en_construction' do + let(:dossier) { create(:dossier, :en_construction, procedure: procedure) } - dossier.reload - expect(dossier.state).to eq(Dossier.states.fetch(:en_construction)) + it 'warns about the error, but doesn’t raise' do + expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) + expect(response).to have_http_status(:ok) + end end - - it { expect(subject.body).to include('.state-button') } end describe '#terminer' do From 768a298c2d351bacb1f747751652050627dda6ff Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 11 Jul 2019 11:52:24 +0200 Subject: [PATCH 3/3] Same for go back in instruction --- .../gestionnaires/dossiers_controller.rb | 8 +++++-- .../gestionnaires/dossiers_controller_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/controllers/gestionnaires/dossiers_controller.rb b/app/controllers/gestionnaires/dossiers_controller.rb index 6584b3af3..d58fed8fb 100644 --- a/app/controllers/gestionnaires/dossiers_controller.rb +++ b/app/controllers/gestionnaires/dossiers_controller.rb @@ -103,8 +103,12 @@ module Gestionnaires end def repasser_en_instruction - flash.notice = "Le dossier #{dossier.id} a été repassé en instruction." - dossier.repasser_en_instruction!(current_gestionnaire) + if dossier.en_instruction? + flash.notice = 'Le dossier est déjà en instruction.' + else + flash.notice = "Le dossier #{dossier.id} a été repassé en instruction." + dossier.repasser_en_instruction!(current_gestionnaire) + end render partial: 'state_button_refresh', locals: { dossier: dossier } end diff --git a/spec/controllers/gestionnaires/dossiers_controller_spec.rb b/spec/controllers/gestionnaires/dossiers_controller_spec.rb index eeee2ee5d..cbf5b775a 100644 --- a/spec/controllers/gestionnaires/dossiers_controller_spec.rb +++ b/spec/controllers/gestionnaires/dossiers_controller_spec.rb @@ -151,6 +151,30 @@ describe Gestionnaires::DossiersController, type: :controller do end end + describe '#repasser_en_instruction' do + let(:dossier) { create(:dossier, :refuse, procedure: procedure) } + + before do + sign_in gestionnaire + post :repasser_en_instruction, + params: { procedure_id: procedure.id, dossier_id: dossier.id }, + format: 'js' + end + + it { expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_instruction)) } + it { expect(response).to have_http_status(:ok) } + it { expect(response.body).to include('.state-button') } + + context 'when the dossier has already been put en_instruction' do + let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) } + + it 'warns about the error, but doesn’t raise' do + expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_instruction)) + expect(response).to have_http_status(:ok) + end + end + end + describe '#terminer' do context "with refuser" do before do