2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2018-03-06 13:44:29 +01:00
|
|
|
|
class ProcedurePresentation < ApplicationRecord
|
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'
|
|
|
|
|
|
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
|
|
|
|
|
2024-10-07 15:00:05 +02:00
|
|
|
|
attribute :displayed_columns, :column, array: true
|
|
|
|
|
|
2024-09-25 17:41:58 +02:00
|
|
|
|
attribute :sorted_column, :sorted_column
|
|
|
|
|
def sorted_column = super || procedure.default_sorted_column # Dummy override to set default value
|
2024-09-20 11:12:52 +02:00
|
|
|
|
|
2024-09-27 12:50:57 +02:00
|
|
|
|
attribute :a_suivre_filters, :filtered_column, array: true
|
|
|
|
|
attribute :suivis_filters, :filtered_column, array: true
|
|
|
|
|
attribute :traites_filters, :filtered_column, array: true
|
|
|
|
|
attribute :tous_filters, :filtered_column, array: true
|
|
|
|
|
attribute :supprimes_filters, :filtered_column, array: true
|
|
|
|
|
attribute :supprimes_recemment_filters, :filtered_column, array: true
|
|
|
|
|
attribute :expirant_filters, :filtered_column, array: true
|
|
|
|
|
attribute :archives_filters, :filtered_column, array: true
|
2024-09-20 11:12:52 +02:00
|
|
|
|
|
2024-10-07 09:54:17 +02:00
|
|
|
|
validates_associated :a_suivre_filters, :suivis_filters, :traites_filters,
|
|
|
|
|
:tous_filters, :supprimes_filters, :expirant_filters, :archives_filters
|
|
|
|
|
|
2024-10-07 18:10:08 +02:00
|
|
|
|
def filters_for(statut)
|
|
|
|
|
send(filters_name_for(statut))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def filters_name_for(statut) = statut.tr('-', '_').then { "#{_1}_filters" }
|
|
|
|
|
|
2022-04-14 10:30:23 +02:00
|
|
|
|
def displayed_fields_for_headers
|
2023-08-31 15:25:29 +02:00
|
|
|
|
[
|
2024-10-07 21:46:59 +02:00
|
|
|
|
Column.new(procedure_id: procedure.id, table: 'self', column: 'id', classname: 'number-col'),
|
2024-10-07 15:00:05 +02:00
|
|
|
|
*displayed_columns,
|
2024-10-07 21:46:59 +02:00
|
|
|
|
Column.new(procedure_id: procedure.id, table: 'self', column: 'state', classname: 'state-col'),
|
2024-08-19 14:34:36 +02:00
|
|
|
|
*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
|
|
|
|
|
2024-09-27 15:45:37 +02:00
|
|
|
|
if filters_for(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
|
|
|
|
|
|
2024-09-27 13:09:59 +02:00
|
|
|
|
def human_value_for_filter(filtered_column)
|
|
|
|
|
if filtered_column.column.table == TYPE_DE_CHAMP
|
|
|
|
|
find_type_de_champ(filtered_column.column.column).dynamic_type.filter_to_human(filtered_column.filter)
|
|
|
|
|
elsif filtered_column.column.column == 'state'
|
|
|
|
|
if filtered_column.filter == 'pending_correction'
|
2023-03-27 19:11:22 +02:00
|
|
|
|
Dossier.human_attribute_name("pending_correction.for_instructeur")
|
|
|
|
|
else
|
2024-09-27 13:09:59 +02:00
|
|
|
|
Dossier.human_attribute_name("state.#{filtered_column.filter}")
|
2023-03-27 19:11:22 +02:00
|
|
|
|
end
|
2024-09-27 13:09:59 +02:00
|
|
|
|
elsif filtered_column.column.table == 'groupe_instructeur' && filtered_column.column.column == 'id'
|
2023-01-13 16:40:48 +01:00
|
|
|
|
instructeur.groupe_instructeurs
|
2024-09-27 13:09:59 +02:00
|
|
|
|
.find { _1.id == filtered_column.filter.to_i }&.label || filtered_column.filter
|
2020-06-23 11:33:46 +02:00
|
|
|
|
else
|
2024-09-27 13:09:59 +02:00
|
|
|
|
column = procedure.columns.find { _1.table == filtered_column.column.table && _1.column == filtered_column.column.column }
|
2023-06-08 19:01:23 +02:00
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
|
if column.type == :date
|
2024-09-27 13:09:59 +02:00
|
|
|
|
parsed_date = safe_parse_date(filtered_column.filter)
|
2023-06-08 19:01:23 +02:00
|
|
|
|
|
|
|
|
|
return parsed_date.present? ? I18n.l(parsed_date) : nil
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-27 13:09:59 +02:00
|
|
|
|
filtered_column.filter
|
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
|
|
|
|
|
|
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)
|
2024-09-26 17:18:43 +02:00
|
|
|
|
table = sorted_column.column.table
|
|
|
|
|
column = sorted_column.column.column
|
|
|
|
|
order = sorted_column.order
|
2024-07-19 18:13:26 +02:00
|
|
|
|
|
|
|
|
|
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)
|
2024-09-27 15:45:37 +02:00
|
|
|
|
filters_for(statut)
|
|
|
|
|
.group_by { |filter| filter.column.then { [_1.table, _1.column] } }
|
|
|
|
|
.map do |(table, column), filters_for_column|
|
|
|
|
|
values = filters_for_column.map(&:filter)
|
|
|
|
|
filtered_column = filters_for_column.first.column
|
|
|
|
|
value_column = filtered_column.value_column
|
|
|
|
|
|
|
|
|
|
if filtered_column.is_a?(Columns::JSONPathColumn)
|
|
|
|
|
filtered_column.filtered_ids(dossiers, values)
|
2024-07-22 14:58:16 +02:00
|
|
|
|
else
|
|
|
|
|
case table
|
|
|
|
|
when 'self'
|
2024-09-27 15:45:37 +02:00
|
|
|
|
if filtered_column.type == :date
|
2024-07-22 14:58:16 +02:00
|
|
|
|
dates = values
|
|
|
|
|
.filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
|
|
|
|
|
|
|
|
|
|
dossiers.filter_by_datetimes(column, dates)
|
2024-09-27 15:45:37 +02:00
|
|
|
|
elsif filtered_column.column == "state" && values.include?("pending_correction")
|
2024-07-22 14:58:16 +02:00
|
|
|
|
dossiers.joins(:corrections).where(corrections: DossierCorrection.pending)
|
2024-09-27 15:45:37 +02:00
|
|
|
|
elsif filtered_column.column == "state" && values.include?("en_construction")
|
2024-07-22 14:58:16 +02:00
|
|
|
|
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
|
2024-09-27 15:45:37 +02:00
|
|
|
|
if filtered_column.type == :enum
|
2024-09-24 20:38:48 +02:00
|
|
|
|
dossiers.with_type_de_champ(column)
|
|
|
|
|
.filter_enum(:champs, value_column, values)
|
|
|
|
|
else
|
|
|
|
|
dossiers.with_type_de_champ(column)
|
|
|
|
|
.filter_ilike(:champs, value_column, values)
|
|
|
|
|
end
|
2024-07-22 14:58:16 +02:00
|
|
|
|
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')
|
2024-09-24 20:16:47 +02:00
|
|
|
|
.filter_ilike('instructeurs_users', :email, values) # ilike OK, user may want to search by *@domain
|
2024-09-24 20:22:39 +02:00
|
|
|
|
when 'user', 'individual' # user_columns: [email], individual_columns: ['nom', 'prenom', 'gender']
|
2024-07-19 18:13:26 +02:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
2024-09-24 20:16:47 +02:00
|
|
|
|
.filter_ilike(table, column, values) # ilike or where column == 'value' are both valid, we opted for ilike
|
2024-07-22 14:58:16 +02:00
|
|
|
|
when 'groupe_instructeur'
|
|
|
|
|
assert_supported_column(table, column)
|
2024-09-24 20:16:47 +02:00
|
|
|
|
|
|
|
|
|
dossiers
|
|
|
|
|
.joins(:groupe_instructeur)
|
|
|
|
|
.where(groupe_instructeur_id: values)
|
2024-07-22 14:58:16 +02:00
|
|
|
|
end.pluck(:id)
|
|
|
|
|
end
|
2024-07-19 18:13:26 +02:00
|
|
|
|
end.reduce(:&)
|
|
|
|
|
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
|
|
|
|
|
|
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
|