Delete expired dossiers

This commit is contained in:
simon lehericey 2019-12-03 10:46:44 +01:00
parent 6391f7ff9c
commit 79bfb8b143
3 changed files with 45 additions and 0 deletions

View file

@ -76,4 +76,7 @@ class DossierMailer < ApplicationMailer
mail(to: user.email, subject: @subject) mail(to: user.email, subject: @subject)
end end
def notify_brouillon_deletion(user, dossiers)
end
end end

View file

@ -18,6 +18,8 @@ class Dossier < ApplicationRecord
TAILLE_MAX_ZIP = 50.megabytes TAILLE_MAX_ZIP = 50.megabytes
DRAFT_EXPIRATION = 1.month + 5.days
has_one :etablissement, dependent: :destroy has_one :etablissement, dependent: :destroy
has_one :individual, dependent: :destroy has_one :individual, dependent: :destroy
has_one :attestation, dependent: :destroy has_one :attestation, dependent: :destroy
@ -167,6 +169,7 @@ class Dossier < ApplicationRecord
.joins(:procedure) .joins(:procedure)
.where("dossiers.created_at + (duree_conservation_dossiers_dans_ds * interval '1 month') - (1 * interval '1 month') <= now()") .where("dossiers.created_at + (duree_conservation_dossiers_dans_ds * interval '1 month') - (1 * interval '1 month') <= now()")
end 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 :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 }) } 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) brouillons.update_all(brouillon_close_to_expiration_notice_sent_at: Time.zone.now)
end 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 end

View file

@ -1078,4 +1078,26 @@ describe Dossier do
end end
end 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 end