diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index 276a5e68c97..d4119701987 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -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 @@ def notify_brouillon_not_submitted(dossier) end end - def notify_transfer(transfer) - I18n.with_locale(transfer.user_locale) do + def notify_transfer + @transfer = params[:dossier_transfer] + + I18n.with_locale(@transfer.user_locale) do @subject = default_i18n_subject() - @transfer = transfer - mail(to: transfer.email, subject: @subject) + mail(to: @transfer.email, subject: @subject) end end @@ -186,6 +194,14 @@ def prevent_perform_deliveries 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 # diff --git a/app/models/dossier_transfer.rb b/app/models/dossier_transfer.rb index aebab7415b2..5596e27a2d5 100644 --- a/app/models/dossier_transfer.rb +++ b/app/models/dossier_transfer.rb @@ -73,6 +73,6 @@ def sender_email private def send_notification - DossierMailer.notify_transfer(self).deliver_later + DossierMailer.with(dossier_transfer: self).notify_transfer.deliver_later end end diff --git a/app/views/users/dossiers/_dossier_actions.html.haml b/app/views/users/dossiers/_dossier_actions.html.haml index eda87bee84b..f803c9b406b 100644 --- a/app/views/users/dossiers/_dossier_actions.html.haml +++ b/app/views/users/dossiers/_dossier_actions.html.haml @@ -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 qu’il contient. Toute suppression entraîne l’annulation 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') diff --git a/config/locales/en.yml b/config/locales/en.yml index 75eb5be32c9..0a545a141a7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index f0528d636a8..4e29b274e9c 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -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 qu’il contient. Toute suppression entraîne l’annulation de la démarche en cours.\n\nConfirmer la suppression ?" + delete_dossier_with_transfer_confirm: "En continuant, vous allez supprimer ce dossier, les informations qu’il contient ainsi que sa demande de transfert. Toute suppression entraîne l’annulation de la démarche en cours.\n\nConfirmer la suppression ?" transfer_dossier: "Transférer le dossier" edit_draft: "Continuer à remplir" actions: "Actions" diff --git a/spec/controllers/manager/dossiers_controller_spec.rb b/spec/controllers/manager/dossiers_controller_spec.rb index 4b83eee337e..2f99e54936e 100644 --- a/spec/controllers/manager/dossiers_controller_spec.rb +++ b/spec/controllers/manager/dossiers_controller_spec.rb @@ -36,23 +36,26 @@ 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("L’adresse 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("L’adresse email est invalide") + end end end diff --git a/spec/mailers/dossier_mailer_spec.rb b/spec/mailers/dossier_mailer_spec.rb index c7dbf27dd56..4f00ee1d110 100644 --- a/spec/mailers/dossier_mailer_spec.rb +++ b/spec/mailers/dossier_mailer_spec.rb @@ -272,7 +272,7 @@ def notify_deletion_to_administration(deleted_dossier, to_email) 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 @@ def notify_deletion_to_administration(deleted_dossier, to_email) 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