From 0dcdcb5643eaa0604ebf597ff24af5c5eadcf167 Mon Sep 17 00:00:00 2001 From: Lisa Durand Date: Mon, 15 Jul 2024 18:02:54 +0200 Subject: [PATCH] add column hidden_by_expired_at --- .../instructeurs/dossiers_controller.rb | 2 +- app/controllers/users/dossiers_controller.rb | 9 +-- app/models/batch_operation.rb | 2 +- app/models/dossier.rb | 16 +++-- .../users/dossiers/_dossiers_list.html.haml | 4 +- ...09_add_hidden_by_expired_at_to_dossiers.rb | 5 ++ db/schema.rb | 1 + .../instructeurs/dossiers_controller_spec.rb | 67 ++++++++++++++++--- spec/factories/dossier.rb | 3 +- spec/models/dossier_spec.rb | 12 ++-- .../expired_dossiers_deletion_service_spec.rb | 20 +++--- spec/system/users/list_dossiers_spec.rb | 4 +- 12 files changed, 99 insertions(+), 46 deletions(-) create mode 100644 db/migrate/20240715144209_add_hidden_by_expired_at_to_dossiers.rb diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index e1192274c..3dc179258 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -18,7 +18,7 @@ module Instructeurs after_action :mark_annotations_privees_as_read, only: [:annotations_privees, :update_annotations] def extend_conservation - dossier.extend_conservation(1.month) + dossier.extend_conservation(1.month, current_instructeur) flash[:notice] = t('views.instructeurs.dossiers.archived_dossier') redirect_back(fallback_location: instructeur_dossier_path(@dossier.procedure, @dossier)) end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 1ea973ece..68d284040 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -57,7 +57,7 @@ module Users @user_dossiers = current_user.dossiers.state_not_termine.merge(@dossiers_visibles) @dossiers_traites = current_user.dossiers.state_termine.merge(@dossiers_visibles) @dossiers_invites = current_user.dossiers_invites.merge(@dossiers_visibles) - @dossiers_supprimes_recemment = current_user.dossiers.hidden_by_user.merge(ordered_dossiers) + @dossiers_supprimes_recemment = (current_user.dossiers.hidden_by_user.or(current_user.dossiers.hidden_by_expired)).merge(ordered_dossiers) @dossier_transferes = @dossiers_visibles.where(dossier_transfer_id: DossierTransfer.for_email(current_user.email)) @dossiers_close_to_expiration = current_user.dossiers.close_to_expiration.merge(@dossiers_visibles) @dossiers_supprimes_definitivement = deleted_dossiers @@ -254,12 +254,7 @@ module Users end def extend_conservation - dossier.extend_conservation(dossier.procedure.duree_conservation_dossiers_dans_ds.months) - - if dossier.hidden_by_reason == 'expired' - dossier.update!(hidden_by_administration_at: nil, hidden_by_user_at: nil, hidden_by_reason: nil) - end - + dossier.extend_conservation(dossier.procedure.duree_conservation_dossiers_dans_ds.months, current_user) flash[:notice] = t('views.users.dossiers.archived_dossier', duree_conservation_dossiers_dans_ds: dossier.procedure.duree_conservation_dossiers_dans_ds) redirect_back(fallback_location: dossier_path(@dossier)) end diff --git a/app/models/batch_operation.rb b/app/models/batch_operation.rb index f6abad9b1..5c9503ec4 100644 --- a/app/models/batch_operation.rb +++ b/app/models/batch_operation.rb @@ -92,7 +92,7 @@ class BatchOperation < ApplicationRecord when BatchOperation.operations.fetch(:follow) instructeur.follow(dossier) when BatchOperation.operations.fetch(:repousser_expiration) - dossier.extend_conservation(1.month) + dossier.extend_conservation(1.month, instructeur) when BatchOperation.operations.fetch(:repasser_en_construction) dossier.repasser_en_construction!(instructeur: instructeur) when BatchOperation.operations.fetch(:unfollow) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 3f0582f77..0c5a74939 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -222,10 +222,11 @@ class Dossier < ApplicationRecord scope :hidden_by_user, -> { where.not(hidden_by_user_at: nil) } scope :hidden_by_administration, -> { where.not(hidden_by_administration_at: nil) } scope :hidden_by_expired, -> { where(hidden_by_reason: 'expired') } - scope :visible_by_user, -> { where(for_procedure_preview: false).where(hidden_by_user_at: nil, editing_fork_origin_id: nil) } + scope :visible_by_user, -> { where(for_procedure_preview: false).where(hidden_by_user_at: nil, editing_fork_origin_id: nil).where(hidden_by_expired_at: nil) } scope :visible_by_administration, -> { state_not_brouillon .where(hidden_by_administration_at: nil) + .where(hidden_by_expired_at: nil) .merge(visible_by_user.or(state_not_en_construction)) } scope :visible_by_user_or_administration, -> { visible_by_user.or(visible_by_administration) } @@ -415,7 +416,7 @@ class Dossier < ApplicationRecord when 'tous' visible_by_administration.all_state when 'supprimes_recemment' - hidden_by_administration + hidden_by_administration.state_termine.or(hidden_by_expired) when 'archives' visible_by_administration.archived when 'expirant' @@ -679,14 +680,15 @@ class Dossier < ApplicationRecord brouillon? || en_construction? end - def extend_conservation(conservation_extension) + def extend_conservation(conservation_extension, author) update(conservation_extension: self.conservation_extension + conservation_extension, brouillon_close_to_expiration_notice_sent_at: nil, en_construction_close_to_expiration_notice_sent_at: nil, termine_close_to_expiration_notice_sent_at: nil) if hidden_by_reason == 'expired' - update(hidden_by_administration_at: nil, hidden_by_user_at: nil, hidden_by_reason: nil) + update(hidden_by_expired_at: nil, hidden_by_reason: nil) + restore(author) end end @@ -854,7 +856,7 @@ class Dossier < ApplicationRecord update(hidden_by_user_at: Time.zone.now, dossier_transfer_id: nil, hidden_by_reason: reason) log_dossier_operation(author, :supprimer, self) elsif author_is_automatic(author) && can_be_deleted_by_automatic?(reason) - update(hidden_by_administration_at: Time.zone.now, hidden_by_user_at: Time.zone.now, hidden_by_reason: reason) + update(hidden_by_expired_at: Time.zone.now, hidden_by_reason: reason) log_automatic_dossier_operation(:supprimer, self) else raise "Unauthorized dossier hide attempt Dossier##{id} by #{author} for reason #{reason}" @@ -879,6 +881,10 @@ class Dossier < ApplicationRecord if !hidden_by_user? && !hidden_by_administration? update(hidden_by_reason: nil) + elsif hidden_by_user? + update(hidden_by_reason: :user_request) + elsif hidden_by_administration? + update(hidden_by_reason: :instructeur_request) end log_dossier_operation(author, :restaurer, self) diff --git a/app/views/users/dossiers/_dossiers_list.html.haml b/app/views/users/dossiers/_dossiers_list.html.haml index 214c0e411..827581775 100644 --- a/app/views/users/dossiers/_dossiers_list.html.haml +++ b/app/views/users/dossiers/_dossiers_list.html.haml @@ -18,7 +18,7 @@ - if dossier.hidden_by_reason == 'expired' %p.fr-icon--sm.fr-icon-delete-line - = t('views.users.dossiers.dossiers_list.deleted_by_automatic', date: l(dossier.hidden_by_user_at.to_date)) + = t('views.users.dossiers.dossiers_list.deleted_by_automatic', date: l(dossier.hidden_by_expired_at.to_date)) - elsif dossier.hidden_by_user? %p.fr-icon--sm.fr-icon-delete-line = t('views.users.dossiers.dossiers_list.deleted_by_user', date: l(dossier.hidden_by_user_at.to_date)) @@ -113,7 +113,7 @@ - else - = render(partial: 'users/dossiers/show/print_dossier', locals: { dossier: dossier }) + = render(partial: 'users/dossiers/show/download_dossier', locals: { dossier: dossier }) = paginate dossiers, views_prefix: 'shared' diff --git a/db/migrate/20240715144209_add_hidden_by_expired_at_to_dossiers.rb b/db/migrate/20240715144209_add_hidden_by_expired_at_to_dossiers.rb new file mode 100644 index 000000000..870e0a51a --- /dev/null +++ b/db/migrate/20240715144209_add_hidden_by_expired_at_to_dossiers.rb @@ -0,0 +1,5 @@ +class AddHiddenByExpiredAtToDossiers < ActiveRecord::Migration[7.0] + def change + add_column :dossiers, :hidden_by_expired_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index ba11bc0a7..9239db004 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -469,6 +469,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_07_16_091043) do t.datetime "groupe_instructeur_updated_at", precision: nil t.datetime "hidden_at", precision: nil t.datetime "hidden_by_administration_at", precision: nil + t.datetime "hidden_by_expired_at" t.string "hidden_by_reason" t.datetime "hidden_by_user_at", precision: nil t.datetime "identity_updated_at", precision: nil diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index ac7d64a87..a9deb4723 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -1308,16 +1308,67 @@ describe Instructeurs::DossiersController, type: :controller do end describe '#extend_conservation and restore' do - before do - dossier.update(hidden_by_administration_at: 1.hour.ago, hidden_by_user_at: 1.hour.ago, hidden_by_reason: 'expired') - end subject { post :extend_conservation, params: { procedure_id: procedure.id, dossier_id: dossier.id } } - it "puts hidden_by to nil and extends conservation_extension by 1 month" do - subject - expect(dossier.reload.hidden_by_administration_at).to eq(nil) - expect(dossier.reload.hidden_by_user_at).to eq(nil) - expect(dossier.reload.conservation_extension).to eq(1.month) + before do + dossier.update(hidden_by_expired_at: 1.hour.ago, hidden_by_reason: 'expired') + end + + context 'when dossier has expired but was not hidden by anyone' do + it 'works' do + expect(subject).to redirect_to(instructeur_dossier_path(procedure, dossier)) + end + + it 'extends conservation_extension by 1 month and let dossier not hidden' do + subject + expect(dossier.reload.conservation_extension).to eq(1.month) + expect(dossier.reload.hidden_by_reason).to eq(nil) + expect(dossier.reload.hidden_by_expired_at).to eq(nil) + expect(dossier.reload.hidden_by_administration_at).to eq(nil) + expect(dossier.reload.hidden_by_user_at).to eq(nil) + end + + it 'flashed notice success' do + subject + expect(flash[:notice]).to eq(I18n.t('views.instructeurs.dossiers.archived_dossier')) + end + end + + context 'when dossier has expired and was hidden by instructeur' do + let!(:dossier) { create(:dossier, :hidden_by_administration, :accepte, :with_individual, procedure: procedure) } + + it 'extends conservation_extension by 1 month and restore dossier for instructeur' do + subject + expect(dossier.reload.conservation_extension).to eq(1.month) + expect(dossier.reload.hidden_by_reason).to eq(nil) + expect(dossier.reload.hidden_by_expired_at).to eq(nil) + expect(dossier.reload.hidden_by_administration_at).to eq(nil) + expect(dossier.reload.hidden_by_user_at).to eq(nil) + end + end + + context 'when dossier has expired and was hidden by user' do + let!(:dossier) { create(:dossier, :hidden_by_user, :accepte, :with_individual, procedure: procedure) } + it 'extends conservation_extension by 1 month and let dossier hidden for user' do + subject + expect(dossier.reload.conservation_extension).to eq(1.month) + expect(dossier.reload.hidden_by_reason).to eq("user_request") + expect(dossier.reload.hidden_by_expired_at).to eq(nil) + expect(dossier.reload.hidden_by_administration_at).to eq(nil) + expect(dossier.reload.hidden_by_user_at).not_to eq(nil) + end + end + + context 'when dossier has expired and was hidden by user and instructeur' do + let!(:dossier) { create(:dossier, :hidden_by_user, :hidden_by_administration, :accepte, :with_individual, procedure: procedure) } + it 'extends conservation_extension by 1 month and let dossier hidden for user' do + subject + expect(dossier.reload.conservation_extension).to eq(1.month) + expect(dossier.reload.hidden_by_reason).to eq("user_request") + expect(dossier.reload.hidden_by_expired_at).to eq(nil) + expect(dossier.reload.hidden_by_administration_at).to eq(nil) + expect(dossier.reload.hidden_by_user_at).not_to eq(nil) + end end end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 392f4c159..133f242e1 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -115,8 +115,7 @@ FactoryBot.define do end trait :hidden_by_expired do - hidden_by_user_at { 1.day.ago } - hidden_by_administration_at { 1.day.ago } + hidden_by_expired_at { 1.day.ago } hidden_by_reason { DeletedDossier.reasons.fetch(:expired) } end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 16476828c..0627c5f08 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -107,8 +107,8 @@ describe Dossier, type: :model do context 'does not include an expiring dossier that has been postponed' do before do - expiring_dossier.extend_conservation(1.month) - expiring_dossier_with_notification.extend_conservation(1.month) + expiring_dossier.extend_conservation(1.month, user) + expiring_dossier_with_notification.extend_conservation(1.month, user) expiring_dossier.reload expiring_dossier_with_notification.reload end @@ -158,8 +158,8 @@ describe Dossier, type: :model do context 'does not include an expiring dossier that has been postponed' do before do - expiring_dossier.extend_conservation(1.month) - expiring_dossier_with_notification.extend_conservation(1.month) + expiring_dossier.extend_conservation(1.month, user) + expiring_dossier_with_notification.extend_conservation(1.month, user) expiring_dossier.reload expiring_dossier_with_notification.reload end @@ -218,8 +218,8 @@ describe Dossier, type: :model do context 'does not include an expiring dossier that has been postponed' do before do - expiring_dossier.extend_conservation(1.month) - expiring_dossier_with_notification.extend_conservation(1.month) + expiring_dossier.extend_conservation(1.month, user) + expiring_dossier_with_notification.extend_conservation(1.month, user) expiring_dossier.reload expiring_dossier_with_notification.reload end diff --git a/spec/services/expired/expired_dossiers_deletion_service_spec.rb b/spec/services/expired/expired_dossiers_deletion_service_spec.rb index 40dbb92c4..7e201d802 100644 --- a/spec/services/expired/expired_dossiers_deletion_service_spec.rb +++ b/spec/services/expired/expired_dossiers_deletion_service_spec.rb @@ -246,8 +246,9 @@ describe Expired::DossiersDeletionService do it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier], dossier.procedure.administrateurs.first.email) } it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier], dossier.followers_instructeurs.first.email) } - it { expect(dossier.reload.hidden_by_user_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } - it { expect(dossier.reload.hidden_by_administration_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } + it { expect(dossier.reload.hidden_by_user_at).to eq(nil) } + it { expect(dossier.reload.hidden_by_administration_at).to eq(nil) } + it { expect(dossier.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } it { expect(dossier.reload.hidden_by_reason).to eq('expired') } end end @@ -271,11 +272,9 @@ describe Expired::DossiersDeletionService do it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_1], dossier_1.procedure.administrateurs.first.email) } it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) } - it { expect(dossier_1.reload.hidden_by_user_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } - it { expect(dossier_1.reload.hidden_by_administration_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } + it { expect(dossier_1.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } it { expect(dossier_1.reload.hidden_by_reason).to eq('expired') } - it { expect(dossier_2.reload.hidden_by_user_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } - it { expect(dossier_2.reload.hidden_by_administration_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } + it { expect(dossier_2.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } it { expect(dossier_2.reload.hidden_by_reason).to eq('expired') } end end @@ -392,8 +391,7 @@ describe Expired::DossiersDeletionService do context 'when a notice has been sent a long time ago' do let(:notice_sent_at) { (warning_period + 4.days).ago } - it { expect(dossier.reload.hidden_by_user_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } - it { expect(dossier.reload.hidden_by_administration_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } + it { expect(dossier.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } it { expect(dossier.reload.hidden_by_reason).to eq('expired') } it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once } @@ -424,11 +422,9 @@ describe Expired::DossiersDeletionService do it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_1], dossier_1.procedure.administrateurs.first.email) } it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) } - it { expect(dossier_1.reload.hidden_by_user_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } - it { expect(dossier_1.reload.hidden_by_administration_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } + it { expect(dossier_1.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } it { expect(dossier_1.reload.hidden_by_reason).to eq('expired') } - it { expect(dossier_2.reload.hidden_by_user_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } - it { expect(dossier_2.reload.hidden_by_administration_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } + it { expect(dossier_2.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) } it { expect(dossier_2.reload.hidden_by_reason).to eq('expired') } end diff --git a/spec/system/users/list_dossiers_spec.rb b/spec/system/users/list_dossiers_spec.rb index 87aa2dc91..116598c05 100644 --- a/spec/system/users/list_dossiers_spec.rb +++ b/spec/system/users/list_dossiers_spec.rb @@ -12,7 +12,7 @@ describe 'user access to the list of their dossiers', js: true do let!(:dossier_for_tiers) { create(:dossier, :en_instruction, :for_tiers_with_notification, user: user) } let!(:dossier_en_construction_with_accuse_lecture) { create(:dossier, :en_construction, user: user, procedure: procedure_accuse_lecture) } let!(:dossier_accepte_with_accuse_lecture) { create(:dossier, :accepte, user: user, procedure: procedure_accuse_lecture) } - let!(:dossier_en_construction_expire) { create(:dossier, :en_construction, :hidden_by_expired, user: user) } + let!(:dossier_en_construction_expire) { create(:dossier, :with_individual, :with_populated_champs, :en_construction, :hidden_by_expired, user: user) } let!(:dossier_traite_expire) { create(:dossier, :accepte, :hidden_by_expired, user: user) } let!(:dossier_en_construction_supprime) { create(:dossier, :with_individual, :with_populated_champs, :en_construction, :hidden_by_user, user: user) } let(:dossiers_per_page) { 25 } @@ -222,7 +222,7 @@ describe 'user access to the list of their dossiers', js: true do click_on "3 supprimés récemment" expect(page).to have_link('Restaurer', href: restore_dossier_path(dossier_en_construction_supprime)) expect(page).to have_button('Restaurer et étendre la conservation') - expect(page).to have_link('imprimer', href: dossier_path("#{dossier_traite_expire.id}.pdf")) + expect(page).to have_link('Télécharger mon dossier', href: dossier_path("#{dossier_traite_expire.id}.pdf")) end context 'when user clicks on restore button', js: true do