2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: groupe_instructeurs
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
2022-06-20 16:14:27 +02:00
|
|
|
# closed :boolean default(FALSE)
|
2020-08-06 16:35:45 +02:00
|
|
|
# label :text not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# procedure_id :bigint not null
|
|
|
|
#
|
2019-08-19 16:12:30 +02:00
|
|
|
class GroupeInstructeur < ApplicationRecord
|
2021-03-03 11:33:10 +01:00
|
|
|
DEFAUT_LABEL = 'défaut'
|
2020-07-20 17:22:44 +02:00
|
|
|
belongs_to :procedure, -> { with_discarded }, inverse_of: :groupe_instructeurs, optional: false
|
2020-06-11 10:08:04 +02:00
|
|
|
has_many :assign_tos, dependent: :destroy
|
|
|
|
has_many :instructeurs, through: :assign_tos
|
2019-08-22 17:58:31 +02:00
|
|
|
has_many :dossiers
|
2020-06-11 12:33:29 +02:00
|
|
|
has_and_belongs_to_many :exports, dependent: :destroy
|
2021-12-09 12:20:22 +01:00
|
|
|
has_and_belongs_to_many :bulk_messages, dependent: :destroy
|
2019-10-09 18:05:43 +02:00
|
|
|
|
|
|
|
validates :label, presence: { message: 'doit être renseigné' }, allow_nil: false
|
|
|
|
validates :label, uniqueness: { scope: :procedure, message: 'existe déjà' }
|
|
|
|
|
|
|
|
before_validation -> { label&.strip! }
|
2022-10-14 17:10:42 +02:00
|
|
|
after_create :toggle_routing
|
2020-01-07 17:39:50 +01:00
|
|
|
|
|
|
|
scope :without_group, -> (group) { where.not(id: group) }
|
2020-04-08 19:30:16 +02:00
|
|
|
scope :for_api_v2, -> { includes(procedure: [:administrateurs]) }
|
2022-10-14 17:10:42 +02:00
|
|
|
scope :actif, -> { where(closed: false) }
|
|
|
|
|
|
|
|
def toggle_routing
|
|
|
|
procedure = self.procedure.reload
|
|
|
|
routing_enabled = procedure.groupe_instructeurs.actif.count > 1
|
|
|
|
procedure.update!(routing_enabled: routing_enabled)
|
|
|
|
end
|
2021-12-21 19:06:38 +01:00
|
|
|
|
|
|
|
def add(instructeur)
|
|
|
|
return if in?(instructeur.groupe_instructeurs)
|
|
|
|
|
|
|
|
default_notification_settings = instructeur.notification_settings(procedure_id)
|
|
|
|
instructeur.assign_to.create(groupe_instructeur: self, **default_notification_settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove(instructeur)
|
|
|
|
return if !in?(instructeur.groupe_instructeurs)
|
|
|
|
|
|
|
|
instructeur.groupe_instructeurs.destroy(self)
|
|
|
|
instructeur.follows
|
|
|
|
.joins(:dossier)
|
|
|
|
.where(dossiers: { groupe_instructeur: self })
|
|
|
|
.update_all(unfollowed_at: Time.zone.now)
|
|
|
|
end
|
2019-08-19 16:12:30 +02:00
|
|
|
end
|