Remove the link between commentaire and user

This commit is contained in:
Paul Chavard 2021-04-22 14:44:58 +01:00
parent a640ec1d43
commit c20ad5ca17
7 changed files with 45 additions and 25 deletions

View file

@ -80,10 +80,10 @@ module Types
def messages(id: nil)
if id.present?
Loaders::Record
.for(Commentaire, where: { dossier: object }, includes: [:instructeur, :user], array: true)
.for(Commentaire, where: { dossier: object }, includes: [:instructeur, :expert], array: true)
.load(ApplicationRecord.id_from_typed_id(id))
else
Loaders::Association.for(object.class, commentaires: [:instructeur, :user]).load(object)
Loaders::Association.for(object.class, commentaires: [:instructeur, :expert]).load(object)
end
end

View file

@ -21,7 +21,6 @@ class Champ < ApplicationRecord
belongs_to :dossier, -> { with_discarded }, inverse_of: false, touch: true, optional: false
belongs_to :type_de_champ, inverse_of: :champ, optional: false
belongs_to :parent, class_name: 'Champ', optional: true
has_many :commentaires
has_one_attached :piece_justificative_file
# We declare champ specific relationships (Champs::CarteChamp, Champs::SiretChamp and Champs::RepetitionChamp)

View file

@ -8,14 +8,15 @@
# created_at :datetime not null
# updated_at :datetime not null
# dossier_id :integer
# expert_id :bigint
# instructeur_id :bigint
# user_id :bigint
#
class Commentaire < ApplicationRecord
self.ignored_columns = [:user_id]
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
belongs_to :user, optional: true
belongs_to :instructeur, optional: true
belongs_to :expert, optional: true
validate :messagerie_available?, on: :create
@ -33,10 +34,10 @@ class Commentaire < ApplicationRecord
after_create :notify
def email
if user
user.email
elsif instructeur
if sent_by_instructeur?
instructeur.email
elsif sent_by_expert?
expert.email
else
read_attribute(:email)
end
@ -47,7 +48,7 @@ class Commentaire < ApplicationRecord
end
def redacted_email
if instructeur.present?
if sent_by_instructeur?
if Flipper.enabled?(:hide_instructeur_email, dossier.procedure)
"Instructeur n° #{instructeur.id}"
else
@ -59,8 +60,15 @@ class Commentaire < ApplicationRecord
end
def sent_by_system?
[CONTACT_EMAIL, OLD_CONTACT_EMAIL].include?(email) &&
user.nil? && instructeur.nil?
[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)
@ -76,15 +84,12 @@ class Commentaire < ApplicationRecord
private
def notify
dossier_user_email = dossier.user.email
invited_users_emails = dossier.invites.pluck(:email).to_a
# - 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
if !email.in?([CONTACT_EMAIL, dossier_user_email, *invited_users_emails])
if sent_by_instructeur? || sent_by_expert?
notify_user
end
end

View file

@ -2,10 +2,10 @@ class CommentaireService
class << self
def build(sender, dossier, params)
case sender
when User
params[:user] = sender
when Instructeur
params[:instructeur] = sender
when Expert
params[:expert] = sender
end
build_with_email(sender.email, dossier, params)