2018-03-06 13:44:29 +01:00
|
|
|
|
class ProcedurePresentation < ApplicationRecord
|
2018-10-03 16:20:27 +02:00
|
|
|
|
EXTRA_SORT_COLUMNS = {
|
|
|
|
|
'notifications' => Set['notifications'],
|
|
|
|
|
'self' => Set['id', 'state']
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
validate :check_allowed_filter_columns
|
|
|
|
|
|
|
|
|
|
def check_allowed_displayed_fields
|
|
|
|
|
displayed_fields.each do |field|
|
|
|
|
|
table = field['table']
|
|
|
|
|
column = field['column']
|
2018-10-04 16:41:09 +02:00
|
|
|
|
if !dossier_field_service.valid_column?(procedure, table, column)
|
2018-10-02 17:04:23 +02:00
|
|
|
|
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?(procedure, table, column)
|
|
|
|
|
errors.add(:sort, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_allowed_filter_columns
|
|
|
|
|
filters.each do |_, columns|
|
|
|
|
|
columns.each do |column|
|
|
|
|
|
table = column['table']
|
|
|
|
|
column = column['column']
|
2018-10-04 16:41:09 +02:00
|
|
|
|
if !dossier_field_service.valid_column?(procedure, table, column)
|
2018-10-02 17:04:23 +02:00
|
|
|
|
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 16:05:27 +02:00
|
|
|
|
def fields
|
|
|
|
|
dossier_field_service.fields(procedure)
|
|
|
|
|
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 10:08:39 +02:00
|
|
|
|
def sorted_ids(dossiers, gestionnaire)
|
|
|
|
|
table = sort['table']
|
|
|
|
|
column = sanitized_column(sort)
|
|
|
|
|
order = sort['order']
|
|
|
|
|
assert_valid_order(order)
|
|
|
|
|
|
|
|
|
|
case table
|
|
|
|
|
when 'notifications'
|
|
|
|
|
dossiers_id_with_notification = gestionnaire.notifications_for_procedure(procedure)
|
|
|
|
|
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)
|
|
|
|
|
else
|
|
|
|
|
return dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.order("#{column} #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 19:17:22 +02:00
|
|
|
|
def filtered_ids(dossiers, statut)
|
|
|
|
|
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'
|
|
|
|
|
dossiers.where("? ILIKE ?", filter['column'], "%#{filter['value']}%")
|
|
|
|
|
|
|
|
|
|
when 'france_connect_information'
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(user: :france_connect_information)
|
|
|
|
|
.where("? ILIKE ?", "france_connect_informations.#{filter['column']}", "%#{filter['value']}%")
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
when 'user'
|
|
|
|
|
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-05 10:08:39 +02:00
|
|
|
|
def assert_valid_order(order)
|
|
|
|
|
if !["asc", "desc"].include?(order)
|
|
|
|
|
raise "Invalid order #{order}"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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-02 17:04:23 +02:00
|
|
|
|
def valid_sort_column?(procedure, table, column)
|
2018-10-04 16:41:09 +02:00
|
|
|
|
dossier_field_service.valid_column?(procedure, 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
|