2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-02-09 10:24:13 +01:00
|
|
|
|
module Experts
|
|
|
|
|
class AvisController < ExpertController
|
|
|
|
|
include CreateAvisConcern
|
2021-09-27 15:07:32 +02:00
|
|
|
|
include Zipline
|
2021-02-09 10:24:13 +01:00
|
|
|
|
|
2021-02-25 10:10:24 +01:00
|
|
|
|
before_action :authenticate_expert!, except: [:sign_up, :update_expert]
|
2024-03-15 17:42:41 +01:00
|
|
|
|
before_action :check_if_avis_revoked, except: [:index, :procedure, :notification_settings, :update_notification_settings]
|
2021-06-29 12:42:18 +02:00
|
|
|
|
before_action :redirect_if_no_sign_up_needed, only: [:sign_up, :update_expert]
|
2023-03-16 10:46:10 +01:00
|
|
|
|
before_action :set_avis_and_dossier, only: [:show, :instruction, :avis_list, :avis_new, :messagerie, :create_commentaire, :delete_commentaire, :update, :telecharger_pjs]
|
2023-03-23 17:14:15 +01:00
|
|
|
|
before_action :check_messaging_allowed, only: [:messagerie, :create_commentaire]
|
2024-03-15 17:42:41 +01:00
|
|
|
|
before_action :set_procedure, only: [:notification_settings, :update_notification_settings]
|
2021-02-09 10:24:13 +01:00
|
|
|
|
|
|
|
|
|
A_DONNER_STATUS = 'a-donner'
|
|
|
|
|
DONNES_STATUS = 'donnes'
|
|
|
|
|
|
|
|
|
|
def index
|
2023-05-22 10:49:25 +02:00
|
|
|
|
avis = current_expert.avis
|
|
|
|
|
.not_revoked
|
2023-10-10 14:57:05 +02:00
|
|
|
|
.includes(:dossier)
|
|
|
|
|
.includes(procedure: { logo_attachment: :blob })
|
2023-05-22 10:49:25 +02:00
|
|
|
|
.not_hidden_by_administration
|
2021-02-09 10:24:13 +01:00
|
|
|
|
@avis_by_procedure = avis.to_a.group_by(&:procedure)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def procedure
|
2022-02-28 14:24:41 +01:00
|
|
|
|
@procedure = current_expert.procedures.find_by(id: params[:procedure_id])
|
2022-11-28 15:31:26 +01:00
|
|
|
|
|
|
|
|
|
if @procedure.nil?
|
|
|
|
|
redirect_to(expert_all_avis_path, flash: { alert: "Vous n’avez pas accès à cette démarche." }) and return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expert_avis = current_expert
|
|
|
|
|
.avis
|
|
|
|
|
.not_revoked
|
2023-10-10 14:58:37 +02:00
|
|
|
|
.includes(:procedure)
|
2023-10-10 14:57:05 +02:00
|
|
|
|
.includes(dossier: :user)
|
2022-11-28 15:31:26 +01:00
|
|
|
|
.not_hidden_by_administration
|
|
|
|
|
.where(dossiers: { groupe_instructeur: GroupeInstructeur.where(procedure: @procedure) })
|
|
|
|
|
|
|
|
|
|
if expert_avis.empty?
|
|
|
|
|
redirect_to(expert_all_avis_path, flash: { alert: "Vous n’avez pas accès à cette démarche." }) and return
|
|
|
|
|
end
|
|
|
|
|
|
2023-04-03 17:05:17 +02:00
|
|
|
|
@avis_a_donner = expert_avis.not_termine.without_answer
|
2021-02-09 10:24:13 +01:00
|
|
|
|
@avis_donnes = expert_avis.with_answer
|
|
|
|
|
|
|
|
|
|
@statut = params[:statut].presence || A_DONNER_STATUS
|
|
|
|
|
|
|
|
|
|
@avis = case @statut
|
|
|
|
|
when A_DONNER_STATUS
|
|
|
|
|
@avis_a_donner
|
|
|
|
|
when DONNES_STATUS
|
|
|
|
|
@avis_donnes
|
|
|
|
|
end
|
2022-11-24 14:23:06 +01:00
|
|
|
|
@avis = @avis.by_latest
|
2021-02-09 10:24:13 +01:00
|
|
|
|
@avis = @avis.page([params[:page].to_i, 1].max)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show
|
2023-10-10 14:58:37 +02:00
|
|
|
|
@dossier = dossier_with_champs
|
2021-02-09 10:24:13 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def instruction
|
|
|
|
|
@new_avis = Avis.new
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-14 16:50:25 +01:00
|
|
|
|
def avis_list
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-15 17:42:41 +01:00
|
|
|
|
def expert_procedure
|
2024-03-18 10:48:39 +01:00
|
|
|
|
ExpertsProcedure.find_by!(expert_id: current_expert.id, procedure_id: @procedure.id)
|
2024-03-15 17:42:41 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def notification_settings
|
|
|
|
|
@expert_procedure = expert_procedure
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update_notification_settings
|
|
|
|
|
expert_procedure.update!(expert_procedure_params)
|
|
|
|
|
flash.notice = 'Vos notifications sont enregistrées.'
|
|
|
|
|
redirect_to procedure_expert_avis_index_path(@procedure)
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-16 10:46:10 +01:00
|
|
|
|
def avis_new
|
|
|
|
|
@new_avis = Avis.new
|
2024-04-16 11:12:32 +02:00
|
|
|
|
@experts_emails = Expert.autocomplete_mails(@dossier.procedure)
|
2023-03-16 10:46:10 +01:00
|
|
|
|
end
|
|
|
|
|
|
2021-02-25 10:10:24 +01:00
|
|
|
|
def create_avis
|
|
|
|
|
@procedure = Procedure.find(params[:procedure_id])
|
2022-11-28 22:13:36 +01:00
|
|
|
|
@new_avis = create_avis_from_params(avis.dossier, current_expert, avis.confidentiel)
|
|
|
|
|
|
|
|
|
|
if @new_avis.nil?
|
2021-02-25 10:10:24 +01:00
|
|
|
|
redirect_to instruction_expert_avis_path(avis.procedure, avis)
|
2022-11-28 22:13:36 +01:00
|
|
|
|
else
|
|
|
|
|
set_avis_and_dossier
|
|
|
|
|
render :instruction
|
2021-02-25 10:10:24 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2022-08-30 21:41:04 +02:00
|
|
|
|
updated_recently = @avis.updated_recently?
|
2021-02-25 10:10:24 +01:00
|
|
|
|
if @avis.update(avis_params)
|
|
|
|
|
flash.notice = 'Votre réponse est enregistrée.'
|
|
|
|
|
@avis.dossier.update!(last_avis_updated_at: Time.zone.now)
|
2022-08-30 21:41:04 +02:00
|
|
|
|
if !updated_recently
|
|
|
|
|
@avis.dossier.followers_instructeurs
|
|
|
|
|
.with_instant_expert_avis_email_notifications_enabled
|
|
|
|
|
.each do |instructeur|
|
|
|
|
|
DossierMailer.notify_new_avis_to_instructeur(@avis, instructeur.email).deliver_later
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-02-25 10:10:24 +01:00
|
|
|
|
redirect_to instruction_expert_avis_path(@avis.procedure, @avis)
|
|
|
|
|
else
|
|
|
|
|
flash.now.alert = @avis.errors.full_messages
|
|
|
|
|
@new_avis = Avis.new
|
|
|
|
|
render :instruction
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sign_up
|
|
|
|
|
@email = params[:email]
|
|
|
|
|
@dossier = Avis.includes(:dossier).find(params[:id]).dossier
|
|
|
|
|
|
|
|
|
|
render
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update_expert
|
|
|
|
|
procedure_id = params[:procedure_id]
|
|
|
|
|
avis_id = params[:id]
|
|
|
|
|
email = params[:email]
|
2024-07-17 16:08:13 +02:00
|
|
|
|
|
|
|
|
|
avis = Avis.joins(:procedure, expert: :user)
|
|
|
|
|
.find_by(id: avis_id, procedure: { id: procedure_id }, user: { email: })
|
|
|
|
|
|
|
|
|
|
if avis.nil?
|
|
|
|
|
return redirect_to root_path, alert: "Vous n’avez pas accès à cet avis."
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-25 10:10:24 +01:00
|
|
|
|
password = params[:user][:password]
|
|
|
|
|
|
|
|
|
|
user = User.create_or_promote_to_expert(email, password)
|
2021-06-29 12:41:33 +02:00
|
|
|
|
user.reset_password(password, password)
|
2021-02-25 10:10:24 +01:00
|
|
|
|
|
|
|
|
|
if user.valid?
|
|
|
|
|
sign_in(user)
|
2024-07-23 10:52:51 +02:00
|
|
|
|
user.update!(email_verified_at: Time.zone.now) if user.unverified_email?
|
2021-02-25 10:10:24 +01:00
|
|
|
|
redirect_to url_for(expert_all_avis_path)
|
|
|
|
|
else
|
|
|
|
|
flash[:alert] = user.errors.full_messages
|
2021-06-15 18:44:33 +02:00
|
|
|
|
redirect_to sign_up_expert_avis_path(procedure_id, avis_id, email: email)
|
2021-02-25 10:10:24 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def messagerie
|
|
|
|
|
@commentaire = Commentaire.new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_commentaire
|
2021-11-30 18:56:12 +01:00
|
|
|
|
@commentaire = CommentaireService.create(current_expert, avis.dossier, commentaire_params)
|
2021-02-25 10:10:24 +01:00
|
|
|
|
|
2021-11-30 18:56:12 +01:00
|
|
|
|
if @commentaire.errors.empty?
|
2021-02-25 10:10:24 +01:00
|
|
|
|
@commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
|
|
|
|
|
flash.notice = "Message envoyé"
|
|
|
|
|
redirect_to messagerie_expert_avis_path(avis.procedure, avis)
|
|
|
|
|
else
|
|
|
|
|
flash.alert = @commentaire.errors.full_messages
|
|
|
|
|
render :messagerie
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def bilans_bdf
|
|
|
|
|
if avis.dossier.etablissement&.entreprise_bilans_bdf.present?
|
|
|
|
|
extension = params[:format]
|
|
|
|
|
render extension.to_sym => avis.dossier.etablissement.entreprise_bilans_bdf_to_sheet(extension)
|
|
|
|
|
else
|
2023-03-17 10:50:44 +01:00
|
|
|
|
redirect_to expert_avis_path(avis)
|
2021-02-25 10:10:24 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-27 15:07:32 +02:00
|
|
|
|
def telecharger_pjs
|
2024-02-07 17:33:34 +01:00
|
|
|
|
files = ActiveStorage::DownloadableFile.create_list_from_dossiers(user_profile: current_expert, dossiers: Dossier.where(id: @dossier.id))
|
2022-09-05 11:20:30 +02:00
|
|
|
|
cleaned_files = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(files)
|
2021-09-27 15:07:32 +02:00
|
|
|
|
|
2022-09-05 11:20:30 +02:00
|
|
|
|
zipline(cleaned_files, "dossier-#{@dossier.id}.zip")
|
2021-09-27 15:07:32 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-02-09 10:24:13 +01:00
|
|
|
|
private
|
|
|
|
|
|
2024-03-15 17:42:41 +01:00
|
|
|
|
def expert_procedure_params
|
|
|
|
|
params.require(:experts_procedure)
|
|
|
|
|
.permit(:notify_on_new_avis, :notify_on_new_message)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_procedure
|
2024-03-18 10:48:39 +01:00
|
|
|
|
@procedure = current_expert.procedures.find(params[:procedure_id])
|
2024-03-15 17:42:41 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-03-23 17:14:15 +01:00
|
|
|
|
def check_messaging_allowed
|
|
|
|
|
if !@avis.procedure.allow_expert_messaging
|
|
|
|
|
flash[:alert] = "Vous n'êtes pas autorisé à acceder à la messagerie"
|
|
|
|
|
redirect_to expert_avis_url(avis.procedure, avis)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-25 10:10:24 +01:00
|
|
|
|
def redirect_if_no_sign_up_needed
|
|
|
|
|
avis = Avis.find(params[:id])
|
|
|
|
|
|
|
|
|
|
if current_expert.present?
|
|
|
|
|
# an expert is authenticated ... lets see if it can view the dossier
|
|
|
|
|
redirect_to expert_avis_url(avis.procedure, avis)
|
|
|
|
|
elsif avis.expert&.email == params[:email] && avis.expert.user.active?.present?
|
2021-06-29 12:42:18 +02:00
|
|
|
|
# The expert already used the sign-in page to change their password: ask them to sign-in instead.
|
2021-02-25 10:10:24 +01:00
|
|
|
|
redirect_to new_user_session_url
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def avis
|
|
|
|
|
current_expert.avis.includes(dossier: [:avis, :commentaires]).find(params[:id])
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-09 10:24:13 +01:00
|
|
|
|
def check_if_avis_revoked
|
|
|
|
|
avis = Avis.find(params[:id])
|
|
|
|
|
if avis.revoked?
|
2021-05-26 15:16:30 +02:00
|
|
|
|
flash.alert = "Vous n’avez plus accès à ce dossier."
|
2021-02-09 10:24:13 +01:00
|
|
|
|
redirect_to url_for(root_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_avis_and_dossier
|
2022-02-28 14:24:41 +01:00
|
|
|
|
@avis = current_expert.avis.find_by(id: params[:id])
|
|
|
|
|
redirect_to(expert_all_avis_path, flash: { alert: "Vous n’avez pas accès à cet avis." }) and return unless @avis
|
2021-02-09 10:24:13 +01:00
|
|
|
|
@dossier = @avis.dossier
|
2023-10-10 14:58:37 +02:00
|
|
|
|
set_sentry_dossier(@dossier)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def dossier_with_champs
|
|
|
|
|
DossierPreloader.load_one(@dossier, pj_template: false)
|
2021-02-09 10:24:13 +01:00
|
|
|
|
end
|
2021-02-25 10:10:24 +01:00
|
|
|
|
|
|
|
|
|
def avis_params
|
2023-03-06 11:05:14 +01:00
|
|
|
|
params.require(:avis).permit(:answer, :piece_justificative_file, :question_answer)
|
2021-02-25 10:10:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def commentaire_params
|
2024-02-13 08:09:58 +01:00
|
|
|
|
params.require(:commentaire).permit(:body, piece_jointe: [])
|
2021-02-25 10:10:24 +01:00
|
|
|
|
end
|
2021-02-09 10:24:13 +01:00
|
|
|
|
end
|
2021-02-25 10:10:24 +01:00
|
|
|
|
end
|