diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 3956c14f9..0e8aee571 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -124,7 +124,7 @@ class Dossier < ApplicationRecord has_many :types_de_champ, through: :revision has_many :types_de_champ_private, through: :revision - belongs_to :transfer, class_name: 'DossierTransfer', foreign_key: 'dossier_transfer_id', optional: true, inverse_of: :dossiers, dependent: :destroy + belongs_to :transfer, class_name: 'DossierTransfer', foreign_key: 'dossier_transfer_id', optional: true, inverse_of: :dossiers has_many :transfer_logs, class_name: 'DossierTransferLog', dependent: :destroy accepts_nested_attributes_for :champs diff --git a/lib/tasks/deployment/20211006154552_remove_orphaned_avis.rake b/lib/tasks/deployment/20211006154552_remove_orphaned_avis.rake new file mode 100644 index 000000000..f782cf6b1 --- /dev/null +++ b/lib/tasks/deployment/20211006154552_remove_orphaned_avis.rake @@ -0,0 +1,22 @@ +namespace :after_party do + desc 'Deployment task: remove_orphaned_avis' + task remove_orphaned_avis: :environment do + puts "Running deploy task 'remove_orphaned_avis'" + + avis = Avis.unscope(:joins) + .joins('LEFT JOIN dossiers ON dossiers.id = avis.dossier_id') + .where(dossiers: { id: nil }) + progress = ProgressReport.new(avis.count) + + avis.find_each do |avis| + avis.destroy + progress.inc + end + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/factories/dossier_transfer.rb b/spec/factories/dossier_transfer.rb new file mode 100644 index 000000000..fef311665 --- /dev/null +++ b/spec/factories/dossier_transfer.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :dossier_transfer do + email { generate(:user_email) } + end +end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 8886cc206..df75ca854 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1439,14 +1439,18 @@ describe Dossier do end describe "#destroy" do - let(:dossier) { create(:dossier) } + let(:transfer) { create(:dossier_transfer) } + let(:dossier) { create(:dossier, transfer: transfer) } + before do + create(:dossier, transfer: transfer) create(:attestation, dossier: dossier) create(:attestation, dossier: dossier) end it "can destroy dossier with two attestations" do expect(dossier.destroy).to be_truthy + expect(transfer.reload).not_to be_nil end end diff --git a/spec/models/dossier_transfer_spec.rb b/spec/models/dossier_transfer_spec.rb index 161b03b11..a834b6a26 100644 --- a/spec/models/dossier_transfer_spec.rb +++ b/spec/models/dossier_transfer_spec.rb @@ -46,5 +46,16 @@ RSpec.describe DossierTransfer, type: :model do it { expect(DossierTransfer.with_dossiers.count).to eq(0) } end end + + describe 'dossier relationship' do + let(:transfer) { create(:dossier_transfer) } + let(:dossier) { create(:dossier, user: user, transfer: transfer) } + + it 'nullify transfer relationship on dossier' do + expect(dossier.transfer).to eq(transfer) + transfer.destroy + expect(dossier.reload.transfer).to be_nil + end + end end end