From ad4be3c482fe68f4041c54ff8a2afcc488e533b1 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 16 Dec 2021 10:25:44 +0000 Subject: [PATCH] refactor(dossier): handle discard by admin and super admin --- .../instructeurs/dossiers_controller.rb | 3 -- app/models/dossier.rb | 51 +++++++++++-------- app/models/procedure.rb | 5 +- app/views/manager/dossiers/show.html.erb | 2 +- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 26579eb7e..ee6af02e3 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -140,9 +140,6 @@ module Instructeurs def repasser_en_instruction begin - if dossier.hidden_by_user_at.present? - dossier.update!(hidden_by_user_at: nil) - end flash.notice = "Le dossier #{dossier.id} a été repassé en instruction." dossier.repasser_en_instruction!(instructeur: current_instructeur) rescue AASM::InvalidTransition => e diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 275c45953..d5912c1e4 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -206,6 +206,7 @@ class Dossier < ApplicationRecord scope :archived, -> { where(archived: true) } scope :not_archived, -> { where(archived: false) } + scope :hidden_by_instructeur, -> { where.not(hidden_by_instructeur_at: nil) } scope :not_hidden_by_user, -> { where(hidden_by_user_at: nil) } scope :not_hidden_by_instructeur, -> { where(hidden_by_instructeur_at: nil) } @@ -236,6 +237,7 @@ class Dossier < ApplicationRecord end scope :downloadable_sorted, -> { state_not_brouillon + .not_hidden_by_instructeur .includes( :user, :individual, @@ -682,8 +684,20 @@ class Dossier < ApplicationRecord !procedure.brouillon? && !brouillon? end + def hidden_by_user? + hidden_by_user_at.present? + end + + def hidden_by_instructeur? + hidden_by_instructeur_at.present? + end + def deleted_by_instructeur_and_user? - termine? && self.hidden_by_instructeur_at.present? && self.hidden_by_user_at.present? + termine? && hidden_by_instructeur? && hidden_by_user? + end + + def can_be_restored_by_manager? + hidden_by_instructeur? || discarded? && !procedure.discarded? end def expose_legacy_carto_api? @@ -738,26 +752,26 @@ class Dossier < ApplicationRecord def discard_and_keep_track!(author, reason) author_is_user = author.is_a?(User) - author_is_instructeur = author.is_a?(Instructeur) + author_is_administration = author.is_a?(Instructeur) || author.is_a?(Administrateur) || author.is_a?(SuperAdmin) - if self.termine? && author_is_instructeur - self.update(hidden_by_instructeur_at: Time.zone.now) + if termine? && author_is_administration + update(hidden_by_administration_at: Time.zone.now) end - if self.termine? && author_is_user - self.update(hidden_by_user_at: Time.zone.now) + if termine? && author_is_user + update(hidden_by_user_at: Time.zone.now) end user_email = user_deleted? ? nil : user_email_for(:notification) deleted_dossier = nil transaction do - if deleted_by_instructeur_and_user? || !author_is_instructeur && keep_track_on_deletion? - log_dossier_operation(author, :supprimer, self) - deleted_dossier = DeletedDossier.create_from_dossier(self, reason) - end + if deleted_by_instructeur_and_user? || en_construction? || brouillon? + if keep_track_on_deletion? + log_dossier_operation(author, :supprimer, self) + deleted_dossier = DeletedDossier.create_from_dossier(self, reason) + end - if deleted_by_instructeur_and_user? || !author_is_instructeur && !termine? update!(dossier_transfer_id: nil) discard! end @@ -784,7 +798,9 @@ class Dossier < ApplicationRecord def restore(author) if discarded? transaction do - if undiscard && keep_track_on_deletion? + if hidden_by_instructeur? + update(hidden_by_instructeur_at: nil) + elsif undiscard && keep_track_on_deletion? deleted_dossier&.destroy! log_dossier_operation(author, :restaurer, self) end @@ -792,12 +808,6 @@ class Dossier < ApplicationRecord end end - def restore_if_discarded_with_procedure(author) - if deleted_dossier&.procedure_removed? - restore(author) - end - end - def attestation_activated? termine? && procedure.attestation_template&.activated? end @@ -857,6 +867,7 @@ class Dossier < ApplicationRecord create_missing_traitemets + self.hidden_by_user_at = nil self.archived = false self.termine_close_to_expiration_notice_sent_at = nil self.conservation_extension = 0.days @@ -1139,10 +1150,6 @@ class Dossier < ApplicationRecord end end - def hidden_by_user? - self.hidden_by_user_at.present? - end - private def create_missing_traitemets diff --git a/app/models/procedure.rb b/app/models/procedure.rb index d75e4372a..d6244f9ca 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -673,7 +673,10 @@ class Procedure < ApplicationRecord def restore(author) if discarded? && undiscard dossiers.with_discarded.discarded.find_each do |dossier| - dossier.restore_if_discarded_with_procedure(author) + dossier.restore(author) + end + dossiers.hidden_by_instructeur.find_each do |dossier| + dossier.restore(author) end end end diff --git a/app/views/manager/dossiers/show.html.erb b/app/views/manager/dossiers/show.html.erb index 12b1415cc..6c2d85945 100644 --- a/app/views/manager/dossiers/show.html.erb +++ b/app/views/manager/dossiers/show.html.erb @@ -33,7 +33,7 @@ as well as a link to its edit page. <% end %> <% if dossier.can_be_deleted_by_manager? %> <%= link_to 'Supprimer le dossier', discard_manager_dossier_path(dossier), method: :post, class: 'button', data: { confirm: "Confirmez vous la suppression du dossier ?" } %> - <% elsif dossier.discarded? && !dossier.procedure.discarded? %> + <% elsif dossier.can_be_restored_by_manager? %> <%= link_to 'Restaurer le dossier', restore_manager_dossier_path(dossier), method: :post, class: 'button', data: { confirm: "Confirmez vous la restauration du dossier ?" } %> <% end %>