2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 12:52:00 +02:00
|
|
|
|
TABLE = 'table'
|
|
|
|
|
COLUMN = 'column'
|
2022-09-26 17:14:20 +02:00
|
|
|
|
ORDER = 'order'
|
|
|
|
|
|
2021-04-16 12:52:00 +02:00
|
|
|
|
SLASH = '/'
|
|
|
|
|
TYPE_DE_CHAMP = 'type_de_champ'
|
|
|
|
|
|
2021-07-20 14:00:29 +02:00
|
|
|
|
FILTERS_VALUE_MAX_LENGTH = 100
|
|
|
|
|
|
2020-07-20 16:59:27 +02:00
|
|
|
|
belongs_to :assign_to, optional: false
|
2022-04-05 15:53:15 +02:00
|
|
|
|
has_many :exports, dependent: :destroy
|
|
|
|
|
|
2022-03-31 16:32:45 +02:00
|
|
|
|
delegate :procedure, :instructeur, to: :assign_to
|
2018-10-02 17:04:23 +02:00
|
|
|
|
|
|
|
|
|
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
|
2021-07-20 14:00:29 +02:00
|
|
|
|
validate :check_filters_max_length
|
2018-10-02 17:04:23 +02:00
|
|
|
|
|
2022-04-14 10:30:23 +02:00
|
|
|
|
def displayed_fields_for_headers
|
2023-08-31 15:25:29 +02:00
|
|
|
|
[
|
2024-08-19 14:34:36 +02:00
|
|
|
|
Column.new(table: 'self', column: 'id', classname: 'number-col'),
|
2024-09-09 16:23:32 +02:00
|
|
|
|
*displayed_fields.map { Column.new(**_1.deep_symbolize_keys.except(:virtual)) }, # TODO: remove virtual after migration
|
2024-08-19 14:34:36 +02:00
|
|
|
|
Column.new(table: 'self', column: 'state', classname: 'state-col'),
|
|
|
|
|
*procedure.sva_svr_columns
|
2022-04-14 10:30:23 +02:00
|
|
|
|
]
|
2023-08-31 15:25:29 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-06 09:12:15 +02:00
|
|
|
|
def filtered_sorted_ids(dossiers, statut, count: nil)
|
2023-05-09 13:39:46 +02:00
|
|
|
|
dossiers_by_statut = dossiers.by_statut(statut, instructeur)
|
2022-04-06 09:12:15 +02:00
|
|
|
|
dossiers_sorted_ids = self.sorted_ids(dossiers_by_statut, count || dossiers_by_statut.size)
|
2022-03-31 18:28:12 +02:00
|
|
|
|
|
|
|
|
|
if filters[statut].present?
|
2022-04-15 12:13:32 +02:00
|
|
|
|
dossiers_sorted_ids.intersection(filtered_ids(dossiers_by_statut, statut))
|
2022-03-31 18:28:12 +02:00
|
|
|
|
else
|
|
|
|
|
dossiers_sorted_ids
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-06-08 14:33:58 +02:00
|
|
|
|
def human_value_for_filter(filter)
|
2024-07-19 16:53:22 +02:00
|
|
|
|
if filter[TABLE] == TYPE_DE_CHAMP
|
2021-04-16 12:52:00 +02:00
|
|
|
|
find_type_de_champ(filter[COLUMN]).dynamic_type.filter_to_human(filter['value'])
|
2022-12-14 23:13:54 +01:00
|
|
|
|
elsif filter['column'] == 'state'
|
2023-03-27 19:11:22 +02:00
|
|
|
|
if filter['value'] == 'pending_correction'
|
|
|
|
|
Dossier.human_attribute_name("pending_correction.for_instructeur")
|
|
|
|
|
else
|
|
|
|
|
Dossier.human_attribute_name("state.#{filter['value']}")
|
|
|
|
|
end
|
2023-01-13 16:40:48 +01:00
|
|
|
|
elsif filter['table'] == 'groupe_instructeur' && filter['column'] == 'id'
|
|
|
|
|
instructeur.groupe_instructeurs
|
2023-04-25 14:15:40 +02:00
|
|
|
|
.find { _1.id == filter['value'].to_i }&.label || filter['value']
|
2020-06-23 11:33:46 +02:00
|
|
|
|
else
|
2024-08-19 14:34:36 +02:00
|
|
|
|
column = procedure.columns.find { _1.table == filter[TABLE] && _1.column == filter[COLUMN] }
|
2023-06-08 19:01:23 +02:00
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
if column.type == :date
|
2023-06-08 19:01:23 +02:00
|
|
|
|
parsed_date = safe_parse_date(filter['value'])
|
|
|
|
|
|
|
|
|
|
return parsed_date.present? ? I18n.l(parsed_date) : nil
|
|
|
|
|
end
|
|
|
|
|
|
2020-06-23 11:33:46 +02:00
|
|
|
|
filter['value']
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-08 19:01:23 +02:00
|
|
|
|
def safe_parse_date(string)
|
|
|
|
|
Date.parse(string)
|
|
|
|
|
rescue Date::Error
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
def add_filter(statut, column_id, value)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
if value.present?
|
2024-08-19 14:34:36 +02:00
|
|
|
|
column = procedure.find_column(id: column_id)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
case column.table
|
2024-07-19 16:53:22 +02:00
|
|
|
|
when TYPE_DE_CHAMP
|
2024-08-19 14:34:36 +02:00
|
|
|
|
value = find_type_de_champ(column.column).dynamic_type.human_to_filter(value)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2020-10-30 14:32:53 +01:00
|
|
|
|
updated_filters = filters.dup
|
2020-06-08 14:33:58 +02:00
|
|
|
|
updated_filters[statut] << {
|
2024-08-19 14:34:36 +02:00
|
|
|
|
'label' => column.label,
|
|
|
|
|
TABLE => column.table,
|
|
|
|
|
COLUMN => column.column,
|
|
|
|
|
'value_column' => column.value_column,
|
2020-06-08 14:33:58 +02:00
|
|
|
|
'value' => value
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 12:13:47 +01:00
|
|
|
|
update(filters: updated_filters)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
def remove_filter(statut, column_id, value)
|
|
|
|
|
column = procedure.find_column(id: column_id)
|
2020-10-30 14:34:00 +01:00
|
|
|
|
updated_filters = filters.dup
|
2024-08-19 14:34:36 +02:00
|
|
|
|
|
2020-10-30 14:34:00 +01:00
|
|
|
|
updated_filters[statut] = filters[statut].reject do |filter|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
filter.values_at(TABLE, COLUMN, 'value') == [column.table, column.column, value]
|
2020-10-30 14:34:00 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
update!(filters: updated_filters)
|
|
|
|
|
end
|
2020-10-30 13:33:42 +01:00
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
def update_displayed_fields(column_ids)
|
|
|
|
|
column_ids = Array.wrap(column_ids)
|
|
|
|
|
columns = column_ids.map { |id| procedure.find_column(id:) }
|
2020-10-30 13:33:42 +01:00
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
update!(displayed_fields: columns)
|
2020-10-30 13:33:42 +01:00
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
if !sort_to_column_id(sort).in?(column_ids)
|
2020-10-30 13:33:42 +01:00
|
|
|
|
update!(sort: Procedure.default_sort)
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-10-30 14:31:24 +01:00
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
def update_sort(column_id, order)
|
|
|
|
|
column = procedure.find_column(id: column_id)
|
2024-07-22 11:32:07 +02:00
|
|
|
|
|
2020-10-30 14:31:24 +01:00
|
|
|
|
update!(sort: {
|
2024-08-19 14:34:36 +02:00
|
|
|
|
TABLE => column.table,
|
|
|
|
|
COLUMN => column.column,
|
|
|
|
|
ORDER => order.presence || opposite_order_for(column.table, column.column)
|
2020-10-30 14:31:24 +01:00
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-26 17:14:20 +02:00
|
|
|
|
def opposite_order_for(table, column)
|
|
|
|
|
if sort.values_at(TABLE, COLUMN) == [table, column]
|
|
|
|
|
sort['order'] == 'asc' ? 'desc' : 'asc'
|
2022-12-20 11:46:17 +01:00
|
|
|
|
elsif [table, column] == ["notifications", "notifications"]
|
|
|
|
|
'desc' # default order for notifications
|
2022-09-26 17:14:20 +02:00
|
|
|
|
else
|
|
|
|
|
'asc'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-05 18:11:12 +02:00
|
|
|
|
def snapshot
|
|
|
|
|
slice(:filters, :sort, :displayed_fields)
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-02 17:04:23 +02:00
|
|
|
|
private
|
|
|
|
|
|
2024-07-19 18:13:26 +02:00
|
|
|
|
def sorted_ids(dossiers, count)
|
|
|
|
|
table, column, order = sort.values_at(TABLE, COLUMN, 'order')
|
|
|
|
|
|
|
|
|
|
case table
|
|
|
|
|
when 'notifications'
|
|
|
|
|
dossiers_id_with_notification = dossiers.merge(instructeur.followed_dossiers).with_notifications.ids
|
|
|
|
|
if order == 'desc'
|
|
|
|
|
dossiers_id_with_notification +
|
|
|
|
|
(dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
|
|
|
|
|
else
|
|
|
|
|
(dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) +
|
|
|
|
|
dossiers_id_with_notification
|
|
|
|
|
end
|
|
|
|
|
when TYPE_DE_CHAMP
|
|
|
|
|
ids = dossiers
|
|
|
|
|
.with_type_de_champ(column)
|
|
|
|
|
.order("champs.value #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
if ids.size != count
|
|
|
|
|
rest = dossiers.where.not(id: ids).order(id: order).pluck(:id)
|
|
|
|
|
order == 'asc' ? ids + rest : rest + ids
|
|
|
|
|
else
|
|
|
|
|
ids
|
|
|
|
|
end
|
|
|
|
|
when 'followers_instructeurs'
|
|
|
|
|
assert_supported_column(table, column)
|
|
|
|
|
# LEFT OUTER JOIN allows to keep dossiers without assigned instructeurs yet
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(:followers_instructeurs)
|
|
|
|
|
.joins('LEFT OUTER JOIN users instructeurs_users ON instructeurs_users.id = instructeurs.user_id')
|
|
|
|
|
.order("instructeurs_users.email #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
.uniq
|
|
|
|
|
when 'avis'
|
|
|
|
|
dossiers.includes(table)
|
|
|
|
|
.order("#{self.class.sanitized_column(table, column)} #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
.uniq
|
|
|
|
|
when 'self', 'user', 'individual', 'etablissement', 'groupe_instructeur'
|
|
|
|
|
(table == 'self' ? dossiers : dossiers.includes(table))
|
|
|
|
|
.order("#{self.class.sanitized_column(table, column)} #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def filtered_ids(dossiers, statut)
|
|
|
|
|
filters.fetch(statut)
|
|
|
|
|
.group_by { |filter| filter.values_at(TABLE, COLUMN) }
|
|
|
|
|
.map do |(table, column), filters|
|
|
|
|
|
values = filters.pluck('value')
|
|
|
|
|
value_column = filters.pluck('value_column').compact.first || :value
|
2024-07-22 14:58:16 +02:00
|
|
|
|
dossier_column = procedure.find_column(id: Column.make_id(table, column)) # hack to find json path columns
|
|
|
|
|
if dossier_column.is_a?(Columns::JSONPathColumn)
|
|
|
|
|
dossier_column.filtered_ids(dossiers, values)
|
|
|
|
|
else
|
|
|
|
|
case table
|
|
|
|
|
when 'self'
|
|
|
|
|
if dossier_column.type == :date
|
|
|
|
|
dates = values
|
|
|
|
|
.filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
|
|
|
|
|
|
|
|
|
|
dossiers.filter_by_datetimes(column, dates)
|
|
|
|
|
elsif dossier_column.column == "state" && values.include?("pending_correction")
|
|
|
|
|
dossiers.joins(:corrections).where(corrections: DossierCorrection.pending)
|
|
|
|
|
elsif dossier_column.column == "state" && values.include?("en_construction")
|
|
|
|
|
dossiers.where("dossiers.#{column} IN (?)", values).includes(:corrections).where.not(corrections: DossierCorrection.pending)
|
|
|
|
|
else
|
|
|
|
|
dossiers.where("dossiers.#{column} IN (?)", values)
|
|
|
|
|
end
|
|
|
|
|
when TYPE_DE_CHAMP
|
|
|
|
|
dossiers.with_type_de_champ(column)
|
|
|
|
|
.filter_ilike(:champs, value_column, values)
|
|
|
|
|
when 'etablissement'
|
|
|
|
|
if column == 'entreprise_date_creation'
|
|
|
|
|
dates = values
|
|
|
|
|
.filter_map { |v| v.to_date rescue nil }
|
|
|
|
|
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.where(table.pluralize => { column => dates })
|
|
|
|
|
else
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.filter_ilike(table, column, values)
|
|
|
|
|
end
|
|
|
|
|
when 'followers_instructeurs'
|
|
|
|
|
assert_supported_column(table, column)
|
2024-07-19 18:13:26 +02:00
|
|
|
|
dossiers
|
2024-07-22 14:58:16 +02:00
|
|
|
|
.includes(:followers_instructeurs)
|
|
|
|
|
.joins('INNER JOIN users instructeurs_users ON instructeurs_users.id = instructeurs.user_id')
|
|
|
|
|
.filter_ilike('instructeurs_users', :email, values)
|
|
|
|
|
when 'user', 'individual', 'avis'
|
2024-07-19 18:13:26 +02:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.filter_ilike(table, column, values)
|
2024-07-22 14:58:16 +02:00
|
|
|
|
when 'groupe_instructeur'
|
|
|
|
|
assert_supported_column(table, column)
|
|
|
|
|
if column == 'label'
|
|
|
|
|
dossiers
|
|
|
|
|
.joins(:groupe_instructeur)
|
|
|
|
|
.filter_ilike(table, column, values)
|
|
|
|
|
else
|
|
|
|
|
dossiers
|
|
|
|
|
.joins(:groupe_instructeur)
|
|
|
|
|
.where(groupe_instructeur_id: values)
|
|
|
|
|
end
|
|
|
|
|
end.pluck(:id)
|
|
|
|
|
end
|
2024-07-19 18:13:26 +02:00
|
|
|
|
end.reduce(:&)
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-19 11:16:40 +02:00
|
|
|
|
# type_de_champ/4373429
|
2024-08-19 14:34:36 +02:00
|
|
|
|
def sort_to_column_id(sort)
|
2024-07-19 15:06:52 +02:00
|
|
|
|
[sort[TABLE], sort[COLUMN]].join(SLASH)
|
2020-10-30 11:38:58 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-10-30 15:07:24 +01:00
|
|
|
|
def find_type_de_champ(column)
|
2022-07-28 11:28:17 +02:00
|
|
|
|
TypeDeChamp
|
|
|
|
|
.joins(:revision_types_de_champ)
|
|
|
|
|
.where(revision_types_de_champ: { revision_id: procedure.revisions })
|
2023-11-14 09:32:25 +01:00
|
|
|
|
.order(created_at: :desc)
|
2022-07-28 11:28:17 +02:00
|
|
|
|
.find_by(stable_id: column)
|
2020-10-30 15:07:24 +01:00
|
|
|
|
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
|
2020-10-01 18:46:39 +02:00
|
|
|
|
filters.each do |key, columns|
|
|
|
|
|
return true if key == 'migrated'
|
2018-10-10 06:04:29 +02:00
|
|
|
|
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 = {})
|
2021-04-16 12:52:00 +02: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
|
|
|
|
|
|
2021-07-20 14:00:29 +02:00
|
|
|
|
def check_filters_max_length
|
2024-02-21 12:13:47 +01:00
|
|
|
|
filters.values.flatten.each do |filter|
|
|
|
|
|
next if !filter.is_a?(Hash)
|
|
|
|
|
next if filter['value']&.length.to_i <= FILTERS_VALUE_MAX_LENGTH
|
|
|
|
|
|
|
|
|
|
errors.add(:base, "Le filtre #{filter['label']} est trop long (maximum: #{FILTERS_VALUE_MAX_LENGTH} caractères)")
|
2021-07-20 14:00:29 +02:00
|
|
|
|
end
|
|
|
|
|
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)
|
2024-08-19 14:34:36 +02:00
|
|
|
|
@column_whitelist ||= procedure.columns
|
2024-07-19 11:16:40 +02:00
|
|
|
|
.group_by(&:table)
|
2024-08-19 14:34:36 +02:00
|
|
|
|
.transform_values { |columns| Set.new(columns.map(&:column)) }
|
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
|
|
|
|
|
|
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
|
2023-01-13 16:40:48 +01:00
|
|
|
|
if table == 'groupe_instructeur' && (column != 'label' && column != 'id')
|
|
|
|
|
raise ArgumentError, 'Table `groupe_instructeur` only supports the `label` or `id` column.'
|
|
|
|
|
end
|
2019-10-16 13:02:42 +02:00
|
|
|
|
end
|
2017-10-02 17:03:30 +02:00
|
|
|
|
end
|