2020-08-06 16:35:45 +02:00
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: procedure_presentations
|
|
|
|
|
#
|
|
|
|
|
# id :integer not null, primary key
|
|
|
|
|
# displayed_fields :jsonb not null
|
|
|
|
|
# filters :jsonb not null
|
|
|
|
|
# sort :jsonb not null
|
|
|
|
|
# created_at :datetime
|
|
|
|
|
# updated_at :datetime
|
|
|
|
|
# assign_to_id :integer
|
|
|
|
|
#
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 16:59:27 +02:00
|
|
|
|
belongs_to :assign_to, optional: false
|
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'),
|
2019-04-09 14:32:29 +02:00
|
|
|
|
field_hash('Demandeur', 'user', 'email'),
|
2019-09-18 15:25:05 +02:00
|
|
|
|
field_hash('Email instructeur', 'followers_instructeurs', 'email'),
|
|
|
|
|
field_hash('Groupe instructeur', 'groupe_instructeur', 'label')
|
2018-10-05 14:45:58 +02:00
|
|
|
|
]
|
|
|
|
|
|
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
|
|
|
|
|
|
2019-07-10 18:27:26 +02:00
|
|
|
|
if !procedure.for_individual
|
2018-10-05 14:45:58 +02:00
|
|
|
|
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
|
|
|
|
|
|
2018-12-24 17:28:20 +01:00
|
|
|
|
explanatory_types_de_champ = [:header_section, :explication].map { |k| TypeDeChamp.type_champs.fetch(k) }
|
2018-10-05 14:45:58 +02:00
|
|
|
|
|
|
|
|
|
fields.concat procedure.types_de_champ
|
2019-02-26 11:39:32 +01:00
|
|
|
|
.where.not(type_champ: explanatory_types_de_champ)
|
2019-03-08 18:35:25 +01:00
|
|
|
|
.order(:id)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
.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
|
2019-02-26 11:39:32 +01:00
|
|
|
|
.where.not(type_champ: explanatory_types_de_champ)
|
2019-03-08 18:35:25 +01:00
|
|
|
|
.order(:id)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
.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
|
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
|
def sorted_ids(dossiers, instructeur)
|
2019-02-26 16:08:35 +01:00
|
|
|
|
table, column, order = sort.values_at('table', 'column', 'order')
|
2018-10-05 10:08:39 +02:00
|
|
|
|
|
|
|
|
|
case table
|
|
|
|
|
when 'notifications'
|
2020-09-02 17:44:51 +02:00
|
|
|
|
dossiers_id_with_notification = dossiers.merge(instructeur.followed_dossiers).with_notifications(instructeur).ids
|
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 'type_de_champ', 'type_de_champ_private'
|
|
|
|
|
return dossiers
|
|
|
|
|
.includes(table == 'type_de_champ' ? :champs : :champs_private)
|
2019-02-26 16:08:35 +01:00
|
|
|
|
.where("champs.type_de_champ_id = #{column.to_i}")
|
2018-10-05 10:08:39 +02:00
|
|
|
|
.order("champs.value #{order}")
|
|
|
|
|
.pluck(:id)
|
2019-10-16 13:02:42 +02:00
|
|
|
|
when 'followers_instructeurs'
|
|
|
|
|
assert_supported_column(table, column)
|
|
|
|
|
# LEFT OUTER JOIN allows to keep dossiers without assignated instructeurs yet
|
|
|
|
|
return dossiers
|
|
|
|
|
.includes(:followers_instructeurs)
|
|
|
|
|
.joins('LEFT OUTER JOIN users instructeurs_users ON instructeurs_users.instructeur_id = instructeurs.id')
|
|
|
|
|
.order("instructeurs_users.email #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
when 'self', 'user', 'individual', 'etablissement', 'groupe_instructeur'
|
2019-02-26 15:57:42 +01:00
|
|
|
|
return (table == 'self' ? dossiers : dossiers.includes(table))
|
2019-02-26 19:17:33 +01:00
|
|
|
|
.order("#{self.class.sanitized_column(table, column)} #{order}")
|
2018-10-05 10:08:39 +02:00
|
|
|
|
.pluck(:id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 19:17:22 +02:00
|
|
|
|
def filtered_ids(dossiers, statut)
|
2019-03-04 18:41:14 +01:00
|
|
|
|
filters[statut].group_by { |filter| filter.values_at('table', 'column') } .map do |(table, column), filters|
|
2019-02-19 17:06:06 +01:00
|
|
|
|
values = filters.pluck('value')
|
2018-10-03 19:17:22 +02:00
|
|
|
|
case table
|
|
|
|
|
when 'self'
|
2019-02-26 16:27:43 +01:00
|
|
|
|
dates = values
|
|
|
|
|
.map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
|
|
|
|
|
.compact
|
2019-02-27 12:03:53 +01:00
|
|
|
|
dossiers.filter_by_datetimes(column, dates)
|
2018-10-03 19:17:22 +02:00
|
|
|
|
when 'type_de_champ', 'type_de_champ_private'
|
|
|
|
|
relation = table == 'type_de_champ' ? :champs : :champs_private
|
2019-02-27 12:12:34 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(relation)
|
|
|
|
|
.where("champs.type_de_champ_id = ?", column.to_i)
|
2019-04-09 14:32:29 +02:00
|
|
|
|
.filter_ilike(relation, :value, values)
|
2018-10-03 19:17:22 +02:00
|
|
|
|
when 'etablissement'
|
2019-02-26 16:13:22 +01:00
|
|
|
|
if column == 'entreprise_date_creation'
|
2019-02-27 12:35:08 +01:00
|
|
|
|
dates = values
|
|
|
|
|
.map { |v| v.to_date rescue nil }
|
|
|
|
|
.compact
|
2019-02-26 17:11:47 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.where(table.pluralize => { column => dates })
|
2018-10-03 19:17:22 +02:00
|
|
|
|
else
|
2019-02-14 15:03:56 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
2019-02-27 12:12:34 +01:00
|
|
|
|
.filter_ilike(table, column, values)
|
|
|
|
|
end
|
2019-10-16 13:02:42 +02:00
|
|
|
|
when 'followers_instructeurs'
|
|
|
|
|
assert_supported_column(table, column)
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(:followers_instructeurs)
|
|
|
|
|
.joins('INNER JOIN users instructeurs_users ON instructeurs_users.instructeur_id = instructeurs.id')
|
|
|
|
|
.filter_ilike('instructeurs_users', :email, values)
|
|
|
|
|
when 'user', 'individual'
|
2019-02-27 12:12:34 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.filter_ilike(table, column, values)
|
2019-09-18 15:25:05 +02:00
|
|
|
|
when 'groupe_instructeur'
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.filter_ilike(table, column, values)
|
2018-10-03 19:17:22 +02:00
|
|
|
|
end.pluck(:id)
|
|
|
|
|
end.reduce(:&)
|
|
|
|
|
end
|
|
|
|
|
|
2019-02-19 16:57:45 +01:00
|
|
|
|
def eager_load_displayed_fields(dossiers)
|
2019-02-27 16:28:31 +01:00
|
|
|
|
relations_to_include = displayed_fields
|
2019-02-27 16:13:22 +01:00
|
|
|
|
.pluck('table')
|
2019-02-27 16:30:19 +01:00
|
|
|
|
.reject { |table| table == 'self' }
|
2019-02-27 16:13:22 +01:00
|
|
|
|
.map do |table|
|
|
|
|
|
case table
|
|
|
|
|
when 'type_de_champ'
|
|
|
|
|
:champs
|
|
|
|
|
when 'type_de_champ_private'
|
|
|
|
|
:champs_private
|
2019-02-19 16:57:45 +01:00
|
|
|
|
else
|
2019-02-27 16:13:22 +01:00
|
|
|
|
table
|
2019-02-19 16:57:45 +01:00
|
|
|
|
end
|
2019-02-27 16:13:22 +01:00
|
|
|
|
end
|
|
|
|
|
.uniq
|
2019-02-19 16:57:45 +01:00
|
|
|
|
|
2019-02-27 16:27:16 +01:00
|
|
|
|
dossiers.includes(relations_to_include)
|
2019-02-19 16:57:45 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-06-08 14:33:58 +02:00
|
|
|
|
def human_value_for_filter(filter)
|
2020-06-23 11:33:46 +02:00
|
|
|
|
case filter['table']
|
|
|
|
|
when 'type_de_champ', 'type_de_champ_private'
|
2020-06-08 14:33:58 +02:00
|
|
|
|
type_de_champ = TypeDeChamp.find_by(id: filter['column'])
|
2020-06-23 11:33:46 +02:00
|
|
|
|
type_de_champ.dynamic_type.filter_to_human(filter['value'])
|
|
|
|
|
else
|
|
|
|
|
filter['value']
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def add_filter(statut, field, value)
|
|
|
|
|
if value.present?
|
|
|
|
|
updated_filters = self.filters
|
|
|
|
|
table, column = field.split('/')
|
|
|
|
|
label = find_field(table, column)['label']
|
|
|
|
|
|
2020-06-23 11:33:46 +02:00
|
|
|
|
case table
|
|
|
|
|
when 'type_de_champ', 'type_de_champ_private'
|
2020-06-08 14:33:58 +02:00
|
|
|
|
type_de_champ = TypeDeChamp.find_by(id: column)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
value = type_de_champ.dynamic_type.human_to_filter(value)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
updated_filters[statut] << {
|
|
|
|
|
'label' => label,
|
|
|
|
|
'table' => table,
|
|
|
|
|
'column' => column,
|
|
|
|
|
'value' => value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update(filters: updated_filters)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-02 17:04:23 +02:00
|
|
|
|
private
|
|
|
|
|
|
2020-06-08 14:33:58 +02:00
|
|
|
|
def find_field(table, column)
|
|
|
|
|
fields.find { |c| c['table'] == table && c['column'] == column }
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-10 06:04:29 +02:00
|
|
|
|
def check_allowed_displayed_fields
|
|
|
|
|
displayed_fields.each do |field|
|
2019-02-27 16:48:56 +01:00
|
|
|
|
check_allowed_field(:displayed_fields, field)
|
2018-10-10 06:04:29 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_allowed_sort_column
|
2019-02-27 16:48:56 +01:00
|
|
|
|
check_allowed_field(:sort, sort, EXTRA_SORT_COLUMNS)
|
2018-10-10 06:04:29 +02:00
|
|
|
|
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|
|
2019-02-27 16:48:56 +01:00
|
|
|
|
check_allowed_field(:filters, column)
|
2018-10-10 06:04:29 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-02-27 16:48:56 +01:00
|
|
|
|
def check_allowed_field(kind, field, extra_columns = {})
|
2019-02-27 17:00:41 +01:00
|
|
|
|
table, column = field.values_at('table', 'column')
|
2019-02-27 16:48:56 +01:00
|
|
|
|
if !valid_column?(table, column, extra_columns)
|
|
|
|
|
errors.add(kind, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
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'
|
2018-10-26 11:21:01 +02:00
|
|
|
|
dossier.send(column)&.strftime('%d/%m/%Y')
|
2018-10-11 10:46:32 +02:00
|
|
|
|
when 'user', 'individual', 'etablissement'
|
|
|
|
|
dossier.send(table)&.send(column)
|
2019-08-06 11:02:54 +02:00
|
|
|
|
when 'followers_instructeurs'
|
2019-04-09 14:32:29 +02:00
|
|
|
|
dossier.send(table)&.map { |g| g.send(column) }&.join(', ')
|
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
|
2019-09-18 15:25:05 +02:00
|
|
|
|
when 'groupe_instructeur'
|
|
|
|
|
dossier.groupe_instructeur.label
|
2018-10-05 15:25:24 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 14:45:58 +02:00
|
|
|
|
def field_hash(label, table, column)
|
|
|
|
|
{
|
|
|
|
|
'label' => label,
|
|
|
|
|
'table' => table,
|
|
|
|
|
'column' => column
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2019-03-04 17:21:48 +01:00
|
|
|
|
def valid_column?(table, column, extra_columns = {})
|
|
|
|
|
valid_columns_for_table(table).include?(column) ||
|
|
|
|
|
extra_columns[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'] }
|
2019-02-19 17:06:06 +01:00
|
|
|
|
.map { |table, fields| [table, Set.new(fields.pluck('column'))] }
|
2018-10-05 14:57:49 +02:00
|
|
|
|
.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
|
|
|
|
|
|
2019-04-09 14:32:29 +02:00
|
|
|
|
def self.sanitized_column(association, column)
|
|
|
|
|
table = if association == 'self'
|
|
|
|
|
Dossier.table_name
|
2019-10-16 13:02:42 +02:00
|
|
|
|
elsif (association_reflection = Dossier.reflect_on_association(association))
|
|
|
|
|
association_reflection.klass.table_name
|
2019-04-09 14:32:29 +02:00
|
|
|
|
else
|
2019-10-16 13:02:42 +02:00
|
|
|
|
# Allow filtering on a joined table alias (which doesn’t exist
|
|
|
|
|
# in the ActiveRecord domain).
|
|
|
|
|
association
|
2019-04-09 14:32:29 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
[table, column]
|
2019-02-26 19:12:05 +01:00
|
|
|
|
.map { |name| ActiveRecord::Base.connection.quote_column_name(name) }
|
|
|
|
|
.join('.')
|
2018-10-05 10:08:39 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-04 16:41:09 +02:00
|
|
|
|
def dossier_field_service
|
|
|
|
|
@dossier_field_service ||= DossierFieldService.new
|
|
|
|
|
end
|
2019-10-16 13:02:42 +02:00
|
|
|
|
|
|
|
|
|
def assert_supported_column(table, column)
|
|
|
|
|
if table == 'followers_instructeurs' && column != 'email'
|
|
|
|
|
raise ArgumentError, 'Table `followers_instructeurs` only supports the `email` column.'
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-10-02 17:03:30 +02:00
|
|
|
|
end
|