Work on deletion mail

This commit is contained in:
simon lehericey 2019-11-28 18:03:23 +01:00
parent 79bfb8b143
commit 006e426a11
7 changed files with 53 additions and 4 deletions

View file

@ -77,6 +77,10 @@ class DossierMailer < ApplicationMailer
mail(to: user.email, subject: @subject)
end
def notify_brouillon_deletion(user, dossiers)
def notify_brouillon_deletion(user, dossier_hashes)
@subject = default_i18n_subject(count: dossier_hashes.count)
@dossier_hashes = dossier_hashes
mail(to: user.email, subject: @subject)
end
end

View file

@ -587,6 +587,10 @@ class Dossier < ApplicationRecord
Dossier.where(id: champs.filter(&:dossier_link?).map(&:value).compact)
end
def hash_for_deletion_mail
{ id: self.id, procedure_libelle: self.procedure.libelle }
end
private
def log_dossier_operation(author, operation, subject = nil)
@ -657,11 +661,12 @@ class Dossier < ApplicationRecord
expired_brouillons = Dossier.expired_brouillon
expired_brouillons
.includes(:user)
.includes(:procedure, :user)
.group_by(&:user)
.each do |(user, dossiers)|
DossierMailer.notify_brouillon_deletion(user, dossiers).deliver_later
dossier_hashes = dossiers.map(&:hash_for_deletion_mail)
DossierMailer.notify_brouillon_deletion(user, dossier_hashes).deliver_later
dossiers.each do |dossier|
DeletedDossier.create_from_dossier(dossier)

View file

@ -0,0 +1,12 @@
- content_for(:title, "#{@subject}")
%p
Bonjour,
%p= t('.automatic_dossier_deletion', count: @dossier_hashes.count)
%ul
- @dossier_hashes.each do |d|
%li n° #{d[:id]} (#{d[:procedure_libelle]})
= render partial: "layouts/mailers/signature"

View file

@ -0,0 +1,9 @@
fr:
dossier_mailer:
notify_brouillon_deletion:
subject:
one: "Un dossier en brouillon a été supprimé automatiquement"
other: "Des dossiers en brouillon ont été supprimés automatiquement"
automatic_dossier_deletion:
one: "Le délai maximum de conservation du dossier en brouillon suivant a été atteint, il a donc été supprimé :"
other: "Le délai maximum de conservation des dossiers en brouillon suivants a été atteint, ils ont donc été supprimés :"

View file

@ -97,4 +97,13 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.body).to include(" #{dossier.id} ") }
it { expect(subject.body).to include(dossier.procedure.libelle) }
end
describe '.notify_brouillon_deletion' do
let(:dossier) { create(:dossier) }
subject { described_class.notify_brouillon_deletion(dossier.user, [dossier.hash_for_deletion_mail]) }
it { expect(subject.subject).to eq("Un dossier en brouillon a été supprimé automatiquement") }
it { expect(subject.body).to include(" #{dossier.id} (#{dossier.procedure.libelle})") }
end
end

View file

@ -27,6 +27,16 @@ class DossierMailerPreview < ActionMailer::Preview
def notify_brouillons_near_deletion
DossierMailer.notify_brouillon_near_deletion(User.new(email: "usager@example.com"), [dossier, dossier])
end
def notify_brouillon_deletion
DossierMailer.notify_brouillon_deletion(User.new(email: "usager@example.com"), [dossier.hash_for_deletion_mail])
end
def notify_brouillons_deletion
dossier_hashes = [dossier, dossier].map(&:hash_for_deletion_mail)
DossierMailer.notify_brouillon_deletion(User.new(email: "usager@example.com"), dossier_hashes)
end
private
def deleted_dossier

View file

@ -1092,7 +1092,7 @@ describe Dossier do
it 'notifies deletion' do
expect(DossierMailer).to have_received(:notify_brouillon_deletion).once
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(expired_brouillon.user, [expired_brouillon])
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(expired_brouillon.user, [expired_brouillon.hash_for_deletion_mail])
end
it 'deletes the expired brouillon' do