refactor(dossier): handle discard by admin and super admin
This commit is contained in:
parent
b408811c5a
commit
ad4be3c482
4 changed files with 34 additions and 27 deletions
|
@ -140,9 +140,6 @@ module Instructeurs
|
||||||
|
|
||||||
def repasser_en_instruction
|
def repasser_en_instruction
|
||||||
begin
|
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."
|
flash.notice = "Le dossier #{dossier.id} a été repassé en instruction."
|
||||||
dossier.repasser_en_instruction!(instructeur: current_instructeur)
|
dossier.repasser_en_instruction!(instructeur: current_instructeur)
|
||||||
rescue AASM::InvalidTransition => e
|
rescue AASM::InvalidTransition => e
|
||||||
|
|
|
@ -206,6 +206,7 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
scope :archived, -> { where(archived: true) }
|
scope :archived, -> { where(archived: true) }
|
||||||
scope :not_archived, -> { where(archived: false) }
|
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_user, -> { where(hidden_by_user_at: nil) }
|
||||||
scope :not_hidden_by_instructeur, -> { where(hidden_by_instructeur_at: nil) }
|
scope :not_hidden_by_instructeur, -> { where(hidden_by_instructeur_at: nil) }
|
||||||
|
|
||||||
|
@ -236,6 +237,7 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
scope :downloadable_sorted, -> {
|
scope :downloadable_sorted, -> {
|
||||||
state_not_brouillon
|
state_not_brouillon
|
||||||
|
.not_hidden_by_instructeur
|
||||||
.includes(
|
.includes(
|
||||||
:user,
|
:user,
|
||||||
:individual,
|
:individual,
|
||||||
|
@ -682,8 +684,20 @@ class Dossier < ApplicationRecord
|
||||||
!procedure.brouillon? && !brouillon?
|
!procedure.brouillon? && !brouillon?
|
||||||
end
|
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?
|
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
|
end
|
||||||
|
|
||||||
def expose_legacy_carto_api?
|
def expose_legacy_carto_api?
|
||||||
|
@ -738,26 +752,26 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
def discard_and_keep_track!(author, reason)
|
def discard_and_keep_track!(author, reason)
|
||||||
author_is_user = author.is_a?(User)
|
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
|
if termine? && author_is_administration
|
||||||
self.update(hidden_by_instructeur_at: Time.zone.now)
|
update(hidden_by_administration_at: Time.zone.now)
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.termine? && author_is_user
|
if termine? && author_is_user
|
||||||
self.update(hidden_by_user_at: Time.zone.now)
|
update(hidden_by_user_at: Time.zone.now)
|
||||||
end
|
end
|
||||||
|
|
||||||
user_email = user_deleted? ? nil : user_email_for(:notification)
|
user_email = user_deleted? ? nil : user_email_for(:notification)
|
||||||
deleted_dossier = nil
|
deleted_dossier = nil
|
||||||
|
|
||||||
transaction do
|
transaction do
|
||||||
if deleted_by_instructeur_and_user? || !author_is_instructeur && keep_track_on_deletion?
|
if deleted_by_instructeur_and_user? || en_construction? || brouillon?
|
||||||
log_dossier_operation(author, :supprimer, self)
|
if keep_track_on_deletion?
|
||||||
deleted_dossier = DeletedDossier.create_from_dossier(self, reason)
|
log_dossier_operation(author, :supprimer, self)
|
||||||
end
|
deleted_dossier = DeletedDossier.create_from_dossier(self, reason)
|
||||||
|
end
|
||||||
|
|
||||||
if deleted_by_instructeur_and_user? || !author_is_instructeur && !termine?
|
|
||||||
update!(dossier_transfer_id: nil)
|
update!(dossier_transfer_id: nil)
|
||||||
discard!
|
discard!
|
||||||
end
|
end
|
||||||
|
@ -784,7 +798,9 @@ class Dossier < ApplicationRecord
|
||||||
def restore(author)
|
def restore(author)
|
||||||
if discarded?
|
if discarded?
|
||||||
transaction do
|
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!
|
deleted_dossier&.destroy!
|
||||||
log_dossier_operation(author, :restaurer, self)
|
log_dossier_operation(author, :restaurer, self)
|
||||||
end
|
end
|
||||||
|
@ -792,12 +808,6 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def restore_if_discarded_with_procedure(author)
|
|
||||||
if deleted_dossier&.procedure_removed?
|
|
||||||
restore(author)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def attestation_activated?
|
def attestation_activated?
|
||||||
termine? && procedure.attestation_template&.activated?
|
termine? && procedure.attestation_template&.activated?
|
||||||
end
|
end
|
||||||
|
@ -857,6 +867,7 @@ class Dossier < ApplicationRecord
|
||||||
|
|
||||||
create_missing_traitemets
|
create_missing_traitemets
|
||||||
|
|
||||||
|
self.hidden_by_user_at = nil
|
||||||
self.archived = false
|
self.archived = false
|
||||||
self.termine_close_to_expiration_notice_sent_at = nil
|
self.termine_close_to_expiration_notice_sent_at = nil
|
||||||
self.conservation_extension = 0.days
|
self.conservation_extension = 0.days
|
||||||
|
@ -1139,10 +1150,6 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def hidden_by_user?
|
|
||||||
self.hidden_by_user_at.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_missing_traitemets
|
def create_missing_traitemets
|
||||||
|
|
|
@ -673,7 +673,10 @@ class Procedure < ApplicationRecord
|
||||||
def restore(author)
|
def restore(author)
|
||||||
if discarded? && undiscard
|
if discarded? && undiscard
|
||||||
dossiers.with_discarded.discarded.find_each do |dossier|
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,7 +33,7 @@ as well as a link to its edit page.
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if dossier.can_be_deleted_by_manager? %>
|
<% 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 ?" } %>
|
<%= 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 ?" } %>
|
<%= link_to 'Restaurer le dossier', restore_manager_dossier_path(dossier), method: :post, class: 'button', data: { confirm: "Confirmez vous la restauration du dossier ?" } %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Reference in a new issue