Move process_expired_dossiers_brouillon to a service
This commit is contained in:
parent
7930bf8bf6
commit
f35ccc9d92
4 changed files with 97 additions and 86 deletions
|
@ -669,36 +669,4 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.send_brouillon_expiration_notices
|
|
||||||
brouillons = Dossier
|
|
||||||
.brouillon_close_to_expiration
|
|
||||||
.without_notice_sent
|
|
||||||
|
|
||||||
brouillons
|
|
||||||
.includes(:user)
|
|
||||||
.group_by(&:user)
|
|
||||||
.each do |(user, dossiers)|
|
|
||||||
DossierMailer.notify_brouillon_near_deletion(user, dossiers).deliver_later
|
|
||||||
end
|
|
||||||
|
|
||||||
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(:procedure, :user)
|
|
||||||
.group_by(&:user)
|
|
||||||
.each do |(user, dossiers)|
|
|
||||||
dossier_hashes = dossiers.map(&:hash_for_deletion_mail)
|
|
||||||
DossierMailer.notify_brouillon_deletion(user, dossier_hashes).deliver_later
|
|
||||||
|
|
||||||
dossiers.each do |dossier|
|
|
||||||
DeletedDossier.create_from_dossier(dossier)
|
|
||||||
dossier.destroy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
54
app/services/expired_dossiers_deletion_service.rb
Normal file
54
app/services/expired_dossiers_deletion_service.rb
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
class ExpiredDossiersDeletionService
|
||||||
|
def self.process_expired_dossiers_brouillon
|
||||||
|
send_brouillon_expiration_notices
|
||||||
|
delete_expired_brouillons_and_notify
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def self.send_brouillon_expiration_notices
|
||||||
|
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)
|
||||||
|
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
|
||||||
|
|
||||||
|
def self.delete_expired_brouillons_and_notify
|
||||||
|
dossier_to_remove = []
|
||||||
|
users_to_notify = {}
|
||||||
|
|
||||||
|
Dossier.brouillon_expired
|
||||||
|
.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)
|
||||||
|
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|
|
||||||
|
DeletedDossier.create_from_dossier(dossier)
|
||||||
|
dossier.destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1072,60 +1072,6 @@ describe Dossier do
|
||||||
it { expect(Dossier.for_procedure(procedure_2)).to contain_exactly(dossier_2_1) }
|
it { expect(Dossier.for_procedure(procedure_2)).to contain_exactly(dossier_2_1) }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#send_brouillon_expiration_notices' do
|
|
||||||
before { Timecop.freeze(Time.zone.parse('12/12/2012 15:00:00')) }
|
|
||||||
|
|
||||||
let!(:procedure) { create(:procedure, duree_conservation_dossiers_dans_ds: 6) }
|
|
||||||
let!(:date_close_to_expiration) { Time.zone.now - procedure.duree_conservation_dossiers_dans_ds.months + 1.month }
|
|
||||||
let!(:date_expired) { Time.zone.now - procedure.duree_conservation_dossiers_dans_ds.months - 6.days }
|
|
||||||
let!(:date_not_expired) { Time.zone.now - procedure.duree_conservation_dossiers_dans_ds.months + 2.months }
|
|
||||||
|
|
||||||
after { Timecop.return }
|
|
||||||
|
|
||||||
context "Envoi de message pour les dossiers expirant dans - d'un mois" do
|
|
||||||
let!(:expired_brouillon) { create(:dossier, procedure: procedure, created_at: date_expired) }
|
|
||||||
let!(:brouillon_close_to_expiration) { create(:dossier, procedure: procedure, created_at: date_close_to_expiration) }
|
|
||||||
let!(:brouillon_close_but_with_notice_sent) { create(:dossier, procedure: procedure, created_at: date_close_to_expiration, brouillon_close_to_expiration_notice_sent_at: Time.zone.now) }
|
|
||||||
let!(:valid_brouillon) { create(:dossier, procedure: procedure, created_at: date_not_expired) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
allow(DossierMailer).to receive(:notify_brouillon_near_deletion).and_return(double(deliver_later: nil))
|
|
||||||
Dossier.send_brouillon_expiration_notices
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'verification de la creation de mail' do
|
|
||||||
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(brouillon_close_to_expiration.user, [brouillon_close_to_expiration])
|
|
||||||
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(expired_brouillon.user, [expired_brouillon])
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'Verification du changement d etat du champ' do
|
|
||||||
expect(brouillon_close_to_expiration.reload.brouillon_close_to_expiration_notice_sent_at).not_to be_nil
|
|
||||||
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.hash_for_deletion_mail])
|
|
||||||
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
|
|
||||||
|
|
||||||
describe '#geo_position' do
|
describe '#geo_position' do
|
||||||
let(:lat) { "46.538192" }
|
let(:lat) { "46.538192" }
|
||||||
let(:lon) { "2.428462" }
|
let(:lon) { "2.428462" }
|
||||||
|
|
43
spec/services/expired_dossiers_deletion_service_spec.rb
Normal file
43
spec/services/expired_dossiers_deletion_service_spec.rb
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe ExpiredDossiersDeletionService do
|
||||||
|
describe '#process_expired_dossiers_brouillon' do
|
||||||
|
let(:draft_expiration) { 1.month + 5.days }
|
||||||
|
let!(:today) { Time.zone.now.at_midnight }
|
||||||
|
let!(:procedure) { create(:procedure, duree_conservation_dossiers_dans_ds: 6) }
|
||||||
|
let!(:date_close_to_expiration) { Date.today - procedure.duree_conservation_dossiers_dans_ds.months + 1.month }
|
||||||
|
let!(:date_expired) { Date.today - procedure.duree_conservation_dossiers_dans_ds.months - 6.days }
|
||||||
|
let!(:date_not_expired) { Date.today - procedure.duree_conservation_dossiers_dans_ds.months + 2.months }
|
||||||
|
|
||||||
|
context 'send messages for dossiers expiring soon and delete expired' do
|
||||||
|
let!(:expired_brouillon) { create(:dossier, procedure: procedure, created_at: date_expired, brouillon_close_to_expiration_notice_sent_at: today - (draft_expiration + 1.day)) }
|
||||||
|
let!(:brouillon_close_to_expiration) { create(:dossier, procedure: procedure, created_at: date_close_to_expiration) }
|
||||||
|
let!(:brouillon_close_but_with_notice_sent) { create(:dossier, procedure: procedure, created_at: date_close_to_expiration, brouillon_close_to_expiration_notice_sent_at: Time.zone.now) }
|
||||||
|
let!(:valid_brouillon) { create(:dossier, procedure: procedure, created_at: date_not_expired) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(DossierMailer).to receive(:notify_brouillon_near_deletion).and_return(double(deliver_later: nil))
|
||||||
|
allow(DossierMailer).to receive(:notify_brouillon_deletion).and_return(double(deliver_later: nil))
|
||||||
|
|
||||||
|
ExpiredDossiersDeletionService.process_expired_dossiers_brouillon
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'emails should be sent' do
|
||||||
|
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once
|
||||||
|
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(brouillon_close_to_expiration.user, [brouillon_close_to_expiration])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'dossier state should change' do
|
||||||
|
expect(brouillon_close_to_expiration.reload.brouillon_close_to_expiration_notice_sent_at).not_to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'deletes and notify expired brouillon' do
|
||||||
|
expect(DossierMailer).to have_received(:notify_brouillon_deletion).once
|
||||||
|
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(expired_brouillon.user, [expired_brouillon.hash_for_deletion_mail])
|
||||||
|
|
||||||
|
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
|
Loading…
Add table
Reference in a new issue