This commit is contained in:
simon lehericey 2020-03-05 10:24:15 +01:00 committed by Paul Chavard
parent 3dd4ea26c7
commit ede61ee22b

View file

@ -10,22 +10,17 @@ class ExpiredDossiersDeletionService
end end
def self.send_brouillon_expiration_notices def self.send_brouillon_expiration_notices
dossiers_close_to_expiration = Dossier.brouillon_close_to_expiration dossiers_close_to_expiration = Dossier
.brouillon_close_to_expiration
.without_brouillon_expiration_notice_sent .without_brouillon_expiration_notice_sent
users_to_notify = {}
dossiers_close_to_expiration dossiers_close_to_expiration
.includes(:user, :procedure) .includes(:user, :procedure)
.find_each do |dossier| .group_by(&:user)
users_to_notify[dossier.user.email] ||= [dossier.user, Set.new] .each do |(user, dossiers)|
users_to_notify[dossier.user.email].last.add(dossier) DossierMailer.notify_brouillon_near_deletion(user, dossiers).deliver_later
end end
users_to_notify.each_value do |(user, dossiers)|
DossierMailer.notify_brouillon_near_deletion(user, dossiers).deliver_later
end
dossiers_close_to_expiration.update_all(brouillon_close_to_expiration_notice_sent_at: Time.zone.now) dossiers_close_to_expiration.update_all(brouillon_close_to_expiration_notice_sent_at: Time.zone.now)
end end
@ -57,26 +52,16 @@ class ExpiredDossiersDeletionService
end end
def self.delete_expired_brouillons_and_notify def self.delete_expired_brouillons_and_notify
dossier_to_remove = [] dossiers_to_remove = Dossier.brouillon_expired
users_to_notify = {}
Dossier.brouillon_expired dossiers_to_remove
.includes(:user, :procedure) .includes(:user, :procedure)
.find_each do |dossier| .group_by(&:user)
dossier_to_remove << dossier .each do |(user, dossiers)|
DossierMailer.notify_brouillon_deletion(user, dossiers.map(&:hash_for_deletion_mail)).deliver_later
users_to_notify[dossier.user.email] ||= [dossier.user, Set.new]
users_to_notify[dossier.user.email].last.add(dossier)
end end
users_to_notify.each_value do |(user, dossiers)| dossiers_to_remove.each do |dossier|
DossierMailer.notify_brouillon_deletion(
user,
dossiers.map(&:hash_for_deletion_mail)
).deliver_later
end
dossier_to_remove.each do |dossier|
DeletedDossier.create_from_dossier(dossier) DeletedDossier.create_from_dossier(dossier)
dossier.destroy dossier.destroy
end end