diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 263609e78..ef4ad39b6 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -183,6 +183,16 @@ class Dossier < ApplicationRecord brouillon? || en_construction? end + def retention_end_date + if instruction_commencee? + en_instruction_at + procedure.duree_conservation_dossiers_dans_ds.months + end + end + + def retention_expired? + instruction_commencee? && retention_end_date <= DateTime.now + end + def text_summary if brouillon? parts = [ diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index ea60ea3be..f2a1be974 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1002,4 +1002,30 @@ describe Dossier do it { is_expected.to be false } end end + + context "retention date" do + let(:procedure) { create(:procedure, duree_conservation_dossiers_dans_ds: 6) } + let(:uninstructed_dossier) { create(:dossier, :en_construction, procedure: procedure) } + let(:young_dossier) { create(:dossier, :en_instruction, en_instruction_at: DateTime.now, procedure: procedure) } + let(:just_expired_dossier) { create(:dossier, :en_instruction, en_instruction_at: 6.months.ago, procedure: procedure) } + let(:long_expired_dossier) { create(:dossier, :en_instruction, en_instruction_at: 1.year.ago, procedure: procedure) } + let(:modif_date) { DateTime.parse('01/01/2100') } + + before { Timecop.freeze(modif_date) } + after { Timecop.return } + + describe "#retention_end_date" do + it { expect(uninstructed_dossier.retention_end_date).to be_nil } + it { expect(young_dossier.retention_end_date).to eq(6.months.from_now) } + it { expect(just_expired_dossier.retention_end_date).to eq(DateTime.now) } + it { expect(long_expired_dossier.retention_end_date).to eq(6.months.ago) } + end + + describe "#retention_expired?" do + it { expect(uninstructed_dossier).not_to be_retention_expired } + it { expect(young_dossier).not_to be_retention_expired } + it { expect(just_expired_dossier).to be_retention_expired } + it { expect(long_expired_dossier).to be_retention_expired } + end + end end