🛠️ Fix | Procedure minimal admins presence

This commit is contained in:
Thibaut Poullain 2024-10-17 12:09:06 +02:00 committed by simon lehericey
parent 857f68c912
commit 5195ebd5c7
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5

View file

@ -119,7 +119,7 @@ class Procedure < ApplicationRecord
end
has_many :administrateurs_procedures, dependent: :delete_all
has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! }
has_many :administrateurs, through: :administrateurs_procedures, before_remove: :check_administrateur_minimal_presence
has_many :groupe_instructeurs, -> { order(:label) }, inverse_of: :procedure, dependent: :destroy
has_many :instructeurs, through: :groupe_instructeurs
has_many :export_templates, through: :groupe_instructeurs
@ -318,6 +318,12 @@ class Procedure < ApplicationRecord
end
end
def check_administrateur_minimal_presence(_object)
if self.administrateurs.count <= 1
raise ActiveRecord::RecordNotDestroyed.new("Cannot remove the last administrateur of procedure #{self.libelle} (#{self.id})")
end
end
def dossiers_close_to_expiration
dossiers.close_to_expiration.count
end