refactor(dossier): stop destroying champs in rebase

This commit is contained in:
Paul Chavard 2024-11-26 22:13:03 +01:00
parent 0594887401
commit 6cbf66dfb8
No known key found for this signature in database
3 changed files with 19 additions and 52 deletions

View file

@ -49,59 +49,27 @@ module DossierRebaseConcern
# revision we are rebasing to # revision we are rebasing to
target_revision = procedure.published_revision target_revision = procedure.published_revision
# index published types de champ coordinates by stable_id changed_stable_ids_by_op = pending_changes
target_coordinates_by_stable_id = target_revision
.revision_types_de_champ
.includes(:parent)
.index_by(&:stable_id)
changes_by_op = pending_changes
.group_by(&:op) .group_by(&:op)
.tap { _1.default = [] } .transform_values { _1.map(&:stable_id) }
updated_stable_ids = changed_stable_ids_by_op.fetch(:update, [])
champs_by_stable_id = champs added_stable_ids = changed_stable_ids_by_op.fetch(:add, [])
.group_by(&:stable_id)
.transform_values { Champ.where(id: _1) }
.tap { _1.default = Champ.none }
# remove champ
changes_by_op[:remove].each { champs_by_stable_id[_1.stable_id].destroy_all }
# update champ
changes_by_op[:update].each { champs_by_stable_id[_1.stable_id].update_all(rebased_at: Time.zone.now) }
# update dossier revision # update dossier revision
update_column(:revision_id, target_revision.id) update_column(:revision_id, target_revision.id)
# add champ (after changing dossier revision to avoid errors) # mark updated champs as rebased
changes_by_op[:add] champs.where(stable_id: updated_stable_ids).update_all(rebased_at: Time.zone.now)
.map { target_coordinates_by_stable_id[_1.stable_id] }
.each { add_new_champs_for_revision(_1) }
end
def add_new_champs_for_revision(target_coordinate) # add rows for new repetitions
if target_coordinate.child? repetition_types_de_champ = target_revision
row_ids = repetition_row_ids(target_coordinate.parent.type_de_champ) .types_de_champ
.repetition
if row_ids.present? .where(stable_id: added_stable_ids)
row_ids.each do |row_id| repetition_types_de_champ.mandatory
create_champ(target_coordinate, row_id:) .or(repetition_types_de_champ.private_only)
end .find_each do |type_de_champ|
elsif target_coordinate.parent.mandatory? self.champs << type_de_champ.build_champ(row_id: ULID.generate, rebased_at: Time.zone.now)
create_champ(target_coordinate, row_id: ULID.generate)
end end
else
create_champ(target_coordinate)
end
end
def create_champ(target_coordinate, row_id: nil)
self.champs << target_coordinate
.type_de_champ
.build_champ(rebased_at: Time.zone.now, row_id:)
end
def purge_piece_justificative_file(champ)
ActiveStorage::Attachment.where(id: champ.piece_justificative_file.ids).delete_all
end end
end end

View file

@ -171,6 +171,7 @@ class TypeDeChamp < ApplicationRecord
scope :not_condition, -> { where(condition: nil) } scope :not_condition, -> { where(condition: nil) }
scope :fillable, -> { where.not(type_champ: [type_champs.fetch(:header_section), type_champs.fetch(:explication)]) } scope :fillable, -> { where.not(type_champ: [type_champs.fetch(:header_section), type_champs.fetch(:explication)]) }
scope :with_header_section, -> { where.not(type_champ: TypeDeChamp.type_champs[:explication]) } scope :with_header_section, -> { where.not(type_champ: TypeDeChamp.type_champs[:explication]) }
scope :mandatory, -> { where(mandatory: true) }
scope :dubious, -> { scope :dubious, -> {
where("unaccent(types_de_champ.libelle) ~* unaccent(?)", DubiousProcedure.forbidden_regexp) where("unaccent(types_de_champ.libelle) ~* unaccent(?)", DubiousProcedure.forbidden_regexp)

View file

@ -356,7 +356,7 @@ describe DossierRebaseConcern do
it "updates the brouillon champs with the latest revision changes" do it "updates the brouillon champs with the latest revision changes" do
expect(dossier.revision).to eq(procedure.published_revision) expect(dossier.revision).to eq(procedure.published_revision)
expect(dossier.project_champs_public.size).to eq(5) expect(dossier.project_champs_public.size).to eq(5)
expect(dossier.champs.count(&:public?)).to eq(7) expect(dossier.champs.count(&:public?)).to eq(6)
expect(repetition_champ.rows.size).to eq(2) expect(repetition_champ.rows.size).to eq(2)
expect(repetition_champ.rows[0].size).to eq(1) expect(repetition_champ.rows[0].size).to eq(1)
expect(repetition_champ.rows[1].size).to eq(1) expect(repetition_champ.rows[1].size).to eq(1)
@ -369,7 +369,7 @@ describe DossierRebaseConcern do
expect(procedure.revisions.size).to eq(3) expect(procedure.revisions.size).to eq(3)
expect(dossier.revision).to eq(procedure.published_revision) expect(dossier.revision).to eq(procedure.published_revision)
expect(dossier.project_champs_public.size).to eq(7) expect(dossier.project_champs_public.size).to eq(7)
expect(dossier.champs.count(&:public?)).to eq(13) expect(dossier.champs.count(&:public?)).to eq(7)
expect(rebased_text_champ.value).to eq(text_champ.value) expect(rebased_text_champ.value).to eq(text_champ.value)
expect(rebased_text_champ.type_de_champ).not_to eq(text_champ.type_de_champ) expect(rebased_text_champ.type_de_champ).not_to eq(text_champ.type_de_champ)
expect(rebased_datetime_champ.type_champ).to eq(TypeDeChamp.type_champs.fetch(:date)) expect(rebased_datetime_champ.type_champ).to eq(TypeDeChamp.type_champs.fetch(:date))
@ -381,7 +381,6 @@ describe DossierRebaseConcern do
expect(rebased_datetime_champ.rebased_at).not_to be_nil expect(rebased_datetime_champ.rebased_at).not_to be_nil
expect(rebased_number_champ.rebased_at).to be_nil expect(rebased_number_champ.rebased_at).to be_nil
expect(rebased_new_repetition_champ).not_to be_nil expect(rebased_new_repetition_champ).not_to be_nil
expect(rebased_new_repetition_champ.rebased_at).not_to be_nil
expect(rebased_new_repetition_champ.rows.size).to eq(1) expect(rebased_new_repetition_champ.rows.size).to eq(1)
expect(rebased_new_repetition_champ.rows[0].size).to eq(2) expect(rebased_new_repetition_champ.rows[0].size).to eq(2)
@ -728,9 +727,8 @@ describe DossierRebaseConcern do
parent.update(type_champ: :integer_number) parent.update(type_champ: :integer_number)
end end
it { expect { subject }.to change { dossier.champs.filter(&:child?).count }.from(2).to(0) }
it { expect { subject }.to change { Champ.count }.from(3).to(1) }
it { expect { subject }.to change { dossier.project_champs_public.find(&:repetition?)&.libelle }.from('p1').to(nil) } it { expect { subject }.to change { dossier.project_champs_public.find(&:repetition?)&.libelle }.from('p1').to(nil) }
it { expect { subject }.not_to change { Champ.count } }
end end
end end
end end