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])
|
2021-11-15 14:17:57 +01:00
|
|
|
.commentaires
|
|
|
|
.find(params[:id])
|
2021-11-15 12:43:53 +01:00
|
|
|
if commentaire.sent_by?(user)
|
2021-11-15 14:17:57 +01:00
|
|
|
commentaire.piece_jointe.purge_later if commentaire.piece_jointe.attached?
|
2021-11-16 14:28:38 +01:00
|
|
|
commentaire.body = ''
|
|
|
|
OpenStruct.new(status: commentaire.discard, error_message: commentaire.errors.full_messages.join(', '))
|
2021-11-15 12:43:53 +01:00
|
|
|
else
|
2021-11-15 14:15:05 +01:00
|
|
|
OpenStruct.new(status: false,
|
|
|
|
error_message: I18n.t('views.shared.commentaires.destroy.alert_reasons.acl'))
|
2021-11-15 12:43:53 +01:00
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
2021-11-15 14:15:05 +01:00
|
|
|
return OpenStruct.new(status: false,
|
|
|
|
error_message: I18n.t('views.shared.commentaires.destroy.alert_reasons.ar_not_found', model_name: e.model.humanize))
|
2021-11-15 12:43:53 +01:00
|
|
|
end
|
2018-09-04 18:19:29 +02:00
|
|
|
end
|
|
|
|
end
|