fix projection
This commit is contained in:
parent
8507733250
commit
7e9b68ed0e
1 changed files with 21 additions and 7 deletions
|
@ -27,6 +27,7 @@ class DossierProjectionService
|
||||||
|
|
||||||
TABLE = 'table'
|
TABLE = 'table'
|
||||||
COLUMN = 'column'
|
COLUMN = 'column'
|
||||||
|
STABLE_ID = 'stable_id'
|
||||||
|
|
||||||
# Returns [DossierProjection(dossier, columns)] ordered by dossiers_ids
|
# Returns [DossierProjection(dossier, columns)] ordered by dossiers_ids
|
||||||
# and the columns orderd by fields.
|
# and the columns orderd by fields.
|
||||||
|
@ -41,7 +42,13 @@ class DossierProjectionService
|
||||||
# - the order of the intermediary query results are unknown
|
# - the order of the intermediary query results are unknown
|
||||||
# - some values can be missing (if a revision added or removed them)
|
# - some values can be missing (if a revision added or removed them)
|
||||||
def self.project(dossiers_ids, columns)
|
def self.project(dossiers_ids, columns)
|
||||||
fields = columns.map { |c| { TABLE => c.table, COLUMN => c.column } }
|
fields = columns.map do |c|
|
||||||
|
if c.is_a?(Columns::ChampColumn)
|
||||||
|
{ TABLE => c.table, STABLE_ID => c.stable_id, original_column: c }
|
||||||
|
else
|
||||||
|
{ TABLE => c.table, COLUMN => c.column }
|
||||||
|
end
|
||||||
|
end
|
||||||
champ_value = champ_value_formatter(dossiers_ids, fields)
|
champ_value = champ_value_formatter(dossiers_ids, fields)
|
||||||
|
|
||||||
state_field = { TABLE => 'self', COLUMN => 'state' }
|
state_field = { TABLE => 'self', COLUMN => 'state' }
|
||||||
|
@ -64,14 +71,17 @@ class DossierProjectionService
|
||||||
when 'type_de_champ', 'type_de_champ_private'
|
when 'type_de_champ', 'type_de_champ_private'
|
||||||
Champ
|
Champ
|
||||||
.where(
|
.where(
|
||||||
stable_id: fields.map { |f| f[COLUMN] },
|
stable_id: fields.map { |f| f[STABLE_ID] },
|
||||||
dossier_id: dossiers_ids
|
dossier_id: dossiers_ids
|
||||||
)
|
)
|
||||||
.select(:dossier_id, :value, :stable_id, :type, :external_id, :data, :value_json) # we cannot pluck :value, as we need the champ.to_s method
|
.select(:dossier_id, :value, :stable_id, :type, :external_id, :data, :value_json) # we cannot pluck :value, as we need the champ.to_s method
|
||||||
.group_by(&:stable_id) # the champs are redispatched to their respective fields
|
.group_by(&:stable_id) # the champs are redispatched to their respective fields
|
||||||
.map do |stable_id, champs|
|
.map do |stable_id, champs|
|
||||||
field = fields.find { |f| f[COLUMN] == stable_id.to_s }
|
fields
|
||||||
field[:id_value_h] = champs.to_h { |c| [c.dossier_id, champ_value.(c)] }
|
.filter { |f| f[STABLE_ID] == stable_id }
|
||||||
|
.each do |field|
|
||||||
|
field[:id_value_h] = champs.to_h { |c| [c.dossier_id, champ_value.(c, field[:original_column])] }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
when 'self'
|
when 'self'
|
||||||
Dossier
|
Dossier
|
||||||
|
@ -173,7 +183,7 @@ class DossierProjectionService
|
||||||
private
|
private
|
||||||
|
|
||||||
def champ_value_formatter(dossiers_ids, fields)
|
def champ_value_formatter(dossiers_ids, fields)
|
||||||
stable_ids = fields.filter { _1[TABLE].in?(['type_de_champ', 'type_de_champ_private']) }.map { _1[COLUMN] }
|
stable_ids = fields.filter { _1[TABLE].in?(['type_de_champ', 'type_de_champ_private']) }.map { _1[STABLE_ID] }
|
||||||
revision_ids_by_dossier_ids = Dossier.where(id: dossiers_ids).pluck(:id, :revision_id).to_h
|
revision_ids_by_dossier_ids = Dossier.where(id: dossiers_ids).pluck(:id, :revision_id).to_h
|
||||||
stable_ids_and_types_de_champ_by_revision_ids = ProcedureRevisionTypeDeChamp.includes(:type_de_champ)
|
stable_ids_and_types_de_champ_by_revision_ids = ProcedureRevisionTypeDeChamp.includes(:type_de_champ)
|
||||||
.where(revision_id: revision_ids_by_dossier_ids.values.uniq, type_de_champ: { stable_id: stable_ids })
|
.where(revision_id: revision_ids_by_dossier_ids.values.uniq, type_de_champ: { stable_id: stable_ids })
|
||||||
|
@ -181,10 +191,14 @@ class DossierProjectionService
|
||||||
.group_by(&:first)
|
.group_by(&:first)
|
||||||
.transform_values { _1.map { |_, type_de_champ| [type_de_champ.stable_id, type_de_champ] }.to_h }
|
.transform_values { _1.map { |_, type_de_champ| [type_de_champ.stable_id, type_de_champ] }.to_h }
|
||||||
stable_ids_and_types_de_champ_by_dossier_ids = revision_ids_by_dossier_ids.transform_values { stable_ids_and_types_de_champ_by_revision_ids[_1] }.compact
|
stable_ids_and_types_de_champ_by_dossier_ids = revision_ids_by_dossier_ids.transform_values { stable_ids_and_types_de_champ_by_revision_ids[_1] }.compact
|
||||||
-> (champ) {
|
-> (champ, column) {
|
||||||
type_de_champ = stable_ids_and_types_de_champ_by_dossier_ids.fetch(champ.dossier_id, {})[champ.stable_id]
|
type_de_champ = stable_ids_and_types_de_champ_by_dossier_ids.fetch(champ.dossier_id, {})[champ.stable_id]
|
||||||
if type_de_champ.present? && type_de_champ.type_champ == champ.last_write_type_champ
|
if type_de_champ.present? && type_de_champ.type_champ == champ.last_write_type_champ
|
||||||
type_de_champ.champ_value(champ)
|
if column.is_a?(Columns::JSONPathColumn)
|
||||||
|
column.get_value(champ)
|
||||||
|
else
|
||||||
|
type_de_champ.champ_value(champ)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue