2018-03-06 13:44:29 +01:00
|
|
|
class Administrateur < ApplicationRecord
|
2023-10-06 17:12:00 +02:00
|
|
|
include UserFindByConcern
|
2023-07-24 17:55:33 +02:00
|
|
|
UNUSED_ADMIN_THRESHOLD = ENV.fetch('UNUSED_ADMIN_THRESHOLD') { 6 }.to_i.months
|
2022-05-02 14:32:12 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
has_and_belongs_to_many :instructeurs
|
2022-07-21 20:05:05 +02:00
|
|
|
has_many :administrateurs_procedures
|
2022-07-21 13:23:13 +02:00
|
|
|
has_many :procedures, through: :administrateurs_procedures
|
2018-04-17 16:11:49 +02:00
|
|
|
has_many :services
|
2022-11-30 10:06:33 +01:00
|
|
|
has_many :api_tokens, inverse_of: :administrateur, dependent: :destroy
|
2023-10-24 17:45:20 +02:00
|
|
|
has_many :commentaire_groupe_gestionnaires, as: :sender
|
2023-04-04 18:28:21 +02:00
|
|
|
has_and_belongs_to_many :default_zones, class_name: 'Zone', join_table: 'default_zones_administrateurs'
|
2024-02-07 17:19:50 +01:00
|
|
|
has_many :archives, as: :user_profile
|
|
|
|
has_many :exports, as: :user_profile
|
2022-03-15 17:28:22 +01:00
|
|
|
belongs_to :user
|
2023-09-04 12:07:04 +02:00
|
|
|
belongs_to :groupe_gestionnaire, optional: true
|
2019-08-09 09:45:11 +02:00
|
|
|
|
2020-02-03 13:44:12 +01:00
|
|
|
default_scope { eager_load(:user) }
|
|
|
|
|
2019-10-07 15:39:35 +02:00
|
|
|
scope :inactive, -> { joins(:user).where(users: { last_sign_in_at: nil }) }
|
2019-12-18 13:28:29 +01:00
|
|
|
scope :with_publiees_ou_closes, -> { joins(:procedures).where(procedures: { aasm_state: [:publiee, :close, :depubliee] }) }
|
2018-01-11 14:17:50 +01:00
|
|
|
|
2022-05-02 14:32:12 +02:00
|
|
|
scope :unused, -> do
|
|
|
|
joins(:user)
|
|
|
|
.where.missing(:services)
|
|
|
|
.left_outer_joins(:administrateurs_procedures) # needed to bypass procedure hidden default scope
|
|
|
|
.where(administrateurs_procedures: { procedure_id: nil })
|
2023-08-01 12:30:23 +02:00
|
|
|
.includes(:api_tokens)
|
|
|
|
.where(users: { last_sign_in_at: ..UNUSED_ADMIN_THRESHOLD.ago })
|
|
|
|
.merge(APIToken.where(last_v1_authenticated_at: nil).or(APIToken.where(last_v1_authenticated_at: ..UNUSED_ADMIN_THRESHOLD.ago)))
|
|
|
|
.merge(APIToken.where(last_v2_authenticated_at: nil).or(APIToken.where(last_v2_authenticated_at: ..UNUSED_ADMIN_THRESHOLD.ago)))
|
2022-05-02 14:32:12 +02:00
|
|
|
end
|
|
|
|
|
2020-02-03 11:07:53 +01:00
|
|
|
def email
|
2020-03-24 14:45:53 +01:00
|
|
|
user&.email
|
2020-02-03 11:07:53 +01:00
|
|
|
end
|
|
|
|
|
2022-11-21 15:19:02 +01:00
|
|
|
def active?
|
|
|
|
user&.active?
|
|
|
|
end
|
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
def self.find_inactive_by_token(reset_password_token)
|
|
|
|
self.inactive.with_reset_password_token(reset_password_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_inactive_by_id(id)
|
|
|
|
self.inactive.find(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def registration_state
|
2019-11-05 10:05:59 +01:00
|
|
|
if user.active?
|
2018-01-11 14:17:50 +01:00
|
|
|
'Actif'
|
2019-08-19 09:48:55 +02:00
|
|
|
elsif user.reset_password_period_valid?
|
2018-01-11 14:17:50 +01:00
|
|
|
'En attente'
|
|
|
|
else
|
|
|
|
'Expiré'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invitation_expired?
|
2019-11-05 10:05:59 +01:00
|
|
|
!user.active? && !user.reset_password_period_valid?
|
2018-01-11 14:17:50 +01:00
|
|
|
end
|
|
|
|
|
2018-05-17 15:39:37 +02:00
|
|
|
def owns?(procedure)
|
2019-02-26 16:18:04 +01:00
|
|
|
procedure.administrateurs.include?(self)
|
2018-05-17 15:39:37 +02:00
|
|
|
end
|
2019-01-07 15:11:55 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
def instructeur
|
2019-10-15 17:44:59 +02:00
|
|
|
user.instructeur
|
2019-01-07 15:11:55 +01:00
|
|
|
end
|
2019-07-22 15:33:58 +02:00
|
|
|
|
|
|
|
def can_be_deleted?
|
2024-03-18 16:20:23 +01:00
|
|
|
procedures.with_discarded.all? { |p| p.administrateurs.count > 1 || p.dossiers.empty? }
|
2019-07-22 15:33:58 +02:00
|
|
|
end
|
2020-01-30 10:48:28 +01:00
|
|
|
|
2021-10-18 13:10:35 +02:00
|
|
|
def merge(old_admin)
|
|
|
|
return if old_admin.nil?
|
|
|
|
|
2022-11-07 14:11:05 +01:00
|
|
|
procedures_with_new_admin, procedures_without_new_admin = old_admin
|
|
|
|
.procedures
|
|
|
|
.with_discarded
|
2021-10-18 13:10:35 +02:00
|
|
|
.partition { |p| p.administrateurs.exists?(id) }
|
|
|
|
|
|
|
|
procedures_with_new_admin.each do |p|
|
|
|
|
p.administrateurs.delete(old_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
procedures_without_new_admin.each do |p|
|
|
|
|
p.administrateurs << self
|
|
|
|
p.administrateurs.delete(old_admin)
|
|
|
|
end
|
|
|
|
|
2022-10-19 16:53:38 +02:00
|
|
|
old_services = old_admin.services
|
|
|
|
new_service_by_nom = services.index_by(&:nom)
|
|
|
|
|
|
|
|
old_services.each do |old_service|
|
|
|
|
corresponding_service = new_service_by_nom[old_service.nom]
|
|
|
|
if corresponding_service.present?
|
2023-04-04 16:03:06 +02:00
|
|
|
old_service.procedures.with_discarded.update_all(service_id: corresponding_service.id)
|
2022-10-19 16:53:38 +02:00
|
|
|
old_service.destroy
|
|
|
|
else
|
2022-10-27 12:44:02 +02:00
|
|
|
old_service.update_column(:administrateur_id, id)
|
2022-10-19 16:53:38 +02:00
|
|
|
end
|
|
|
|
end
|
2021-10-18 13:10:35 +02:00
|
|
|
|
|
|
|
instructeurs_with_new_admin, instructeurs_without_new_admin = old_admin.instructeurs
|
|
|
|
.partition { |i| i.administrateurs.exists?(id) }
|
|
|
|
|
|
|
|
instructeurs_with_new_admin.each do |i|
|
|
|
|
i.administrateurs.delete(old_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
instructeurs_without_new_admin.each do |i|
|
|
|
|
i.administrateurs << self
|
|
|
|
i.administrateurs.delete(old_admin)
|
|
|
|
end
|
2023-06-28 13:37:33 +02:00
|
|
|
|
2023-07-17 15:40:38 +02:00
|
|
|
old_admin.api_tokens.where('version >= ?', 3).find_each do |token|
|
2023-06-28 13:37:33 +02:00
|
|
|
self.api_tokens << token
|
|
|
|
end
|
2021-10-18 13:10:35 +02:00
|
|
|
end
|
|
|
|
|
2022-10-24 16:03:49 +02:00
|
|
|
def zones
|
2024-01-16 17:28:04 +01:00
|
|
|
procedures.includes(:zones).flat_map(&:zones).uniq
|
2022-10-24 16:03:49 +02:00
|
|
|
end
|
|
|
|
|
2020-03-26 16:17:07 +01:00
|
|
|
# required to display feature flags field in manager
|
|
|
|
def features
|
|
|
|
end
|
2023-11-06 14:55:31 +01:00
|
|
|
|
|
|
|
def unread_commentaires?
|
2023-12-10 18:25:58 +01:00
|
|
|
commentaire_groupe_gestionnaires.last && (commentaire_seen_at.nil? || commentaire_seen_at < commentaire_groupe_gestionnaires.last.created_at)
|
2023-11-06 14:55:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def mark_commentaire_as_seen
|
|
|
|
update(commentaire_seen_at: Time.zone.now)
|
|
|
|
end
|
2015-10-23 16:19:55 +02:00
|
|
|
end
|