feat(transfer): make it silent in case dossier had been destroyed between queuing and processing

Co-authored-by: mfo <mfo@users.noreply.github.com>
This commit is contained in:
Colin Darie 2024-01-24 16:02:09 +01:00
parent 27cfae8c6a
commit 4aa1306536
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
4 changed files with 44 additions and 14 deletions

View file

@ -1,12 +1,19 @@
# Preview all emails at http://localhost:3000/rails/mailers/dossier_mailer
class DossierMailer < ApplicationMailer
class AbortDeliveryError < StandardError; end
helper ServiceHelper
helper MailerHelper
helper ProcedureHelper
layout 'mailers/layout'
default from: NO_REPLY_EMAIL
after_action :prevent_perform_deliveries, only: [:notify_new_draft, :notify_new_answer, :notify_pending_correction]
before_action :abort_perform_deliveries, only: [:notify_transfer]
after_action :prevent_perform_deliveries, only: [:notify_new_draft, :notify_new_answer, :notify_pending_correction, :notify_transfer]
# when we don't want to render the view
rescue_from AbortDeliveryError, with: -> {}
def notify_new_draft
@dossier = params[:dossier]
@ -162,12 +169,13 @@ class DossierMailer < ApplicationMailer
end
end
def notify_transfer(transfer)
I18n.with_locale(transfer.user_locale) do
@subject = default_i18n_subject()
@transfer = transfer
def notify_transfer
@transfer = params[:dossier_transfer]
mail(to: transfer.email, subject: @subject)
I18n.with_locale(@transfer.user_locale) do
@subject = default_i18n_subject()
mail(to: @transfer.email, subject: @subject)
end
end
@ -186,6 +194,14 @@ class DossierMailer < ApplicationMailer
end
end
def abort_perform_deliveries
dossier_transfer = params[:dossier_transfer]
if dossier_transfer.dossiers.empty?
raise AbortDeliveryError.new
end
end
# This is an override of `default_i18n_subject` method
# https://api.rubyonrails.org/v5.0.0/classes/ActionMailer/Base.html#method-i-default_i18n_subject
#

View file

@ -73,6 +73,6 @@ class DossierTransfer < ApplicationRecord
private
def send_notification
DossierMailer.notify_transfer(self).deliver_later
DossierMailer.with(dossier_transfer: self).notify_transfer.deliver_later
end
end

View file

@ -36,23 +36,26 @@ describe Manager::DossiersController, type: :controller do
end
describe "POST #transfer" do
before do
allow(DossierMailer).to receive(:notify_transfer).and_call_original
subject do
post :transfer, params: { id: @dossier.id, email: }
end
context 'with valid email' do
let(:email) { "chouette.gars@laposte.net" }
it { expect(flash[:success]).to eq("Une invitation de transfert a été envoyée à chouette.gars@laposte.net") }
it { expect(DossierMailer).to have_received(:notify_transfer) }
it do
expect { subject }.to have_enqueued_mail(DossierMailer, :notify_transfer)
expect(flash[:success]).to eq("Une invitation de transfert a été envoyée à chouette.gars@laposte.net")
end
end
context 'with invalid email' do
let(:email) { "chouette" }
it { expect(flash[:alert]).to eq("Ladresse email est invalide") }
it { expect(DossierMailer).not_to have_received(:notify_transfer) }
it do
expect { subject }.not_to have_enqueued_mail
expect(flash[:alert]).to eq("Ladresse email est invalide")
end
end
end

View file

@ -272,7 +272,7 @@ RSpec.describe DossierMailer, type: :mailer do
let(:dossier_transfer) { create(:dossier_transfer) }
let!(:dossier) { create(:dossier, user: user, transfer: dossier_transfer, procedure: procedure) }
subject { described_class.notify_transfer(dossier_transfer) }
subject { described_class.with(dossier_transfer: dossier_transfer).notify_transfer }
context 'when it is a transfer of one dossier' do
it { expect(subject.subject).to include("Vous avez une demande de transfert en attente.") }
@ -294,5 +294,16 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.subject).to include("Vous avez une demande de transfert en attente.") }
it { expect(subject.body).to include("Le support technique vous adresse une demande de transfert") }
end
context 'when dossiers have been dissociated from transfer' do
before do
dossier.update!(transfer: nil)
dossier_transfer.reload
end
it 'does not send an email' do
expect { subject.perform_now }.not_to raise_error
end
end
end
end