2018-03-06 13:44:29 +01:00
|
|
|
|
class Commentaire < ApplicationRecord
|
2021-11-16 14:28:38 +01:00
|
|
|
|
include Discard::Model
|
2020-07-20 16:34:42 +02:00
|
|
|
|
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
|
2022-10-19 11:53:10 +02:00
|
|
|
|
belongs_to :instructeur, inverse_of: :commentaires, optional: true
|
|
|
|
|
belongs_to :expert, inverse_of: :commentaires, optional: true
|
2023-03-14 18:48:19 +01:00
|
|
|
|
has_one :dossier_correction, inverse_of: :commentaire, dependent: :nullify
|
2018-11-29 15:00:26 +01:00
|
|
|
|
|
2021-07-22 14:30:48 +02:00
|
|
|
|
validate :messagerie_available?, on: :create, unless: -> { dossier.brouillon? }
|
2019-06-25 17:12:44 +02:00
|
|
|
|
|
|
|
|
|
has_one_attached :piece_jointe
|
|
|
|
|
|
2021-11-16 16:10:08 +01:00
|
|
|
|
validates :body, presence: { message: "ne peut être vide" }, unless: :discarded?
|
2021-01-18 13:50:24 +01:00
|
|
|
|
|
2021-09-14 18:03:40 +02:00
|
|
|
|
FILE_MAX_SIZE = 20.megabytes
|
2021-01-18 13:50:24 +01:00
|
|
|
|
validates :piece_jointe,
|
|
|
|
|
content_type: AUTHORIZED_CONTENT_TYPES,
|
2021-11-24 10:04:20 +01:00
|
|
|
|
size: { less_than: FILE_MAX_SIZE }
|
2017-10-30 16:07:02 +01:00
|
|
|
|
|
2017-10-05 13:43:37 +02:00
|
|
|
|
default_scope { order(created_at: :asc) }
|
2017-10-05 16:10:00 +02:00
|
|
|
|
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }
|
2017-07-20 14:17:12 +02:00
|
|
|
|
|
2017-05-12 13:32:48 +02:00
|
|
|
|
after_create :notify
|
2016-12-26 11:08:53 +01:00
|
|
|
|
|
2018-11-29 15:00:26 +01:00
|
|
|
|
def email
|
2021-04-22 15:44:58 +02:00
|
|
|
|
if sent_by_instructeur?
|
2019-08-06 11:02:54 +02:00
|
|
|
|
instructeur.email
|
2021-04-22 15:44:58 +02:00
|
|
|
|
elsif sent_by_expert?
|
|
|
|
|
expert.email
|
2018-11-29 15:00:26 +01:00
|
|
|
|
else
|
|
|
|
|
read_attribute(:email)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-25 11:08:58 +01:00
|
|
|
|
def header
|
2019-07-01 18:14:02 +02:00
|
|
|
|
"#{redacted_email}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}"
|
2018-07-26 16:14:24 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-07-01 18:14:02 +02:00
|
|
|
|
def redacted_email
|
2021-04-22 15:44:58 +02:00
|
|
|
|
if sent_by_instructeur?
|
2022-10-12 11:48:50 +02:00
|
|
|
|
if dossier.procedure.feature_enabled?(:hide_instructeur_email)
|
2020-05-12 17:58:00 +02:00
|
|
|
|
"Instructeur n° #{instructeur.id}"
|
|
|
|
|
else
|
|
|
|
|
instructeur.email.split('@').first
|
|
|
|
|
end
|
2019-07-01 18:14:02 +02:00
|
|
|
|
else
|
|
|
|
|
email
|
2018-07-26 16:14:24 +02:00
|
|
|
|
end
|
2016-11-25 11:08:58 +01:00
|
|
|
|
end
|
2016-12-26 11:08:53 +01:00
|
|
|
|
|
2019-07-01 18:14:02 +02:00
|
|
|
|
def sent_by_system?
|
2021-04-22 15:44:58 +02:00
|
|
|
|
[CONTACT_EMAIL, OLD_CONTACT_EMAIL].include?(email)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sent_by_instructeur?
|
|
|
|
|
instructeur_id.present?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sent_by_expert?
|
|
|
|
|
expert_id.present?
|
2019-07-01 18:14:02 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sent_by?(someone)
|
2021-11-15 12:01:56 +01:00
|
|
|
|
someone.present? && email == someone&.email
|
2019-07-01 18:14:02 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-11-15 15:01:56 +01:00
|
|
|
|
def soft_deletable?(connected_user)
|
2022-03-31 15:33:37 +02:00
|
|
|
|
sent_by?(connected_user) && (sent_by_instructeur? || sent_by_expert?) && !discarded?
|
2021-11-15 14:55:35 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-11-07 17:15:05 +01:00
|
|
|
|
def file_url
|
2019-08-14 14:00:52 +02:00
|
|
|
|
if piece_jointe.attached? && piece_jointe.virus_scanner.safe?
|
|
|
|
|
Rails.application.routes.url_helpers.url_for(piece_jointe)
|
2017-11-07 17:15:05 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-19 09:40:59 +02:00
|
|
|
|
def soft_delete!
|
2023-12-07 15:41:50 +01:00
|
|
|
|
transaction do
|
|
|
|
|
discard!
|
|
|
|
|
dossier_correction&.resolve!
|
|
|
|
|
update! body: ''
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-19 09:40:59 +02:00
|
|
|
|
piece_jointe.purge_later if piece_jointe.attached?
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-14 18:48:19 +01:00
|
|
|
|
def flagged_pending_correction?
|
|
|
|
|
DossierCorrection.exists?(commentaire: self)
|
2023-03-14 17:23:17 +01:00
|
|
|
|
end
|
|
|
|
|
|
2016-12-26 11:08:53 +01:00
|
|
|
|
private
|
|
|
|
|
|
2017-05-12 13:32:48 +02:00
|
|
|
|
def notify
|
2018-03-06 12:03:32 +01:00
|
|
|
|
# - If the email is the contact email, the commentaire is a copy
|
|
|
|
|
# of an automated notification email we sent to a user, so do nothing.
|
|
|
|
|
# - If a user or an invited user posted a commentaire, do nothing,
|
|
|
|
|
# the notification system will properly
|
2019-08-06 11:02:54 +02:00
|
|
|
|
# - Otherwise, a instructeur posted a commentaire, we need to notify the user
|
2022-03-31 15:33:37 +02:00
|
|
|
|
if sent_by_instructeur? || sent_by_expert?
|
2021-11-16 15:12:05 +01:00
|
|
|
|
notify_user(wait: 5.minutes)
|
2024-01-03 13:23:48 +01:00
|
|
|
|
elsif !sent_by_system?
|
|
|
|
|
notify_administration
|
2016-12-26 14:38:00 +01:00
|
|
|
|
end
|
2016-12-26 11:08:53 +01:00
|
|
|
|
end
|
2017-05-12 13:40:32 +02:00
|
|
|
|
|
2021-11-16 16:10:08 +01:00
|
|
|
|
def notify_user(job_options = {})
|
2023-03-14 18:48:19 +01:00
|
|
|
|
if flagged_pending_correction?
|
2023-06-20 18:14:34 +02:00
|
|
|
|
DossierMailer.with(commentaire: self).notify_pending_correction.deliver_later(job_options)
|
2023-03-14 18:48:19 +01:00
|
|
|
|
else
|
|
|
|
|
DossierMailer.with(commentaire: self).notify_new_answer.deliver_later(job_options)
|
|
|
|
|
end
|
2017-05-12 13:40:32 +02:00
|
|
|
|
end
|
2017-11-08 16:37:04 +01:00
|
|
|
|
|
2024-01-03 13:23:48 +01:00
|
|
|
|
def notify_administration
|
|
|
|
|
dossier.followers_instructeurs
|
|
|
|
|
.with_instant_email_message_notifications
|
|
|
|
|
.find_each do |instructeur|
|
|
|
|
|
DossierMailer.notify_new_commentaire_to_instructeur(dossier, instructeur.email).deliver_later
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
experts_contactes = Set.new
|
|
|
|
|
|
|
|
|
|
dossier.avis.includes(:expert).find_each do |avis|
|
2024-03-15 17:42:51 +01:00
|
|
|
|
expert_procedure = avis.expert.experts_procedures.find_by(procedure_id: dossier.procedure.id)
|
|
|
|
|
if expert_procedure.notify_on_new_message? && avis.expert.present?
|
2024-01-03 13:23:48 +01:00
|
|
|
|
expert_id = avis.expert.id
|
|
|
|
|
if !experts_contactes.include?(expert_id)
|
|
|
|
|
AvisMailer.notify_new_commentaire_to_expert(dossier, avis, avis.expert).deliver_later
|
|
|
|
|
experts_contactes.add(expert_id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-01 18:14:02 +02:00
|
|
|
|
def messagerie_available?
|
|
|
|
|
return if sent_by_system?
|
|
|
|
|
if dossier.present? && !dossier.messagerie_available?
|
2022-03-17 13:53:33 +01:00
|
|
|
|
errors.add(:dossier, "Il n’est pas possible d’envoyer un message sur un dossier supprimé, archivé ou en brouillon")
|
2019-07-01 18:14:02 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
end
|