2019-03-25 16:54:46 +01:00
|
|
|
module Gestionnaires
|
2017-12-14 11:20:41 +01:00
|
|
|
class AvisController < GestionnaireController
|
2018-10-31 14:56:01 +01:00
|
|
|
include CreateAvisConcern
|
|
|
|
|
2017-12-22 17:34:58 +01:00
|
|
|
before_action :authenticate_gestionnaire!, except: [:sign_up, :create_gestionnaire]
|
|
|
|
before_action :redirect_if_no_sign_up_needed, only: [:sign_up]
|
|
|
|
before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up, :create_gestionnaire]
|
2018-02-27 16:58:22 +01:00
|
|
|
before_action :set_avis_and_dossier, only: [:show, :instruction, :messagerie, :create_commentaire, :update]
|
2017-11-07 17:53:11 +01:00
|
|
|
|
2017-09-06 11:21:29 +02:00
|
|
|
A_DONNER_STATUS = 'a-donner'
|
|
|
|
DONNES_STATUS = 'donnes'
|
|
|
|
|
|
|
|
def index
|
|
|
|
gestionnaire_avis = current_gestionnaire.avis.includes(dossier: [:procedure, :user])
|
2017-09-21 17:19:15 +02:00
|
|
|
@avis_a_donner = gestionnaire_avis.without_answer
|
|
|
|
@avis_donnes = gestionnaire_avis.with_answer
|
2017-09-06 11:21:29 +02:00
|
|
|
|
2018-03-06 13:44:29 +01:00
|
|
|
@statut = params[:statut].presence || A_DONNER_STATUS
|
2017-09-06 11:21:29 +02:00
|
|
|
|
|
|
|
@avis = case @statut
|
|
|
|
when A_DONNER_STATUS
|
|
|
|
@avis_a_donner
|
|
|
|
when DONNES_STATUS
|
|
|
|
@avis_donnes
|
|
|
|
end
|
2017-09-21 17:19:15 +02:00
|
|
|
|
|
|
|
@avis = @avis.page([params[:page].to_i, 1].max)
|
2017-09-06 11:21:29 +02:00
|
|
|
end
|
2017-09-06 11:26:32 +02:00
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
2017-09-06 16:14:03 +02:00
|
|
|
def instruction
|
2018-02-27 16:58:22 +01:00
|
|
|
@new_avis = Avis.new
|
2017-09-06 16:14:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-02-27 16:58:22 +01:00
|
|
|
if @avis.update(avis_params)
|
|
|
|
flash.notice = 'Votre réponse est enregistrée.'
|
|
|
|
redirect_to instruction_gestionnaire_avis_path(@avis)
|
|
|
|
else
|
|
|
|
flash.now.alert = @avis.errors.full_messages
|
|
|
|
@new_avis = Avis.new
|
|
|
|
render :instruction
|
|
|
|
end
|
2017-09-06 16:14:03 +02:00
|
|
|
end
|
|
|
|
|
2017-09-06 17:06:03 +02:00
|
|
|
def messagerie
|
2017-11-07 17:53:11 +01:00
|
|
|
@commentaire = Commentaire.new
|
2017-09-06 17:06:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_commentaire
|
2018-11-29 15:00:26 +01:00
|
|
|
@commentaire = CommentaireService.build(current_gestionnaire, avis.dossier, commentaire_params)
|
2017-11-07 17:53:11 +01:00
|
|
|
|
|
|
|
if @commentaire.save
|
|
|
|
flash.notice = "Message envoyé"
|
2018-02-21 13:42:12 +01:00
|
|
|
redirect_to messagerie_gestionnaire_avis_path(avis)
|
2017-11-07 17:53:11 +01:00
|
|
|
else
|
|
|
|
flash.alert = @commentaire.errors.full_messages
|
|
|
|
render :messagerie
|
|
|
|
end
|
2017-09-06 17:06:03 +02:00
|
|
|
end
|
|
|
|
|
2017-09-20 10:52:48 +02:00
|
|
|
def create_avis
|
2018-10-31 14:56:01 +01:00
|
|
|
@new_avis = create_avis_from_params(avis.dossier, avis.confidentiel)
|
2018-02-27 16:58:22 +01:00
|
|
|
|
2018-10-31 14:56:01 +01:00
|
|
|
if @new_avis.nil?
|
2018-02-27 16:58:22 +01:00
|
|
|
redirect_to instruction_gestionnaire_avis_path(avis)
|
|
|
|
else
|
|
|
|
set_avis_and_dossier
|
|
|
|
render :instruction
|
|
|
|
end
|
2017-09-20 10:52:48 +02:00
|
|
|
end
|
|
|
|
|
2017-12-22 17:34:58 +01:00
|
|
|
def sign_up
|
|
|
|
@email = params[:email]
|
|
|
|
@dossier = Avis.includes(:dossier).find(params[:id]).dossier
|
|
|
|
|
2019-01-07 11:59:56 +01:00
|
|
|
render
|
2017-12-22 17:34:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_gestionnaire
|
|
|
|
email = params[:email]
|
|
|
|
password = params['gestionnaire']['password']
|
|
|
|
|
|
|
|
gestionnaire = Gestionnaire.new(email: email, password: password)
|
|
|
|
|
|
|
|
if gestionnaire.save
|
2018-04-12 11:27:25 +02:00
|
|
|
user = User.find_by(email: email)
|
|
|
|
if user.blank?
|
2018-10-25 15:07:15 +02:00
|
|
|
user = User.create(email: email, password: password, confirmed_at: Time.zone.now)
|
2018-04-12 11:27:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
sign_in(user)
|
2017-12-22 17:34:58 +01:00
|
|
|
sign_in(gestionnaire, scope: :gestionnaire)
|
2018-04-12 11:27:25 +02:00
|
|
|
|
2017-12-22 17:34:58 +01:00
|
|
|
Avis.link_avis_to_gestionnaire(gestionnaire)
|
2018-02-21 15:45:21 +01:00
|
|
|
redirect_to url_for(gestionnaire_avis_index_path)
|
2017-12-22 17:34:58 +01:00
|
|
|
else
|
|
|
|
flash[:alert] = gestionnaire.errors.full_messages
|
2018-02-21 13:42:12 +01:00
|
|
|
redirect_to url_for(sign_up_gestionnaire_avis_path(params[:id], email))
|
2017-12-22 17:34:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-06 11:26:32 +02:00
|
|
|
private
|
|
|
|
|
2017-11-07 17:53:11 +01:00
|
|
|
def set_avis_and_dossier
|
|
|
|
@avis = avis
|
|
|
|
@dossier = avis.dossier
|
|
|
|
end
|
|
|
|
|
2017-12-22 17:34:58 +01:00
|
|
|
def redirect_if_no_sign_up_needed
|
|
|
|
avis = Avis.find(params[:id])
|
|
|
|
|
|
|
|
if current_gestionnaire.present?
|
|
|
|
# a gestionnaire is authenticated ... lets see if it can view the dossier
|
|
|
|
|
2018-02-21 15:45:21 +01:00
|
|
|
redirect_to gestionnaire_avis_url(avis)
|
2018-05-30 18:45:46 +02:00
|
|
|
elsif avis.gestionnaire&.email == params[:email]
|
2017-12-22 17:34:58 +01:00
|
|
|
# the avis gestionnaire has already signed up and it sould sign in
|
|
|
|
|
|
|
|
redirect_to new_gestionnaire_session_url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_avis_exists_and_email_belongs_to_avis
|
|
|
|
if !Avis.avis_exists_and_email_belongs_to_avis?(params[:id], params[:email])
|
|
|
|
redirect_to url_for(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-06 11:26:32 +02:00
|
|
|
def avis
|
2017-09-06 17:06:03 +02:00
|
|
|
current_gestionnaire.avis.includes(dossier: [:avis, :commentaires]).find(params[:id])
|
2017-09-06 11:26:32 +02:00
|
|
|
end
|
2017-09-06 16:14:03 +02:00
|
|
|
|
|
|
|
def avis_params
|
|
|
|
params.require(:avis).permit(:answer)
|
|
|
|
end
|
2017-09-06 17:06:03 +02:00
|
|
|
|
|
|
|
def commentaire_params
|
2017-11-07 17:53:11 +01:00
|
|
|
params.require(:commentaire).permit(:body, :file)
|
2017-09-06 17:06:03 +02:00
|
|
|
end
|
2017-09-06 11:21:29 +02:00
|
|
|
end
|
|
|
|
end
|