fix(avis): destroy avis for discarded en_construction dossiers

This commit is contained in:
Paul Chavard 2021-11-03 21:29:00 +01:00
parent 6008b81c19
commit 122cdacbc2
3 changed files with 38 additions and 0 deletions

View file

@ -50,6 +50,7 @@ class Avis < ApplicationRecord
scope :by_latest, -> { order(updated_at: :desc) }
scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) }
scope :discarded_termine_expired, -> { unscope(:joins).where(dossier: Dossier.discarded_termine_expired) }
scope :discarded_en_construction_expired, -> { unscope(:joins).where(dossier: Dossier.discarded_en_construction_expired) }
# The form allows subtmitting avis requests to several emails at once,
# hence this virtual attribute.

View file

@ -977,6 +977,7 @@ class Dossier < ApplicationRecord
transaction do
DossierOperationLog.discarded_en_construction_expired.destroy_all
Avis.discarded_en_construction_expired.destroy_all
discarded_en_construction_expired.destroy_all
end

View file

@ -1461,6 +1461,42 @@ describe Dossier do
expect(dossier.destroy).to be_truthy
expect(transfer.reload).not_to be_nil
end
context 'discarded' do
context 'en_construction' do
let(:dossier) { create(:dossier, :en_construction) }
before do
create(:avis, dossier: dossier)
Timecop.travel(2.weeks.ago) do
dossier.discard!
end
dossier.reload
end
it "can destroy dossier with avis" do
Avis.discarded_en_construction_expired.destroy_all
expect(dossier.destroy).to be_truthy
end
end
context 'termine' do
let(:dossier) { create(:dossier, :accepte) }
before do
create(:avis, dossier: dossier)
Timecop.travel(2.weeks.ago) do
dossier.discard!
end
dossier.reload
end
it "can destroy dossier with avis" do
Avis.discarded_termine_expired.destroy_all
expect(dossier.destroy).to be_truthy
end
end
end
end
describe "#spreadsheet_columns" do