From ede61ee22b5a71bdee3ebf6cb146383fbab9833a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 5 Mar 2020 10:24:15 +0100 Subject: [PATCH] refacto --- .../expired_dossiers_deletion_service.rb | 37 ++++++------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/app/services/expired_dossiers_deletion_service.rb b/app/services/expired_dossiers_deletion_service.rb index b67e2ac00..1a7383ca9 100644 --- a/app/services/expired_dossiers_deletion_service.rb +++ b/app/services/expired_dossiers_deletion_service.rb @@ -10,22 +10,17 @@ class ExpiredDossiersDeletionService end 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 - users_to_notify = {} - dossiers_close_to_expiration .includes(:user, :procedure) - .find_each do |dossier| - users_to_notify[dossier.user.email] ||= [dossier.user, Set.new] - users_to_notify[dossier.user.email].last.add(dossier) + .group_by(&:user) + .each do |(user, dossiers)| + DossierMailer.notify_brouillon_near_deletion(user, dossiers).deliver_later 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) end @@ -57,26 +52,16 @@ class ExpiredDossiersDeletionService end def self.delete_expired_brouillons_and_notify - dossier_to_remove = [] - users_to_notify = {} + dossiers_to_remove = Dossier.brouillon_expired - Dossier.brouillon_expired + dossiers_to_remove .includes(:user, :procedure) - .find_each do |dossier| - dossier_to_remove << dossier - - users_to_notify[dossier.user.email] ||= [dossier.user, Set.new] - users_to_notify[dossier.user.email].last.add(dossier) + .group_by(&:user) + .each do |(user, dossiers)| + DossierMailer.notify_brouillon_deletion(user, dossiers.map(&:hash_for_deletion_mail)).deliver_later end - users_to_notify.each_value do |(user, dossiers)| - DossierMailer.notify_brouillon_deletion( - user, - dossiers.map(&:hash_for_deletion_mail) - ).deliver_later - end - - dossier_to_remove.each do |dossier| + dossiers_to_remove.each do |dossier| DeletedDossier.create_from_dossier(dossier) dossier.destroy end