diff --git a/app/jobs/reset_expiring_dossiers_job.rb b/app/jobs/reset_expiring_dossiers_job.rb index 7f4b4d166..6ab941bf3 100644 --- a/app/jobs/reset_expiring_dossiers_job.rb +++ b/app/jobs/reset_expiring_dossiers_job.rb @@ -1,13 +1,15 @@ class ResetExpiringDossiersJob < ApplicationJob def perform(procedure) - procedure.dossiers - .where.not(brouillon_close_to_expiration_notice_sent_at: nil) - .or(Dossier.where.not(en_construction_close_to_expiration_notice_sent_at: nil)) - .or(Dossier.where.not(termine_close_to_expiration_notice_sent_at: nil)) + procedure + .dossiers .in_batches do |relation| - relation.update_all(brouillon_close_to_expiration_notice_sent_at: nil, - en_construction_close_to_expiration_notice_sent_at: nil, - termine_close_to_expiration_notice_sent_at: nil) + relation.each do |dossier| + if dossier.expiration_started? + dossier.update(brouillon_close_to_expiration_notice_sent_at: nil, + en_construction_close_to_expiration_notice_sent_at: nil, + termine_close_to_expiration_notice_sent_at: nil) + end + end end end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 84b98c6a0..d0827dee6 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -484,6 +484,14 @@ class Dossier < ApplicationRecord end end + def expiration_started? + [ + brouillon_close_to_expiration_notice_sent_at, + en_construction_close_to_expiration_notice_sent_at, + termine_close_to_expiration_notice_sent_at + ].any?(&:present?) + end + def motivation if termine? traitement&.motivation || read_attribute(:motivation)