fix(dossiers): dossier extend_conservation actually works

This commit is contained in:
Paul Chavard 2022-04-08 13:16:08 +02:00
parent ab14acef6c
commit 90dd5bc9c5
7 changed files with 133 additions and 14 deletions

View file

@ -20,7 +20,7 @@ module Instructeurs
end end
def extend_conservation def extend_conservation
dossier.update(conservation_extension: dossier.conservation_extension + 1.month) dossier.extend_conservation(1.month)
flash[:notice] = t('views.instructeurs.dossiers.archived_dossier') flash[:notice] = t('views.instructeurs.dossiers.archived_dossier')
redirect_back(fallback_location: instructeur_dossier_path(@dossier.procedure, @dossier)) redirect_back(fallback_location: instructeur_dossier_path(@dossier.procedure, @dossier))
end end

View file

@ -174,7 +174,7 @@ module Users
end end
def extend_conservation def extend_conservation
dossier.update(conservation_extension: dossier.conservation_extension + dossier.procedure.duree_conservation_dossiers_dans_ds.months) dossier.extend_conservation(dossier.procedure.duree_conservation_dossiers_dans_ds.months)
flash[:notice] = t('views.users.dossiers.archived_dossier', duree_conservation_dossiers_dans_ds: dossier.procedure.duree_conservation_dossiers_dans_ds) flash[:notice] = t('views.users.dossiers.archived_dossier', duree_conservation_dossiers_dans_ds: dossier.procedure.duree_conservation_dossiers_dans_ds)
redirect_back(fallback_location: dossier_path(@dossier)) redirect_back(fallback_location: dossier_path(@dossier))
end end

View file

@ -63,7 +63,7 @@ module Types
def date_expiration def date_expiration
if !object.en_instruction? if !object.en_instruction?
object.expiration_date.presence || object.approximative_expiration_date object.expiration_date
end end
end end

View file

@ -99,8 +99,7 @@ module DossierHelper
end end
def safe_expiration_date(dossier) def safe_expiration_date(dossier)
date = dossier.expiration_date.presence || dossier.approximative_expiration_date l(dossier.expiration_date, format: '%d/%m/%Y')
l(date, format: '%d/%m/%Y')
end end
def annuaire_link(siren) def annuaire_link(siren)

View file

@ -573,7 +573,7 @@ class Dossier < ApplicationRecord
].any? ].any?
end end
def approximative_expiration_date_reference def expiration_date_reference
if brouillon? if brouillon?
created_at created_at
elsif en_construction? elsif en_construction?
@ -581,24 +581,28 @@ class Dossier < ApplicationRecord
elsif termine? elsif termine?
processed_at processed_at
else else
fail "approximative_expiration_date_reference should not be called in state #{self.state}" fail "expiration_date_reference should not be called in state #{self.state}"
end end
end end
def approximative_expiration_date def expiration_date_with_extention
[ [
approximative_expiration_date_reference, expiration_date_reference,
conservation_extension, conservation_extension,
procedure.duree_conservation_dossiers_dans_ds.months procedure.duree_conservation_dossiers_dans_ds.months
].sum - REMAINING_WEEKS_BEFORE_EXPIRATION.weeks ].sum
end
def expiration_notification_date
expiration_date_with_extention - REMAINING_WEEKS_BEFORE_EXPIRATION.weeks
end end
def close_to_expiration? def close_to_expiration?
return false if en_instruction? return false if en_instruction?
approximative_expiration_date < Time.zone.now expiration_notification_date < Time.zone.now
end end
def expiration_date def after_notification_expiration_date
if brouillon? && brouillon_close_to_expiration_notice_sent_at.present? if brouillon? && brouillon_close_to_expiration_notice_sent_at.present?
brouillon_close_to_expiration_notice_sent_at + duration_after_notice brouillon_close_to_expiration_notice_sent_at + duration_after_notice
elsif en_construction? && en_construction_close_to_expiration_notice_sent_at.present? elsif en_construction? && en_construction_close_to_expiration_notice_sent_at.present?
@ -608,6 +612,10 @@ class Dossier < ApplicationRecord
end end
end end
def expiration_date
after_notification_expiration_date.presence || expiration_date_with_extention
end
def duration_after_notice def duration_after_notice
MONTHS_AFTER_EXPIRATION.month + DAYS_AFTER_EXPIRATION.days MONTHS_AFTER_EXPIRATION.month + DAYS_AFTER_EXPIRATION.days
end end
@ -616,6 +624,13 @@ class Dossier < ApplicationRecord
brouillon? || en_construction? brouillon? || en_construction?
end end
def extend_conservation(conservation_extension)
update(conservation_extension: self.conservation_extension + conservation_extension,
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
def show_groupe_instructeur_details? def show_groupe_instructeur_details?
procedure.routee? && groupe_instructeur.present? && (!procedure.feature_enabled?(:procedure_routage_api) || !defaut_groupe_instructeur?) procedure.routee? && groupe_instructeur.present? && (!procedure.feature_enabled?(:procedure_routage_api) || !defaut_groupe_instructeur?)
end end

View file

@ -0,0 +1,51 @@
namespace :after_party do
desc 'Deployment task: fix_dossiers_expiration_dates'
task fix_dossiers_expiration_dates: :environment do
puts "Running deploy task 'fix_dossiers_expiration_dates'"
duree_conservation = "procedures.duree_conservation_dossiers_dans_ds * INTERVAL '1 month'"
expiration_after_notice_brouillon = "dossiers.brouillon_close_to_expiration_notice_sent_at + INTERVAL '#{Dossier::INTERVAL_EXPIRATION}'"
expiration_with_extention_brouillon = "dossiers.created_at + dossiers.conservation_extension + (#{duree_conservation}) - INTERVAL '#{Dossier::INTERVAL_BEFORE_EXPIRATION}'"
dossiers_brouillon = Dossier
.joins(:procedure)
.state_brouillon
.visible_by_user
.where.not(brouillon_close_to_expiration_notice_sent_at: nil)
.where.not(conservation_extension: 0.seconds)
.where("(#{expiration_after_notice_brouillon}) < (#{expiration_with_extention_brouillon})")
expiration_after_notice_en_construction = "dossiers.en_construction_close_to_expiration_notice_sent_at + INTERVAL '#{Dossier::INTERVAL_EXPIRATION}'"
expiration_with_extention_en_construction = "dossiers.en_construction_at + dossiers.conservation_extension + (#{duree_conservation}) - INTERVAL '#{Dossier::INTERVAL_BEFORE_EXPIRATION}'"
dossiers_en_construction = Dossier
.joins(:procedure)
.state_en_construction
.visible_by_user_or_administration
.where.not(en_construction_close_to_expiration_notice_sent_at: nil)
.where.not(conservation_extension: 0.seconds)
.where("(#{expiration_after_notice_en_construction}) < (#{expiration_with_extention_en_construction})")
expiration_after_notice_termine = "dossiers.termine_close_to_expiration_notice_sent_at + INTERVAL '#{Dossier::INTERVAL_EXPIRATION}'"
expiration_with_extention_termine = "dossiers.processed_at + dossiers.conservation_extension + (#{duree_conservation}) - INTERVAL '#{Dossier::INTERVAL_BEFORE_EXPIRATION}'"
dossiers_termine = Dossier
.joins(:procedure)
.state_termine
.visible_by_user_or_administration
.where.not(termine_close_to_expiration_notice_sent_at: nil)
.where.not(conservation_extension: 0.seconds)
.where("(#{expiration_after_notice_termine}) < (#{expiration_with_extention_termine})")
rake_puts "brouillon: #{dossiers_brouillon.count}"
rake_puts "en_construction: #{dossiers_en_construction.count}"
rake_puts "termine: #{dossiers_termine.count}"
dossiers_brouillon.update_all(brouillon_close_to_expiration_notice_sent_at: nil)
dossiers_en_construction.update_all(en_construction_close_to_expiration_notice_sent_at: nil)
dossiers_termine.update_all(termine_close_to_expiration_notice_sent_at: nil)
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -40,6 +40,7 @@ describe Dossier do
let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6) } let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6) }
let!(:young_dossier) { create(:dossier, :en_construction, procedure: procedure) } let!(:young_dossier) { create(:dossier, :en_construction, procedure: procedure) }
let!(:expiring_dossier) { create(:dossier, created_at: 175.days.ago, procedure: procedure) } let!(:expiring_dossier) { create(:dossier, created_at: 175.days.ago, procedure: procedure) }
let!(:expiring_dossier_with_notification) { create(:dossier, created_at: 175.days.ago, brouillon_close_to_expiration_notice_sent_at: Time.zone.now, procedure: procedure) }
let!(:just_expired_dossier) { create(:dossier, created_at: (6.months + 1.hour + 10.seconds).ago, procedure: procedure) } let!(:just_expired_dossier) { create(:dossier, created_at: (6.months + 1.hour + 10.seconds).ago, procedure: procedure) }
let!(:long_expired_dossier) { create(:dossier, created_at: 1.year.ago, procedure: procedure) } let!(:long_expired_dossier) { create(:dossier, created_at: 1.year.ago, procedure: procedure) }
@ -52,13 +53,27 @@ describe Dossier do
is_expected.to include(long_expired_dossier) is_expected.to include(long_expired_dossier)
end end
it do
expect(expiring_dossier.close_to_expiration?).to be_truthy
expect(expiring_dossier_with_notification.close_to_expiration?).to be_truthy
end
context 'does not include an expiring dossier that has been postponed' do context 'does not include an expiring dossier that has been postponed' do
before do before do
expiring_dossier.update(conservation_extension: 1.month) expiring_dossier.extend_conservation(1.month)
expiring_dossier_with_notification.extend_conservation(1.month)
expiring_dossier.reload expiring_dossier.reload
expiring_dossier_with_notification.reload
end end
it { is_expected.not_to include(expiring_dossier) } it { is_expected.not_to include(expiring_dossier) }
it do
expect(expiring_dossier.close_to_expiration?).to be_falsey
expect(expiring_dossier_with_notification.close_to_expiration?).to be_falsey
expect(expiring_dossier.expiration_date).to eq(expiring_dossier.expiration_date_with_extention)
expect(expiring_dossier_with_notification.expiration_date).to eq(expiring_dossier_with_notification.expiration_date_with_extention)
end
end end
context 'when .close_to_expiration' do context 'when .close_to_expiration' do
@ -76,6 +91,7 @@ describe Dossier do
let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6) } let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6) }
let!(:young_dossier) { create(:dossier, procedure: procedure) } let!(:young_dossier) { create(:dossier, procedure: procedure) }
let!(:expiring_dossier) { create(:dossier, :en_construction, en_construction_at: 175.days.ago, procedure: procedure) } let!(:expiring_dossier) { create(:dossier, :en_construction, en_construction_at: 175.days.ago, procedure: procedure) }
let!(:expiring_dossier_with_notification) { create(:dossier, :en_construction, en_construction_at: 175.days.ago, en_construction_close_to_expiration_notice_sent_at: Time.zone.now, procedure: procedure) }
let!(:just_expired_dossier) { create(:dossier, :en_construction, en_construction_at: (6.months + 1.hour + 10.seconds).ago, procedure: procedure) } let!(:just_expired_dossier) { create(:dossier, :en_construction, en_construction_at: (6.months + 1.hour + 10.seconds).ago, procedure: procedure) }
let!(:long_expired_dossier) { create(:dossier, :en_construction, en_construction_at: 1.year.ago, procedure: procedure) } let!(:long_expired_dossier) { create(:dossier, :en_construction, en_construction_at: 1.year.ago, procedure: procedure) }
@ -88,13 +104,27 @@ describe Dossier do
is_expected.to include(long_expired_dossier) is_expected.to include(long_expired_dossier)
end end
it do
expect(expiring_dossier.close_to_expiration?).to be_truthy
expect(expiring_dossier_with_notification.close_to_expiration?).to be_truthy
end
context 'does not include an expiring dossier that has been postponed' do context 'does not include an expiring dossier that has been postponed' do
before do before do
expiring_dossier.update(conservation_extension: 1.month) expiring_dossier.extend_conservation(1.month)
expiring_dossier_with_notification.extend_conservation(1.month)
expiring_dossier.reload expiring_dossier.reload
expiring_dossier_with_notification.reload
end end
it { is_expected.not_to include(expiring_dossier) } it { is_expected.not_to include(expiring_dossier) }
it do
expect(expiring_dossier.close_to_expiration?).to be_falsey
expect(expiring_dossier_with_notification.close_to_expiration?).to be_falsey
expect(expiring_dossier.expiration_date).to eq(expiring_dossier.expiration_date_with_extention)
expect(expiring_dossier_with_notification.expiration_date).to eq(expiring_dossier_with_notification.expiration_date_with_extention)
end
end end
context 'when .close_to_expiration' do context 'when .close_to_expiration' do
@ -121,6 +151,7 @@ describe Dossier do
let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6, procedure_expires_when_termine_enabled: true) } let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds: 6, procedure_expires_when_termine_enabled: true) }
let!(:young_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 2.days.ago) } let!(:young_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 2.days.ago) }
let!(:expiring_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 175.days.ago) } let!(:expiring_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 175.days.ago) }
let!(:expiring_dossier_with_notification) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 175.days.ago, termine_close_to_expiration_notice_sent_at: Time.zone.now) }
let!(:just_expired_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: (6.months + 1.hour + 10.seconds).ago) } let!(:just_expired_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: (6.months + 1.hour + 10.seconds).ago) }
let!(:long_expired_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 1.year.ago) } let!(:long_expired_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 1.year.ago) }
@ -133,6 +164,29 @@ describe Dossier do
is_expected.to include(long_expired_dossier) is_expected.to include(long_expired_dossier)
end end
it do
expect(expiring_dossier.close_to_expiration?).to be_truthy
expect(expiring_dossier_with_notification.close_to_expiration?).to be_truthy
end
context 'does not include an expiring dossier that has been postponed' do
before do
expiring_dossier.extend_conservation(1.month)
expiring_dossier_with_notification.extend_conservation(1.month)
expiring_dossier.reload
expiring_dossier_with_notification.reload
end
it { is_expected.not_to include(expiring_dossier) }
it do
expect(expiring_dossier.close_to_expiration?).to be_falsey
expect(expiring_dossier_with_notification.close_to_expiration?).to be_falsey
expect(expiring_dossier.expiration_date).to eq(expiring_dossier.expiration_date_with_extention)
expect(expiring_dossier_with_notification.expiration_date).to eq(expiring_dossier_with_notification.expiration_date_with_extention)
end
end
context 'when .close_to_expiration' do context 'when .close_to_expiration' do
subject { Dossier.close_to_expiration } subject { Dossier.close_to_expiration }
it do it do