fix(dossier): do not attempt to destroy transfers when a dossier is destroyed

we had a dependent option on both sides of a relationship which created race conditions
This commit is contained in:
Paul Chavard 2021-10-06 13:16:25 +02:00
parent f8a454d529
commit 9d5c5447c2
4 changed files with 22 additions and 2 deletions

View file

@ -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

View file

@ -0,0 +1,5 @@
FactoryBot.define do
factory :dossier_transfer do
email { generate(:user_email) }
end
end

View file

@ -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

View file

@ -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