move facets list to facets

This commit is contained in:
simon lehericey 2024-07-19 15:40:43 +02:00 committed by mfo
parent 49eaf10bce
commit 2239172fac
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 125 additions and 95 deletions

View file

@ -1,5 +1,8 @@
class Facet
def initialize(table:, column:, label: nil, virtual: false, type: :text, value_column: :value, filterable: true, classname: nil, scope: '')
TYPE_DE_CHAMP = 'type_de_champ'
TYPE_DE_CHAMP_PRIVATE = 'type_de_champ_private'
def initialize(table:, column:, label: nil, virtual: false, type: :text, value_column: :value, filterable: true, classname: '', scope: '')
@table = table
@column = column
@label = label || I18n.t(column, scope: [:activerecord, :attributes, :procedure_presentation, :fields, table])
@ -22,4 +25,118 @@ class Facet
table:, column:, label:, classname:, virtual:, type:, scope:, value_column:, filterable:
}
end
def self.dossier_facets(procedure:)
[
new(table: 'self', column: 'created_at', type: :date),
new(table: 'self', column: 'updated_at', type: :date),
new(table: 'self', column: 'depose_at', type: :date),
new(table: 'self', column: 'en_construction_at', type: :date),
new(table: 'self', column: 'en_instruction_at', type: :date),
new(table: 'self', column: 'processed_at', type: :date),
*sva_svr_facets(procedure:, for_filters: true),
new(table: 'self', column: 'updated_since', type: :date, virtual: true),
new(table: 'self', column: 'depose_since', type: :date, virtual: true),
new(table: 'self', column: 'en_construction_since', type: :date, virtual: true),
new(table: 'self', column: 'en_instruction_since', type: :date, virtual: true),
new(table: 'self', column: 'processed_since', type: :date, virtual: true),
new(table: 'self', column: 'state', type: :enum, scope: 'instructeurs.dossiers.filterable_state', virtual: true)
].compact_blank
end
def self.facets(procedure:)
facets = Facet.dossier_facets(procedure:)
facets.push(
new(table: 'user', column: 'email', type: :text),
new(table: 'followers_instructeurs', column: 'email', type: :text),
new(table: 'groupe_instructeur', column: 'id', type: :enum),
new(table: 'avis', column: 'question_answer', filterable: false)
)
if procedure.for_individual
facets.push(
new(table: "individual", column: "prenom", type: :text),
new(table: "individual", column: "nom", type: :text),
new(table: "individual", column: "gender", type: :text)
)
end
if !procedure.for_individual
facets.push(
new(table: 'etablissement', column: 'entreprise_siren', type: :text),
new(table: 'etablissement', column: 'entreprise_forme_juridique', type: :text),
new(table: 'etablissement', column: 'entreprise_nom_commercial', type: :text),
new(table: 'etablissement', column: 'entreprise_raison_sociale', type: :text),
new(table: 'etablissement', column: 'entreprise_siret_siege_social', type: :text),
new(table: 'etablissement', column: 'entreprise_date_creation', type: :date)
)
facets.push(
new(table: 'etablissement', column: 'siret', type: :text),
new(table: 'etablissement', column: 'libelle_naf', type: :text),
new(table: 'etablissement', column: 'code_postal', type: :text)
)
end
facets.concat(procedure.types_de_champ_for_procedure_presentation
.pluck(:type_champ, :libelle, :private, :stable_id)
.reject { |(type_champ)| type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
.flat_map do |(type_champ, libelle, is_private, stable_id)|
tdc = TypeDeChamp.new(type_champ:, libelle:, private: is_private, stable_id:)
if is_private
facets_for_type_de_champ_private(tdc)
else
facets_for_type_de_champ_public(tdc)
end
end)
facets
end
def self.facets_for_type_de_champ_public(tdc)
tdc.dynamic_type.search_paths.map do |path_struct|
new(
table: TYPE_DE_CHAMP,
column: tdc.stable_id.to_s,
label: path_struct[:libelle],
type: TypeDeChamp.filter_hash_type(tdc.type_champ),
value_column: path_struct[:path]
)
end
end
def self.facets_for_type_de_champ_private(tdc)
tdc.dynamic_type.search_paths.map do |path_struct|
new(
table: TYPE_DE_CHAMP_PRIVATE,
column: tdc.stable_id.to_s,
label: path_struct[:libelle],
type: TypeDeChamp.filter_hash_type(tdc.type_champ),
value_column: path_struct[:path]
)
end
end
def self.sva_svr_facets(procedure:, for_filters: false)
return if !procedure.sva_svr_enabled?
i18n_scope = [:activerecord, :attributes, :procedure_presentation, :fields, :self]
facets = []
facets << new(table: 'self', column: 'sva_svr_decision_on',
type: :date,
label: I18n.t("#{procedure.sva_svr_decision}_decision_on", scope: i18n_scope),
classname: for_filters ? '' : 'sva-col')
if for_filters
facets << new(table: 'self', column: 'sva_svr_decision_before',
label: I18n.t("#{procedure.sva_svr_decision}_decision_before", scope: i18n_scope),
type: :date, virtual: true)
end
facets
end
end

View file

@ -25,82 +25,15 @@ class ProcedurePresentation < ApplicationRecord
validate :check_allowed_filter_columns
validate :check_filters_max_length
def self_fields
[
field_hash('self', 'created_at', type: :date),
field_hash('self', 'updated_at', type: :date),
field_hash('self', 'depose_at', type: :date),
field_hash('self', 'en_construction_at', type: :date),
field_hash('self', 'en_instruction_at', type: :date),
field_hash('self', 'processed_at', type: :date),
*sva_svr_fields(for_filters: true),
field_hash('self', 'updated_since', type: :date, virtual: true),
field_hash('self', 'depose_since', type: :date, virtual: true),
field_hash('self', 'en_construction_since', type: :date, virtual: true),
field_hash('self', 'en_instruction_since', type: :date, virtual: true),
field_hash('self', 'processed_since', type: :date, virtual: true),
field_hash('self', 'state', type: :enum, scope: 'instructeurs.dossiers.filterable_state', virtual: true)
].compact_blank
end
def facets
facets = self_fields
facets.push(
field_hash('user', 'email', type: :text),
field_hash('followers_instructeurs', 'email', type: :text),
field_hash('groupe_instructeur', 'id', type: :enum),
field_hash('avis', 'question_answer', filterable: false)
)
if procedure.for_individual
facets.push(
field_hash("individual", "prenom", type: :text),
field_hash("individual", "nom", type: :text),
field_hash("individual", "gender", type: :text)
)
end
if !procedure.for_individual
facets.push(
field_hash('etablissement', 'entreprise_siren', type: :text),
field_hash('etablissement', 'entreprise_forme_juridique', type: :text),
field_hash('etablissement', 'entreprise_nom_commercial', type: :text),
field_hash('etablissement', 'entreprise_raison_sociale', type: :text),
field_hash('etablissement', 'entreprise_siret_siege_social', type: :text),
field_hash('etablissement', 'entreprise_date_creation', type: :date)
)
facets.push(
field_hash('etablissement', 'siret', type: :text),
field_hash('etablissement', 'libelle_naf', type: :text),
field_hash('etablissement', 'code_postal', type: :text)
)
end
facets.concat(procedure.types_de_champ_for_procedure_presentation
.pluck(:type_champ, :libelle, :private, :stable_id)
.reject { |(type_champ)| type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
.map do |(type_champ, libelle, is_private, stable_id)|
if is_private
field_hash_for_type_de_champ_private(type_champ, libelle, stable_id)
else
field_hash_for_type_de_champ_public(type_champ, libelle, stable_id)
end
end)
facets
end
def displayable_fields_for_select
[
facets.reject(&:virtual).map { |facet| [facet.label, facet.id] },
Facet.facets(procedure:).reject(&:virtual).map { |facet| [facet.label, facet.id] },
displayed_fields.map { Facet.new(**_1.deep_symbolize_keys).id }
]
end
def filterable_fields_options
facets.filter_map do |facet|
Facet.facets(procedure:).filter_map do |facet|
next if facet.filterable == false
[facet.label, facet.id]
@ -112,30 +45,10 @@ class ProcedurePresentation < ApplicationRecord
field_hash('self', 'id', classname: 'number-col'),
*displayed_fields.map { Facet.new(**_1.deep_symbolize_keys) },
field_hash('self', 'state', classname: 'state-col'),
*sva_svr_fields
*Facet.sva_svr_facets(procedure:)
]
end
def sva_svr_fields(for_filters: false)
return if !procedure.sva_svr_enabled?
i18n_scope = [:activerecord, :attributes, :procedure_presentation, :fields, :self]
fields = []
fields << field_hash('self', 'sva_svr_decision_on',
type: :date,
label: I18n.t("#{procedure.sva_svr_decision}_decision_on", scope: i18n_scope),
classname: for_filters ? '' : 'sva-col')
if for_filters
fields << field_hash('self', 'sva_svr_decision_before',
label: I18n.t("#{procedure.sva_svr_decision}_decision_before", scope: i18n_scope),
type: :date, virtual: true)
end
fields
end
def sorted_ids(dossiers, count)
table, column, order = sort.values_at(TABLE, COLUMN, 'order')
@ -200,7 +113,7 @@ class ProcedurePresentation < ApplicationRecord
value_column = filters.pluck('value_column').compact.first || :value
case table
when 'self'
field = self_fields.find { |h| h.column == column }
field = Facet.dossier_facets(procedure:).find { |h| h.column == column }
if field.type == :date
dates = values
.filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
@ -281,7 +194,7 @@ class ProcedurePresentation < ApplicationRecord
instructeur.groupe_instructeurs
.find { _1.id == filter['value'].to_i }&.label || filter['value']
else
facet = facets.find { _1.table == filter[TABLE] && _1.column == filter[COLUMN] }
facet = Facet.facets(procedure:).find { _1.table == filter[TABLE] && _1.column == filter[COLUMN] }
if facet.type == :date
parsed_date = safe_parse_date(filter['value'])
@ -417,7 +330,7 @@ class ProcedurePresentation < ApplicationRecord
end
def find_facet(facet_id)
facets.find { _1.id == facet_id }
Facet.facets(procedure:).find { _1.id == facet_id }
end
def find_type_de_champ(column)
@ -494,7 +407,7 @@ class ProcedurePresentation < ApplicationRecord
end
def valid_columns_for_table(table)
@column_whitelist ||= facets
@column_whitelist ||= Facet.facets(procedure:)
.group_by(&:table)
.transform_values { |facets| Set.new(facets.map(&:column)) }