feat(Instructeurs::Dossiers#next/prev): add route to redirect user from a dossier id to either a next dossier, either a previous

This commit is contained in:
mfo 2024-11-29 11:52:36 +01:00
parent a8e96a843c
commit b6e773f9fa
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 92 additions and 0 deletions

View file

@ -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

View file

@ -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'

View file

@ -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)