amelioration(procedure.duree_conservation_dossiers_dans_ds_changes): reset les flag d'expirations lorsqu'on change la date de conservation des dosseirs d'une procedure

This commit is contained in:
Martin 2023-11-09 16:41:32 +01:00 committed by mfo
parent 5bf38ea06f
commit 66cf41b6d2
4 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,13 @@
class ResetExpiringDossiersJob < ApplicationJob
def perform(procedure)
procedure.dossiers
.where.not(brouillon_close_to_expiration_notice_sent_at: nil)
.or(Dossier.where.not(en_construction_close_to_expiration_notice_sent_at: nil))
.or(Dossier.where.not(termine_close_to_expiration_notice_sent_at: nil))
.in_batches do |relation|
relation.update_all(brouillon_close_to_expiration_notice_sent_at: nil,
en_construction_close_to_expiration_notice_sent_at: nil,
termine_close_to_expiration_notice_sent_at: nil)
end
end
end

View file

@ -312,6 +312,8 @@ class Procedure < ApplicationRecord
validate :validate_auto_archive_on_in_the_future, if: :will_save_change_to_auto_archive_on? validate :validate_auto_archive_on_in_the_future, if: :will_save_change_to_auto_archive_on?
before_save :update_juridique_required before_save :update_juridique_required
after_save :extend_conservation_for_dossiers
after_initialize :ensure_path_exists after_initialize :ensure_path_exists
before_save :ensure_path_exists before_save :ensure_path_exists
after_create :ensure_defaut_groupe_instructeur after_create :ensure_defaut_groupe_instructeur
@ -904,6 +906,15 @@ class Procedure < ApplicationRecord
end end
end end
def extend_conservation_for_dossiers
return if previous_changes.include?(:duree_conservation_dossiers_dans_ds)
before, after = duree_conservation_dossiers_dans_ds_previous_change
return if [before, after].any?(&:nil?)
return if (after - before).negative?
ResetExpiringDossiersJob.perform_later(self)
end
def ensure_defaut_groupe_instructeur def ensure_defaut_groupe_instructeur
if self.groupe_instructeurs.empty? if self.groupe_instructeurs.empty?
gi = groupe_instructeurs.create(label: GroupeInstructeur::DEFAUT_LABEL) gi = groupe_instructeurs.create(label: GroupeInstructeur::DEFAUT_LABEL)

View file

@ -0,0 +1,19 @@
describe ResetExpiringDossiersJob do
subject { described_class.new(procedure).perform_now }
let(:duree_conservation_dossiers_dans_ds) { 2 }
let(:procedure) { create(:procedure, duree_conservation_dossiers_dans_ds:) }
describe '.perform_now' do
it 'resets flags' do
expiring_dossier_brouillon = create(:dossier, :brouillon, procedure: procedure, brouillon_close_to_expiration_notice_sent_at: duree_conservation_dossiers_dans_ds.months.ago)
expiring_dossier_en_construction = create(:dossier, :en_construction, procedure: procedure, en_construction_close_to_expiration_notice_sent_at: duree_conservation_dossiers_dans_ds.months.ago)
expiring_dossier_en_termine = create(:dossier, :accepte, procedure: procedure, termine_close_to_expiration_notice_sent_at: duree_conservation_dossiers_dans_ds.months.ago)
subject
expect(expiring_dossier_brouillon.reload.brouillon_close_to_expiration_notice_sent_at).to eq(nil)
expect(expiring_dossier_en_construction.reload.en_construction_close_to_expiration_notice_sent_at).to eq(nil)
expect(expiring_dossier_en_termine.reload.termine_close_to_expiration_notice_sent_at).to eq(nil)
end
end
end

View file

@ -1656,6 +1656,43 @@ describe Procedure do
end end
end end
describe 'extend_conservation_for_dossiers' do
let(:duree_conservation_dossiers_dans_ds) { 2 }
let(:procedure) { create(:procedure, duree_conservation_dossiers_dans_ds:) }
let(:expiring_dossier_brouillon) { create(:dossier, :brouillon, procedure: procedure, brouillon_close_to_expiration_notice_sent_at: duree_conservation_dossiers_dans_ds.months.ago) }
let(:expiring_dossier_en_construction) { create(:dossier, :en_construction, procedure: procedure, en_construction_close_to_expiration_notice_sent_at: duree_conservation_dossiers_dans_ds.months.ago) }
let(:expiring_dossier_en_termine) { create(:dossier, :accepte, procedure: procedure, termine_close_to_expiration_notice_sent_at: duree_conservation_dossiers_dans_ds.months.ago) }
let(:not_expiring_dossie) { create(:dossier, :accepte, procedure: procedure, created_at: duree_conservation_dossiers_dans_ds.months.ago) }
before do
procedure
expiring_dossier_brouillon
expiring_dossier_en_construction
expiring_dossier_en_termine
not_expiring_dossie
end
context 'when duree_conservation_dossiers_dans_ds does not changes' do
it 'does not enqueues any job' do
expect(ResetExpiringDossiersJob).not_to receive(:perform_later)
procedure.update!(libelle: 'does not change duree_conservation_dossiers_dans_ds')
end
end
context 'when duree_conservation_dossiers_dans_ds decreases' do
it 'calls extend_conservation_for_dossiers' do
expect(ResetExpiringDossiersJob).not_to receive(:perform_later)
procedure.update(duree_conservation_dossiers_dans_ds: duree_conservation_dossiers_dans_ds - 1)
end
end
context 'when duree_conservation_dossiers_dans_ds increases' do
it 'calls extend_conservation_for_dossiers' do
expect(ResetExpiringDossiersJob).not_to receive(:perform_later)
procedure.update(duree_conservation_dossiers_dans_ds: duree_conservation_dossiers_dans_ds + 1)
end
end
end
private private
def create_dossier_with_pj_of_size(size, procedure) def create_dossier_with_pj_of_size(size, procedure)