Merge pull request #5029 from betagouv/5015-recevoir-notif-apres-depot

5015: Les instructeurs qui le souhaitent reçoivent notif par mail après chaque dépôt de dossier
This commit is contained in:
krichtof 2020-04-09 17:25:15 +02:00 committed by GitHub
commit 8dc0814727
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 72 additions and 2 deletions

View file

@ -254,7 +254,8 @@ module Instructeurs
private private
def assign_to_params def assign_to_params
params.require(:assign_to).permit(:instant_email_message_notifications_enabled, :daily_email_notifications_enabled, :weekly_email_notifications_enabled) params.require(:assign_to)
.permit(:instant_email_dossier_notifications_enabled, :instant_email_message_notifications_enabled, :daily_email_notifications_enabled, :weekly_email_notifications_enabled)
end end
def assign_exports def assign_exports

View file

@ -153,6 +153,9 @@ module Users
if passage_en_construction? && errors.blank? if passage_en_construction? && errors.blank?
@dossier.en_construction! @dossier.en_construction!
NotificationMailer.send_initiated_notification(@dossier).deliver_later NotificationMailer.send_initiated_notification(@dossier).deliver_later
@dossier.procedure.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
end
return redirect_to(merci_dossier_path(@dossier)) return redirect_to(merci_dossier_path(@dossier))
elsif errors.present? elsif errors.present?
flash.now.alert = errors flash.now.alert = errors

View file

@ -35,6 +35,12 @@ class DossierMailer < ApplicationMailer
mail(from: NO_REPLY_EMAIL, to: instructeur_email, subject: @subject) mail(from: NO_REPLY_EMAIL, to: instructeur_email, subject: @subject)
end end
def notify_new_dossier_depose_to_instructeur(dossier, instructeur_email)
@dossier = dossier
@subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle)
mail(from: NO_REPLY_EMAIL, to: instructeur_email, subject: @subject)
end
def notify_revert_to_instruction(dossier) def notify_revert_to_instruction(dossier)
@dossier = dossier @dossier = dossier
@service = dossier.procedure.service @service = dossier.procedure.service

View file

@ -23,6 +23,10 @@ class Instructeur < ApplicationRecord
includes(:assign_to).where(assign_tos: { instant_email_message_notifications_enabled: true }) includes(:assign_to).where(assign_tos: { instant_email_message_notifications_enabled: true })
} }
scope :with_instant_email_dossier_notifications, -> {
includes(:assign_to).where(assign_tos: { instant_email_dossier_notifications_enabled: true })
}
default_scope { eager_load(:user) } default_scope { eager_load(:user) }
def self.by_email(email) def self.by_email(email)

View file

@ -26,6 +26,7 @@ class Procedure < ApplicationRecord
has_many :administrateurs_procedures has_many :administrateurs_procedures
has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! } has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! }
has_many :groupe_instructeurs, dependent: :destroy has_many :groupe_instructeurs, dependent: :destroy
has_many :instructeurs, through: :groupe_instructeurs
has_many :dossiers, through: :groupe_instructeurs, dependent: :restrict_with_exception has_many :dossiers, through: :groupe_instructeurs, dependent: :restrict_with_exception

View file

@ -0,0 +1,10 @@
- content_for(:title, "#{@subject}")
%p
Bonjour,
%p
= t('.body', dossier_id: @dossier.id, libelle_demarche: @dossier.procedure.libelle)
%p= link_to("Consulter le dossier n°#{@dossier.id}", instructeur_dossier_url(procedure_id: @dossier.procedure.id, dossier_id: @dossier.id))
= render partial: "layouts/mailers/signature"

View file

@ -11,6 +11,22 @@
.explication .explication
Configurez sur cette page les notifications que vous souhaitez recevoir par email pour cette démarche. Configurez sur cette page les notifications que vous souhaitez recevoir par email pour cette démarche.
= form.label :email_notification, "Recevoir une notification à chaque dossier déposé"
%p.notice
Cet email vous signale le dépôt d'un nouveau dossier.
%p.notice
Il est envoyé à chaque fois qu'un usager dépose un dossier.
.radios
%label
= form.radio_button :instant_email_dossier_notifications_enabled, true
Oui
%label
= form.radio_button :instant_email_dossier_notifications_enabled, false
Non
= form.label :email_notification, "Recevoir une notification à chaque message déposé" = form.label :email_notification, "Recevoir une notification à chaque message déposé"
%p.notice %p.notice

View file

@ -0,0 +1,5 @@
fr:
dossier_mailer:
notify_new_dossier_depose_to_instructeur:
subject: Nouveau dossier déposé pour la démarche %{libelle_demarche}
body: Un nouveau dossier a été déposé (n° %{dossier_id}) pour la démarche %{libelle_demarche}

View file

@ -0,0 +1,5 @@
class AddInstantEmailDossierNotificationsToAssignTos < ActiveRecord::Migration[5.2]
def change
add_column :assign_tos, :instant_email_dossier_notifications_enabled, :boolean, default: false, null: false
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_04_07_135256) do ActiveRecord::Schema.define(version: 2020_04_09_075320) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -104,6 +104,7 @@ ActiveRecord::Schema.define(version: 2020_04_07_135256) do
t.boolean "weekly_email_notifications_enabled", default: true, null: false t.boolean "weekly_email_notifications_enabled", default: true, null: false
t.boolean "daily_email_notifications_enabled", default: false, null: false t.boolean "daily_email_notifications_enabled", default: false, null: false
t.boolean "instant_email_message_notifications_enabled", default: false, null: false t.boolean "instant_email_message_notifications_enabled", default: false, null: false
t.boolean "instant_email_dossier_notifications_enabled", default: false, null: false
t.index ["groupe_instructeur_id", "instructeur_id"], name: "unique_couple_groupe_instructeur_instructeur", unique: true t.index ["groupe_instructeur_id", "instructeur_id"], name: "unique_couple_groupe_instructeur_instructeur", unique: true
t.index ["groupe_instructeur_id"], name: "index_assign_tos_on_groupe_instructeur_id" t.index ["groupe_instructeur_id"], name: "index_assign_tos_on_groupe_instructeur_id"
t.index ["instructeur_id", "procedure_id"], name: "index_assign_tos_on_instructeur_id_and_procedure_id", unique: true t.index ["instructeur_id", "procedure_id"], name: "index_assign_tos_on_instructeur_id_and_procedure_id", unique: true

View file

@ -421,6 +421,24 @@ describe Users::DossiersController, type: :controller do
expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction))
end end
context 'with instructeurs ok to be notified instantly' do
let!(:instructeur_with_instant_email_dossier) { create(:instructeur) }
let!(:instructeur_without_instant_email_dossier) { create(:instructeur) }
before do
allow(DossierMailer).to receive(:notify_new_dossier_depose_to_instructeur).and_return(double(deliver_later: nil))
create(:assign_to, instructeur: instructeur_with_instant_email_dossier, procedure: dossier.procedure, instant_email_dossier_notifications_enabled: true, groupe_instructeur: dossier.procedure.defaut_groupe_instructeur)
create(:assign_to, instructeur: instructeur_without_instant_email_dossier, procedure: dossier.procedure, instant_email_dossier_notifications_enabled: false, groupe_instructeur: dossier.procedure.defaut_groupe_instructeur)
end
it "sends notification mail to instructeurs" do
subject
expect(DossierMailer).to have_received(:notify_new_dossier_depose_to_instructeur).once.with(dossier, instructeur_with_instant_email_dossier.email)
expect(DossierMailer).not_to have_received(:notify_new_dossier_depose_to_instructeur).with(dossier, instructeur_without_instant_email_dossier.email)
end
end
context "on an closed procedure" do context "on an closed procedure" do
before { dossier.procedure.close! } before { dossier.procedure.close! }