amelioration(ExpiredUsersDeletionService): ne touche pas aux dossiers en instruction

This commit is contained in:
Martin 2023-11-06 10:43:21 +01:00 committed by mfo
parent 054b3be02a
commit fb51710309
2 changed files with 42 additions and 2 deletions

View file

@ -34,9 +34,17 @@ class ExpiredUsersDeletionService
# rubocop:disable DS/Unscoped
def expiring_users_with_dossiers
users = User.arel_table
dossiers = Dossier.arel_table
User.unscoped # avoid default_scope eager_loading :export, :instructeur, :administrateur
.where.missing(:expert, :instructeur, :administrateur)
.joins(:dossiers)
.joins(
users.join(dossiers, Arel::Nodes::InnerJoin)
.on(users[:id].eq(dossiers[:user_id])
.and(dossiers[:state].not_eq(Dossier.states.fetch(:en_instruction))))
.join_sources
)
.having('MAX(dossiers.created_at) < ?', EXPIRABLE_AFTER_IN_YEAR.years.ago)
.group('users.id')
end

View file

@ -45,6 +45,38 @@ describe ExpiredUsersDeletionService do
expect { subject }.to change { Dossier.count }.by(-1)
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
context 'when dossier brouillon' do
let(:dossier) { create(:dossier, :brouillon, user:, created_at: last_signed_in_expired) }
it 'destroys user and dossier' do
expect { subject }.to change { Dossier.count }.by(-1)
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when dossier en_construction' do
let(:dossier) { create(:dossier, :en_construction, user:, created_at: last_signed_in_expired) }
it 'destroys user and dossier' do
expect { subject }.to change { Dossier.count }.by(-1)
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when dossier en_instruction' do
let(:dossier) { create(:dossier, :en_instruction, user:, created_at: last_signed_in_expired) }
it 'does not do anything' do
expect { subject }.not_to change { Dossier.count }
expect { user.reload }.not_to raise_error
end
end
context 'when dossier termine' do
let(:dossier) { create(:dossier, :accepte, user:, created_at: last_signed_in_expired) }
it 'marks dossier as hidden_at due to user_removal and remove user' do
expect { subject }.to change { dossier.reload.hidden_by_user_at }.from(nil).to(anything)
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
@ -122,7 +154,7 @@ describe ExpiredUsersDeletionService do
end
context 'when user has a dossier created 3 years ago' do
let(:dossier) { create(:dossier, user:, created_at: last_signed_in_expired) }
let(:dossier) { create(:dossier, :brouillon, user:, created_at: last_signed_in_expired) }
it { is_expected.to include(user) }
end