Add a job to remove discarded dossiers

This commit is contained in:
Paul Chavard 2020-03-19 13:11:45 +01:00
parent 26b41caca9
commit c086f6d580
4 changed files with 48 additions and 0 deletions

View file

@ -80,6 +80,7 @@ En local, un utilisateur de test est créé automatiquement, avec les identifian
ExpiredDossiersDeletionJob.set(cron: "0 7 * * *").perform_later
PurgeStaleExportsJob.set(cron: "*/5 * * * *").perform_later
NotifyDraftNotSubmittedJob.set(cron: "0 7 * * *").perform_later
DiscardedDossiersDeletionJob.set(cron: "0 7 * * *").perform_later
### Voir les emails envoyés en local

View file

@ -0,0 +1,8 @@
class DiscardedDossiersDeletionJob < ApplicationJob
queue_as :cron
def perform(*args)
Dossier.discarded_brouillon_expired.destroy_all
Dossier.discarded_en_construction_expired.destroy_all
end
end

View file

@ -199,6 +199,21 @@ class Dossier < ApplicationRecord
scope :without_brouillon_expiration_notice_sent, -> { where(brouillon_close_to_expiration_notice_sent_at: nil) }
scope :without_en_construction_expiration_notice_sent, -> { where(en_construction_close_to_expiration_notice_sent_at: nil) }
scope :discarded_brouillon_expired, -> do
with_discarded
.discarded
.state_brouillon
.where('hidden_at < ?', 1.month.ago)
end
scope :discarded_en_construction_expired, -> do
with_discarded
.discarded
.state_en_construction
.joins(:procedure)
.where('dossiers.hidden_at < ?', 1.month.ago)
.where(procedures: { hidden_at: nil })
end
scope :brouillon_near_procedure_closing_date, -> do
# select users who have submitted dossier for the given 'procedures.id'
users_who_submitted =

View file

@ -1226,4 +1226,28 @@ describe Dossier do
expect(DossierOperationLog.count).to eq(1)
end
end
describe 'discarded_brouillon_expired and discarded_en_construction_expired' do
before do
create(:dossier)
create(:dossier, :en_construction)
create(:dossier).discard!
create(:dossier, :en_construction).discard!
Timecop.travel(2.months.ago) do
create(:dossier).discard!
create(:dossier, :en_construction).discard!
create(:dossier).procedure.hide!
create(:dossier, :en_construction).procedure.hide!
end
Timecop.travel(1.week.ago) do
create(:dossier).discard!
create(:dossier, :en_construction).discard!
end
end
it { expect(Dossier.discarded_brouillon_expired.count).to eq(2) }
it { expect(Dossier.discarded_en_construction_expired.count).to eq(1) }
end
end