Cleanup dossier mailers

This commit is contained in:
Paul Chavard 2020-03-19 09:49:25 +01:00
parent e826bfbb63
commit cd0acb1344
23 changed files with 233 additions and 182 deletions

View file

@ -29,20 +29,6 @@ class DossierMailer < ApplicationMailer
end
end
def notify_deletion_to_user(deleted_dossier, to_email)
@deleted_dossier = deleted_dossier
subject = "Votre dossier nº #{@deleted_dossier.dossier_id} a bien été supprimé"
mail(to: to_email, subject: subject)
end
def notify_deletion_to_administration(deleted_dossier, to_email)
@deleted_dossier = deleted_dossier
subject = "Le dossier nº #{@deleted_dossier.dossier_id} a été supprimé à la demande de l'usager"
mail(to: to_email, subject: subject)
end
def notify_revert_to_instruction(dossier)
@dossier = dossier
@service = dossier.procedure.service
@ -55,40 +41,60 @@ class DossierMailer < ApplicationMailer
end
end
def notify_brouillon_near_deletion(user, dossiers)
def notify_brouillon_near_deletion(dossiers, to_email)
@subject = default_i18n_subject(count: dossiers.count)
@dossiers = dossiers
mail(to: user.email, subject: @subject)
mail(to: to_email, subject: @subject)
end
def notify_brouillon_deletion(user, dossier_hashes)
def notify_brouillon_deletion(dossier_hashes, to_email)
@subject = default_i18n_subject(count: dossier_hashes.count)
@dossier_hashes = dossier_hashes
mail(to: user.email, subject: @subject)
mail(to: to_email, subject: @subject)
end
def notify_automatic_deletion_to_user(email, dossier_hashes)
@subject = default_i18n_subject(count: dossier_hashes.count)
@dossier_hashes = dossier_hashes
def notify_deletion_to_user(deleted_dossier, to_email)
@subject = default_i18n_subject(dossier_id: deleted_dossier.dossier_id)
@deleted_dossier = deleted_dossier
mail(to: email, subject: @subject)
mail(to: to_email, subject: @subject)
end
def notify_automatic_deletion_to_administration(email, dossier_hashes)
@subject = default_i18n_subject(count: dossier_hashes.count)
@dossier_hashes = dossier_hashes
def notify_deletion_to_administration(deleted_dossier, to_email)
@subject = default_i18n_subject(dossier_id: deleted_dossier.dossier_id)
@deleted_dossier = deleted_dossier
mail(to: email, subject: @subject)
mail(to: to_email, subject: @subject)
end
def notify_en_construction_near_deletion(email, dossiers, for_user)
def notify_automatic_deletion_to_user(deleted_dossiers, to_email)
@subject = default_i18n_subject(count: deleted_dossiers.count)
@deleted_dossiers = deleted_dossiers
mail(to: to_email, subject: @subject)
end
def notify_automatic_deletion_to_administration(deleted_dossiers, to_email)
@subject = default_i18n_subject(count: deleted_dossiers.count)
@deleted_dossiers = deleted_dossiers
mail(to: to_email, subject: @subject)
end
def notify_en_construction_near_deletion_to_user(dossiers, to_email)
@subject = default_i18n_subject(count: dossiers.count)
@dossiers = dossiers
@for_user = for_user
mail(to: email, subject: @subject)
mail(to: to_email, subject: @subject)
end
def notify_en_construction_near_deletion_to_administration(dossiers, to_email)
@subject = default_i18n_subject(count: dossiers.count)
@dossiers = dossiers
mail(to: to_email, subject: @subject)
end
def notify_groupe_instructeur_changed(instructeur, dossier)

View file

@ -18,7 +18,10 @@ class ExpiredDossiersDeletionService
.includes(:user, :procedure)
.group_by(&:user)
.each do |(user, dossiers)|
DossierMailer.notify_brouillon_near_deletion(user, dossiers).deliver_later
DossierMailer.notify_brouillon_near_deletion(
dossiers,
user.email
).deliver_later
end
dossiers_close_to_expiration.update_all(brouillon_close_to_expiration_notice_sent_at: Time.zone.now)
@ -33,18 +36,16 @@ class ExpiredDossiersDeletionService
.includes(:user)
.group_by(&:user)
.each do |(user, dossiers)|
DossierMailer.notify_en_construction_near_deletion(
user.email,
DossierMailer.notify_en_construction_near_deletion_to_user(
dossiers,
true
user.email
).deliver_later
end
group_by_fonctionnaire_email(dossiers_close_to_expiration).each do |(destinataire, dossiers)|
DossierMailer.notify_en_construction_near_deletion(
destinataire,
group_by_fonctionnaire_email(dossiers_close_to_expiration).each do |(email, dossiers)|
DossierMailer.notify_en_construction_near_deletion_to_administration(
dossiers,
false
email
).deliver_later
end
@ -58,7 +59,10 @@ class ExpiredDossiersDeletionService
.includes(:user, :procedure)
.group_by(&:user)
.each do |(user, dossiers)|
DossierMailer.notify_brouillon_deletion(user, dossiers.map(&:hash_for_deletion_mail)).deliver_later
DossierMailer.notify_brouillon_deletion(
dossiers.map(&:hash_for_deletion_mail),
user.email
).deliver_later
end
dossiers_to_remove.each do |dossier|
@ -70,27 +74,28 @@ class ExpiredDossiersDeletionService
def self.delete_expired_en_construction_and_notify
dossiers_to_remove = Dossier.en_construction_expired
dossiers_to_remove.each do |dossier|
DeletedDossier.create_from_dossier(dossier)
end
dossiers_to_remove
.includes(:user)
.group_by(&:user)
.each do |(user, dossiers)|
DossierMailer.notify_automatic_deletion_to_user(
user.email,
dossiers.map(&:hash_for_deletion_mail)
DeletedDossier.where(dossier_id: dossiers.map(&:id)),
user.email
).deliver_later
end
self.group_by_fonctionnaire_email(dossiers_to_remove).each do |(destinataire, dossiers)|
self.group_by_fonctionnaire_email(dossiers_to_remove).each do |(email, dossiers)|
DossierMailer.notify_automatic_deletion_to_administration(
destinataire,
dossiers.map(&:hash_for_deletion_mail)
DeletedDossier.where(dossier_id: dossiers.map(&:id)),
email
).deliver_later
end
dossiers_to_remove.each do |dossier|
DeletedDossier.create_from_dossier(dossier)
dossier.destroy
end
dossiers_to_remove.destroy_all
end
private

View file

@ -3,10 +3,10 @@
%p
Bonjour,
%p= t('.automatic_dossier_deletion', count: @dossier_hashes.count)
%ul
- @dossier_hashes.each do |d|
%li= "n° #{d[:id]} (#{d[:procedure_libelle]})"
%p
= t('.header', count: @deleted_dossiers.count)
%ul
- @deleted_dossiers.each do |d|
%li n° #{d.dossier_id} (#{d.procedure.libelle})
= render partial: "layouts/mailers/signature"

View file

@ -3,12 +3,12 @@
%p
Bonjour,
%p= t('.automatic_dossier_deletion', count: @dossier_hashes.count)
%p
= t('.header', count: @deleted_dossiers.count)
%ul
- @deleted_dossiers.each do |d|
%li n° #{d.dossier_id} (#{d.procedure.libelle})
%ul
- @dossier_hashes.each do |d|
%li= "n° #{d[:id]} (#{d[:procedure_libelle]})"
%p= t('.dossier_will_not_be_processed', count: @dossier_hashes.count)
%p= t('.footer', count: @deleted_dossiers.count)
= render partial: "layouts/mailers/signature"

View file

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

View file

@ -4,13 +4,11 @@
Bonjour,
%p
Afin de limiter la conservation de vos données personnelles,
= t('.automatic_dossier_deletion', count: @dossiers.count)
= t('.header', count: @dossiers.count)
%ul
- @dossiers.each do |d|
%li= link_to("n° #{d.id} (#{d.procedure.libelle})", dossier_url(d))
%p
#{sanitize(t('.send_your_draft', count: @dossiers.count))}. Et sinon, vous n'avez rien à faire.
%p= sanitize(t('.footer', count: @dossiers.count))
= render partial: "layouts/mailers/signature"

View file

@ -1,11 +1,9 @@
- content_for(:title, "Suppression du dossier n° #{@deleted_dossier.dossier_id}")
- content_for(:title, "#{@subject}")
%p
Bonjour,
%p
À la demande de l'usager, le dossier n° #{@deleted_dossier.dossier_id}
(sur la démarche «&nbsp;#{@deleted_dossier.procedure.libelle}&nbsp;»)
a été supprimé.
= t('.body', dossier_id: @deleted_dossier.dossier_id, procedure: @deleted_dossier.procedure.libelle)
= render partial: "layouts/mailers/signature"

View file

@ -1,11 +1,9 @@
- content_for(:title, "Suppression du dossier n° #{@deleted_dossier.dossier_id}")
- content_for(:title, "#{@subject}")
%p
Bonjour,
%p
Votre dossier n° #{@deleted_dossier.dossier_id}
(«&nbsp;#{@deleted_dossier.procedure.libelle}&nbsp;») a bien été supprimé.
Une trace anonyme de ce traitement sera conservée pour ladministration.
= t('.body', dossier_id: @deleted_dossier.dossier_id, procedure: @deleted_dossier.procedure.libelle)
= render partial: "layouts/mailers/signature"

View file

@ -1,27 +0,0 @@
- content_for(:title, "#{@subject}")
%p
Bonjour,
%p
- if !@for_user
Afin de limiter la conservation de vos données personnelles,
= t('.automatic_dossier_deletion', count: @dossiers.count)
%ul
- @dossiers.each do |d|
- if !@for_user
%li
#{link_to("n° #{d.id} (#{d.procedure.libelle})", dossier_url(d))}. Retrouvez le dossier au format #{link_to("PDF", instructeur_dossier_url(d.procedure, d, format: :pdf))}
- else
%li
#{link_to("n° #{d.id} (#{d.procedure.libelle})", dossier_url(d))}. Retrouvez le dossier au format #{link_to("PDF", dossier_url(d, format: :pdf))}
%p
- if @for_user
= sanitize(t('.send_user_draft', count: @dossiers.count))
- else
= sanitize(t('.send_other_draft', count: @dossiers.count))
= render partial: "layouts/mailers/signature"

View file

@ -0,0 +1,16 @@
- content_for(:title, "#{@subject}")
%p
Bonjour,
%p
= t('.header', count: @dossiers.count)
%ul
- @dossiers.each do |d|
%li
#{link_to("n° #{d.id} (#{d.procedure.libelle})", dossier_url(d))}. Retrouvez le dossier au format #{link_to("PDF", instructeur_dossier_url(d.procedure, d, format: :pdf))}
%p
= sanitize(t('.footer', count: @dossiers.count))
= render partial: "layouts/mailers/signature"

View file

@ -0,0 +1,16 @@
- content_for(:title, "#{@subject}")
%p
Bonjour,
%p
= t('.header', count: @dossiers.count)
%ul
- @dossiers.each do |d|
%li
#{link_to("n° #{d.id} (#{d.procedure.libelle})", dossier_url(d))}. Retrouvez le dossier au format #{link_to("PDF", dossier_url(d, format: :pdf))}
%p
= sanitize(t('.footer', count: @dossiers.count))
= render partial: "layouts/mailers/signature"

View file

@ -4,6 +4,6 @@ fr:
subject:
one: "Un dossier a été supprimé automatiquement"
other: "Des dossiers ont été supprimés automatiquement"
automatic_dossier_deletion:
header:
one: "Le délai maximum de conservation du dossier suivant a été atteint, il a donc été supprimé :"
other: "Le délai maximum de conservation des dossiers suivants a été atteint, ils ont donc été supprimés :"

View file

@ -4,9 +4,9 @@ fr:
subject:
one: "Un dossier a été supprimé automatiquement"
other: "Des dossiers ont été supprimés automatiquement"
automatic_dossier_deletion:
header:
one: "Le délai maximum de conservation du dossier suivant a été atteint, il a donc été supprimé :"
other: "Le délai maximum de conservation des dossiers suivants a été atteint, ils ont donc été supprimés :"
dossier_will_not_be_processed:
footer:
one: "Le dossier ne sera pas traité, nous nous excusons de la gène occasionnée."
other: "Les dossiers ne seront pas traités, nous nous excusons de la gène occasionnée."

View file

@ -4,6 +4,6 @@ fr:
subject:
one: "Un dossier en brouillon a été supprimé automatiquement"
other: "Des dossiers en brouillon ont été supprimés automatiquement"
automatic_dossier_deletion:
header:
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

@ -4,9 +4,9 @@ fr:
subject:
one: Un dossier en brouillon va bientôt être supprimé
other: Des dossiers en brouillon vont bientôt être supprimés
automatic_dossier_deletion:
one: "le dossier en brouillon suivant sera bientôt automatiquement supprimé :"
other: "les dossiers en brouillon suivant seront bientôt automatiquement supprimés :"
send_your_draft:
one: "Si vous souhaitez toujours déposer ce dossier, vous pouvez retrouver votre brouillon pendant encore <b>un mois</b>"
other: "Si vous souhaitez toujours déposer ces dossiers, vous pouvez retrouver vos brouillons pendant encore <b>un mois</b>"
header:
one: "Afin de limiter la conservation de vos données personnelles, le dossier en brouillon suivant sera bientôt automatiquement supprimé :"
other: "Afin de limiter la conservation de vos données personnelles, les dossiers en brouillon suivant seront bientôt automatiquement supprimés :"
footer:
one: "Si vous souhaitez toujours déposer ce dossier, vous pouvez retrouver votre brouillon pendant encore <b>un mois</b>. Et sinon, vous navez rien à faire."
other: "Si vous souhaitez toujours déposer ces dossiers, vous pouvez retrouver vos brouillons pendant encore <b>un mois</b>. Et sinon, vous navez rien à faire."

View file

@ -0,0 +1,5 @@
fr:
dossier_mailer:
notify_deletion_to_administration:
subject: Le dossier nº %{dossier_id} a été supprimé à la demande de lusager
body: À la demande de lusager, le dossier n° %{dossier_id} (%{procedure}) a été supprimé.

View file

@ -0,0 +1,5 @@
fr:
dossier_mailer:
notify_deletion_to_user:
subject: Votre dossier nº %{dossier_id} a bien été supprimé
body: Votre dossier n° %{dossier_id} (%{procedure}) a bien été supprimé. Une trace de ce traitement sera conservée pour ladministration.

View file

@ -1,15 +0,0 @@
fr:
dossier_mailer:
notify_en_construction_near_deletion:
subject:
one: Un dossier en construction va bientôt être supprimé
other: Des dossiers en construction vont bientôt être supprimés
automatic_dossier_deletion:
one: "le dossier en construction suivant sera bientôt automatiquement supprimé :"
other: "les dossiers en construction suivant seront bientôt automatiquement supprimés :"
send_user_draft:
one: "Vous pouvez retrouver votre dossier pendant encore <b>un mois</b>. Vous n'avez rien à faire."
other: "Vous pouvez retrouver vos dossiers pendant encore <b>un mois</b>. Vous n'avez rien à faire."
send_other_draft:
one: "Vous avez <b>un mois</b> pour traiter le dossier."
other: "Vous avez <b>un mois</b> pour traiter les dossiers."

View file

@ -0,0 +1,12 @@
fr:
dossier_mailer:
notify_en_construction_near_deletion_to_administration:
subject:
one: Un dossier en construction va bientôt être supprimé
other: Des dossiers en construction vont bientôt être supprimés
header:
one: "Le dossier en construction suivant sera bientôt automatiquement supprimé :"
other: "Les dossiers en construction suivant seront bientôt automatiquement supprimés :"
footer:
one: "Vous avez <b>un mois</b> pour commencer l'instruction du dossier."
other: "Vous avez <b>un mois</b> pour commencer l'instruction des dossiers."

View file

@ -0,0 +1,12 @@
fr:
dossier_mailer:
notify_en_construction_near_deletion_to_user:
subject:
one: Un dossier en construction va bientôt être supprimé
other: Des dossiers en construction vont bientôt être supprimés
header:
one: "Afin de limiter la conservation de vos données personnelles, le dossier en construction suivant sera bientôt automatiquement supprimé :"
other: "Afin de limiter la conservation de vos données personnelles, les dossiers en construction suivant seront bientôt automatiquement supprimés :"
footer:
one: "Vous pouvez retrouver votre dossier pendant encore <b>un mois</b>. Vous navez rien à faire."
other: "Vous pouvez retrouver vos dossiers pendant encore <b>un mois</b>. Vous navez rien à faire."

View file

@ -55,8 +55,8 @@ RSpec.describe DossierMailer, type: :mailer do
subject { described_class.notify_deletion_to_administration(deleted_dossier, to_email) }
it { expect(subject.subject).to eq("Le dossier nº #{deleted_dossier.dossier_id} a été supprimé à la demande de l'usager") }
it { expect(subject.body).to include("À la demande de l'usager") }
it { expect(subject.subject).to eq("Le dossier nº #{deleted_dossier.dossier_id} a été supprimé à la demande de lusager") }
it { expect(subject.body).to include("À la demande de lusager") }
it { expect(subject.body).to include(deleted_dossier.dossier_id) }
it { expect(subject.body).to include(deleted_dossier.procedure.libelle) }
end
@ -81,7 +81,7 @@ RSpec.describe DossierMailer, type: :mailer do
@date_suppression = dossier.created_at + duree.months
end
subject { described_class.notify_brouillon_near_deletion(dossier.user, [dossier]) }
subject { described_class.notify_brouillon_near_deletion([dossier], dossier.user.email) }
it { expect(subject.body).to include(" #{dossier.id} ") }
it { expect(subject.body).to include(dossier.procedure.libelle) }
@ -90,7 +90,7 @@ RSpec.describe DossierMailer, type: :mailer do
describe '.notify_brouillon_deletion' do
let(:dossier) { create(:dossier) }
subject { described_class.notify_brouillon_deletion(dossier.user, [dossier.hash_for_deletion_mail]) }
subject { described_class.notify_brouillon_deletion([dossier.hash_for_deletion_mail], dossier.user.email) }
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})") }
@ -98,13 +98,14 @@ RSpec.describe DossierMailer, type: :mailer do
describe '.notify_automatic_deletion_to_user' do
let(:dossier) { create(:dossier) }
let(:deleted_dossier) { DeletedDossier.create_from_dossier(dossier) }
before do
duree = dossier.procedure.duree_conservation_dossiers_dans_ds
@date_suppression = dossier.created_at + duree.months
end
subject { described_class.notify_automatic_deletion_to_user(dossier.user.email, [dossier.hash_for_deletion_mail]) }
subject { described_class.notify_automatic_deletion_to_user([deleted_dossier], dossier.user.email) }
it { expect(subject.to).to eq([dossier.user.email]) }
it { expect(subject.subject).to eq("Un dossier a été supprimé automatiquement") }
@ -115,8 +116,9 @@ RSpec.describe DossierMailer, type: :mailer do
describe '.notify_automatic_deletion_to_administration' do
let(:dossier) { create(:dossier) }
let(:deleted_dossier) { DeletedDossier.create_from_dossier(dossier) }
subject { described_class.notify_automatic_deletion_to_administration(dossier.user, [dossier.hash_for_deletion_mail]) }
subject { described_class.notify_automatic_deletion_to_administration([deleted_dossier], dossier.user.email) }
it { expect(subject.subject).to eq("Un dossier a été supprimé automatiquement") }
it { expect(subject.body).to include("#{dossier.id} (#{dossier.procedure.libelle})") }
@ -130,13 +132,13 @@ RSpec.describe DossierMailer, type: :mailer do
@date_suppression = dossier.created_at + duree.months
end
subject { described_class.notify_en_construction_near_deletion(dossier.user.email, [dossier], true) }
subject { described_class.notify_en_construction_near_deletion_to_administration([dossier], dossier.user.email) }
it { expect(subject.subject).to eq("Un dossier en construction va bientôt être supprimé") }
it { expect(subject.body).to include("#{dossier.id} ") }
it { expect(subject.body).to include(dossier.procedure.libelle) }
it { expect(subject.body).to include("PDF") }
it { expect(subject.body).to include("Vous pouvez retrouver votre dossier pendant encore <b>un mois</b>. Vous n'avez rien à faire.") }
it { expect(subject.body).to include("Vous avez <b>un mois</b> pour commencer l'instruction du dossier.") }
end
describe '.notify_en_construction_near_deletion_to_user' do
@ -147,14 +149,14 @@ RSpec.describe DossierMailer, type: :mailer do
@date_suppression = dossier.created_at + duree.months
end
subject { described_class.notify_en_construction_near_deletion(dossier.user.email, [dossier], false) }
subject { described_class.notify_en_construction_near_deletion_to_user([dossier], dossier.user.email) }
it { expect(subject.to).to eq([dossier.user.email]) }
it { expect(subject.subject).to eq("Un dossier en construction va bientôt être supprimé") }
it { expect(subject.body).to include("#{dossier.id} ") }
it { expect(subject.body).to include(dossier.procedure.libelle) }
it { expect(subject.body).to include("PDF") }
it { expect(subject.body).to include("Vous avez <b>un mois</b> pour traiter le dossier.") }
it { expect(subject.body).to include("Vous pouvez retrouver votre dossier pendant encore <b>un mois</b>. Vous navez rien à faire.") }
end
describe '.notify_groupe_instructeur_changed_to_instructeur' do

View file

@ -8,47 +8,61 @@ class DossierMailerPreview < ActionMailer::Preview
DossierMailer.notify_new_answer(dossier)
end
def notify_deletion_to_user
DossierMailer.notify_deletion_to_user(deleted_dossier, "user@ds.fr")
end
def notify_deletion_to_administration
DossierMailer.notify_deletion_to_administration(deleted_dossier, "admin@ds.fr")
end
def notify_revert_to_instruction
DossierMailer.notify_revert_to_instruction(dossier)
end
def notify_brouillon_near_deletion
DossierMailer.notify_brouillon_near_deletion(User.new(email: "usager@example.com"), [dossier])
DossierMailer.notify_brouillon_near_deletion([dossier], usager_email)
end
def notify_brouillons_near_deletion
DossierMailer.notify_brouillon_near_deletion(User.new(email: "usager@example.com"), [dossier, dossier])
DossierMailer.notify_brouillon_near_deletion([dossier, dossier], usager_email)
end
def notify_en_construction_near_deletion_to_user
DossierMailer.notify_en_construction_near_deletion_to_user([dossier], usager_email)
end
def notify_en_construction_near_deletion_to_administration
DossierMailer.notify_en_construction_near_deletion_to_administration([dossier, dossier], administration_email)
end
def notify_brouillon_deletion
DossierMailer.notify_brouillon_deletion(User.new(email: "usager@example.com"), [dossier.hash_for_deletion_mail])
DossierMailer.notify_brouillon_deletion([dossier.hash_for_deletion_mail], usager_email)
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)
DossierMailer.notify_brouillon_deletion(dossier_hashes, usager_email)
end
def notify_deletion_to_user
DossierMailer.notify_deletion_to_user(deleted_dossier, usager_email)
end
def notify_deletion_to_administration
DossierMailer.notify_deletion_to_administration(deleted_dossier, administration_email)
end
def notify_automatic_deletion_to_user
dossier_hashes = [dossier, dossier].map(&:hash_for_deletion_mail)
DossierMailer.notify_automatic_deletion_to_user("usager@example.com", dossier_hashes)
DossierMailer.notify_automatic_deletion_to_user([deleted_dossier, deleted_dossier], usager_email)
end
def notify_automatic_deletion_to_administration
dossier_hashes = [dossier, dossier].map(&:hash_for_deletion_mail)
DossierMailer.notify_automatic_deletion_to_administration("admin@example.com", dossier_hashes)
DossierMailer.notify_automatic_deletion_to_administration([deleted_dossier, deleted_dossier], administration_email)
end
private
def usager_email
"usager@example.com"
end
def administration_email
"administration@example.com"
end
def deleted_dossier
DeletedDossier.new(dossier_id: 1, procedure: procedure)
end
@ -62,7 +76,7 @@ class DossierMailerPreview < ActionMailer::Preview
end
def procedure
Procedure.new(libelle: 'Dotation dÉquipement des Territoires Ruraux - Exercice 2019', service: service, logo: Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png'))
Procedure.new(id: 1234, libelle: 'Dotation dÉquipement des Territoires Ruraux - Exercice 2019', service: service, logo: Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png'))
end
def service

View file

@ -24,7 +24,7 @@ describe ExpiredDossiersDeletionService do
it 'emails should be sent' do
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(brouillon_close_to_expiration.user, [brouillon_close_to_expiration])
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with([brouillon_close_to_expiration], brouillon_close_to_expiration.user.email)
end
it 'dossier state should change' do
@ -33,8 +33,7 @@ describe ExpiredDossiersDeletionService do
it 'deletes and notify expired brouillon' do
expect(DossierMailer).to have_received(:notify_brouillon_deletion).once
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(expired_brouillon.user, [expired_brouillon.hash_for_deletion_mail])
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with([expired_brouillon.hash_for_deletion_mail], expired_brouillon.user.email)
expect(DeletedDossier.find_by(dossier_id: expired_brouillon.id)).to be_present
expect { expired_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
@ -68,7 +67,7 @@ describe ExpiredDossiersDeletionService do
it { expect(dossier.reload.brouillon_close_to_expiration_notice_sent_at).not_to be_nil }
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once }
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(dossier.user, [dossier]) }
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with([dossier], dossier.user.email) }
end
end
@ -80,7 +79,7 @@ describe ExpiredDossiersDeletionService do
before { ExpiredDossiersDeletionService.send_brouillon_expiration_notices }
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once }
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(user, match_array([dossier_1, dossier_2])) }
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(match_array([dossier_1, dossier_2]), user.email) }
end
end
@ -119,7 +118,7 @@ describe ExpiredDossiersDeletionService do
it { expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound) }
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).once }
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(dossier.user, [dossier.hash_for_deletion_mail]) }
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with([dossier.hash_for_deletion_mail], dossier.user.email) }
end
end
@ -131,7 +130,7 @@ describe ExpiredDossiersDeletionService do
before { ExpiredDossiersDeletionService.delete_expired_brouillons_and_notify }
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).once }
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(user, match_array([dossier_1, dossier_2].map(&:hash_for_deletion_mail))) }
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(match_array([dossier_1.hash_for_deletion_mail, dossier_2.hash_for_deletion_mail]), user.email) }
end
end
@ -142,7 +141,8 @@ describe ExpiredDossiersDeletionService do
after { Timecop.return }
before do
allow(DossierMailer).to receive(:notify_en_construction_near_deletion).and_return(double(deliver_later: nil))
allow(DossierMailer).to receive(:notify_en_construction_near_deletion_to_user).and_return(double(deliver_later: nil))
allow(DossierMailer).to receive(:notify_en_construction_near_deletion_to_administration).and_return(double(deliver_later: nil))
end
context 'with a single dossier' do
@ -154,7 +154,8 @@ describe ExpiredDossiersDeletionService do
let(:en_construction_at) { (conservation_par_defaut - 1.month - 1.day).ago }
it { expect(dossier.reload.en_construction_close_to_expiration_notice_sent_at).to be_nil }
it { expect(DossierMailer).not_to have_received(:notify_en_construction_near_deletion) }
it { expect(DossierMailer).not_to have_received(:notify_en_construction_near_deletion_to_user) }
it { expect(DossierMailer).not_to have_received(:notify_en_construction_near_deletion_to_administration) }
end
context 'when the dossier is near deletion' do
@ -162,10 +163,11 @@ describe ExpiredDossiersDeletionService do
it { expect(dossier.reload.en_construction_close_to_expiration_notice_sent_at).not_to be_nil }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).thrice }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(dossier.user.email, [dossier], true) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(dossier.procedure.administrateurs.first.email, [dossier], false) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(dossier.followers_instructeurs.first.email, [dossier], false) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_user).once }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).twice }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_user).with([dossier], dossier.user.email) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).with([dossier], dossier.procedure.administrateurs.first.email) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).with([dossier], dossier.followers_instructeurs.first.email) }
end
end
@ -181,11 +183,12 @@ describe ExpiredDossiersDeletionService do
ExpiredDossiersDeletionService.send_en_construction_expiration_notices
end
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).exactly(4).times }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(user.email, match_array([dossier_1, dossier_2]), true) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(instructeur.email, match_array([dossier_1, dossier_2]), false) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(dossier_1.procedure.administrateurs.first.email, [dossier_1], false) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(dossier_2.procedure.administrateurs.first.email, [dossier_2], false) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_user).once }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).exactly(3).times }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_user).with(match_array([dossier_1, dossier_2]), user.email) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).with(match_array([dossier_1, dossier_2]), instructeur.email) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).with([dossier_1], dossier_1.procedure.administrateurs.first.email) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) }
end
context 'when an instructeur is also administrateur' do
@ -198,9 +201,9 @@ describe ExpiredDossiersDeletionService do
ExpiredDossiersDeletionService.send_en_construction_expiration_notices
end
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).exactly(2).times }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(dossier.user.email, [dossier], true) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion).with(administrateur.email, [dossier], false) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_user).once }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_user).with([dossier], dossier.user.email) }
it { expect(DossierMailer).to have_received(:notify_en_construction_near_deletion_to_administration).with([dossier], administrateur.email) }
end
end
@ -217,6 +220,7 @@ describe ExpiredDossiersDeletionService do
context 'with a single dossier' do
let!(:dossier) { create(:dossier, :en_construction, :followed, en_construction_close_to_expiration_notice_sent_at: notice_sent_at) }
let(:deleted_dossier) { DeletedDossier.find_by(dossier_id: dossier.id) }
before { ExpiredDossiersDeletionService.delete_expired_en_construction_and_notify }
@ -242,11 +246,11 @@ describe ExpiredDossiersDeletionService do
it { expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with(dossier.user.email, [dossier.hash_for_deletion_mail]) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with([deleted_dossier], dossier.user.email) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).twice }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(dossier.procedure.administrateurs.first.email, [dossier.hash_for_deletion_mail]) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(dossier.followers_instructeurs.first.email, [dossier.hash_for_deletion_mail]) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([deleted_dossier], dossier.procedure.administrateurs.first.email) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([deleted_dossier], dossier.followers_instructeurs.first.email) }
end
end
@ -254,6 +258,8 @@ describe ExpiredDossiersDeletionService do
let!(:user) { create(:user) }
let!(:dossier_1) { create(:dossier, :en_construction, user: user, en_construction_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
let!(:dossier_2) { create(:dossier, :en_construction, user: user, en_construction_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
let(:deleted_dossier_1) { DeletedDossier.find_by(dossier_id: dossier_1.id) }
let(:deleted_dossier_2) { DeletedDossier.find_by(dossier_id: dossier_2.id) }
let!(:instructeur) { create(:instructeur) }
@ -263,12 +269,12 @@ describe ExpiredDossiersDeletionService do
end
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with(user.email, match_array([dossier_1, dossier_2].map(&:hash_for_deletion_mail))) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with(match_array([deleted_dossier_1, deleted_dossier_2]), user.email) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).thrice }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(instructeur.email, match_array([dossier_1, dossier_2].map(&:hash_for_deletion_mail))) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(dossier_1.procedure.administrateurs.first.email, [dossier_1.hash_for_deletion_mail]) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(dossier_2.procedure.administrateurs.first.email, [dossier_2.hash_for_deletion_mail]) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(match_array([deleted_dossier_1, deleted_dossier_2]), instructeur.email) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([deleted_dossier_1], dossier_1.procedure.administrateurs.first.email) }
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([deleted_dossier_2], dossier_2.procedure.administrateurs.first.email) }
end
end
end