Merge pull request #10127 from demarches-simplifiees/feat/10005

ETQ expert, je veux pouvoir gérer les notifications que je reçois depuis mon interface
This commit is contained in:
Kara Diaby 2024-03-18 12:27:37 +00:00 committed by GitHub
commit e73318e3bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 182 additions and 13 deletions

View file

@ -59,7 +59,9 @@ module CreateAvisConcern
persisted.each do |avis|
avis.dossier.demander_un_avis!(avis)
if avis.dossier == dossier
AvisMailer.avis_invitation(avis).deliver_later
if avis.experts_procedure.notify_on_new_avis?
AvisMailer.avis_invitation(avis).deliver_later
end
sent_emails_addresses << avis.expert.email
# the email format is already verified, we update value to nil
avis.update_column(:email, nil)

View file

@ -4,10 +4,11 @@ module Experts
include Zipline
before_action :authenticate_expert!, except: [:sign_up, :update_expert]
before_action :check_if_avis_revoked, except: [:index, :procedure]
before_action :check_if_avis_revoked, except: [:index, :procedure, :notification_settings, :update_notification_settings]
before_action :redirect_if_no_sign_up_needed, only: [:sign_up, :update_expert]
before_action :set_avis_and_dossier, only: [:show, :instruction, :avis_list, :avis_new, :messagerie, :create_commentaire, :delete_commentaire, :update, :telecharger_pjs]
before_action :check_messaging_allowed, only: [:messagerie, :create_commentaire]
before_action :set_procedure, only: [:notification_settings, :update_notification_settings]
A_DONNER_STATUS = 'a-donner'
DONNES_STATUS = 'donnes'
@ -66,6 +67,20 @@ module Experts
def avis_list
end
def expert_procedure
ExpertsProcedure.find_by!(expert_id: current_expert.id, procedure_id: @procedure.id)
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
def avis_new
@new_avis = Avis.new
if @dossier.procedure.experts_require_administrateur_invitation?
@ -167,6 +182,15 @@ module Experts
private
def expert_procedure_params
params.require(:experts_procedure)
.permit(:notify_on_new_avis, :notify_on_new_message)
end
def set_procedure
@procedure = current_expert.procedures.find(params[:procedure_id])
end
def check_messaging_allowed
if !@avis.procedure.allow_expert_messaging
flash[:alert] = "Vous n'êtes pas autorisé à acceder à la messagerie"

View file

@ -120,7 +120,8 @@ class Commentaire < ApplicationRecord
experts_contactes = Set.new
dossier.avis.includes(:expert).find_each do |avis|
if avis.expert.present?
expert_procedure = avis.expert.experts_procedures.find_by(procedure_id: dossier.procedure.id)
if expert_procedure.notify_on_new_message? && avis.expert.present?
expert_id = avis.expert.id
if !experts_contactes.include?(expert_id)
AvisMailer.notify_new_commentaire_to_expert(dossier, avis, avis.expert).deliver_later

View file

@ -0,0 +1,60 @@
.sub-header
.fr-container.flex
.procedure-logo{ style: "background-image: url(#{@procedure.logo_url})",
role: 'img', 'aria-label': "logo de la démarche #{@procedure.libelle}" }
.procedure-header
%h1.fr-h3= procedure_libelle @procedure
.fr-container
%h1.fr-h3
= t('.title')
= form_for @expert_procedure, url: update_notification_settings_expert_procedure_path(@procedure), html: { class: 'form' } do |form|
= render Dsfr::AlertComponent.new(state: :info, size: :sm, extra_class_names: 'fr-mb-2w') do |c|
- c.with_body do
%p
= t('.subtitle')
%fieldset.fr-fieldset
%legend.fr-fieldset__legend
= t(".for_each_avis_submitted.title")
%span.fr-hint-text= t('.for_each_avis_submitted.notice_1')
%span.fr-hint-text= t('.for_each_avis_submitted.notice_2')
.fr-fieldset__element.fr-fieldset__element--inline
.fr-radio-group
= form.radio_button :notify_on_new_avis, true, id: 'notify_on_new_avis_true'
%label.fr-label{ for: 'notify_on_new_avis_true' }
= t('.utils.positive')
.fr-fieldset__element.fr-fieldset__element--inline
.fr-radio-group
= form.radio_button :notify_on_new_avis, false, id: 'notify_on_new_avis_false'
%label.fr-label{ for: 'notify_on_new_avis_false' }
= t('.utils.negative')
%fieldset.fr-fieldset
%legend.fr-fieldset__legend
= t(".for_each_message_submitted.title")
%span.fr-hint-text= t('.for_each_message_submitted.notice_1')
%span.fr-hint-text= t('.for_each_message_submitted.notice_2')
.fr-fieldset__element.fr-fieldset__element--inline
.fr-radio-group
= form.radio_button :notify_on_new_message, true, id: 'notify_on_new_message_true'
%label.fr-label{ for: 'notify_on_new_message_true' }
= t('.utils.positive')
.fr-fieldset__element.fr-fieldset__element--inline
.fr-radio-group
= form.radio_button :notify_on_new_message, false, id: 'notify_on_new_message_false'
%label.fr-label{ for: 'notify_on_new_message_false' }
= t('.utils.negative')
%ul.fr-btns-group.fr-btns-group--inline
%li= form.submit t('.buttons.save'), class: "fr-btn"
%li= link_to t('.buttons.back_to_procedure', procedure_id: @procedure.id), procedure_expert_avis_index_path(@procedure), class: 'fr-btn fr-btn--secondary'

View file

@ -10,6 +10,9 @@
.procedure-header
%h1.fr-h3= procedure_libelle @procedure
= link_to t('.management', procedure_id: @procedure.id), notification_settings_expert_procedure_path(@procedure), class: 'header-link'
%nav.fr-tabs
%ul.fr-tabs__list{ role: 'tablist' }

View file

@ -0,0 +1,22 @@
en:
experts:
avis:
procedure:
management: Notification management of the procedure
notification_settings:
utils:
positive: Yes
negative: No
title: Email Notifications
subtitle: Configure on this page the notifications you wish to receive by email
for_each_avis_submitted:
title: Receive a notification for each avis requested from you
notice_1: This email alerts you that an avis is requested from you.
notice_2: It is sent each time an instructor asks you for an avis.
for_each_message_submitted:
title: Receive a notification for each message received
notice_1: This email notifies you that a new message is available in the messaging system.
notice_2: It is sent every time an instructor or user sends a message.
buttons:
back_to_procedure: Return to the procedure notices
save: Save

View file

@ -0,0 +1,22 @@
fr:
experts:
avis:
procedure:
management: Gestion des notifications de la démarche
notification_settings:
utils:
positive: Oui
negative: Non
title: Notifications par email
subtitle: Configurez sur cette page les notifications que vous souhaitez recevoir par email
for_each_avis_submitted:
title: Recevoir une notification à chaque avis qui vous est demandé
notice_1: Cet email vous signale qu'un avis vous est demandé.
notice_2: Il est envoyé à chaque fois quun instructeur vous demande un avis.
for_each_message_submitted:
title: Recevoir une notification à chaque message reçu
notice_1: Cet email vous signale qu'un nouveau message est disponible dans la messagerie.
notice_2: Il est envoyé à chaque fois quun instructeur ou usager envoie un message.
buttons:
back_to_procedure: Revenir sur les avis de la démarche
save: Enregistrer

View file

@ -398,6 +398,9 @@ Rails.application.routes.draw do
resources :procedures, only: [], param: :procedure_id do
member do
get 'notification_settings', to: 'avis#notification_settings'
patch 'update_notification_settings', to: 'avis#update_notification_settings'
resources :avis, only: [:show, :update] do
get '', action: 'procedure', on: :collection, as: :procedure
member do

View file

@ -0,0 +1,6 @@
class AddExpertNotificationSettingsToExpertsProcedures < ActiveRecord::Migration[7.0]
def change
add_column :experts_procedures, :notify_on_new_avis, :boolean, default: true, null: false
add_column :experts_procedures, :notify_on_new_message, :boolean, default: false, null: false
end
end

View file

@ -586,6 +586,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_03_16_065520) do
t.boolean "allow_decision_access", default: false, null: false
t.datetime "created_at", null: false
t.bigint "expert_id", null: false
t.boolean "notify_on_new_avis", default: true, null: false
t.boolean "notify_on_new_message", default: false, null: false
t.bigint "procedure_id", null: false
t.datetime "revoked_at", precision: nil
t.datetime "updated_at", null: false

View file

@ -5,7 +5,7 @@ describe Instructeurs::AvisController, type: :controller do
let(:now) { Time.zone.parse('01/02/2345') }
let(:expert) { create(:expert) }
let(:claimant) { create(:instructeur) }
let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: procedure) }
let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: procedure, notify_on_new_avis: false) }
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, :en_construction, procedure: procedure) }

View file

@ -826,6 +826,13 @@ describe Instructeurs::DossiersController, type: :controller do
it { expect(saved_avis.expert.email).to eq("titi@titimail.com") }
end
context 'when the expert do not want to receive notification' do
let(:emails) { "[\"email@a.com\"]" }
let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: dossier.procedure, notify_on_new_avis: false) }
before { subject }
end
context 'with linked dossiers' do
let(:asked_confidentiel) { false }
let(:previous_avis_confidentiel) { false }

View file

@ -1197,21 +1197,38 @@ describe Users::DossiersController, type: :controller do
end
end
context 'email notification to experts' do
context 'notify on new message to experts' do
let(:expert) { create(:expert) }
let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: procedure) }
let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: procedure, notify_on_new_message: true) }
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur_with_instant_message, experts_procedure: experts_procedure) }
let(:avis2) { create(:avis, dossier: dossier, claimant: instructeur_with_instant_message, experts_procedure: experts_procedure) }
before do
allow(AvisMailer).to receive(:notify_new_commentaire_to_expert).and_return(double(deliver_later: nil))
avis
avis2
subject
context 'when notify_on_new_message is true' do
before do
allow(AvisMailer).to receive(:notify_new_commentaire_to_expert).and_return(double(deliver_later: nil))
avis
avis2
subject
end
it 'sends just one email to the expert linked to several avis on the same dossier' do
expect(AvisMailer).to have_received(:notify_new_commentaire_to_expert).with(dossier, avis, expert).once
end
end
it 'sends just one email to the expert linked to several avis on the same dossier' do
expect(AvisMailer).to have_received(:notify_new_commentaire_to_expert).with(dossier, avis, expert).once
context 'when notify_on_new_message is false' do
let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: procedure, notify_on_new_message: false) }
before do
allow(AvisMailer).to receive(:notify_new_commentaire_to_expert).and_return(double(deliver_later: nil))
avis
avis2
subject
end
it 'does not send any email to the expert' do
expect(AvisMailer).not_to have_received(:notify_new_commentaire_to_expert)
end
end
end