From 79bfb8b143a4a8945ac032f14f3ec3362518868b Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 3 Dec 2019 10:46:44 +0100 Subject: [PATCH] Delete expired dossiers --- app/mailers/dossier_mailer.rb | 3 +++ app/models/dossier.rb | 20 ++++++++++++++++++++ spec/models/dossier_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index 2dfeff363..245ec7369 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -76,4 +76,7 @@ class DossierMailer < ApplicationMailer mail(to: user.email, subject: @subject) end + + def notify_brouillon_deletion(user, dossiers) + end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 7e3aa37cf..8fe10c8b4 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -18,6 +18,8 @@ class Dossier < ApplicationRecord TAILLE_MAX_ZIP = 50.megabytes + DRAFT_EXPIRATION = 1.month + 5.days + has_one :etablissement, dependent: :destroy has_one :individual, dependent: :destroy has_one :attestation, dependent: :destroy @@ -167,6 +169,7 @@ class Dossier < ApplicationRecord .joins(:procedure) .where("dossiers.created_at + (duree_conservation_dossiers_dans_ds * interval '1 month') - (1 * interval '1 month') <= now()") end + scope :expired_brouillon, -> { brouillon.where("brouillon_close_to_expiration_notice_sent_at < ?", (Time.zone.now - (DRAFT_EXPIRATION))) } scope :without_notice_sent, -> { where(brouillon_close_to_expiration_notice_sent_at: nil) } scope :for_procedure, -> (procedure) { includes(:user, :groupe_instructeur).where(groupe_instructeurs: { procedure: procedure }) } @@ -649,4 +652,21 @@ class Dossier < ApplicationRecord brouillons.update_all(brouillon_close_to_expiration_notice_sent_at: Time.zone.now) end + + def self.destroy_brouillons_and_notify + expired_brouillons = Dossier.expired_brouillon + + expired_brouillons + .includes(:user) + .group_by(&:user) + .each do |(user, dossiers)| + + DossierMailer.notify_brouillon_deletion(user, dossiers).deliver_later + + dossiers.each do |dossier| + DeletedDossier.create_from_dossier(dossier) + dossier.destroy + end + end + end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 26c96e067..f7f0b28e3 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1078,4 +1078,26 @@ describe Dossier do end end end + + describe '#destroy_brouillons_and_notify' do + let!(:today) { Time.zone.now.at_midnight } + + let!(:expired_brouillon) { create(:dossier, brouillon_close_to_expiration_notice_sent_at: today - (Dossier::DRAFT_EXPIRATION + 1.day)) } + let!(:other_brouillon) { create(:dossier, brouillon_close_to_expiration_notice_sent_at: today - (Dossier::DRAFT_EXPIRATION - 1.day)) } + + before do + allow(DossierMailer).to receive(:notify_brouillon_deletion).and_return(double(deliver_later: nil)) + Dossier.destroy_brouillons_and_notify + end + + it 'notifies deletion' do + expect(DossierMailer).to have_received(:notify_brouillon_deletion).once + expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(expired_brouillon.user, [expired_brouillon]) + end + + it 'deletes the expired brouillon' do + expect(DeletedDossier.find_by(dossier_id: expired_brouillon.id)).to be_present + expect { expired_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + end end