diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index 2d1875cec..44b0faef4 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -101,11 +101,15 @@ module NewUser def ask_deletion 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 + + if !dossier.instruction_commencee? + dossier.delete_and_keep_track + flash.notice = 'Votre dossier a bien été supprimé.' + redirect_to users_dossiers_path + else + flash.notice = "L'instruction de votre dossier a commencé, il n'est plus possible de supprimer votre dossier. Si vous souhaitez annuler l'instruction contactez votre administration par la messagerie de votre dossier." + redirect_to users_dossier_path(dossier) + end end private diff --git a/app/helpers/dossier_helper.rb b/app/helpers/dossier_helper.rb index bb90efa41..8e2bc2b35 100644 --- a/app/helpers/dossier_helper.rb +++ b/app/helpers/dossier_helper.rb @@ -14,14 +14,4 @@ module DossierHelper "highlighted" end end - - def delete_dossier_confirm(dossier) - message = ["Vous vous apprêtez à supprimer votre dossier ainsi que les informations qu’il contient."] - if dossier.en_construction_ou_instruction? - message << "Nous vous rappelons que toute suppression entraine l’annulation de la démarche en cours." - end - message << "Confirmer la suppression ?" - - message.join(" ") - end end diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index 96e2b46b0..03147e8b9 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -14,4 +14,11 @@ class DossierMailer < ApplicationMailer mail(to: to_email, subject: subject) end + + def notify_unhide_to_user(dossier) + @dossier = dossier + subject = "Votre dossier n° #{@dossier.id} n'a pas pu être supprimé" + + mail(to: dossier.user.email, subject: subject) + end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 940116a3d..646aee849 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -44,6 +44,7 @@ class Dossier < ApplicationRecord scope :state_en_construction, -> { where(state: 'en_construction') } scope :state_en_instruction, -> { where(state: 'en_instruction') } scope :state_en_construction_ou_instruction, -> { where(state: EN_CONSTRUCTION_OU_INSTRUCTION) } + scope :state_instruction_commencee, -> { where(state: INSTRUCTION_COMMENCEE) } scope :state_termine, -> { where(state: TERMINE) } scope :archived, -> { where(archived: true) } @@ -126,6 +127,10 @@ class Dossier < ApplicationRecord TERMINE.include?(state) end + def instruction_commencee? + INSTRUCTION_COMMENCEE.include?(state) + end + def cerfa_available? procedure.cerfa_flag? && cerfa.size != 0 end @@ -276,7 +281,8 @@ class Dossier < ApplicationRecord now = Time.now deleted_dossier = DeletedDossier.create!(dossier_id: id, procedure: procedure, state: state, deleted_at: now) update(hidden_at: now) - deleted_dossier + DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later + DossierMailer.notify_deletion_to_administration(deleted_dossier, procedure.administrateur.email).deliver_later end private diff --git a/app/views/dossier_mailer/notify_unhide_to_user.html.haml b/app/views/dossier_mailer/notify_unhide_to_user.html.haml new file mode 100644 index 000000000..6901a01e3 --- /dev/null +++ b/app/views/dossier_mailer/notify_unhide_to_user.html.haml @@ -0,0 +1,13 @@ +- content_for(:title, "Votre dossier n° #{@dossier.id} n'a pas pu être supprimé") + +%h1 Bonjour, + +%p + L'instruction de votre dossier n° #{@dossier.id} ayant commencé, il n'a pas pu être supprimé. + Le dossier a été rétabli dans votre tableau de bord. + +%p + Bonne journée, + +%p + L'équipe demarches-simplifiees.fr diff --git a/app/views/layouts/left_panels/_left_panel_users_recapitulatifcontroller_show.html.haml b/app/views/layouts/left_panels/_left_panel_users_recapitulatifcontroller_show.html.haml index 6c3499cd4..6f2031b6a 100644 --- a/app/views/layouts/left_panels/_left_panel_users_recapitulatifcontroller_show.html.haml +++ b/app/views/layouts/left_panels/_left_panel_users_recapitulatifcontroller_show.html.haml @@ -14,9 +14,9 @@ .dossier-state= @facade.dossier.display_state .split-hr-left - - if current_user.owns?(@facade.dossier) + - if current_user.owns?(@facade.dossier) && !@facade.dossier.instruction_commencee? .text-center.mt-1 - = link_to ask_deletion_dossier_path(@facade.dossier), method: :post, class: "btn btn-danger", data: { confirm: delete_dossier_confirm(@facade.dossier) } do + = link_to ask_deletion_dossier_path(@facade.dossier), method: :post, class: "btn btn-danger", data: { confirm: "Vous vous apprêtez à supprimer votre dossier ainsi que les informations qu’il contient. Nous vous rappelons que toute suppression entraine l’annulation de la démarche en cours. Confirmer la suppression ?" } do Supprimer définitivement %br ce dossier diff --git a/lib/tasks/2018_06_13_unhide_dossiers.rake b/lib/tasks/2018_06_13_unhide_dossiers.rake new file mode 100644 index 000000000..c5221fe74 --- /dev/null +++ b/lib/tasks/2018_06_13_unhide_dossiers.rake @@ -0,0 +1,11 @@ +namespace :'2018_06_13_unhide_dossiers' do + task run: :environment do + Dossier.unscoped.where.not(hidden_at: nil).state_instruction_commencee.each do |d| + if !d.procedure.nil? # ensure the procedure was not deleted by administrateur for testing + d.update(hidden_at: nil) + DeletedDossier.find_by(dossier_id: d.id)&.destroy + DossierMailer.notify_unhide_to_user(d).deliver_later + end + end + end +end diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 887d81beb..5be0295da 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -412,6 +412,20 @@ describe NewUser::DossiersController, type: :controller do subject { post :ask_deletion, params: { id: dossier.id } } + shared_examples_for "the dossier can not be deleted" do + it do + 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 + end + context 'when dossier is owned by signed in user' do let(:dossier) { create(:dossier, user: user, autorisation_donnees: true) } @@ -429,25 +443,22 @@ describe NewUser::DossiersController, type: :controller do 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) } + + context "and the instruction has started" do + let(:dossier) { create(:dossier, :en_instruction, user: user, autorisation_donnees: true) } + + it_behaves_like "the dossier can not be deleted" + it { is_expected.to redirect_to(users_dossier_path(dossier)) } + end end context 'when dossier is not owned by signed in user' do let(:user2) { create(:user) } let(:dossier) { create(:dossier, user: user2, autorisation_donnees: true) } - it do - 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_behaves_like "the dossier can not be deleted" it { is_expected.to redirect_to(root_path) } end end