Merge pull request #7875 from betagouv/fix_draft_reset

fix: supprime les types de champ orphelins due au reset de la version brouillon
This commit is contained in:
LeSim 2022-10-07 18:29:35 +02:00 committed by GitHub
commit acdbf5ed5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -803,6 +803,7 @@ class Procedure < ApplicationRecord
if published_revision.present? && draft_changed? if published_revision.present? && draft_changed?
transaction do transaction do
reset! 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.update(attestation_template: nil, dossier_submitted_message: nil)
draft_revision.destroy draft_revision.destroy
update!(draft_revision: create_new_revision(published_revision)) update!(draft_revision: create_new_revision(published_revision))

View file

@ -908,7 +908,15 @@ describe Procedure do
end end
context "published procedure" do 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 it "should reset draft revision" do
procedure.draft_revision.add_type_de_champ(tdc_attributes) 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.attestation_template).to eq(previous_attestation_template)
expect(procedure.draft_revision.dossier_submitted_message).to eq(previous_dossier_submitted_message) expect(procedure.draft_revision.dossier_submitted_message).to eq(previous_dossier_submitted_message)
end 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
end end