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/helpers/dossier_helper.rb b/app/helpers/dossier_helper.rb index 8e2bc2b35..7cddd4bac 100644 --- a/app/helpers/dossier_helper.rb +++ b/app/helpers/dossier_helper.rb @@ -14,4 +14,12 @@ 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 ?" + end end 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/deleted_dossier.rb b/app/models/deleted_dossier.rb new file mode 100644 index 000000000..31cffa793 --- /dev/null +++ b/app/models/deleted_dossier.rb @@ -0,0 +1,3 @@ +class DeletedDossier < ApplicationRecord + belongs_to :procedure +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/models/procedure.rb b/app/models/procedure.rb index a5f06c2a1..b2b5420eb 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -3,6 +3,7 @@ class Procedure < ApplicationRecord has_many :types_de_champ, -> { public_only }, dependent: :destroy has_many :types_de_champ_private, -> { private_only }, class_name: 'TypeDeChamp', dependent: :destroy has_many :dossiers + has_many :deleted_dossiers, dependent: :destroy has_one :module_api_carto, dependent: :destroy has_one :attestation_template, dependent: :destroy 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/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 8f5502c44..1e5a3a253 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,8 +14,9 @@ .dossier-state= @facade.dossier.display_state .split-hr-left - .text-center.mt-1 - = link_to ask_deletion_dossier_path(@facade.dossier), method: :post, class:"btn btn-danger", data: { confirm: "Confirmez-vous la demande de suppression de ce dossier ?" } do - Demander la suppression - %br - du dossier + - if @facade.dossier.user == current_user + .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 + Supprimer définitivement + %br + ce dossier diff --git a/db/migrate/20180530095508_create_deleted_dossiers.rb b/db/migrate/20180530095508_create_deleted_dossiers.rb new file mode 100644 index 000000000..9558b23d6 --- /dev/null +++ b/db/migrate/20180530095508_create_deleted_dossiers.rb @@ -0,0 +1,12 @@ +class CreateDeletedDossiers < ActiveRecord::Migration[5.2] + def change + create_table :deleted_dossiers do |t| + t.references :procedure + t.bigint :dossier_id + t.datetime :deleted_at + t.string :state + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 16ea44e85..69ee7bffb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_05_22_142109) do +ActiveRecord::Schema.define(version: 2018_05_30_095508) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -220,6 +220,16 @@ ActiveRecord::Schema.define(version: 2018_05_22_142109) do t.index ["priority", "run_at"], name: "delayed_jobs_priority" end + create_table "deleted_dossiers", force: :cascade do |t| + t.bigint "procedure_id" + t.bigint "dossier_id" + t.datetime "deleted_at" + t.string "state" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["procedure_id"], name: "index_deleted_dossiers_on_procedure_id" + end + create_table "dossiers", id: :serial, force: :cascade do |t| t.boolean "autorisation_donnees" t.integer "procedure_id" 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