Merge pull request #8514 from tchak/fix-dossier-rebase-no-touch-all

fix(dossier): set rebased_at only on changed champs
This commit is contained in:
Paul Chavard 2023-01-30 12:09:45 +01:00 committed by GitHub
commit 4f70185a6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 26 deletions

View file

@ -54,32 +54,32 @@ module DossierRebaseConcern
.group_by(&:op) .group_by(&:op)
.tap { _1.default = [] } .tap { _1.default = [] }
champs_by_stable_id = champs
.joins(:type_de_champ)
.group_by(&:stable_id)
.transform_values { Champ.where(id: _1) }
# add champ # add champ
changes_by_op[:add] changes_by_op[:add]
.map(&:stable_id) .each { add_new_champs_for_revision(target_coordinates_by_stable_id[_1.stable_id]) }
.map { target_coordinates_by_stable_id[_1] }
.each { add_new_champs_for_revision(_1) }
# remove champ # remove champ
changes_by_op[:remove] changes_by_op[:remove]
.each { delete_champs_for_revision(_1.stable_id) } .each { champs_by_stable_id[_1.stable_id].destroy_all }
if brouillon? if brouillon?
changes_by_op[:update] changes_by_op[:update]
.map { |change| [change, champs.joins(:type_de_champ).where(type_de_champ: { stable_id: change.stable_id })] } .each { apply(_1, champs_by_stable_id[_1.stable_id]) }
.each { |change, champs| apply(change, champs) }
end end
# due to repetition tdc clone on update or erase # due to repetition tdc clone on update or erase
# we must reassign tdc to the latest version # we must reassign tdc to the latest version
Champ champs_by_stable_id
.includes(:type_de_champ) .filter_map { |stable_id, champs| [target_coordinates_by_stable_id[stable_id].type_de_champ_id, champs] if champs.present? }
.where(dossier: self) .each { |type_de_champ_id, champs| champs.update_all(type_de_champ_id:) }
.map { [_1, target_coordinates_by_stable_id[_1.stable_id].type_de_champ] }
.each { |champ, target_tdc| champ.update_columns(type_de_champ_id: target_tdc.id, rebased_at: Time.zone.now) }
# update dossier revision # update dossier revision
self.update_column(:revision_id, target_revision.id) update_column(:revision_id, target_revision.id)
end end
def apply(change, champs) def apply(change, champs)
@ -92,18 +92,27 @@ module DossierRebaseConcern
value: nil, value: nil,
value_json: nil, value_json: nil,
external_id: nil, external_id: nil,
data: nil) data: nil,
rebased_at: Time.zone.now)
when :drop_down_options when :drop_down_options
# we are removing options, we need to remove the value if it contains one of the removed options # we are removing options, we need to remove the value if it contains one of the removed options
removed_options = change.from - change.to removed_options = change.from - change.to
if removed_options.present? && champs.any? { _1.in?(removed_options) } if removed_options.present? && champs.any? { _1.in?(removed_options) }
champs.filter { _1.in?(removed_options) }.each { _1.remove_option(removed_options) } champs.filter { _1.in?(removed_options) }.each do
_1.remove_option(removed_options)
_1.update_column(:rebased_at, Time.zone.now)
end
end end
when :carte_layers when :carte_layers
# if we are removing cadastres layer, we need to remove cadastre geo areas # if we are removing cadastres layer, we need to remove cadastre geo areas
if change.from.include?(:cadastres) && !change.to.include?(:cadastres) if change.from.include?(:cadastres) && !change.to.include?(:cadastres)
champs.each { _1.cadastres.each(&:destroy) } champs.filter { _1.cadastres.present? }.each do
_1.cadastres.each(&:destroy)
_1.update_column(:rebased_at, Time.zone.now)
end
end end
else
champs.update_all(rebased_at: Time.zone.now)
end end
end end
@ -126,20 +135,13 @@ module DossierRebaseConcern
end end
def create_champ(target_coordinate, parent, row_id: nil) def create_champ(target_coordinate, parent, row_id: nil)
params = { revision: target_coordinate.revision, row_id: }.compact params = { revision: target_coordinate.revision, rebased_at: Time.zone.now, row_id: }.compact
champ = target_coordinate champ = target_coordinate
.type_de_champ .type_de_champ
.build_champ(params) .build_champ(params)
parent.champs << champ parent.champs << champ
end end
def delete_champs_for_revision(stable_id)
champs
.joins(:type_de_champ)
.where(types_de_champ: { stable_id: })
.destroy_all
end
def purge_piece_justificative_file(champ) def purge_piece_justificative_file(champ)
ActiveStorage::Attachment.where(id: champ.piece_justificative_file.ids).delete_all ActiveStorage::Attachment.where(id: champ.piece_justificative_file.ids).delete_all
end end

View file

@ -239,7 +239,7 @@ describe Dossier do
end end
describe "#rebase" do describe "#rebase" do
let(:procedure) { create(:procedure, :with_type_de_champ_mandatory, :with_yes_no, :with_repetition, :with_datetime) } let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :text, mandatory: true }, { type: :repetition, children: [{ type: :text }] }, { type: :datetime }, { type: :yes_no }, { type: :integer_number }]) }
let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier) { create(:dossier, procedure: procedure) }
let(:yes_no_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |tdc| tdc.type_champ == TypeDeChamp.type_champs.fetch(:yes_no) } } let(:yes_no_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |tdc| tdc.type_champ == TypeDeChamp.type_champs.fetch(:yes_no) } }
@ -248,6 +248,8 @@ describe Dossier do
let(:text_champ) { dossier.champs_public.find(&:mandatory?) } let(:text_champ) { dossier.champs_public.find(&:mandatory?) }
let(:rebased_text_champ) { dossier.champs_public.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:text) } } let(:rebased_text_champ) { dossier.champs_public.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:text) } }
let(:rebased_number_champ) { dossier.champs_public.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:integer_number) } }
let(:datetime_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |tdc| tdc.type_champ == TypeDeChamp.type_champs.fetch(:datetime) } } let(:datetime_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |tdc| tdc.type_champ == TypeDeChamp.type_champs.fetch(:datetime) } }
let(:datetime_champ) { dossier.champs_public.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:datetime) } } let(:datetime_champ) { dossier.champs_public.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:datetime) } }
let(:rebased_datetime_champ) { dossier.champs_public.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:date) } } let(:rebased_datetime_champ) { dossier.champs_public.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:date) } }
@ -287,7 +289,7 @@ describe Dossier do
libelle = text_type_de_champ.libelle libelle = text_type_de_champ.libelle
expect(dossier.revision).to eq(procedure.published_revision) expect(dossier.revision).to eq(procedure.published_revision)
expect(dossier.champs_public.size).to eq(4) expect(dossier.champs_public.size).to eq(5)
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)
@ -299,7 +301,7 @@ describe Dossier 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.champs_public.size).to eq(4) expect(dossier.champs_public.size).to eq(5)
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_id).not_to eq(text_champ.type_de_champ_id) expect(rebased_text_champ.type_de_champ_id).not_to eq(text_champ.type_de_champ_id)
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))
@ -309,6 +311,7 @@ describe Dossier do
expect(rebased_repetition_champ.rows[1].size).to eq(2) expect(rebased_repetition_champ.rows[1].size).to eq(2)
expect(rebased_text_champ.rebased_at).not_to be_nil expect(rebased_text_champ.rebased_at).not_to be_nil
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
end end
end end