Skip to content

Commit

Permalink
[#10950] Rewrite empty brouillon to never touched
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagn committed Jan 21, 2025
1 parent d93bba0 commit eaac42a
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 89 deletions.
13 changes: 0 additions & 13 deletions app/jobs/cron/empty_dossiers_brouillon_deletion_job.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

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

def perform(*args)
Expired::DossiersDeletionService.new.process_never_touched_dossiers_brouillon
end
end
29 changes: 0 additions & 29 deletions app/models/concerns/dossier_empty_concern.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Dossier < ApplicationRecord
include DossierSectionsConcern
include DossierStateConcern
include DossierChampsConcern
include DossierEmptyConcern
include DossierExportConcern

enum :state, {
Expand Down Expand Up @@ -327,6 +326,7 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon
end
end

scope :never_touched_brouillon_expired, -> { brouillon.where(last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: nil).where(created_at: ..2.weeks.ago) }
scope :brouillon_expired, -> do
state_brouillon
.visible_by_user
Expand Down
2 changes: 1 addition & 1 deletion app/services/expired.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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'
when 'Cron::NeverTouchedDossiersBrouillonDeletionJob'
"every day at 5 am"
when 'Cron::ExpiredPrefilledDossiersDeletionJob'
"every day at 3 am"
Expand Down
21 changes: 5 additions & 16 deletions app/services/expired/dossiers_deletion_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class Expired::DossiersDeletionService < Expired::MailRateLimiter
MAX_BROUILLON_DELETION_EMAILS_TO_PROCESS_PER_DAY = 10000

def process_empty_dossiers_brouillon(deletion_window)
delete_empty_brouillons_and_notify(deletion_window)
def process_never_touched_dossiers_brouillon
delete_never_touched_brouillons
end

def process_expired_dossiers_brouillon
Expand Down Expand Up @@ -56,21 +56,10 @@ def send_termine_expiration_notices
)
end

def delete_empty_brouillons_and_notify(deletion_window)
empty_brouillons = Dossier.empty_brouillon(deletion_window)
def delete_never_touched_brouillons
never_touched_brouillons = Dossier.never_touched_brouillon_expired

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
never_touched_brouillons.in_batches.destroy_all
end

def delete_expired_brouillons_and_notify
Expand Down
27 changes: 0 additions & 27 deletions spec/models/concerns/dossier_empty_concern_spec.rb

This file was deleted.

19 changes: 19 additions & 0 deletions spec/models/dossier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,25 @@
end
end

describe '#never_touched_brouillon_expired' do
let!(:dossier) { travel_to(2.weeks.ago) { create(:dossier, :brouillon, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: nil) } }
let!(:dossier_2) { travel_to(1.week.ago) { create(:dossier, :brouillon, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: nil) } }
let!(:dossier_with_champ_updated) { travel_to(2.weeks.ago) { create(:dossier, :brouillon, last_champ_updated_at: 1.day.ago, last_champ_piece_jointe_updated_at: nil) } }
let!(:dossier_with_piece_jointe_updated) { travel_to(2.weeks.ago) { create(:dossier, :brouillon, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: 1.day.ago) } }

let!(:dossier_en_construction) { create(:dossier, :en_construction, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: nil) }

subject { Dossier.never_touched_brouillon_expired }

it { is_expected.to contain_exactly(dossier) }

context 'when the dossier has been cloned' do
let!(:cloned_dossier) { dossier_with_champ_updated.clone }

it { is_expected.to contain_exactly(dossier, cloned_dossier) }
end
end

private

def count_for_month(processed_by_month, month)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@
end
end

describe '#process_empty_dossiers_brouillon' do
describe '#process_never_touched_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) }
subject { service.process_never_touched_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) } }
Expand Down

0 comments on commit eaac42a

Please sign in to comment.