Merge pull request #11119 from demarches-simplifiees/fix_crazy_cron

Correction du bug qui envoyait une notification de suppression pour des dossiers déjà supprimés
This commit is contained in:
Colin Darie 2024-12-09 08:43:40 +00:00 committed by GitHub
commit d56b63bc90
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 12 deletions

View file

@ -1,12 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
class Cron::PurgeOldBrouillonDossiersJob < Cron::CronJob class Cron::HideOldBrouillonDossiersJob < Cron::CronJob
self.schedule_expression = "every day at 5:30" self.schedule_expression = "every day at 21:00"
def perform def perform
Dossier Dossier
.visible_by_user
.state_brouillon .state_brouillon
.where(updated_at: ..(3.months + 2.weeks).ago) .where(updated_at: ..(3.months + 2.weeks).ago, notified_soon_deleted_sent_at: ..2.weeks.ago)
.find_each do |dossier| .find_each do |dossier|
dossier.hide_and_keep_track!(:automatic, :not_modified_for_a_long_time) dossier.hide_and_keep_track!(:automatic, :not_modified_for_a_long_time)
DossierMailer.notify_old_brouillon_after_deletion(dossier).deliver_later DossierMailer.notify_old_brouillon_after_deletion(dossier).deliver_later

View file

@ -5,6 +5,7 @@ class Cron::NotifyOldBrouillonDossiersSoonDeletedJob < Cron::CronJob
def perform def perform
Dossier Dossier
.visible_by_user
.state_brouillon .state_brouillon
.where(updated_at: ..3.months.ago) .where(updated_at: ..3.months.ago)
.where("notified_soon_deleted_sent_at IS NULL OR notified_soon_deleted_sent_at < updated_at") .where("notified_soon_deleted_sent_at IS NULL OR notified_soon_deleted_sent_at < updated_at")

View file

@ -1,12 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe Cron::PurgeOldBrouillonDossiersJob, type: :job do RSpec.describe Cron::HideOldBrouillonDossiersJob, type: :job do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let!(:recent_brouillon) { travel_to(3.months.ago) { create(:dossier, :brouillon, procedure: procedure) } } let!(:recent_brouillon) { travel_to(3.months.ago) { create(:dossier, :brouillon, procedure: procedure, notified_soon_deleted_sent_at: 3.weeks.ago) } }
let!(:old_brouillon) { travel_to(5.months.ago) { create(:dossier, :brouillon, procedure: procedure) } } let!(:old_brouillon) { travel_to(5.months.ago) { create(:dossier, :brouillon, procedure: procedure, notified_soon_deleted_sent_at: 3.weeks.ago) } }
let!(:very_old_brouillon) { travel_to(6.months.ago) { create(:dossier, :brouillon, procedure: procedure) } } let!(:very_old_brouillon) { travel_to(6.months.ago) { create(:dossier, :brouillon, procedure: procedure, notified_soon_deleted_sent_at: 3.weeks.ago) } }
let!(:old_en_construction) { travel_to(5.months.ago) { create(:dossier, :en_construction, procedure: procedure) } } let!(:very_old_brouillon_but_not_notified) { travel_to(6.months.ago) { create(:dossier, :brouillon, procedure: procedure, notified_soon_deleted_sent_at: nil) } }
let!(:old_en_construction) { travel_to(5.months.ago) { create(:dossier, :en_construction, procedure: procedure, notified_soon_deleted_sent_at: 3.weeks.ago) } }
let!(:not_visible_dossier) { travel_to(6.months.ago) { create(:dossier, :brouillon, :hidden_by_user, procedure: procedure, notified_soon_deleted_sent_at: 3.weeks.ago) } }
let!(:not_visible_dossier2) { travel_to(6.months.ago) { create(:dossier, :brouillon, :hidden_by_expired, procedure: procedure, notified_soon_deleted_sent_at: 3.weeks.ago) } }
subject(:perform_job) { described_class.perform_now } subject(:perform_job) { described_class.perform_now }
@ -17,8 +20,6 @@ RSpec.describe Cron::PurgeOldBrouillonDossiersJob, type: :job do
it 'hides only old brouillon dossiers' do it 'hides only old brouillon dossiers' do
expect { perform_job }.to change { Dossier.visible_by_user.count }.by(-2) expect { perform_job }.to change { Dossier.visible_by_user.count }.by(-2)
expect(Dossier.visible_by_user.pluck(:id)).to match_array([recent_brouillon.id, old_en_construction.id])
end end
it 'sends notification emails for each hidden dossier' do it 'sends notification emails for each hidden dossier' do
@ -28,6 +29,8 @@ RSpec.describe Cron::PurgeOldBrouillonDossiersJob, type: :job do
expect(DossierMailer).to have_received(:notify_old_brouillon_after_deletion).with(very_old_brouillon).once expect(DossierMailer).to have_received(:notify_old_brouillon_after_deletion).with(very_old_brouillon).once
expect(DossierMailer).not_to have_received(:notify_old_brouillon_after_deletion).with(recent_brouillon) expect(DossierMailer).not_to have_received(:notify_old_brouillon_after_deletion).with(recent_brouillon)
expect(DossierMailer).not_to have_received(:notify_old_brouillon_after_deletion).with(old_en_construction) expect(DossierMailer).not_to have_received(:notify_old_brouillon_after_deletion).with(old_en_construction)
expect(DossierMailer).not_to have_received(:notify_old_brouillon_after_deletion).with(not_visible_dossier)
expect(DossierMailer).not_to have_received(:notify_old_brouillon_after_deletion).with(not_visible_dossier2)
end end
it 'sets the correct hidden_by attributes' do it 'sets the correct hidden_by attributes' do

View file

@ -18,6 +18,8 @@ RSpec.describe Cron::NotifyOldBrouillonDossiersSoonDeletedJob, type: :job do
end end
let!(:recent_draft) { travel_to(2.months.ago) { create(:dossier, :brouillon) } } let!(:recent_draft) { travel_to(2.months.ago) { create(:dossier, :brouillon) } }
let!(:old_non_draft) { travel_to(4.months.ago) { create(:dossier, :en_construction) } } let!(:old_non_draft) { travel_to(4.months.ago) { create(:dossier, :en_construction) } }
let!(:not_visible_dossier) { travel_to(6.months.ago) { create(:dossier, :brouillon, :hidden_by_user) } }
let!(:not_visible_dossier2) { travel_to(6.months.ago) { create(:dossier, :brouillon, :hidden_by_expired) } }
it "sends notifications only for eligible draft dossiers" do it "sends notifications only for eligible draft dossiers" do
expect(DossierMailer).to receive(:notify_old_brouillon_soon_deleted) expect(DossierMailer).to receive(:notify_old_brouillon_soon_deleted)
@ -30,8 +32,10 @@ RSpec.describe Cron::NotifyOldBrouillonDossiersSoonDeletedJob, type: :job do
.and_return(double(deliver_later: true)) .and_return(double(deliver_later: true))
.once .once
expect(DossierMailer).not_to receive(:notify_old_brouillon_soon_deleted) [old_draft_recently_notified, not_visible_dossier, not_visible_dossier2].each do |dossier|
.with(old_draft_recently_notified) expect(DossierMailer).not_to receive(:notify_old_brouillon_soon_deleted)
.with(dossier)
end
job.perform job.perform