diff --git a/app/controllers/new_gestionnaire/procedures_controller.rb b/app/controllers/new_gestionnaire/procedures_controller.rb index 0dcd55db4..ab78a563b 100644 --- a/app/controllers/new_gestionnaire/procedures_controller.rb +++ b/app/controllers/new_gestionnaire/procedures_controller.rb @@ -1,6 +1,7 @@ module NewGestionnaire class ProceduresController < GestionnaireController before_action :ensure_ownership!, except: [:index] + before_action :redirect_to_avis_if_needed, only: [:index] def index @procedures = current_gestionnaire.procedures.order(archived_at: :desc, published_at: :desc) @@ -76,5 +77,11 @@ module NewGestionnaire redirect_to root_path end end + + def redirect_to_avis_if_needed + if current_gestionnaire.procedures.count == 0 && current_gestionnaire.avis.count > 0 + redirect_to avis_index_path + end + end end end diff --git a/spec/controllers/new_gestionnaire/procedures_controller_spec.rb b/spec/controllers/new_gestionnaire/procedures_controller_spec.rb index 1a581eed9..30f70803a 100644 --- a/spec/controllers/new_gestionnaire/procedures_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/procedures_controller_spec.rb @@ -42,6 +42,47 @@ describe NewGestionnaire::ProceduresController, type: :controller do end end + describe "before_action: redirect_to_avis_if_needed" do + it "is present" do + before_actions = NewGestionnaire::ProceduresController + ._process_action_callbacks + .find_all{|process_action_callbacks| process_action_callbacks.kind == :before} + .map(&:filter) + + expect(before_actions).to include(:redirect_to_avis_if_needed) + end + end + + describe "redirect_to_avis_if_needed" do + let(:gestionnaire) { create(:gestionnaire) } + + before do + expect(@controller).to receive(:current_gestionnaire).at_least(:once).and_return(gestionnaire) + allow(@controller).to receive(:redirect_to) + end + + context "when a gestionnaire has some procedures" do + let!(:some_procedure) { create(:procedure, gestionnaires: [gestionnaire]) } + + before { @controller.send(:redirect_to_avis_if_needed) } + + it "does not redirects nor flash" do + expect(@controller).not_to have_received(:redirect_to) + end + end + + context "when a gestionnaire has no procedure and some avis" do + before do + Avis.create!(dossier: create(:dossier), claimant: create(:gestionnaire), gestionnaire: gestionnaire) + @controller.send(:redirect_to_avis_if_needed) + end + + it "redirects avis" do + expect(@controller).to have_received(:redirect_to).with(avis_index_path) + end + end + end + describe "#index" do let(:gestionnaire) { create(:gestionnaire) } subject { get :index }