Merge pull request #10123 from tchak/fix-multiple-champ-copies-bug

fix(champ): fix multiple champ copies bug
This commit is contained in:
Paul Chavard 2024-03-18 16:46:17 +00:00 committed by GitHub
commit f21192bb06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 46 additions and 39 deletions

View file

@ -41,17 +41,19 @@ class Champs::RepetitionChamp < Champ
end
def rows_for_export
champs = dossier.champs_by_stable_id_with_row
row_ids.map.with_index(1) do |row_id, index|
Champs::RepetitionChamp::Row.new(index: index, row_id:, dossier_id: dossier_id.to_s, champs:)
Champs::RepetitionChamp::Row.new(index:, row_id:, dossier:)
end
end
class Row < Hashie::Dash
property :index
property :row_id
property :dossier_id
property :champs
property :dossier
def dossier_id
dossier.id.to_s
end
def read_attribute_for_serialization(attribute)
self[attribute]
@ -61,7 +63,7 @@ class Champs::RepetitionChamp < Champ
[
['Dossier ID', :dossier_id],
['Ligne', :index]
] + Dossier.champs_for_export(types_de_champ, champs, row_id)
] + dossier.champs_for_export(types_de_champ, row_id)
end
end
end

View file

@ -1270,29 +1270,20 @@ class Dossier < ApplicationRecord
if procedure.routing_enabled?
columns << ['Groupe instructeur', groupe_instructeur.label]
end
columns + self.class.champs_for_export(types_de_champ, champs_by_stable_id_with_row)
columns + champs_for_export(types_de_champ)
end
# Get all the champs values for the types de champ in the final list.
# Dossier might not have corresponding champ display nil.
# To do so, we build a virtual champ when there is no value so we can call for_export with all indexes
def self.champs_for_export(types_de_champ, champs, row_id = nil)
def champs_for_export(types_de_champ, row_id = nil)
types_de_champ.flat_map do |type_de_champ|
champ = champs[[row_id, type_de_champ.stable_id].compact]
exported_values = if champ.nil? || !champ.visible?
# some champs export multiple columns
# ex: commune.for_export => [commune, insee, departement]
# so we build a fake champ to have the right export
type_de_champ.champ.build.for_export
else
champ.for_export
end
champ = champ_for_export(type_de_champ, row_id)
# nil => [nil]
# text => [text]
# [commune, insee, departement] => [commune, insee, departement]
wrapped_exported_values = [exported_values].flatten
wrapped_exported_values = [champ.for_export].flatten
wrapped_exported_values.map.with_index do |champ_value, index|
[type_de_champ.libelle_for_export(index), champ_value]
@ -1393,12 +1384,10 @@ class Dossier < ApplicationRecord
user.france_connect_information.present?
end
def champs_by_stable_id_with_row
champs_for_revision.index_by(&:stable_id_with_row)
end
def champs_for_revision(scope: nil, root: false)
champs_index = champs.group_by(&:stable_id)
# Due to some bad data we can have multiple copies of the same champ. Ignore extra copy.
.transform_values { _1.sort_by(&:id).uniq(&:row_id) }
if scope.is_a?(TypeDeChamp)
revision.children_of(scope)
@ -1413,7 +1402,7 @@ class Dossier < ApplicationRecord
def project_champ(type_de_champ, row_id)
stable_id_with_row = [row_id, type_de_champ.stable_id].compact
champ = champs.find { _1.stable_id_with_row == stable_id_with_row }
champ = champs_by_stable_id_with_row[stable_id_with_row]
if champ.nil?
type_de_champ.build_champ(dossier: self, row_id:)
else
@ -1421,8 +1410,25 @@ class Dossier < ApplicationRecord
end
end
def champ_for_export(type_de_champ, row_id)
stable_id_with_row = [row_id, type_de_champ.stable_id].compact
champ = champs_by_stable_id_with_row[stable_id_with_row]
if champ.nil? || !champ.visible?
# some champs export multiple columns
# ex: commune.for_export => [commune, insee, departement]
# so we build a fake champ to have the right export
type_de_champ.build_champ(dossier: self, row_id:)
else
champ
end
end
private
def champs_by_stable_id_with_row
@champs_by_stable_id_with_row ||= champs.sort_by(&:id).index_by(&:stable_id_with_row)
end
def create_missing_traitemets
if en_construction_at.present? && traitements.en_construction.empty?
self.traitements.passer_en_construction(processed_at: en_construction_at)