Merge pull request #9935 from colinux/transfer-empty-no-fail

Tech: plus d'erreur lorsque l'email de transfert n'a plus de dossier associé
This commit is contained in:
Colin Darie 2024-01-29 08:43:50 +00:00 committed by GitHub
commit b64a32444c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 50 additions and 16 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

@ -43,8 +43,8 @@
- if has_delete_action
- menu.with_item(class: 'danger') do
= link_to(dossier_path(dossier), role: 'menuitem', method: :delete, data: { disable: true, confirm: "En continuant, vous allez supprimer ce dossier ainsi que les informations quil contient. Toute suppression entraîne lannulation de la démarche en cours.\n\nConfirmer la suppression ?" }) do
- confirm = dossier.transfer.present? ? t("views.users.dossiers.dossier_action.delete_dossier_with_transfer_confirm") : t("views.users.dossiers.dossier_action.delete_dossier_confirm")
= link_to(dossier_path(dossier), role: 'menuitem', method: :delete, data: { disable: true, confirm: }) do
= dsfr_icon('fr-icon-delete-fill', :sm)
.dropdown-description
= t('views.users.dossiers.dossier_action.delete_dossier')

View file

@ -510,6 +510,8 @@ en:
start_other_dossier: "Start another empty file"
clone: "Duplicate the file"
delete_dossier: "Delete the file"
delete_dossier_confirm: "If you continue, you will delete this file and the information it contains. Any deletion will result in the cancellation of the current procedure.\n\nConfirm deletion?"
delete_dossier_with_transfer_confirm: "If you continue, you will delete this file, the information it contains and its transfer request. Any deletion will result in the cancellation of the current procedure.\n\nConfirm deletion?"
transfer_dossier: "Transfer the file"
edit_draft: "Keep filling"
actions: "Actions"

View file

@ -503,6 +503,8 @@ fr:
start_other_dossier: "Commencer un autre dossier vide"
clone: "Dupliquer ce dossier"
delete_dossier: "Supprimer le dossier"
delete_dossier_confirm: "En continuant, vous allez supprimer ce dossier ainsi que les informations quil contient. Toute suppression entraîne lannulation de la démarche en cours.\n\nConfirmer la suppression ?"
delete_dossier_with_transfer_confirm: "En continuant, vous allez supprimer ce dossier, les informations quil contient ainsi que sa demande de transfert. Toute suppression entraîne lannulation de la démarche en cours.\n\nConfirmer la suppression ?"
transfer_dossier: "Transférer le dossier"
edit_draft: "Continuer à remplir"
actions: "Actions"

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