2020-02-26 17:28:39 +01:00
|
|
|
class ExpiredDossiersDeletionService
|
|
|
|
def self.process_expired_dossiers_brouillon
|
|
|
|
send_brouillon_expiration_notices
|
|
|
|
delete_expired_brouillons_and_notify
|
|
|
|
end
|
|
|
|
|
2020-02-26 17:45:17 +01:00
|
|
|
def self.process_expired_dossiers_en_construction
|
|
|
|
send_en_construction_expiration_notices
|
|
|
|
delete_expired_en_construction_and_notify
|
|
|
|
end
|
|
|
|
|
2020-02-26 17:28:39 +01:00
|
|
|
def self.send_brouillon_expiration_notices
|
2020-03-05 10:24:15 +01:00
|
|
|
dossiers_close_to_expiration = Dossier
|
|
|
|
.brouillon_close_to_expiration
|
2020-02-26 17:28:39 +01:00
|
|
|
.without_brouillon_expiration_notice_sent
|
|
|
|
|
|
|
|
dossiers_close_to_expiration
|
|
|
|
.includes(:user, :procedure)
|
2020-03-05 10:24:15 +01:00
|
|
|
.group_by(&:user)
|
|
|
|
.each do |(user, dossiers)|
|
2020-03-19 09:49:25 +01:00
|
|
|
DossierMailer.notify_brouillon_near_deletion(
|
|
|
|
dossiers,
|
|
|
|
user.email
|
|
|
|
).deliver_later
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
dossiers_close_to_expiration.update_all(brouillon_close_to_expiration_notice_sent_at: Time.zone.now)
|
|
|
|
end
|
|
|
|
|
2020-02-26 17:45:17 +01:00
|
|
|
def self.send_en_construction_expiration_notices
|
2020-03-04 20:01:01 +01:00
|
|
|
dossiers_close_to_expiration = Dossier
|
|
|
|
.en_construction_close_to_expiration
|
2020-02-26 17:45:17 +01:00
|
|
|
.without_en_construction_expiration_notice_sent
|
|
|
|
|
|
|
|
dossiers_close_to_expiration
|
2020-03-04 20:01:01 +01:00
|
|
|
.includes(:user)
|
|
|
|
.group_by(&:user)
|
|
|
|
.each do |(user, dossiers)|
|
2020-03-19 09:49:25 +01:00
|
|
|
DossierMailer.notify_en_construction_near_deletion_to_user(
|
2020-03-04 20:01:01 +01:00
|
|
|
dossiers,
|
2020-03-19 09:49:25 +01:00
|
|
|
user.email
|
2020-03-04 20:01:01 +01:00
|
|
|
).deliver_later
|
2020-02-26 17:45:17 +01:00
|
|
|
end
|
|
|
|
|
2020-03-19 09:49:25 +01:00
|
|
|
group_by_fonctionnaire_email(dossiers_close_to_expiration).each do |(email, dossiers)|
|
|
|
|
DossierMailer.notify_en_construction_near_deletion_to_administration(
|
2020-02-26 17:45:17 +01:00
|
|
|
dossiers,
|
2020-03-19 09:49:25 +01:00
|
|
|
email
|
2020-02-26 17:45:17 +01:00
|
|
|
).deliver_later
|
|
|
|
end
|
|
|
|
|
|
|
|
dossiers_close_to_expiration.update_all(en_construction_close_to_expiration_notice_sent_at: Time.zone.now)
|
|
|
|
end
|
|
|
|
|
2020-02-26 17:28:39 +01:00
|
|
|
def self.delete_expired_brouillons_and_notify
|
2020-03-05 10:24:15 +01:00
|
|
|
dossiers_to_remove = Dossier.brouillon_expired
|
2020-02-26 17:28:39 +01:00
|
|
|
|
2020-03-05 10:24:15 +01:00
|
|
|
dossiers_to_remove
|
2020-02-26 17:28:39 +01:00
|
|
|
.includes(:user, :procedure)
|
2020-03-05 10:24:15 +01:00
|
|
|
.group_by(&:user)
|
|
|
|
.each do |(user, dossiers)|
|
2020-03-19 09:49:25 +01:00
|
|
|
DossierMailer.notify_brouillon_deletion(
|
|
|
|
dossiers.map(&:hash_for_deletion_mail),
|
|
|
|
user.email
|
|
|
|
).deliver_later
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|
|
|
|
|
2020-03-19 11:53:25 +01:00
|
|
|
dossiers_to_remove.destroy_all
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|
2020-02-26 17:45:17 +01:00
|
|
|
|
|
|
|
def self.delete_expired_en_construction_and_notify
|
2020-03-04 20:01:01 +01:00
|
|
|
dossiers_to_remove = Dossier.en_construction_expired
|
2020-03-19 11:53:25 +01:00
|
|
|
dossiers_to_remove.each(&:expired_keep_track!)
|
2020-03-19 09:49:25 +01:00
|
|
|
|
2020-03-04 20:01:01 +01:00
|
|
|
dossiers_to_remove
|
|
|
|
.includes(:user)
|
|
|
|
.group_by(&:user)
|
|
|
|
.each do |(user, dossiers)|
|
|
|
|
DossierMailer.notify_automatic_deletion_to_user(
|
2020-03-19 09:49:25 +01:00
|
|
|
DeletedDossier.where(dossier_id: dossiers.map(&:id)),
|
|
|
|
user.email
|
2020-03-04 20:01:01 +01:00
|
|
|
).deliver_later
|
2020-02-26 17:45:17 +01:00
|
|
|
end
|
|
|
|
|
2020-03-19 09:49:25 +01:00
|
|
|
self.group_by_fonctionnaire_email(dossiers_to_remove).each do |(email, dossiers)|
|
2020-03-04 20:01:01 +01:00
|
|
|
DossierMailer.notify_automatic_deletion_to_administration(
|
2020-03-19 09:49:25 +01:00
|
|
|
DeletedDossier.where(dossier_id: dossiers.map(&:id)),
|
|
|
|
email
|
2020-02-26 17:45:17 +01:00
|
|
|
).deliver_later
|
|
|
|
end
|
|
|
|
|
2020-03-19 09:49:25 +01:00
|
|
|
dossiers_to_remove.destroy_all
|
2020-02-26 17:45:17 +01:00
|
|
|
end
|
2020-03-04 20:01:01 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.group_by_fonctionnaire_email(dossiers)
|
|
|
|
dossiers
|
|
|
|
.includes(:followers_instructeurs, procedure: [:administrateurs])
|
|
|
|
.each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |dossier, h|
|
|
|
|
(dossier.followers_instructeurs + dossier.procedure.administrateurs).each { |destinataire| h[destinataire.email] << dossier }
|
|
|
|
end
|
|
|
|
end
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|