2018-09-04 18:19:29 +02:00
|
|
|
class CommentaireService
|
|
|
|
class << self
|
2018-11-29 15:00:26 +01:00
|
|
|
def build(sender, dossier, params)
|
|
|
|
case sender
|
2019-08-06 11:02:54 +02:00
|
|
|
when Instructeur
|
|
|
|
params[:instructeur] = sender
|
2021-04-22 15:44:58 +02:00
|
|
|
when Expert
|
|
|
|
params[:expert] = sender
|
2018-09-04 18:19:29 +02:00
|
|
|
end
|
|
|
|
|
2018-11-29 15:00:26 +01:00
|
|
|
build_with_email(sender.email, dossier, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_with_email(email, dossier, params)
|
|
|
|
attributes = params.merge(email: email, dossier: dossier)
|
2019-06-25 17:12:44 +02:00
|
|
|
# For some reason ActiveStorage trows an error in tests if we passe an empty string here.
|
|
|
|
# I suspect it could be resolved in rails 6 by using explicit `attach()`
|
|
|
|
if attributes[:piece_jointe].blank?
|
|
|
|
attributes.delete(:piece_jointe)
|
|
|
|
end
|
2018-09-04 18:19:29 +02:00
|
|
|
Commentaire.new(attributes)
|
|
|
|
end
|
2021-11-15 12:43:53 +01:00
|
|
|
|
|
|
|
def soft_delete(user, params)
|
|
|
|
commentaire = Dossier.find(params[:dossier_id])
|
|
|
|
.commentaires
|
|
|
|
.find(params[:commentaire_id])
|
|
|
|
if commentaire.sent_by?(user)
|
|
|
|
commentaire.piece_jointe.purge_later if commentaire.piece_jointe.attached?
|
|
|
|
commentaire.update!(body: "Message supprimé", deleted_at: Time.now.utc)
|
|
|
|
OpenStruct.new(status: true)
|
|
|
|
else
|
|
|
|
OpenStruct.new(status: false, error_message: "Impossible de supprimer le commentaire, celui ci ne vous appartient pas")
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
return OpenStruct.new(status: false, error_message: "#{e.model.humanize} introuvable")
|
|
|
|
end
|
2018-09-04 18:19:29 +02:00
|
|
|
end
|
|
|
|
end
|