mailers: fix last_week_overview crash when the overview is missing

This is because procedures may be unpublished between the time where
the job is enqueued and the time the mailer is run

Fix #3745
This commit is contained in:
Pierre de La Morinerie 2019-04-04 10:41:44 +02:00
parent c0a9f103b3
commit 8ca683c515
2 changed files with 15 additions and 3 deletions

View file

@ -21,11 +21,13 @@ class GestionnaireMailer < ApplicationMailer
def last_week_overview(gestionnaire)
email = gestionnaire.email
@overview = gestionnaire.last_week_overview
headers['X-mailjet-campaign'] = 'last_week_overview'
@subject = 'Votre activité hebdomadaire'
@overview = gestionnaire.last_week_overview
mail(to: email, subject: @subject)
if @overview.present?
headers['X-mailjet-campaign'] = 'last_week_overview'
mail(to: email, subject: @subject)
end
end
def send_dossier(sender, dossier, recipient)

View file

@ -35,5 +35,15 @@ RSpec.describe GestionnaireMailer, type: :mailer do
subject { described_class.last_week_overview(gestionnaire) }
it { expect(subject.body).to include('Votre activité hebdomadaire') }
context 'when the gestionnaire has no active procedures' do
let(:procedure) { nil }
let(:last_week_overview) { nil }
it 'doesnt send the email' do
expect(subject.message).to be_kind_of(ActionMailer::Base::NullMail)
expect(subject.body).to be_blank
end
end
end
end