demarches-normaliennes/app/controllers/instructeurs/avis_controller.rb

145 lines
3.9 KiB
Ruby
Raw Normal View History

module Instructeurs
class AvisController < InstructeurController
include CreateAvisConcern
before_action :authenticate_instructeur!, except: [:sign_up, :create_instructeur]
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_instructeur]
before_action :set_avis_and_dossier, only: [:show, :instruction, :messagerie, :create_commentaire, :update]
2017-09-06 11:21:29 +02:00
A_DONNER_STATUS = 'a-donner'
DONNES_STATUS = 'donnes'
def index
instructeur_avis = current_instructeur.avis.includes(dossier: [:procedure, :user])
@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
@new_avis = Avis.new
2017-09-06 16:14:03 +02:00
end
def update
if @avis.update(avis_params)
flash.notice = 'Votre réponse est enregistrée.'
redirect_to instruction_instructeur_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
@commentaire = Commentaire.new
2017-09-06 17:06:03 +02:00
end
def create_commentaire
@commentaire = CommentaireService.build(current_instructeur, avis.dossier, commentaire_params)
if @commentaire.save
flash.notice = "Message envoyé"
redirect_to messagerie_instructeur_avis_path(avis)
else
flash.alert = @commentaire.errors.full_messages
render :messagerie
end
2017-09-06 17:06:03 +02:00
end
def create_avis
@new_avis = create_avis_from_params(avis.dossier, avis.confidentiel)
if @new_avis.nil?
redirect_to instruction_instructeur_avis_path(avis)
else
set_avis_and_dossier
render :instruction
end
end
def sign_up
@email = params[:email]
@dossier = Avis.includes(:dossier).find(params[:id]).dossier
render
end
def create_instructeur
email = params[:email]
password = params['instructeur']['password']
instructeur = Instructeur.new(email: email, password: password)
if instructeur.save
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)
end
sign_in(user)
sign_in(instructeur, scope: :instructeur)
Avis.link_avis_to_instructeur(instructeur)
redirect_to url_for(instructeur_avis_index_path)
else
flash[:alert] = instructeur.errors.full_messages
redirect_to url_for(sign_up_instructeur_avis_path(params[:id], email))
end
end
2017-09-06 11:26:32 +02:00
private
def set_avis_and_dossier
@avis = avis
@dossier = avis.dossier
end
def redirect_if_no_sign_up_needed
avis = Avis.find(params[:id])
if current_instructeur.present?
# a instructeur is authenticated ... lets see if it can view the dossier
redirect_to instructeur_avis_url(avis)
elsif avis.instructeur&.email == params[:email]
# the avis instructeur has already signed up and it sould sign in
redirect_to new_instructeur_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
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
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
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