[fix #3427] Administration can soft delete a dossier

This commit is contained in:
simon lehericey 2019-02-13 16:13:37 +01:00 committed by Mathieu Magnin
parent 210088132e
commit a7e068003a
10 changed files with 79 additions and 6 deletions

View file

@ -29,6 +29,17 @@ module Manager
redirect_to manager_dossier_path(dossier)
end
def hide
dossier = Dossier.find(params[:id])
deleted_dossier = dossier.hide!(current_administration)
DossierMailer.notify_deletion_to_user(deleted_dossier, dossier.user.email).deliver_later
logger.info("Le dossier #{dossier.id} est supprimé par #{current_administration.email}")
flash[:notice] = "Le dossier #{dossier.id} est supprimé"
redirect_to manager_dossier_path(dossier)
end
private
def unfiltered_list?

View file

@ -1,3 +1,7 @@
class DeletedDossier < ApplicationRecord
belongs_to :procedure
def self.create_from_dossier(dossier)
DeletedDossier.create!(dossier_id: dossier.id, procedure: dossier.procedure, state: dossier.state, deleted_at: Time.now.utc)
end
end

View file

@ -258,9 +258,8 @@ class Dossier < ApplicationRecord
end
def delete_and_keep_track
now = Time.zone.now
deleted_dossier = DeletedDossier.create!(dossier_id: id, procedure: procedure, state: state, deleted_at: now)
update(hidden_at: now)
deleted_dossier = DeletedDossier.create_from_dossier(self)
update(hidden_at: deleted_dossier.deleted_at)
if en_construction?
administration_emails = followers_gestionnaires.present? ? followers_gestionnaires.pluck(:email) : [procedure.administrateur.email]
@ -318,6 +317,13 @@ class Dossier < ApplicationRecord
log_dossier_operation(nil, :accepter, automatic_operation: true)
end
def hide!(administration)
update(hidden_at: Time.zone.now)
log_administration_dossier_operation(administration, :supprimer)
DeletedDossier.create_from_dossier(self)
end
def refuser!(gestionnaire, motivation)
self.motivation = motivation
self.en_instruction_at ||= Time.zone.now
@ -356,6 +362,13 @@ class Dossier < ApplicationRecord
)
end
def log_administration_dossier_operation(administration, operation)
dossier_operation_logs.create(
administration: administration,
operation: DossierOperationLog.operations.fetch(operation)
)
end
def update_state_dates
if en_construction? && !self.en_construction_at
self.en_construction_at = Time.zone.now

View file

@ -4,9 +4,11 @@ class DossierOperationLog < ApplicationRecord
repasser_en_construction: 'repasser_en_construction',
accepter: 'accepter',
refuser: 'refuser',
classer_sans_suite: 'classer_sans_suite'
classer_sans_suite: 'classer_sans_suite',
supprimer: 'supprimer'
}
belongs_to :dossier
belongs_to :gestionnaire
belongs_to :administration
end

View file

@ -23,7 +23,7 @@ as well as a link to its edit page.
<h1 class="main-content__page-title">
<%= content_for(:title) %>
<% if dossier.hidden_at %>
(SUPPRIMÉ)
(Supprimé)
<% end %>
</h1>
@ -31,6 +31,9 @@ as well as a link to its edit page.
<% if dossier.termine? %>
<%= link_to 'Repasser en instruction', change_state_to_instruction_manager_dossier_path(dossier), method: :post, class: 'button', data: { confirm: "Repasser en instruction ?" } %>
<% end %>
<% if dossier.hidden_at.nil? %>
<%= link_to 'Supprimer le dossier', hide_manager_dossier_path(dossier), method: :post, class: 'button', data: { confirm: "Confirmez vous la suppression du dossier ?" } %>
<% end %>
<div>
</header>

View file

@ -14,6 +14,7 @@ Rails.application.routes.draw do
resources :dossiers, only: [:index, :show] do
post 'change_state_to_instruction', on: :member
post 'hide', on: :member
end
resources :administrateurs, only: [:index, :show, :new, :create] do

View file

@ -0,0 +1,5 @@
class AddAdministrationColumnToLogDossierOperation < ActiveRecord::Migration[5.2]
def change
add_reference :dossier_operation_logs, :administration, foreign_key: true
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_01_10_163655) do
ActiveRecord::Schema.define(version: 2019_02_13_144145) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -219,6 +219,8 @@ ActiveRecord::Schema.define(version: 2019_01_10_163655) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "automatic_operation", default: false, null: false
t.bigint "administration_id"
t.index ["administration_id"], name: "index_dossier_operation_logs_on_administration_id"
t.index ["dossier_id"], name: "index_dossier_operation_logs_on_dossier_id"
t.index ["gestionnaire_id"], name: "index_dossier_operation_logs_on_gestionnaire_id"
end
@ -597,6 +599,7 @@ ActiveRecord::Schema.define(version: 2019_01_10_163655) do
add_foreign_key "champs", "champs", column: "parent_id"
add_foreign_key "closed_mails", "procedures"
add_foreign_key "commentaires", "dossiers"
add_foreign_key "dossier_operation_logs", "administrations"
add_foreign_key "dossier_operation_logs", "dossiers"
add_foreign_key "dossier_operation_logs", "gestionnaires"
add_foreign_key "dossiers", "users"

View file

@ -0,0 +1,14 @@
describe Manager::DossiersController, type: :controller do
describe '#hide' do
let(:administration) { create :administration }
let!(:dossier) { create(:dossier) }
before do
sign_in administration
post :hide, params: { id: dossier.id }
dossier.reload
end
it { expect(dossier.hidden_at).not_to be_nil }
end
end

View file

@ -892,4 +892,21 @@ describe Dossier do
end
end
end
describe '#hide!' do
let(:dossier) { create(:dossier) }
let(:administration) { create(:administration) }
let(:last_operation) { dossier.dossier_operation_logs.last }
before do
Timecop.freeze
dossier.hide!(administration)
end
after { Timecop.return }
it { expect(dossier.hidden_at).to eq(Time.zone.now) }
it { expect(last_operation.operation).to eq('supprimer') }
it { expect(last_operation.administration).to eq(administration) }
end
end