From b6e773f9fa28393cf30bc5fad89ee28e1e2dafa9 Mon Sep 17 00:00:00 2001 From: mfo Date: Fri, 29 Nov 2024 11:52:36 +0100 Subject: [PATCH] feat(Instructeurs::Dossiers#next/prev): add route to redirect user from a dossier id to either a next dossier, either a previous --- .../instructeurs/dossiers_controller.rb | 28 +++++++++ config/routes.rb | 2 + .../instructeurs/dossiers_controller_spec.rb | 62 +++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 0d1bf2600..bd8d9f203 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -390,8 +390,36 @@ module Instructeurs @pieces_jointes_seen_at = current_instructeur.follows.find_by(dossier: dossier)&.pieces_jointes_seen_at end + def next + navigate_throw_dossier_list do |cache| + cache.next_dossier_id(from_id: params[:dossier_id]) + end + end + + def previous + navigate_throw_dossier_list do |cache| + cache.previous_dossier_id(from_id: params[:dossier_id]) + end + end + private + def navigate_throw_dossier_list + dossier = dossier_scope.find(params[:dossier_id]) + procedure_presentation = current_instructeur.procedure_presentation_for_procedure_id(dossier.procedure.id) + cache = Cache::ProcedureDossierPagination.new(procedure_presentation:, statut: params[:statut]) + + next_or_previous_dossier_id = yield(cache) + + if next_or_previous_dossier_id + redirect_to instructeur_dossier_path(procedure_id: procedure.id, dossier_id: next_or_previous_dossier_id, statut: params[:statut]) + else + redirect_back fallback_location: instructeur_dossier_path(procedure_id: procedure.id, dossier_id: dossier.id, statut: params[:statut]), alert: "Une erreur est survenue" + end + rescue ActiveRecord::RecordNotFound + redirect_to instructeur_procedure_path(procedure_id: procedure.id), alert: "Une erreur est survenue" + end + def dossier_scope if action_name == 'update_annotations' Dossier diff --git a/config/routes.rb b/config/routes.rb index 73014cab8..aba3a0e80 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -485,6 +485,8 @@ Rails.application.routes.draw do resources :dossiers, only: [:show, :destroy], param: :dossier_id, path: "(:statut)/dossiers", defaults: { statut: 'a-suivre' } do member do resources :commentaires, only: [:destroy] + get 'next' + get 'previous' post 'repousser-expiration' => 'dossiers#extend_conservation' post 'repousser-expiration-and-restore' => 'dossiers#extend_conservation_and_restore' post 'dossier_labels' => 'dossiers#dossier_labels' diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index aa0f53c4a..7ca846868 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -984,6 +984,68 @@ describe Instructeurs::DossiersController, type: :controller do end end + describe 'navigation accross next/prev dossiers' do + let(:dossier_id) { dossier.id } + let(:statut) { 'a-suivre' } + let(:previous_dossier) { create(:dossier, :en_construction, procedure:) } + let(:next_dossier) { create(:dossier, :en_construction, procedure:) } + let(:cached_ids) { [previous_dossier, dossier, next_dossier].map(&:id) } + before do + cache = Cache::ProcedureDossierPagination.new(procedure_presentation: double(procedure:, instructeur:), statut:) + cache.save_context(incoming_page: 1, ids: cached_ids) + end + + context 'when nexting' do + subject { get :next, params: { procedure_id: procedure.id, dossier_id: from_id, statut: } } + + context 'when their is a next id' do + let(:from_id) { dossier.id } + it { is_expected.to redirect_to(instructeur_dossier_path(procedure_id: procedure.id, dossier_id: next_dossier.id)) } + end + + context 'when their is not next id (en of list)' do + let(:from_id) { cached_ids.last } + it 'redirect on fallback location being current dossier and flashes an error' do + expect(subject).to redirect_to(instructeur_dossier_path(procedure_id: procedure.id, dossier_id: from_id)) + expect(flash.alert).to eq("Une erreur est survenue") + end + end + + context 'when id does not exists' do + let(:from_id) { 'kthxbye' } + it 'redirect on fallback location being current dossier and flashes an error' do + expect(subject).to redirect_to(instructeur_procedure_path(procedure_id: procedure.id)) + expect(flash.alert).to eq("Une erreur est survenue") + end + end + end + + context 'when previousing' do + subject { get :previous, params: { procedure_id: procedure.id, dossier_id: from_id, statut: } } + + context 'when their is a previous id' do + let(:from_id) { dossier.id } + it { is_expected.to redirect_to(instructeur_dossier_path(procedure_id: procedure.id, dossier_id: previous_dossier.id)) } + end + + context 'when their is not previous id (before list)' do + let(:from_id) { cached_ids.first } + it 'redirect on fallback location being current dossier and flashes an error' do + expect(subject).to redirect_to(instructeur_dossier_path(procedure_id: procedure.id, dossier_id: from_id)) + expect(flash.alert).to eq("Une erreur est survenue") + end + end + + context 'when id does not exists' do + let(:from_id) { 'kthxbye' } + it 'redirect on fallback location being current dossier and flashes an error' do + expect(subject).to redirect_to(instructeur_procedure_path(procedure_id: procedure.id)) + expect(flash.alert).to eq("Une erreur est survenue") + end + end + end + end + describe "#update_annotations" do let(:procedure) do create(:procedure, :published, types_de_champ_public:, types_de_champ_private:, instructeurs: instructeurs)