fix(revision): fix clone démarches with repetitions

This commit is contained in:
Paul Chavard 2022-05-31 13:28:56 +02:00
parent 9faca4b670
commit c0a253078c
2 changed files with 21 additions and 12 deletions

View file

@ -778,11 +778,15 @@ class Procedure < ApplicationRecord
private private
def move_new_children_to_new_parent_coordinate(new_draft) def move_new_children_to_new_parent_coordinate(new_draft)
children = new_draft.revision_types_de_champ.where.not(parent_id: nil) children = new_draft.revision_types_de_champ
.includes(parent: :type_de_champ)
.where.not(parent_id: nil)
coordinates_by_stable_id = new_draft.revision_types_de_champ
.includes(:type_de_champ)
.index_by(&:stable_id)
children.each do |child| children.each do |child|
old_parent = draft_revision.revision_types_de_champ.find(child.parent_id) child.update!(parent: coordinates_by_stable_id.fetch(child.parent.stable_id))
new_parent = new_draft.revision_types_de_champ.find_by(type_de_champ_id: old_parent.type_de_champ_id)
child.update!(parent_id: new_parent.id)
end end
new_draft.revision_types_de_champ.reload new_draft.revision_types_de_champ.reload
end end

View file

@ -432,8 +432,8 @@ describe Procedure do
received_mail: received_mail, received_mail: received_mail,
service: service, service: service,
attestation_template: build(:attestation_template, logo: logo, signature: signature), attestation_template: build(:attestation_template, logo: logo, signature: signature),
types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj, type_de_champ_repetition], types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj],
types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2, type_de_champ_private_repetition], types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2],
api_particulier_token: '123456789012345', api_particulier_token: '123456789012345',
api_particulier_scopes: ['cnaf_famille']) api_particulier_scopes: ['cnaf_famille'])
end end
@ -441,11 +441,11 @@ describe Procedure do
let(:type_de_champ_1) { build(:type_de_champ, position: 1) } let(:type_de_champ_1) { build(:type_de_champ, position: 1) }
let(:type_de_champ_2) { build(:type_de_champ_drop_down_list, position: 2) } let(:type_de_champ_2) { build(:type_de_champ_drop_down_list, position: 2) }
let(:type_de_champ_pj) { build(:type_de_champ_piece_justificative, position: 3, old_pj: { stable_id: 2713 }) } let(:type_de_champ_pj) { build(:type_de_champ_piece_justificative, position: 3, old_pj: { stable_id: 2713 }) }
let(:type_de_champ_repetition) { build(:type_de_champ_repetition, position: 4, types_de_champ: [build(:type_de_champ)]) } let(:type_de_champ_repetition) { build(:type_de_champ_repetition, position: 4, procedure: procedure, types_de_champ: [build(:type_de_champ)]) }
let(:type_de_champ_private_0) { build(:type_de_champ, :private, position: 0) } let(:type_de_champ_private_0) { build(:type_de_champ, :private, position: 0) }
let(:type_de_champ_private_1) { build(:type_de_champ, :private, position: 1) } let(:type_de_champ_private_1) { build(:type_de_champ, :private, position: 1) }
let(:type_de_champ_private_2) { build(:type_de_champ_drop_down_list, :private, position: 2) } let(:type_de_champ_private_2) { build(:type_de_champ_drop_down_list, :private, position: 2) }
let(:type_de_champ_private_repetition) { build(:type_de_champ_repetition, :private, position: 3, types_de_champ: [build(:type_de_champ, :private)]) } let(:type_de_champ_private_repetition) { build(:type_de_champ_repetition, :private, position: 3, procedure: procedure, types_de_champ: [build(:type_de_champ, :private)]) }
let(:received_mail) { build(:received_mail) } let(:received_mail) { build(:received_mail) }
let(:from_library) { false } let(:from_library) { false }
let(:administrateur) { procedure.administrateurs.first } let(:administrateur) { procedure.administrateurs.first }
@ -459,6 +459,9 @@ describe Procedure do
let!(:assign_to_2) { create(:assign_to, procedure: procedure, groupe_instructeur: groupe_instructeur_1, instructeur: instructeur_2) } let!(:assign_to_2) { create(:assign_to, procedure: procedure, groupe_instructeur: groupe_instructeur_1, instructeur: instructeur_2) }
before do before do
type_de_champ_repetition
type_de_champ_private_repetition
@procedure = procedure.clone(administrateur, from_library) @procedure = procedure.clone(administrateur, from_library)
@procedure.save @procedure.save
end end
@ -490,8 +493,9 @@ describe Procedure do
expect(stc.revision).to eq(subject.draft_revision) expect(stc.revision).to eq(subject.draft_revision)
end end
public_repetition = procedure.draft_types_de_champ.repetition.first public_repetition = type_de_champ_repetition
procedure.draft_revision.children_of(public_repetition).zip(subject.draft_revision.children_of(public_repetition)).each do |ptc, stc| cloned_public_repetition = subject.draft_types_de_champ.repetition.first
procedure.draft_revision.children_of(public_repetition).zip(subject.draft_revision.children_of(cloned_public_repetition)).each do |ptc, stc|
expect(stc).to have_same_attributes_as(ptc) expect(stc).to have_same_attributes_as(ptc)
expect(stc.revision).to eq(subject.draft_revision) expect(stc.revision).to eq(subject.draft_revision)
end end
@ -501,8 +505,9 @@ describe Procedure do
expect(stc.revision).to eq(subject.draft_revision) expect(stc.revision).to eq(subject.draft_revision)
end end
private_repetition = procedure.draft_types_de_champ_private.repetition.first private_repetition = type_de_champ_private_repetition
procedure.draft_revision.children_of(private_repetition).zip(subject.draft_revision.children_of(private_repetition)).each do |ptc, stc| cloned_private_repetition = subject.draft_types_de_champ_private.repetition.first
procedure.draft_revision.children_of(private_repetition).zip(subject.draft_revision.children_of(cloned_private_repetition)).each do |ptc, stc|
expect(stc).to have_same_attributes_as(ptc) expect(stc).to have_same_attributes_as(ptc)
expect(stc.revision).to eq(subject.draft_revision) expect(stc.revision).to eq(subject.draft_revision)
end end