diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index 56bf50f4a..d2daad675 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -99,10 +99,12 @@ module NewUser end def ask_deletion - @dossier = current_user.dossiers.find(params[:id]) - DossierMailer.ask_deletion(@dossier).deliver_later - flash.notice = 'Une demande de suppression de votre dossier a été envoyée, elle sera traitée dans les plus brefs délais.' - redirect_to users_dossier_recapitulatif_path(@dossier) + dossier = current_user.dossiers.includes(:user, procedure: :administrateur).find(params[:id]) + deleted_dossier = dossier.delete_and_keep_track + DossierMailer.notify_deletion_to_user(deleted_dossier, dossier.user.email).deliver_later + DossierMailer.notify_deletion_to_administration(deleted_dossier, dossier.procedure.administrateur.email).deliver_later + flash.notice = 'Votre dossier a bien été supprimé.' + redirect_to users_dossiers_path end private diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index 68ef28d7c..1d8643314 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -1,8 +1,13 @@ class DossierMailer < ApplicationMailer layout 'mailers/layout' - def ask_deletion(dossier) - @dossier = dossier - mail(to: "contact@demarches-simplifiees.fr", subject: "Demande de suppression de dossier") + def notify_deletion_to_user(deleted_dossier, to_email) + @deleted_dossier = deleted_dossier + mail(to: to_email, subject: "Votre dossier n° #{@deleted_dossier.dossier_id} a bien été supprimé") + end + + def notify_deletion_to_administration(deleted_dossier, to_email) + @deleted_dossier = deleted_dossier + mail(to: to_email, subject: "Le dossier n° #{@deleted_dossier.dossier_id} a été supprimé à la demande de l'usager") end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 8fbe99ee4..9452cc0eb 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -272,6 +272,13 @@ class Dossier < ApplicationRecord end end + def delete_and_keep_track + now = Time.now + deleted_dossier = DeletedDossier.create!(dossier_id: id, procedure: procedure, state: state, deleted_at: now) + update(hidden_at: now) + deleted_dossier + end + private def update_state_dates diff --git a/app/views/dossier_mailer/ask_deletion.html.haml b/app/views/dossier_mailer/ask_deletion.html.haml deleted file mode 100644 index 6b1717e2d..000000000 --- a/app/views/dossier_mailer/ask_deletion.html.haml +++ /dev/null @@ -1,10 +0,0 @@ -- content_for(:title, 'Demande de suppression de dossier') - -%h1 Bonjour - -%p - Une demande de suppression a été effectuée pour le dossier #{@dossier.id} - (état : #{@dossier.state}) - par l'utilisateur #{@dossier.user.email}. -%p - Merci de le notifier quand cela est fait. diff --git a/app/views/dossier_mailer/notify_deletion_to_administration.html.haml b/app/views/dossier_mailer/notify_deletion_to_administration.html.haml new file mode 100644 index 000000000..dc2705328 --- /dev/null +++ b/app/views/dossier_mailer/notify_deletion_to_administration.html.haml @@ -0,0 +1,11 @@ +- content_for(:title, "Suppression du dossier n° #{@deleted_dossier.dossier_id}") + +%h1 Bonjour, + +%p + À la demande de l'usager le dossier n° #{@deleted_dossier.dossier_id} a été supprimé. +%br +Bonne journée, +%br +%br +L'équipe demarches-simplifiees.fr diff --git a/app/views/dossier_mailer/notify_deletion_to_user.html.haml b/app/views/dossier_mailer/notify_deletion_to_user.html.haml new file mode 100644 index 000000000..12845ce61 --- /dev/null +++ b/app/views/dossier_mailer/notify_deletion_to_user.html.haml @@ -0,0 +1,12 @@ +- content_for(:title, "Suppression du dossier n° #{@deleted_dossier.dossier_id}") + +%h1 Bonjour, + +%p + Votre dossier n° #{@deleted_dossier.dossier_id} a bien été supprimé. + Une trace anonyme de ce traitement sera conservée pour l’administration. +%br +Bonne journée, +%br +%br +L'équipe demarches-simplifiees.fr diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 2717a6883..8d65338dc 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -394,11 +394,20 @@ describe NewUser::DossiersController, type: :controller do let(:dossier) { create(:dossier, user: user, autorisation_donnees: true) } it do - expect(DossierMailer).to receive(:ask_deletion).and_return(double(deliver_later: nil)) + expect(DossierMailer).to receive(:notify_deletion_to_administration).with(kind_of(DeletedDossier), dossier.procedure.administrateur.email).and_return(double(deliver_later: nil)) + expect(DossierMailer).to receive(:notify_deletion_to_user).with(kind_of(DeletedDossier), dossier.user.email).and_return(double(deliver_later: nil)) subject end - it { is_expected.to redirect_to(users_dossier_recapitulatif_path(dossier)) } + it do + procedure = dossier.procedure + dossier_id = dossier.id + subject + expect(Dossier.find_by(id: dossier_id)).to eq(nil) + expect(procedure.deleted_dossiers.count).to eq(1) + expect(procedure.deleted_dossiers.first.dossier_id).to eq(dossier_id) + end + it { is_expected.to redirect_to(users_dossiers_path) } end context 'when dossier is not owned by signed in user' do @@ -406,10 +415,17 @@ describe NewUser::DossiersController, type: :controller do let(:dossier) { create(:dossier, user: user2, autorisation_donnees: true) } it do - expect(DossierMailer).not_to receive(:ask_deletion) + expect(DossierMailer).not_to receive(:notify_deletion_to_administration) + expect(DossierMailer).not_to receive(:notify_deletion_to_user) subject end + it do + subject + expect(Dossier.find_by(id: dossier.id)).not_to eq(nil) + expect(dossier.procedure.deleted_dossiers.count).to eq(0) + end + it { is_expected.to redirect_to(root_path) } end end diff --git a/spec/mailers/previews/dossier_mailer_preview.rb b/spec/mailers/previews/dossier_mailer_preview.rb index bb625c37e..c74b23ae2 100644 --- a/spec/mailers/previews/dossier_mailer_preview.rb +++ b/spec/mailers/previews/dossier_mailer_preview.rb @@ -1,6 +1,10 @@ # Preview all emails at http://localhost:3000/rails/mailers/dossier_mailer class DossierMailerPreview < ActionMailer::Preview - def ask_deletion - DossierMailer.ask_deletion(Dossier.last) + def notify_deletion_to_user + DossierMailer.notify_deletion_to_user(DeletedDossier.last, "user@ds.fr") + end + + def notify_deletion_to_administration + DossierMailer.notify_deletion_to_administration(DeletedDossier.last, "admin@ds.fr") end end