2020-08-06 16:35:45 +02:00
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: commentaires
|
|
|
|
|
#
|
|
|
|
|
# id :integer not null, primary key
|
|
|
|
|
# body :string
|
|
|
|
|
# email :string
|
|
|
|
|
# created_at :datetime not null
|
|
|
|
|
# updated_at :datetime not null
|
|
|
|
|
# dossier_id :integer
|
|
|
|
|
# instructeur_id :bigint
|
|
|
|
|
# user_id :bigint
|
|
|
|
|
#
|
2018-03-06 13:44:29 +01:00
|
|
|
|
class Commentaire < ApplicationRecord
|
2020-07-20 16:34:42 +02:00
|
|
|
|
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
|
2016-11-25 11:08:58 +01:00
|
|
|
|
|
2020-07-20 16:06:03 +02:00
|
|
|
|
belongs_to :user, optional: true
|
|
|
|
|
belongs_to :instructeur, optional: true
|
2018-11-29 15:00:26 +01:00
|
|
|
|
|
2019-07-01 18:14:02 +02:00
|
|
|
|
validate :messagerie_available?, on: :create
|
2019-06-25 17:12:44 +02:00
|
|
|
|
|
|
|
|
|
has_one_attached :piece_jointe
|
|
|
|
|
|
2019-11-12 15:51:51 +01:00
|
|
|
|
validates :body, presence: { message: "ne peut être vide" }
|
2020-03-16 17:55:16 +01:00
|
|
|
|
validates :piece_jointe, size: { less_than: 20.megabytes }
|
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
|
|
|
|
|
if user
|
|
|
|
|
user.email
|
2019-08-06 11:02:54 +02:00
|
|
|
|
elsif instructeur
|
|
|
|
|
instructeur.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
|
2019-08-06 11:02:54 +02:00
|
|
|
|
if instructeur.present?
|
2020-05-12 17:58:00 +02:00
|
|
|
|
if Flipper.enabled?(:hide_instructeur_email, dossier.procedure)
|
|
|
|
|
"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?
|
|
|
|
|
[CONTACT_EMAIL, OLD_CONTACT_EMAIL].include?(email) &&
|
2019-08-06 11:02:54 +02:00
|
|
|
|
user.nil? && instructeur.nil?
|
2019-07-01 18:14:02 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sent_by?(someone)
|
|
|
|
|
email == someone.email
|
|
|
|
|
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
|
|
|
|
|
|
2016-12-26 11:08:53 +01:00
|
|
|
|
private
|
|
|
|
|
|
2017-05-12 13:32:48 +02:00
|
|
|
|
def notify
|
|
|
|
|
dossier_user_email = dossier.user.email
|
2018-10-10 09:23:08 +02:00
|
|
|
|
invited_users_emails = dossier.invites.pluck(:email).to_a
|
2017-05-12 13:32:48 +02:00
|
|
|
|
|
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
|
2018-05-31 15:45:25 +02:00
|
|
|
|
if !email.in?([CONTACT_EMAIL, dossier_user_email, *invited_users_emails])
|
2017-05-12 13:32:48 +02:00
|
|
|
|
notify_user
|
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
|
|
|
|
|
2017-05-10 10:56:30 +02:00
|
|
|
|
def notify_user
|
2018-11-20 11:50:25 +01:00
|
|
|
|
DossierMailer.notify_new_answer(dossier).deliver_later
|
2017-05-12 13:40:32 +02:00
|
|
|
|
end
|
2017-11-08 16:37:04 +01:00
|
|
|
|
|
2019-07-01 18:14:02 +02:00
|
|
|
|
def messagerie_available?
|
|
|
|
|
return if sent_by_system?
|
|
|
|
|
if dossier.present? && !dossier.messagerie_available?
|
|
|
|
|
errors.add(:dossier, "Il n’est pas possible d’envoyer un message sur un dossier archivé ou en brouillon")
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
end
|