2015-08-10 11:05:06 +02:00
|
|
|
class CommentairesController < ApplicationController
|
2016-11-14 18:00:26 +01:00
|
|
|
def index
|
|
|
|
@facade = DossierFacades.new(
|
2016-12-22 20:40:23 +01:00
|
|
|
params[:dossier_id],
|
|
|
|
(current_gestionnaire || current_user).email,
|
|
|
|
params[:champs_id]
|
2016-11-14 18:00:26 +01:00
|
|
|
)
|
|
|
|
render layout: false
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash.alert = t('errors.messages.dossier_not_found')
|
|
|
|
redirect_to url_for(controller: '/')
|
|
|
|
end
|
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
def create
|
2015-08-11 15:22:07 +02:00
|
|
|
@commentaire = Commentaire.new
|
2015-08-10 11:05:06 +02:00
|
|
|
@commentaire.dossier = Dossier.find(params['dossier_id'])
|
2016-11-14 18:00:26 +01:00
|
|
|
@commentaire.champ = @commentaire.dossier.champs.find(params[:champ_id]) if params[:champ_id]
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2015-09-25 10:46:09 +02:00
|
|
|
if is_gestionnaire?
|
|
|
|
@commentaire.email = current_gestionnaire.email
|
|
|
|
@commentaire.dossier.next_step! 'gestionnaire', 'comment'
|
2016-02-08 18:16:18 +01:00
|
|
|
else
|
2015-09-25 10:46:09 +02:00
|
|
|
@commentaire.email = current_user.email
|
2016-02-08 18:16:18 +01:00
|
|
|
@commentaire.dossier.next_step! 'user', 'comment' if current_user.email == @commentaire.dossier.user.email
|
2015-09-25 10:46:09 +02:00
|
|
|
end
|
|
|
|
|
2016-04-20 16:51:57 +02:00
|
|
|
unless params[:piece_justificative].nil?
|
|
|
|
pj = PiecesJustificativesService.upload_one! @commentaire.dossier, current_user, params
|
|
|
|
|
|
|
|
if pj.errors.empty?
|
|
|
|
@commentaire.piece_justificative = pj
|
|
|
|
else
|
|
|
|
flash.alert = pj.errors.full_messages.join("<br>").html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-25 10:46:09 +02:00
|
|
|
@commentaire.body = params['texte_commentaire']
|
2016-06-14 16:00:37 +02:00
|
|
|
saved = false
|
2016-12-07 16:59:35 +01:00
|
|
|
unless @commentaire.body.blank? && @commentaire.piece_justificative.nil?
|
|
|
|
saved = @commentaire.save unless flash.alert
|
|
|
|
else
|
|
|
|
flash.alert = "Veuillez rédiger un message ou ajouter une pièce jointe."
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2015-09-22 15:00:59 +02:00
|
|
|
if is_gestionnaire?
|
2016-07-18 18:24:29 +02:00
|
|
|
unless current_gestionnaire.follow? @commentaire.dossier
|
|
|
|
current_gestionnaire.toggle_follow_dossier @commentaire.dossier
|
|
|
|
end
|
|
|
|
|
2016-06-14 16:00:37 +02:00
|
|
|
NotificationMailer.new_answer(@commentaire.dossier).deliver_now! if saved
|
2016-12-22 20:40:23 +01:00
|
|
|
|
2015-09-22 15:00:59 +02:00
|
|
|
redirect_to url_for(controller: 'backoffice/dossiers', action: :show, id: params['dossier_id'])
|
2015-08-10 11:05:06 +02:00
|
|
|
else
|
2016-12-22 20:40:23 +01:00
|
|
|
NotificationService.new('commentaire', @commentaire.dossier.id).notify if saved
|
|
|
|
|
|
|
|
if current_user.email != @commentaire.dossier.user.email
|
|
|
|
invite = Invite.where(dossier: @commentaire.dossier, user: current_user).first
|
|
|
|
redirect_to url_for(controller: 'users/dossiers/invites', action: :show, id: invite.id)
|
|
|
|
else
|
|
|
|
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: params['dossier_id'])
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
end
|
2015-09-22 15:00:59 +02:00
|
|
|
|
|
|
|
def is_gestionnaire?
|
|
|
|
false
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|