add grace period to deleted dossiers

This commit is contained in:
kara Diaby 2020-11-26 15:13:32 +01:00 committed by simon lehericey
parent d2f3bb18f9
commit f3c4040d2d
10 changed files with 103 additions and 51 deletions

View file

@ -216,7 +216,7 @@ module Instructeurs
def delete_dossier
if dossier.termine?
dossier.deleted_by_instructeur_and_keep_track!(current_instructeur)
dossier.discard_and_keep_track!(current_instructeur, :instructeur_request)
flash.notice = 'Le dossier a bien été supprimé'
redirect_to instructeur_procedure_path(procedure)
else

View file

@ -14,12 +14,12 @@ module Users
before_action :forbid_closed_submission!, only: [:update_brouillon]
before_action :show_demarche_en_test_banner
before_action :store_user_location!, only: :new
before_action :statut, only: :index
def index
@user_dossiers = current_user.dossiers.includes(:procedure).order_by_updated_at.page(page)
@dossiers_invites = current_user.dossiers_invites.includes(:procedure).order_by_updated_at.page(page)
@dossiers_supprimes = DeletedDossier.where(user_id: current_user.id).order_by_updated_at.page(page)
@statut = statut(@user_dossiers.count, @dossiers_invites.count, @dossiers_supprimes.count)
end
def show
@ -275,9 +275,17 @@ module Users
private
def statut
def statut(mes_dossiers_count, dossiers_invites_count, dossiers_supprimes_count)
if dossiers_invites_count == 0 && dossiers_supprimes_count == 0
'mes-dossiers'
elsif mes_dossiers_count == 0 && dossiers_invites_count > 0
'dossiers-invites'
elsif dossiers_invites_count == 0 && mes_dossiers_count == 0 && dossiers_supprimes_count > 0
'dossiers-supprimes'
else
@statut = params[:statut].presence || 'mes-dossiers'
end
end
def store_user_location!
store_location_for(:user, request.fullpath)

View file

@ -2,7 +2,15 @@ class Cron::DiscardedDossiersDeletionJob < Cron::CronJob
self.schedule_expression = "every day at 2 am"
def perform(*args)
DossierOperationLog.where(dossier: Dossier.discarded_en_construction_expired)
.where.not(operation: DossierOperationLog.operations.fetch(:supprimer))
.destroy_all
DossierOperationLog.where(dossier: Dossier.discarded_termine_expired)
.where.not(operation: DossierOperationLog.operations.fetch(:supprimer))
.destroy_all
Dossier.discarded_brouillon_expired.destroy_all
Dossier.discarded_en_construction_expired.destroy_all
Dossier.discarded_termine_expired.destroy_all
end
end

View file

@ -263,13 +263,19 @@ class Dossier < ApplicationRecord
with_discarded
.discarded
.state_brouillon
.where('hidden_at < ?', 1.month.ago)
.where('hidden_at < ?', 1.week.ago)
end
scope :discarded_en_construction_expired, -> do
with_discarded
.discarded
.state_en_construction
.where('dossiers.hidden_at < ?', 1.month.ago)
.where('dossiers.hidden_at < ?', 1.week.ago)
end
scope :discarded_termine_expired, -> do
with_discarded
.discarded
.state_termine
.where('dossiers.hidden_at < ?', 1.week.ago)
end
scope :brouillon_near_procedure_closing_date, -> do
@ -520,18 +526,9 @@ class Dossier < ApplicationRecord
end
end
def deleted_by_instructeur_and_keep_track!(author)
if keep_track_on_deletion?
deleted_dossier = DeletedDossier.create_from_dossier(self, :instructeur_request)
self.delete_operations_logs
log_dossier_operation(author, :supprime_par_instructeur, self)
DossierMailer.notify_instructeur_deletion_to_user(deleted_dossier, user.email).deliver_later
self.destroy
end
end
def discard_and_keep_track!(author, reason)
if keep_track_on_deletion? && en_construction?
if keep_track_on_deletion?
if en_construction?
deleted_dossier = DeletedDossier.create_from_dossier(self, reason)
administration_emails = followers_instructeurs.present? ? followers_instructeurs.map(&:email) : procedure.administrateurs.map(&:email)
@ -541,6 +538,13 @@ class Dossier < ApplicationRecord
DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later
log_dossier_operation(author, :supprimer, self)
elsif termine?
deleted_dossier = DeletedDossier.create_from_dossier(self, reason)
DossierMailer.notify_instructeur_deletion_to_user(deleted_dossier, user.email).deliver_later
log_dossier_operation(author, :supprimer, self)
end
end
discard!
@ -807,10 +811,6 @@ class Dossier < ApplicationRecord
private
def delete_operations_logs
DossierOperationLog.where(dossier: self).destroy_all
end
def geo_areas
champs.includes(:geo_areas).flat_map(&:geo_areas) + champs_private.includes(:geo_areas).flat_map(&:geo_areas)
end

View file

@ -28,8 +28,7 @@ class DossierOperationLog < ApplicationRecord
modifier_annotation: 'modifier_annotation',
demander_un_avis: 'demander_un_avis',
archiver: 'archiver',
desarchiver: 'desarchiver',
supprime_par_instructeur: 'supprime_par_instructeur'
desarchiver: 'desarchiver'
}
has_one_attached :serialized
@ -88,11 +87,11 @@ class DossierOperationLog < ApplicationRecord
def self.serialize_subject(subject, operation = nil)
if subject.nil?
nil
elsif operation == "supprime_par_instructeur"
elsif operation == operations.fetch(:supprimer)
{
date_de_depot: subject.en_construction_at,
date_de_mise_en_instruction: subject.en_instruction_at,
date_de_decision: subject.traitements.last.processed_at
date_de_decision: subject.termine? ? subject.traitements.last.processed_at : nil
}.as_json
else
case subject

View file

@ -15,16 +15,19 @@
- else
%h1.page-title Dossiers
%ul.tabs
- if @user_dossiers.count > 0
= tab_item('mes dossiers',
dossiers_path(statut: 'mes-dossiers'),
active: @statut == 'mes-dossiers',
badge: number_with_html_delimiter(@user_dossiers.count))
- if @dossiers_invites.count > 0
= tab_item('dossiers invités',
dossiers_path(statut: 'dossiers-invites'),
active: @statut == 'dossiers-invites',
badge: number_with_html_delimiter(@dossiers_invites.count))
- if @dossiers_supprimes.count > 0
= tab_item('dossiers supprimés',
dossiers_path(statut: 'dossiers-supprimes'),
active: @statut == 'dossiers-supprimes',

View file

@ -746,8 +746,8 @@ describe Instructeurs::DossiersController, type: :controller do
end
it 'deletes previous logs and add a suppression log' do
expect(DossierOperationLog.where(dossier_id: dossier.id).count).to eq(1)
expect(DossierOperationLog.where(dossier_id: dossier.id).first.operation).to eq('supprime_par_instructeur')
expect(DossierOperationLog.where(dossier_id: dossier.id).count).to eq(3)
expect(DossierOperationLog.where(dossier_id: dossier.id).last.operation).to eq('supprimer')
end
it 'send an email to the user' do
@ -761,8 +761,8 @@ describe Instructeurs::DossiersController, type: :controller do
expect(DeletedDossier.where(dossier_id: dossier.id).first.groupe_instructeur_id).to eq(dossier.groupe_instructeur_id)
end
it 'delete the dossier' do
expect { dossier.reload }.to raise_error ActiveRecord::RecordNotFound
it 'discard the dossier' do
expect(dossier.reload.hidden_at).not_to eq(nil)
end
end

View file

@ -759,7 +759,7 @@ describe Users::DossiersController, type: :controller do
before { get(:index) }
it { expect(assigns(:statut)).to eq('mes-dossiers') }
it { expect(assigns(:statut)).to eq('dossiers-invites') }
it { expect(assigns(:dossiers_invites)).to match([invite.dossier]) }
end

View file

@ -0,0 +1,34 @@
RSpec.describe DiscardedDossiersDeletionJob, type: :job do
include ActiveJob::TestHelper
let(:instructeur) { create(:instructeur) }
let!(:dossier_brouillon) { create(:dossier) }
let!(:dossier) { create(:dossier, :en_construction) }
let!(:discarded_dossier_brouillon) { create(:dossier, hidden_at: 2.weeks.ago) }
let!(:discarded_dossier_en_construction) { create(:dossier, :en_construction, hidden_at: 2.weeks.ago) }
let!(:discarded_dossier_termine) { create(:dossier, :accepte, hidden_at: 2.weeks.ago) }
let!(:discarded_dossier_termine_today) { create(:dossier, :accepte, hidden_at: 1.hour.ago) }
before do
discarded_dossier_en_construction.send(:log_dossier_operation, instructeur, :passer_en_instruction, discarded_dossier_en_construction)
discarded_dossier_termine.send(:log_dossier_operation, instructeur, :passer_en_instruction, discarded_dossier_termine)
discarded_dossier_termine_today.send(:log_dossier_operation, instructeur, :passer_en_instruction, discarded_dossier_termine_today)
discarded_dossier_en_construction.send(:log_dossier_operation, instructeur, :supprimer, discarded_dossier_en_construction)
discarded_dossier_termine.send(:log_dossier_operation, instructeur, :supprimer, discarded_dossier_termine)
discarded_dossier_termine_today.send(:log_dossier_operation, instructeur, :supprimer, discarded_dossier_termine_today)
end
context 'cleanup discared dossiers' do
it 'delete dossiers and operation logs' do
expect(Dossier.with_discarded.count).to eq(6)
expect(DossierOperationLog.count).to eq(6)
DiscardedDossiersDeletionJob.perform_now
expect(Dossier.with_discarded.count).to eq(3)
expect(DossierOperationLog.count).to eq(4)
end
end
end

View file

@ -1268,8 +1268,8 @@ describe Dossier do
end
end
it { expect(Dossier.discarded_brouillon_expired.count).to eq(2) }
it { expect(Dossier.discarded_en_construction_expired.count).to eq(2) }
it { expect(Dossier.discarded_brouillon_expired.count).to eq(3) }
it { expect(Dossier.discarded_en_construction_expired.count).to eq(3) }
end
describe "discarded procedure dossier should be able to access it's procedure" do