demarches-normaliennes/app/models/commentaire.rb

121 lines
3.1 KiB
Ruby
Raw Normal View History

2020-08-06 16:35:45 +02:00
# == Schema Information
#
# Table name: commentaires
#
# id :integer not null, primary key
# body :string
# discarded_at :datetime
2020-08-06 16:35:45 +02:00
# email :string
# created_at :datetime not null
# updated_at :datetime not null
# dossier_id :integer
# expert_id :bigint
2020-08-06 16:35:45 +02:00
# instructeur_id :bigint
#
2018-03-06 13:44:29 +01:00
class Commentaire < ApplicationRecord
include Discard::Model
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
belongs_to :instructeur, inverse_of: :commentaires, optional: true
belongs_to :expert, inverse_of: :commentaires, optional: true
2021-07-22 14:30:48 +02:00
validate :messagerie_available?, on: :create, unless: -> { dossier.brouillon? }
has_one_attached :piece_jointe
validates :body, presence: { message: "ne peut être vide" }, unless: :discarded?
FILE_MAX_SIZE = 20.megabytes
validates :piece_jointe,
content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: FILE_MAX_SIZE }
2017-10-30 16:07:02 +01:00
default_scope { order(created_at: :asc) }
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }
after_create :notify
2016-12-26 11:08:53 +01:00
def email
if sent_by_instructeur?
instructeur.email
elsif sent_by_expert?
expert.email
else
read_attribute(:email)
end
end
def header
"#{redacted_email}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}"
end
def redacted_email
if sent_by_instructeur?
if dossier.procedure.feature_enabled?(:hide_instructeur_email)
"Instructeur n° #{instructeur.id}"
else
instructeur.email.split('@').first
end
else
email
end
end
2016-12-26 11:08:53 +01:00
def sent_by_system?
[CONTACT_EMAIL, OLD_CONTACT_EMAIL].include?(email)
end
def sent_by_instructeur?
instructeur_id.present?
end
def sent_by_expert?
expert_id.present?
end
def sent_by?(someone)
someone.present? && email == someone&.email
end
def soft_deletable?(connected_user)
2022-03-31 15:33:37 +02:00
sent_by?(connected_user) && (sent_by_instructeur? || sent_by_expert?) && !discarded?
end
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)
end
end
def soft_delete!
piece_jointe.purge_later if piece_jointe.attached?
discard!
update! body: ''
end
2016-12-26 11:08:53 +01:00
private
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
# - 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?
notify_user(wait: 5.minutes)
end
2016-12-26 11:08:53 +01:00
end
def notify_user(job_options = {})
DossierMailer.with(commentaire: self).notify_new_answer.deliver_later(job_options)
end
2017-11-08 16:37:04 +01: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 nest pas possible denvoyer un message sur un dossier supprimé, archivé ou en brouillon")
end
end
2015-08-10 11:05:06 +02:00
end