2018-03-06 13:44:29 +01:00
|
|
|
|
class ProcedurePresentation < ApplicationRecord
|
2018-10-03 16:20:27 +02:00
|
|
|
|
EXTRA_SORT_COLUMNS = {
|
2018-10-05 15:50:38 +02:00
|
|
|
|
'notifications' => ['notifications'],
|
|
|
|
|
'self' => ['id', 'state']
|
2018-10-03 16:20:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 17:03:30 +02:00
|
|
|
|
belongs_to :assign_to
|
2018-10-02 17:04:23 +02:00
|
|
|
|
|
|
|
|
|
delegate :procedure, to: :assign_to
|
|
|
|
|
|
|
|
|
|
validate :check_allowed_displayed_fields
|
|
|
|
|
validate :check_allowed_sort_column
|
2018-10-05 15:31:24 +02:00
|
|
|
|
validate :check_allowed_sort_order
|
2018-10-02 17:04:23 +02:00
|
|
|
|
validate :check_allowed_filter_columns
|
|
|
|
|
|
2018-10-03 16:05:27 +02:00
|
|
|
|
def fields
|
2018-10-05 14:45:58 +02:00
|
|
|
|
fields = [
|
|
|
|
|
field_hash('Créé le', 'self', 'created_at'),
|
2018-10-17 11:29:52 +02:00
|
|
|
|
field_hash('En construction le', 'self', 'en_construction_at'),
|
2018-10-05 14:45:58 +02:00
|
|
|
|
field_hash('Mis à jour le', 'self', 'updated_at'),
|
|
|
|
|
field_hash('Demandeur', 'user', 'email')
|
|
|
|
|
]
|
|
|
|
|
|
2018-10-11 10:45:03 +02:00
|
|
|
|
if procedure.for_individual
|
|
|
|
|
fields.push(
|
|
|
|
|
field_hash("Prénom", "individual", "prenom"),
|
|
|
|
|
field_hash("Nom", "individual", "nom"),
|
|
|
|
|
field_hash("Civilité", "individual", "gender")
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 14:45:58 +02:00
|
|
|
|
if !procedure.for_individual || (procedure.for_individual && procedure.individual_with_siret)
|
|
|
|
|
fields.push(
|
|
|
|
|
field_hash('SIREN', 'etablissement', 'entreprise_siren'),
|
|
|
|
|
field_hash('Forme juridique', 'etablissement', 'entreprise_forme_juridique'),
|
|
|
|
|
field_hash('Nom commercial', 'etablissement', 'entreprise_nom_commercial'),
|
|
|
|
|
field_hash('Raison sociale', 'etablissement', 'entreprise_raison_sociale'),
|
|
|
|
|
field_hash('SIRET siège social', 'etablissement', 'entreprise_siret_siege_social'),
|
|
|
|
|
field_hash('Date de création', 'etablissement', 'entreprise_date_creation')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
fields.push(
|
|
|
|
|
field_hash('SIRET', 'etablissement', 'siret'),
|
|
|
|
|
field_hash('Libellé NAF', 'etablissement', 'libelle_naf'),
|
|
|
|
|
field_hash('Code postal', 'etablissement', 'code_postal')
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
explanatory_types_de_champ = [:header_section, :explication].map{ |k| TypeDeChamp.type_champs.fetch(k) }
|
|
|
|
|
|
|
|
|
|
fields.concat procedure.types_de_champ
|
|
|
|
|
.reject { |tdc| explanatory_types_de_champ.include?(tdc.type_champ) }
|
|
|
|
|
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.id.to_s) }
|
|
|
|
|
|
|
|
|
|
fields.concat procedure.types_de_champ_private
|
|
|
|
|
.reject { |tdc| explanatory_types_de_champ.include?(tdc.type_champ) }
|
|
|
|
|
.map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.id.to_s) }
|
|
|
|
|
|
|
|
|
|
fields
|
2018-10-03 16:05:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 14:46:12 +02:00
|
|
|
|
def fields_for_select
|
2018-10-03 16:05:27 +02:00
|
|
|
|
fields.map do |field|
|
2018-10-03 14:46:12 +02:00
|
|
|
|
[field['label'], "#{field['table']}/#{field['column']}"]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 15:25:24 +02:00
|
|
|
|
def displayed_field_values(dossier)
|
2018-10-05 15:48:37 +02:00
|
|
|
|
assert_matching_procedure(dossier)
|
2018-10-05 15:25:24 +02:00
|
|
|
|
displayed_fields.map { |field| get_value(dossier, field['table'], field['column']) }
|
2018-10-05 11:30:14 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 10:08:39 +02:00
|
|
|
|
def sorted_ids(dossiers, gestionnaire)
|
2018-10-05 15:48:37 +02:00
|
|
|
|
dossiers.each { |dossier| assert_matching_procedure(dossier) }
|
2018-10-05 10:08:39 +02:00
|
|
|
|
table = sort['table']
|
|
|
|
|
column = sanitized_column(sort)
|
|
|
|
|
order = sort['order']
|
|
|
|
|
|
|
|
|
|
case table
|
|
|
|
|
when 'notifications'
|
2018-10-23 18:48:05 +02:00
|
|
|
|
dossiers_id_with_notification = gestionnaire.dossiers_id_with_notifications(dossiers)
|
2018-10-05 10:08:39 +02:00
|
|
|
|
if order == 'desc'
|
|
|
|
|
return dossiers_id_with_notification +
|
|
|
|
|
(dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
|
|
|
|
|
else
|
|
|
|
|
return (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) +
|
|
|
|
|
dossiers_id_with_notification
|
|
|
|
|
end
|
|
|
|
|
when 'self'
|
|
|
|
|
return dossiers
|
|
|
|
|
.order("#{column} #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
when 'type_de_champ', 'type_de_champ_private'
|
|
|
|
|
return dossiers
|
|
|
|
|
.includes(table == 'type_de_champ' ? :champs : :champs_private)
|
|
|
|
|
.where("champs.type_de_champ_id = #{sort['column'].to_i}")
|
|
|
|
|
.order("champs.value #{order}")
|
|
|
|
|
.pluck(:id)
|
2018-10-11 11:28:51 +02:00
|
|
|
|
when 'user', 'individual', 'etablissement'
|
2018-10-05 10:08:39 +02:00
|
|
|
|
return dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.order("#{column} #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 19:17:22 +02:00
|
|
|
|
def filtered_ids(dossiers, statut)
|
2018-10-05 15:48:37 +02:00
|
|
|
|
dossiers.each { |dossier| assert_matching_procedure(dossier) }
|
2018-10-03 19:17:22 +02:00
|
|
|
|
filters[statut].map do |filter|
|
|
|
|
|
table = filter['table']
|
2018-10-05 10:08:39 +02:00
|
|
|
|
column = sanitized_column(filter)
|
2018-10-03 19:17:22 +02:00
|
|
|
|
case table
|
|
|
|
|
when 'self'
|
2018-10-10 18:18:28 +02:00
|
|
|
|
date = filter['value'].to_date rescue nil
|
|
|
|
|
dossiers.where("DATE_TRUNC('day', #{column}) = ?", date)
|
2018-10-03 19:17:22 +02:00
|
|
|
|
when 'type_de_champ', 'type_de_champ_private'
|
|
|
|
|
relation = table == 'type_de_champ' ? :champs : :champs_private
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(relation)
|
|
|
|
|
.where("champs.type_de_champ_id = ?", filter['column'].to_i)
|
|
|
|
|
.where("champs.value ILIKE ?", "%#{filter['value']}%")
|
|
|
|
|
when 'etablissement'
|
|
|
|
|
if filter['column'] == 'entreprise_date_creation'
|
|
|
|
|
date = filter['value'].to_date rescue nil
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.where("#{column} = ?", date)
|
|
|
|
|
else
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.where("#{column} ILIKE ?", "%#{filter['value']}%")
|
|
|
|
|
end
|
2018-10-11 11:17:58 +02:00
|
|
|
|
when 'user', 'individual'
|
2018-10-03 19:17:22 +02:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.where("#{column} ILIKE ?", "%#{filter['value']}%")
|
|
|
|
|
end.pluck(:id)
|
|
|
|
|
end.reduce(:&)
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-02 17:04:23 +02:00
|
|
|
|
private
|
|
|
|
|
|
2018-10-10 06:04:29 +02:00
|
|
|
|
def check_allowed_displayed_fields
|
|
|
|
|
displayed_fields.each do |field|
|
|
|
|
|
table = field['table']
|
|
|
|
|
column = field['column']
|
|
|
|
|
if !valid_column?(table, column)
|
|
|
|
|
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_allowed_sort_column
|
|
|
|
|
table = sort['table']
|
|
|
|
|
column = sort['column']
|
|
|
|
|
if !valid_sort_column?(table, column)
|
|
|
|
|
errors.add(:sort, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 15:31:24 +02:00
|
|
|
|
def check_allowed_sort_order
|
|
|
|
|
order = sort['order']
|
|
|
|
|
if !["asc", "desc"].include?(order)
|
|
|
|
|
errors.add(:sort, "#{order} n’est pas une ordre permis")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-10 06:04:29 +02:00
|
|
|
|
def check_allowed_filter_columns
|
|
|
|
|
filters.each do |_, columns|
|
|
|
|
|
columns.each do |column|
|
|
|
|
|
table = column['table']
|
|
|
|
|
column = column['column']
|
|
|
|
|
if !valid_column?(table, column)
|
|
|
|
|
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 15:48:37 +02:00
|
|
|
|
def assert_matching_procedure(dossier)
|
|
|
|
|
if dossier.procedure != procedure
|
|
|
|
|
raise "Procedure mismatch (expected #{procedure.id}, got #{dossier.procedure.id})"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 15:25:24 +02:00
|
|
|
|
def get_value(dossier, table, column)
|
|
|
|
|
case table
|
|
|
|
|
when 'self'
|
|
|
|
|
dossier.send(column)
|
2018-10-11 10:46:32 +02:00
|
|
|
|
when 'user', 'individual', 'etablissement'
|
|
|
|
|
dossier.send(table)&.send(column)
|
2018-10-05 15:25:24 +02:00
|
|
|
|
when 'type_de_champ'
|
|
|
|
|
dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value
|
|
|
|
|
when 'type_de_champ_private'
|
|
|
|
|
dossier.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 14:45:58 +02:00
|
|
|
|
def field_hash(label, table, column)
|
|
|
|
|
{
|
|
|
|
|
'label' => label,
|
|
|
|
|
'table' => table,
|
|
|
|
|
'column' => column
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 14:57:49 +02:00
|
|
|
|
def valid_column?(table, column)
|
|
|
|
|
valid_columns_for_table(table).include?(column)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 14:57:49 +02:00
|
|
|
|
def valid_columns_for_table(table)
|
|
|
|
|
@column_whitelist ||= fields
|
|
|
|
|
.group_by { |field| field['table'] }
|
|
|
|
|
.map { |table, fields| [table, Set.new(fields.map { |field| field['column'] }) ] }
|
|
|
|
|
.to_h
|
2018-10-05 14:45:58 +02:00
|
|
|
|
|
2018-10-05 14:57:49 +02:00
|
|
|
|
@column_whitelist[table] || []
|
2018-10-05 14:45:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 10:08:39 +02:00
|
|
|
|
def sanitized_column(field)
|
|
|
|
|
table = field['table']
|
|
|
|
|
table = ActiveRecord::Base.connection.quote_column_name((table == 'self' ? 'dossier' : table).pluralize)
|
|
|
|
|
column = ActiveRecord::Base.connection.quote_column_name(field['column'])
|
|
|
|
|
|
|
|
|
|
table + '.' + column
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-04 16:41:09 +02:00
|
|
|
|
def dossier_field_service
|
|
|
|
|
@dossier_field_service ||= DossierFieldService.new
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 14:57:49 +02:00
|
|
|
|
def valid_sort_column?(table, column)
|
|
|
|
|
valid_column?(table, column) || EXTRA_SORT_COLUMNS[table]&.include?(column)
|
2018-10-02 17:04:23 +02:00
|
|
|
|
end
|
2017-10-02 17:03:30 +02:00
|
|
|
|
end
|