fix: orphan tdc due to reset_draft

This commit is contained in:
simon lehericey 2022-10-07 14:13:42 +02:00
parent 1c37fd7a6f
commit ceaf8ebbe7
2 changed files with 20 additions and 1 deletions

View file

@ -803,6 +803,7 @@ class Procedure < ApplicationRecord
if published_revision.present? && draft_changed?
transaction do
reset!
draft_revision.types_de_champ.filter(&:only_present_on_draft?).each(&:destroy)
draft_revision.update(attestation_template: nil, dossier_submitted_message: nil)
draft_revision.destroy
update!(draft_revision: create_new_revision(published_revision))

View file

@ -908,7 +908,15 @@ describe Procedure do
end
context "published procedure" do
let(:procedure) { create(:procedure, :published, attestation_template: create(:attestation_template), dossier_submitted_message: create(:dossier_submitted_message)) }
let(:procedure) do
create(
:procedure,
:published,
attestation_template: create(:attestation_template),
dossier_submitted_message: create(:dossier_submitted_message),
types_de_champ_public: [{ type: :text, libelle: 'published tdc' }]
)
end
it "should reset draft revision" do
procedure.draft_revision.add_type_de_champ(tdc_attributes)
@ -924,6 +932,16 @@ describe Procedure do
expect(procedure.draft_revision.attestation_template).to eq(previous_attestation_template)
expect(procedure.draft_revision.dossier_submitted_message).to eq(previous_dossier_submitted_message)
end
it "should erase orphan tdc" do
published_tdc = procedure.published_revision.types_de_champ.first
draft_tdc = procedure.draft_revision.add_type_de_champ(tdc_attributes)
procedure.reset_draft_revision!
expect { published_tdc.reload }.not_to raise_error(ActiveRecord::RecordNotFound)
expect { draft_tdc.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end