2019-08-06 11:02:54 +02:00
|
|
|
module Instructeurs
|
|
|
|
class AvisController < InstructeurController
|
2018-10-31 14:56:01 +01:00
|
|
|
include CreateAvisConcern
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
before_action :authenticate_instructeur!, except: [:sign_up, :create_instructeur]
|
2020-07-16 11:14:37 +02:00
|
|
|
before_action :check_if_avis_revoked, only: [:show]
|
2017-12-22 17:34:58 +01:00
|
|
|
before_action :redirect_if_no_sign_up_needed, only: [:sign_up]
|
2019-08-06 11:02:54 +02:00
|
|
|
before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up, :create_instructeur]
|
2020-10-12 09:24:34 +02: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'
|
|
|
|
|
2020-06-26 20:02:01 +02:00
|
|
|
def index
|
2020-06-25 10:05:39 +02:00
|
|
|
avis = current_instructeur.avis.includes(dossier: [groupe_instructeur: :procedure])
|
|
|
|
@avis_by_procedure = avis.to_a.group_by(&:procedure)
|
|
|
|
end
|
|
|
|
|
2020-06-26 19:28:28 +02:00
|
|
|
def procedure
|
2020-06-24 22:25:10 +02:00
|
|
|
@procedure = Procedure.find(params[:procedure_id])
|
|
|
|
instructeur_avis = current_instructeur.avis.includes(:dossier).where(dossiers: { groupe_instructeur: GroupeInstructeur.where(procedure: @procedure.id) })
|
2019-08-06 11:02:54 +02:00
|
|
|
@avis_a_donner = instructeur_avis.without_answer
|
|
|
|
@avis_donnes = instructeur_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.'
|
2020-07-22 16:57:00 +02:00
|
|
|
@avis.dossier.update!(last_avis_updated_at: Time.zone.now)
|
2020-04-20 14:47:24 +02:00
|
|
|
redirect_to instruction_instructeur_avis_path(@avis.procedure, @avis)
|
2018-02-27 16:58:22 +01:00
|
|
|
else
|
|
|
|
flash.now.alert = @avis.errors.full_messages
|
|
|
|
@new_avis = Avis.new
|
|
|
|
render :instruction
|
|
|
|
end
|
2017-09-06 16:14:03 +02:00
|
|
|
end
|
|
|
|
|
2020-10-12 09:24:34 +02:00
|
|
|
def messagerie
|
|
|
|
@commentaire = Commentaire.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_commentaire
|
|
|
|
@commentaire = CommentaireService.build(current_instructeur, avis.dossier, commentaire_params)
|
|
|
|
|
|
|
|
if @commentaire.save
|
|
|
|
@commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
|
|
|
|
flash.notice = "Message envoyé"
|
|
|
|
redirect_to messagerie_instructeur_avis_path(avis.procedure, avis)
|
|
|
|
else
|
|
|
|
flash.alert = @commentaire.errors.full_messages
|
|
|
|
render :messagerie
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-20 10:52:48 +02:00
|
|
|
def create_avis
|
2020-10-01 18:29:53 +02:00
|
|
|
@procedure = Procedure.find(params[:procedure_id])
|
|
|
|
if !feature_enabled_for?(:expert_not_allowed_to_invite, @procedure)
|
|
|
|
@new_avis = create_avis_from_params(avis.dossier, avis.confidentiel)
|
|
|
|
|
|
|
|
if @new_avis.nil?
|
|
|
|
redirect_to instruction_instructeur_avis_path(avis.procedure, avis)
|
|
|
|
else
|
|
|
|
set_avis_and_dossier
|
|
|
|
render :instruction
|
|
|
|
end
|
2018-02-27 16:58:22 +01:00
|
|
|
else
|
2020-10-01 18:29:53 +02:00
|
|
|
flash.alert = "Cette démarche ne vous permet pas de demander un avis externe"
|
|
|
|
redirect_to instruction_instructeur_avis_path(avis.procedure, avis)
|
2018-02-27 16:58:22 +01:00
|
|
|
end
|
2017-09-20 10:52:48 +02:00
|
|
|
end
|
|
|
|
|
2020-05-11 12:55:59 +02:00
|
|
|
def bilans_bdf
|
2020-06-04 13:55:12 +02:00
|
|
|
if avis.dossier.etablissement&.entreprise_bilans_bdf.present?
|
|
|
|
extension = params[:format]
|
|
|
|
render extension.to_sym => avis.dossier.etablissement.entreprise_bilans_bdf_to_sheet(extension)
|
2020-05-11 12:55:59 +02:00
|
|
|
else
|
|
|
|
redirect_to instructeur_avis_path(avis)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
def create_instructeur
|
2020-05-20 17:17:39 +02:00
|
|
|
procedure_id = params[:procedure_id]
|
|
|
|
avis_id = params[:id]
|
2017-12-22 17:34:58 +01:00
|
|
|
email = params[:email]
|
2019-10-21 11:24:43 +02:00
|
|
|
password = params[:user][:password]
|
2017-12-22 17:34:58 +01:00
|
|
|
|
2019-08-14 17:14:19 +02:00
|
|
|
# Not perfect because the password will not be changed if the user already exists
|
|
|
|
user = User.create_or_promote_to_instructeur(email, password)
|
2018-04-12 11:27:25 +02:00
|
|
|
|
2019-08-14 17:14:19 +02:00
|
|
|
if user.valid?
|
2018-04-12 11:27:25 +02:00
|
|
|
sign_in(user)
|
|
|
|
|
2019-08-14 17:14:19 +02:00
|
|
|
Avis.link_avis_to_instructeur(user.instructeur)
|
2020-04-16 17:52:58 +02:00
|
|
|
redirect_to url_for(instructeur_all_avis_path)
|
2017-12-22 17:34:58 +01:00
|
|
|
else
|
2019-08-07 16:22:17 +02:00
|
|
|
flash[:alert] = user.errors.full_messages
|
2020-05-20 17:17:39 +02:00
|
|
|
redirect_to url_for(sign_up_instructeur_avis_path(procedure_id, avis_id, email))
|
2017-12-22 17:34:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-16 11:14:37 +02:00
|
|
|
def revoquer
|
|
|
|
avis = Avis.find(params[:id])
|
2020-07-16 20:42:50 +02:00
|
|
|
if avis.revoke_by!(current_instructeur)
|
2020-07-16 11:14:37 +02:00
|
|
|
flash.notice = "#{avis.email_to_display} ne peut plus donner son avis sur ce dossier."
|
|
|
|
redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-20 16:26:16 +02:00
|
|
|
def revive
|
|
|
|
avis = Avis.find(params[:id])
|
2020-07-21 13:14:07 +02:00
|
|
|
if avis.revivable_by?(current_instructeur)
|
|
|
|
if avis.answer.blank?
|
|
|
|
AvisMailer.avis_invitation(avis).deliver_later
|
|
|
|
flash.notice = "Un mail de relance a été envoyé à #{avis.email_to_display}"
|
|
|
|
redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier))
|
|
|
|
else
|
|
|
|
flash.alert = "#{avis.email} a déjà donné son avis"
|
|
|
|
redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier))
|
|
|
|
end
|
2020-07-20 16:26:16 +02: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])
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
if current_instructeur.present?
|
|
|
|
# a instructeur is authenticated ... lets see if it can view the dossier
|
2017-12-22 17:34:58 +01:00
|
|
|
|
2020-05-20 16:32:06 +02:00
|
|
|
redirect_to instructeur_avis_url(avis.procedure, avis)
|
2019-08-06 11:02:54 +02:00
|
|
|
elsif avis.instructeur&.email == params[:email]
|
|
|
|
# the avis instructeur has already signed up and it sould sign in
|
2017-12-22 17:34:58 +01:00
|
|
|
|
2019-08-08 10:19:47 +02:00
|
|
|
redirect_to new_user_session_url
|
2017-12-22 17:34:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-16 11:14:37 +02:00
|
|
|
def check_if_avis_revoked
|
|
|
|
avis = Avis.find(params[:id])
|
|
|
|
if avis.revoked?
|
|
|
|
flash.alert = "Vous n'avez plus accès à ce dossier."
|
|
|
|
redirect_to url_for(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-22 17:34:58 +01:00
|
|
|
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
|
2019-08-06 11:02:54 +02:00
|
|
|
current_instructeur.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
|
2019-03-01 17:16:56 +01:00
|
|
|
params.require(:avis).permit(:answer, :piece_justificative_file)
|
2017-09-06 16:14:03 +02:00
|
|
|
end
|
2017-09-06 17:06:03 +02:00
|
|
|
|
|
|
|
def commentaire_params
|
2019-06-25 17:12:44 +02:00
|
|
|
params.require(:commentaire).permit(:body, :piece_jointe)
|
2017-09-06 17:06:03 +02:00
|
|
|
end
|
2017-09-06 11:21:29 +02:00
|
|
|
end
|
|
|
|
end
|