Skip to content

Commit

Permalink
[#10950] Delete empty brouillons older than 2 weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagn committed Jan 9, 2025
1 parent bf04e15 commit 7ef0d47
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/jobs/cron/empty_dossiers_brouillon_deletion_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class Cron::EmptyDossiersBrouillonDeletionJob < Cron::CronJob
self.schedule_expression = Expired.schedule_at(self)

def self.deletion_window
3.weeks.ago..2.weeks.ago
end

def perform(*args)
Expired::DossiersDeletionService.new.process_empty_dossiers_brouillon(deletion_window)
end
end
2 changes: 2 additions & 0 deletions app/services/expired.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module Expired
# it send a lot o email, so we spread our jobs through the day
def self.schedule_at(caller)
case caller.name
when 'Cron::EmptyDossiersBrouillonDeletionJob'
"every day at 5 am"
when 'Cron::ExpiredPrefilledDossiersDeletionJob'
"every day at 3 am"
when 'Cron::ExpiredDossiersTermineDeletionJob'
Expand Down
21 changes: 21 additions & 0 deletions app/services/expired/dossiers_deletion_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

class Expired::DossiersDeletionService < Expired::MailRateLimiter
def process_empty_dossiers_brouillon(created_at)
delete_empty_brouillons_and_notify(created_at)
end

def process_expired_dossiers_brouillon
send_brouillon_expiration_notices
delete_expired_brouillons_and_notify
Expand Down Expand Up @@ -48,6 +52,23 @@ def send_termine_expiration_notices
)
end

def delete_empty_brouillons_and_notify(created_at)
empty_brouillons = Dossier.empty_brouillon(created_at)

user_notifications = group_by_user_email(empty_brouillons)
.map { |(email, dossiers)| [email, dossiers.map(&:hash_for_deletion_mail)] }

empty_brouillons.destroy_all

user_notifications.each do |(email, dossiers_hash)|
mail = DossierMailer.notify_brouillon_deletion(
dossiers_hash,
email
)
send_with_delay(mail)
end
end

def delete_expired_brouillons_and_notify
user_notifications = group_by_user_email(Dossier.brouillon_expired)
.map { |(email, dossiers)| [email, dossiers.map(&:hash_for_deletion_mail)] }
Expand Down
64 changes: 64 additions & 0 deletions spec/services/expired/expired_dossiers_deletion_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,70 @@
end
end

describe '#process_empty_dossiers_brouillon' do
let(:types) { [{ type: :text }] }
let(:procedure_opts) { { types_de_champ_public: types } }

before do
allow(DossierMailer).to receive(:notify_brouillon_deletion).and_return(double(deliver_later: nil))
end

subject { service.process_empty_dossiers_brouillon(3.weeks.ago..2.weeks.ago) }

context 'with empty brouillon dossiers' do
let!(:empty_brouillon) { travel_to(15.days.ago) { create(:dossier, procedure: procedure) } }
let!(:empty_brouillon_2) { travel_to(15.days.ago) { create(:dossier, procedure: procedure, user: empty_brouillon.user) } }
let!(:empty_brouillon_3) { travel_to(7.days.ago) { create(:dossier, procedure: procedure, user: empty_brouillon.user) } }

it 'deletes empty brouillons and sends notifications' do
subject
expect(DossierMailer).to have_received(:notify_brouillon_deletion).once
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(
match_array([empty_brouillon.hash_for_deletion_mail, empty_brouillon_2.hash_for_deletion_mail]),
empty_brouillon.user.email
)
expect { empty_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { empty_brouillon_2.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { empty_brouillon_3.reload }.not_to raise_error
end
end

context 'with non-empty brouillon dossiers' do
let!(:not_empty_brouillon) { travel_to(15.days.ago) { create(:dossier, procedure: procedure) } }

before do
not_empty_brouillon.champs.first.update!(value: 'filled')
end

it 'does not delete non-empty brouillons' do
subject
expect(DossierMailer).not_to have_received(:notify_brouillon_deletion)
expect { not_empty_brouillon.reload }.not_to raise_error
end
end

context 'with dossiers too recent' do
let!(:recent_empty_brouillon) { travel_to(7.days.ago) { create(:dossier, procedure: procedure) } }

it 'does not delete recent brouillons' do
subject
expect(DossierMailer).not_to have_received(:notify_brouillon_deletion)
expect { recent_empty_brouillon.reload }.not_to raise_error
end
end

context 'with non-brouillon dossiers' do
let!(:en_construction_dossier) { travel_to(15.days.ago) { create(:dossier, :en_construction, procedure: procedure) } }

it 'does not delete non-brouillon dossiers' do
subject

expect(DossierMailer).not_to have_received(:notify_brouillon_deletion)
expect { en_construction_dossier.reload }.not_to raise_error
end
end
end

describe '#delete_expired_brouillons_and_notify' do
before { Timecop.freeze(reference_date) }
after { Timecop.return }
Expand Down

0 comments on commit 7ef0d47

Please sign in to comment.