amelioration(ExpiredUsersDeletionService): prepare le grand ménage

This commit is contained in:
Martin 2023-11-03 09:24:38 +01:00 committed by mfo
parent 256babbf74
commit a49a5c2c93
2 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,17 @@
class ExpiredUsersDeletionService
def self.process_expired
users = find_expired_user
users.find_each do |user|
user.delete_and_keep_track_dossiers_also_delete_user(nil)
end
end
# rubocop:disable DS/Unscoped
def self.expiring_users
User.unscoped # avoid default_scope eager_loading :export, :instructeur, :administrateur
.joins(:dossiers)
.having('MAX(dossiers.created_at) < ?', 2.years.ago)
.group('users.id')
end
# rubocop:enable DS/Unscoped
end

View file

@ -0,0 +1,42 @@
describe ExpiredUsersDeletionService do
let(:user) { create(:user) }
before { user && dossier }
describe '#process_expired' do
subject { ExpiredUsersDeletionService.process_expired }
context 'when user has a dossier created 1 year ago' do
let(:dossier) { create(:dossier, user:, created_at: 1.year.ago) }
it 'does not destroy anything' do
expect { subject }.not_to change { Dossier.count }
expect(user.reload).to be_truthy
end
end
context 'when user has a dossier created 3 years ago' do
let(:dossier) { create(:dossier, user:, created_at: 3.years.ago) }
it 'destroys user and dossier' do
expect { subject }.to change { Dossier.count }.by(-1)
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
describe '#find_expired_user' do
subject { ExpiredUsersDeletionService.find_expired_user }
context 'when user has no dossiers (TODO: just drop all user without dossier, no need to alert them)' do
let(:dossier) { nil }
xit { is_expected.to include(user) }
end
context 'when user has a dossier created 1 year ago' do
let(:dossier) { create(:dossier, user:, created_at: 1.year.ago) }
it { is_expected.not_to include(user) }
end
context 'when user has a dossier created 3 years ago' do
let(:dossier) { create(:dossier, user:, created_at: 3.years.ago) }
it { is_expected.to include(user) }
end
end
end