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-04-02 15:08:16 +02:00
|
|
|
def self.process_expired_dossiers_termine
|
|
|
|
send_termine_expiration_notices
|
|
|
|
delete_expired_termine_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
|
|
|
|
|
2021-10-27 10:02:56 +02:00
|
|
|
user_notifications = group_by_user_email(dossiers_close_to_expiration)
|
2020-04-22 17:24:54 +02:00
|
|
|
|
2020-02-26 17:28:39 +01:00
|
|
|
dossiers_close_to_expiration.update_all(brouillon_close_to_expiration_notice_sent_at: Time.zone.now)
|
2021-10-27 10:02:56 +02:00
|
|
|
|
|
|
|
user_notifications.each do |(email, dossiers)|
|
|
|
|
DossierMailer.notify_brouillon_near_deletion(
|
|
|
|
dossiers,
|
|
|
|
email
|
|
|
|
).deliver_later
|
|
|
|
end
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|
|
|
|
|
2020-02-26 17:45:17 +01:00
|
|
|
def self.send_en_construction_expiration_notices
|
2020-04-22 17:24:54 +02:00
|
|
|
send_expiration_notices(
|
|
|
|
Dossier.en_construction_close_to_expiration.without_en_construction_expiration_notice_sent,
|
|
|
|
:en_construction_close_to_expiration_notice_sent_at
|
|
|
|
)
|
2020-02-26 17:45:17 +01:00
|
|
|
end
|
|
|
|
|
2020-04-02 15:08:16 +02:00
|
|
|
def self.send_termine_expiration_notices
|
2020-04-22 17:24:54 +02:00
|
|
|
send_expiration_notices(
|
|
|
|
Dossier.termine_close_to_expiration.without_termine_expiration_notice_sent,
|
|
|
|
:termine_close_to_expiration_notice_sent_at
|
|
|
|
)
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
|
2020-02-26 17:28:39 +01:00
|
|
|
def self.delete_expired_brouillons_and_notify
|
2021-10-27 10:02:56 +02:00
|
|
|
user_notifications = group_by_user_email(Dossier.brouillon_expired)
|
|
|
|
.map { |(email, dossiers)| [email, dossiers.map(&:hash_for_deletion_mail)] }
|
2020-02-26 17:28:39 +01:00
|
|
|
|
2021-10-27 10:02:56 +02:00
|
|
|
Dossier.brouillon_expired.destroy_all
|
2020-02-26 17:28:39 +01:00
|
|
|
|
2021-10-27 10:02:56 +02:00
|
|
|
user_notifications.each do |(email, dossiers_hash)|
|
|
|
|
DossierMailer.notify_brouillon_deletion(
|
|
|
|
dossiers_hash,
|
|
|
|
email
|
|
|
|
).deliver_later
|
|
|
|
end
|
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-04-01 17:08:50 +02:00
|
|
|
delete_expired_and_notify(Dossier.en_construction_expired)
|
|
|
|
end
|
|
|
|
|
2020-04-02 15:08:16 +02:00
|
|
|
def self.delete_expired_termine_and_notify
|
2020-04-29 19:18:12 +02:00
|
|
|
delete_expired_and_notify(Dossier.termine_expired, notify_on_closed_procedures_to_user: true)
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
|
2020-04-01 17:08:50 +02:00
|
|
|
private
|
|
|
|
|
2020-04-22 17:24:54 +02:00
|
|
|
def self.send_expiration_notices(dossiers_close_to_expiration, close_to_expiration_flag)
|
2021-10-27 10:02:56 +02:00
|
|
|
user_notifications = group_by_user_email(dossiers_close_to_expiration)
|
|
|
|
administration_notifications = group_by_fonctionnaire_email(dossiers_close_to_expiration)
|
2020-04-01 17:08:50 +02:00
|
|
|
|
2021-10-27 10:02:56 +02:00
|
|
|
dossiers_close_to_expiration.update_all(close_to_expiration_flag => Time.zone.now)
|
2020-04-22 17:24:54 +02:00
|
|
|
|
2021-10-27 10:02:56 +02:00
|
|
|
user_notifications.each do |(email, dossiers)|
|
|
|
|
DossierMailer.notify_near_deletion_to_user(dossiers, email).deliver_later
|
|
|
|
end
|
|
|
|
administration_notifications.each do |(email, dossiers)|
|
|
|
|
DossierMailer.notify_near_deletion_to_administration(dossiers, email).deliver_later
|
2020-04-01 17:08:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-29 19:18:12 +02:00
|
|
|
def self.delete_expired_and_notify(dossiers_to_remove, notify_on_closed_procedures_to_user: false)
|
2021-10-27 10:02:56 +02:00
|
|
|
user_notifications = group_by_user_email(dossiers_to_remove, notify_on_closed_procedures_to_user: notify_on_closed_procedures_to_user)
|
|
|
|
.map { |(email, dossiers)| [email, dossiers.map(&:id)] }
|
|
|
|
administration_notifications = group_by_fonctionnaire_email(dossiers_to_remove)
|
|
|
|
.map { |(email, dossiers)| [email, dossiers.map(&:id)] }
|
|
|
|
|
|
|
|
deleted_dossier_ids = []
|
|
|
|
dossiers_to_remove.find_each do |dossier|
|
|
|
|
if dossier.expired_keep_track_and_destroy!
|
|
|
|
deleted_dossier_ids << dossier.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
user_notifications.each do |(email, dossier_ids)|
|
|
|
|
dossier_ids = dossier_ids.intersection(deleted_dossier_ids)
|
|
|
|
if dossier_ids.present?
|
2020-03-04 20:01:01 +01:00
|
|
|
DossierMailer.notify_automatic_deletion_to_user(
|
2021-10-27 10:02:56 +02:00
|
|
|
DeletedDossier.where(dossier_id: dossier_ids).to_a,
|
|
|
|
email
|
|
|
|
).deliver_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
administration_notifications.each do |(email, dossier_ids)|
|
|
|
|
dossier_ids = dossier_ids.intersection(deleted_dossier_ids)
|
|
|
|
if dossier_ids.present?
|
|
|
|
DossierMailer.notify_automatic_deletion_to_administration(
|
|
|
|
DeletedDossier.where(dossier_id: dossier_ids).to_a,
|
|
|
|
email
|
2020-03-04 20:01:01 +01:00
|
|
|
).deliver_later
|
2020-02-26 17:45:17 +01:00
|
|
|
end
|
|
|
|
end
|
2021-10-27 10:02:56 +02:00
|
|
|
end
|
2020-02-26 17:45:17 +01:00
|
|
|
|
2021-10-27 10:02:56 +02:00
|
|
|
def self.group_by_user_email(dossiers, notify_on_closed_procedures_to_user: false)
|
|
|
|
dossiers
|
|
|
|
.with_notifiable_procedure(notify_on_closed: notify_on_closed_procedures_to_user)
|
|
|
|
.includes(:user, :procedure)
|
|
|
|
.group_by(&:user)
|
|
|
|
.map { |(user, dossiers)| [user.email, dossiers] }
|
2020-02-26 17:45:17 +01:00
|
|
|
end
|
2020-03-04 20:01:01 +01:00
|
|
|
|
|
|
|
def self.group_by_fonctionnaire_email(dossiers)
|
|
|
|
dossiers
|
2020-04-29 19:18:12 +02:00
|
|
|
.with_notifiable_procedure(notify_on_closed: true)
|
2020-03-04 20:01:01 +01:00
|
|
|
.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
|
2021-10-27 10:02:56 +02:00
|
|
|
.map { |(email, dossiers)| [email, dossiers.to_a] }
|
2020-03-04 20:01:01 +01:00
|
|
|
end
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|