2017-04-25 17:02:54 +02:00
|
|
|
class Backoffice::AvisController < ApplicationController
|
2017-05-12 10:22:53 +02:00
|
|
|
before_action :authenticate_gestionnaire!, except: [:sign_up, :create_gestionnaire]
|
2017-05-05 17:38:43 +02:00
|
|
|
before_action :redirect_if_no_sign_up_needed, only: [:sign_up]
|
2017-05-12 10:22:53 +02:00
|
|
|
before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up, :create_gestionnaire]
|
2017-05-02 16:20:16 +02:00
|
|
|
|
2017-04-25 17:02:54 +02:00
|
|
|
def create
|
2017-09-22 17:58:16 +02:00
|
|
|
avis = Avis.new(create_params.merge(claimant: current_gestionnaire, dossier: dossier, confidentiel: true))
|
2017-05-02 16:20:16 +02:00
|
|
|
|
2017-05-18 16:14:26 +02:00
|
|
|
if avis.save
|
2017-09-13 11:42:41 +02:00
|
|
|
flash[:notice] = "Votre demande d'avis a bien été envoyée à #{avis.email_to_display}"
|
2017-05-18 16:14:26 +02:00
|
|
|
end
|
2017-04-25 17:02:54 +02:00
|
|
|
|
|
|
|
redirect_to backoffice_dossier_path(dossier)
|
|
|
|
end
|
|
|
|
|
2017-05-24 18:45:31 +02:00
|
|
|
def update
|
|
|
|
if avis.update(update_params)
|
2017-05-22 17:10:17 +02:00
|
|
|
NotificationService.new('avis', params[:dossier_id]).notify
|
2017-05-24 18:45:31 +02:00
|
|
|
flash[:notice] = 'Merci, votre avis a été enregistré.'
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to backoffice_dossier_path(avis.dossier_id)
|
|
|
|
end
|
|
|
|
|
2017-05-12 10:22:18 +02:00
|
|
|
def sign_up
|
|
|
|
@email = params[:email]
|
|
|
|
@dossier = Avis.includes(:dossier).find(params[:id]).dossier
|
|
|
|
|
|
|
|
render layout: 'new_application'
|
|
|
|
end
|
|
|
|
|
2017-05-12 10:22:53 +02:00
|
|
|
def create_gestionnaire
|
|
|
|
email = params[:email]
|
|
|
|
password = params['gestionnaire']['password']
|
|
|
|
|
|
|
|
gestionnaire = Gestionnaire.new(email: email, password: password)
|
|
|
|
|
|
|
|
if gestionnaire.save
|
|
|
|
sign_in(gestionnaire, scope: :gestionnaire)
|
|
|
|
Avis.link_avis_to_gestionnaire(gestionnaire)
|
|
|
|
avis = Avis.find(params[:id])
|
2017-12-19 15:08:08 +01:00
|
|
|
redirect_to url_for(avis_index_path)
|
2017-05-12 10:22:53 +02:00
|
|
|
else
|
2017-06-21 16:16:40 +02:00
|
|
|
flash[:alert] = gestionnaire.errors.full_messages
|
2017-05-12 10:22:53 +02:00
|
|
|
redirect_to url_for(avis_sign_up_path(params[:id], email))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-25 17:02:54 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def dossier
|
|
|
|
current_gestionnaire.dossiers.find(params[:dossier_id])
|
|
|
|
end
|
|
|
|
|
2017-05-24 18:45:31 +02:00
|
|
|
def avis
|
|
|
|
current_gestionnaire.avis.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2017-04-25 17:02:54 +02:00
|
|
|
def create_params
|
|
|
|
params.require(:avis).permit(:email, :introduction)
|
|
|
|
end
|
|
|
|
|
2017-05-24 18:45:31 +02:00
|
|
|
def update_params
|
|
|
|
params.require(:avis).permit(:answer)
|
|
|
|
end
|
|
|
|
|
2017-05-05 17:38:43 +02: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
|
|
|
|
|
|
|
|
redirect_to backoffice_dossier_url(avis.dossier)
|
|
|
|
elsif avis.gestionnaire.present? && avis.gestionnaire.email == params[:email]
|
|
|
|
# the avis gestionnaire has already signed up and it sould sign in
|
|
|
|
|
|
|
|
redirect_to new_gestionnaire_session_url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-12 10:22:18 +02: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-04-25 17:02:54 +02:00
|
|
|
end
|