diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 7b022faf4a1..312b9f3d617 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -346,9 +346,19 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon joins(:procedure).scoping do interval_en_construction_close_to_expiration .or(interval_termine_close_to_expiration) + .or(where(id: expiring_due_to_user)) end end + scope :expiring_due_to_user, -> do + state_en_construction + .visible_by_user_or_administration + .where(created_at: ..ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR.years.ago) + .joins(:user) + .having('MAX(dossiers.created_at) < ?', ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR.years.ago) + .group('dossiers.id') + end + scope :brouillon_expired, -> do state_brouillon .visible_by_user diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 86b4619072e..2add6e8fbdb 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -218,8 +218,32 @@ is_expected.to include(long_expired_dossier) end end + end + + describe '.termine_or_en_construction_close_to_expiration ' do + context 'with expiring user' do + let(:duree_conservation_dossiers_dans_ds) { ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR * 12 + 2 } + let(:expiring_user_created_at) { (ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR.years + 1.month).ago } + let(:not_expiring_user_created_at) { (ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR.years - 1.month).ago } + let(:procedure) { create(:procedure, :published, duree_conservation_dossiers_dans_ds:, procedure_expires_when_termine_enabled: true) } + let!(:expired_dossier_by_user) { create(:dossier, :en_construction, procedure:, created_at: expiring_user_created_at, en_construction_at: not_expiring_user_created_at) } + let!(:not_expired_dossier_by_user) { create(:dossier, :en_construction, procedure:, created_at: not_expiring_user_created_at, en_construction_at: not_expiring_user_created_at) } + + subject { Dossier.termine_or_en_construction_close_to_expiration } + it do + is_expected.to include(expired_dossier_by_user) + is_expected.not_to include(not_expired_dossier_by_user) + end + end + + context 'when dossier termines' do + 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!(: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!(:long_expired_dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: 1.year.ago) } - context 'when .close_to_expiration' do subject { Dossier.termine_or_en_construction_close_to_expiration } it do is_expected.not_to include(young_dossier) @@ -230,6 +254,20 @@ end end + describe '#expiring_due_to_user' do + let(:procedure) { create(:procedure, :published) } + let(:expiring_user_created_at) { (ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR + 1).years.ago } + let(:not_expiring_user_created_at) { (ExpiredUsersDeletionService::EXPIRABLE_AFTER_IN_YEAR - 1).years.ago } + let!(:young_dossier) { create(:dossier, :en_construction, created_at: not_expiring_user_created_at, procedure:) } + let!(:expiring_dossier) { create(:dossier, :en_construction, created_at: expiring_user_created_at, procedure:) } + + subject { Dossier.expiring_due_to_user } + it do + is_expected.to include(expiring_dossier) + is_expected.not_to include(young_dossier) + end + end + describe 'with_notifications' do let(:dossier) { create(:dossier) } let(:instructeur) { create(:instructeur) }