modify experts avis controllers, concern and serializer
This commit is contained in:
parent
5519ee8417
commit
38740d1b5b
4 changed files with 136 additions and 17 deletions
|
@ -3,9 +3,8 @@ module CreateAvisConcern
|
|||
|
||||
private
|
||||
|
||||
def create_avis_from_params(dossier, confidentiel = false)
|
||||
def create_avis_from_params(dossier, instructeur_or_expert, confidentiel = false)
|
||||
confidentiel ||= create_avis_params[:confidentiel]
|
||||
|
||||
# Because of a limitation of the email_field rails helper,
|
||||
# the :emails parameter is a 1-element array.
|
||||
# Hence the call to first
|
||||
|
@ -14,7 +13,7 @@ module CreateAvisConcern
|
|||
allowed_dossiers = [dossier]
|
||||
|
||||
if create_avis_params[:invite_linked_dossiers].present?
|
||||
allowed_dossiers += dossier.linked_dossiers_for(current_instructeur)
|
||||
allowed_dossiers += dossier.linked_dossiers_for(instructeur_or_expert)
|
||||
end
|
||||
|
||||
create_results = Avis.create(
|
||||
|
@ -26,8 +25,7 @@ module CreateAvisConcern
|
|||
email: email,
|
||||
introduction: create_avis_params[:introduction],
|
||||
introduction_file: create_avis_params[:introduction_file],
|
||||
claimant: current_instructeur,
|
||||
claimant_type: current_instructeur.dossiers.present? ? 'Instructeur' : 'Expert',
|
||||
claimant: instructeur_or_expert,
|
||||
dossier: dossier,
|
||||
confidentiel: confidentiel,
|
||||
experts_procedure: experts_procedure
|
||||
|
@ -43,10 +41,11 @@ module CreateAvisConcern
|
|||
sent_emails_addresses = []
|
||||
persisted.each do |avis|
|
||||
avis.dossier.demander_un_avis!(avis)
|
||||
|
||||
if avis.dossier == dossier
|
||||
AvisMailer.avis_invitation(avis).deliver_later
|
||||
sent_emails_addresses << avis.email_to_display
|
||||
sent_emails_addresses << avis.expert.email
|
||||
# the email format is already verified, we update value to nil
|
||||
avis.update_column(:email, nil)
|
||||
end
|
||||
end
|
||||
flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses.uniq.join(", ")}"
|
||||
|
|
|
@ -2,10 +2,9 @@ module Experts
|
|||
class AvisController < ExpertController
|
||||
include CreateAvisConcern
|
||||
|
||||
before_action :authenticate_expert!, except: [:sign_up, :create_instructeur]
|
||||
before_action :authenticate_expert!, except: [:sign_up, :update_expert]
|
||||
before_action :check_if_avis_revoked, only: [:show]
|
||||
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]
|
||||
|
||||
A_DONNER_STATUS = 'a-donner'
|
||||
|
@ -41,8 +40,107 @@ module Experts
|
|||
@new_avis = Avis.new
|
||||
end
|
||||
|
||||
def create_avis
|
||||
@procedure = Procedure.find(params[:procedure_id])
|
||||
if !@procedure.feature_enabled?(:expert_not_allowed_to_invite)
|
||||
@new_avis = create_avis_from_params(avis.dossier, current_expert, avis.confidentiel)
|
||||
|
||||
if @new_avis.nil?
|
||||
redirect_to instruction_expert_avis_path(avis.procedure, avis)
|
||||
else
|
||||
set_avis_and_dossier
|
||||
render :instruction
|
||||
end
|
||||
else
|
||||
flash.alert = "Cette démarche ne vous permet pas de demander un avis externe"
|
||||
redirect_to instruction_expert_avis_path(avis.procedure, avis)
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if @avis.update(avis_params)
|
||||
flash.notice = 'Votre réponse est enregistrée.'
|
||||
@avis.dossier.update!(last_avis_updated_at: Time.zone.now)
|
||||
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]
|
||||
password = params[:user][:password]
|
||||
|
||||
# Not perfect because the password will not be changed if the user already exists
|
||||
user = User.create_or_promote_to_expert(email, password)
|
||||
|
||||
if user.valid?
|
||||
sign_in(user)
|
||||
|
||||
redirect_to url_for(expert_all_avis_path)
|
||||
else
|
||||
flash[:alert] = user.errors.full_messages
|
||||
redirect_to url_for(sign_up_expert_avis_path(procedure_id, avis_id, email))
|
||||
end
|
||||
end
|
||||
|
||||
def messagerie
|
||||
@commentaire = Commentaire.new
|
||||
end
|
||||
|
||||
def create_commentaire
|
||||
@commentaire = CommentaireService.build(current_expert, avis.dossier, commentaire_params)
|
||||
|
||||
if @commentaire.save
|
||||
@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
|
||||
redirect_to instructeur_avis_path(avis)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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?
|
||||
|
||||
redirect_to new_user_session_url
|
||||
end
|
||||
end
|
||||
|
||||
def avis
|
||||
current_expert.avis.includes(dossier: [:avis, :commentaires]).find(params[:id])
|
||||
end
|
||||
|
||||
def check_if_avis_revoked
|
||||
avis = Avis.find(params[:id])
|
||||
if avis.revoked?
|
||||
|
@ -55,5 +153,13 @@ module Experts
|
|||
@avis = Avis.find(params[:id])
|
||||
@dossier = @avis.dossier
|
||||
end
|
||||
|
||||
def avis_params
|
||||
params.require(:avis).permit(:answer, :piece_justificative_file)
|
||||
end
|
||||
|
||||
def commentaire_params
|
||||
params.require(:commentaire).permit(:body, :piece_jointe)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -74,6 +74,7 @@ class Dossier < ApplicationRecord
|
|||
has_many :followers_instructeurs, through: :follows, source: :instructeur
|
||||
has_many :previous_followers_instructeurs, -> { distinct }, through: :previous_follows, source: :instructeur
|
||||
has_many :avis, inverse_of: :dossier, dependent: :destroy
|
||||
has_many :experts, through: :avis
|
||||
has_many :traitements, -> { order(:processed_at) }, inverse_of: :dossier, dependent: :destroy
|
||||
|
||||
has_many :dossier_operation_logs, -> { order(:created_at) }, inverse_of: :dossier
|
||||
|
@ -475,18 +476,32 @@ class Dossier < ApplicationRecord
|
|||
parts.join
|
||||
end
|
||||
|
||||
def avis_for(instructeur)
|
||||
def avis_for_instructeur(instructeur)
|
||||
if instructeur.dossiers.include?(self)
|
||||
avis.order(created_at: :asc)
|
||||
else
|
||||
avis
|
||||
.where(confidentiel: false)
|
||||
.or(avis.where(claimant: instructeur))
|
||||
.or(avis.where(claimant_id: instructeur.id, claimant_type: 'Instructeur'))
|
||||
.or(avis.where(instructeur: instructeur))
|
||||
.order(created_at: :asc)
|
||||
end
|
||||
end
|
||||
|
||||
def avis_for_expert(expert)
|
||||
if expert.dossiers.include?(self)
|
||||
avis.order(created_at: :asc)
|
||||
else
|
||||
instructeur = expert.user.instructeur.id if expert.user.instructeur
|
||||
avis
|
||||
.where(confidentiel: false)
|
||||
.or(avis.where(claimant_id: expert.id, claimant_type: 'Expert', tmp_expert_migrated: true))
|
||||
.or(avis.where(claimant_id: instructeur, claimant_type: 'Instructeur', tmp_expert_migrated: false))
|
||||
.or(avis.where(expert: expert))
|
||||
.order(created_at: :asc)
|
||||
end
|
||||
end
|
||||
|
||||
def owner_name
|
||||
if etablissement.present?
|
||||
etablissement.entreprise_raison_sociale
|
||||
|
@ -811,9 +826,9 @@ class Dossier < ApplicationRecord
|
|||
&& PiecesJustificativesService.pieces_justificatives_total_size(self) < Dossier::TAILLE_MAX_ZIP
|
||||
end
|
||||
|
||||
def linked_dossiers_for(instructeur)
|
||||
def linked_dossiers_for(instructeur_or_expert)
|
||||
dossier_ids = champs.filter(&:dossier_link?).map(&:value).compact
|
||||
(instructeur.dossiers.where(id: dossier_ids) + instructeur.dossiers_from_avis.where(id: dossier_ids)).uniq
|
||||
instructeur_or_expert.dossiers.where(id: dossier_ids)
|
||||
end
|
||||
|
||||
def hash_for_deletion_mail
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
class AvisSerializer < ActiveModel::Serializer
|
||||
attributes :email,
|
||||
:answer,
|
||||
attributes :answer,
|
||||
:introduction,
|
||||
:created_at,
|
||||
:answered_at
|
||||
|
||||
def email
|
||||
object.email_to_display
|
||||
object.expert.email
|
||||
end
|
||||
|
||||
def created_at
|
||||
|
|
Loading…
Reference in a new issue