diff --git a/app/controllers/instructeurs/avis_controller.rb b/app/controllers/instructeurs/avis_controller.rb index 28bcd74b1..4b1b72ff4 100644 --- a/app/controllers/instructeurs/avis_controller.rb +++ b/app/controllers/instructeurs/avis_controller.rb @@ -16,15 +16,10 @@ module Instructeurs def revive avis = Avis.find(params[:id]) - 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.expert.email}" - redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier)) - else - flash.alert = "#{avis.expert.email} a déjà donné son avis" - redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier)) - end + if avis.remind_by!(current_instructeur) + AvisMailer.avis_invitation(avis).deliver_later + flash.notice = "Un mail de relance a été envoyé à #{avis.expert.email}" + redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier)) end end end diff --git a/app/models/avis.rb b/app/models/avis.rb index 9efa98164..9c76f649a 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -101,4 +101,9 @@ class Avis < ApplicationRecord destroy! end end + + def remind_by!(revocator) + return false if !revivable_by?(revocator) || answer.present? + update!(revived_at: Time.zone.now) + end end diff --git a/spec/controllers/instructeurs/avis_controller_spec.rb b/spec/controllers/instructeurs/avis_controller_spec.rb index fe4d55b77..0128bc160 100644 --- a/spec/controllers/instructeurs/avis_controller_spec.rb +++ b/spec/controllers/instructeurs/avis_controller_spec.rb @@ -33,6 +33,7 @@ describe Instructeurs::AvisController, type: :controller do get :revive, params: { procedure_id: procedure.id, id: avis.id } expect(AvisMailer).to have_received(:avis_invitation).once.with(avis) expect(flash.notice).to eq("Un mail de relance a été envoyé à #{avis.expert.email}") + expect(avis.reload.reminded_at).to be_present end end end