Merge pull request #2770 from betagouv/frederic/fix_2750-move_dossier_field_service_to_procedure_presentation
Move dossier field service to procedure presentation
This commit is contained in:
commit
4bf209a838
9 changed files with 534 additions and 499 deletions
|
@ -28,7 +28,9 @@ module NewGestionnaire
|
||||||
|
|
||||||
@current_filters = current_filters
|
@current_filters = current_filters
|
||||||
@available_fields_to_filters = available_fields_to_filters
|
@available_fields_to_filters = available_fields_to_filters
|
||||||
@displayed_fields = procedure_presentation.displayed_fields
|
# Technically, procedure_presentation already sets the attribute.
|
||||||
|
# Setting it here to make clear that it is used by the view
|
||||||
|
@procedure_presentation = procedure_presentation
|
||||||
@displayed_fields_values = displayed_fields_values
|
@displayed_fields_values = displayed_fields_values
|
||||||
|
|
||||||
@a_suivre_dossiers = procedure
|
@a_suivre_dossiers = procedure
|
||||||
|
@ -67,11 +69,10 @@ module NewGestionnaire
|
||||||
@archived_dossiers
|
@archived_dossiers
|
||||||
end
|
end
|
||||||
|
|
||||||
dossier_field_service = DossierFieldService.new
|
sorted_ids = procedure_presentation.sorted_ids(@dossiers, current_gestionnaire)
|
||||||
sorted_ids = dossier_field_service.sorted_ids(@dossiers, procedure_presentation, current_gestionnaire)
|
|
||||||
|
|
||||||
if @current_filters.count > 0
|
if @current_filters.count > 0
|
||||||
filtered_ids = dossier_field_service.filtered_ids(@dossiers, current_filters)
|
filtered_ids = procedure_presentation.filtered_ids(@dossiers, statut)
|
||||||
filtered_sorted_ids = sorted_ids.select { |id| filtered_ids.include?(id) }
|
filtered_sorted_ids = sorted_ids.select { |id| filtered_ids.include?(id) }
|
||||||
else
|
else
|
||||||
filtered_sorted_ids = sorted_ids
|
filtered_sorted_ids = sorted_ids
|
||||||
|
@ -103,7 +104,7 @@ module NewGestionnaire
|
||||||
fields = values.map do |value|
|
fields = values.map do |value|
|
||||||
table, column = value.split("/")
|
table, column = value.split("/")
|
||||||
|
|
||||||
procedure.fields.find do |field|
|
procedure_presentation.fields.find do |field|
|
||||||
field['table'] == table && field['column'] == column
|
field['table'] == table && field['column'] == column
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -144,7 +145,7 @@ module NewGestionnaire
|
||||||
if params[:value].present?
|
if params[:value].present?
|
||||||
filters = procedure_presentation.filters
|
filters = procedure_presentation.filters
|
||||||
table, column = params[:field].split('/')
|
table, column = params[:field].split('/')
|
||||||
label = procedure.fields.find { |c| c['table'] == table && c['column'] == column }['label']
|
label = procedure_presentation.fields.find { |c| c['table'] == table && c['column'] == column }['label']
|
||||||
|
|
||||||
filters[statut] << {
|
filters[statut] << {
|
||||||
'label' => label,
|
'label' => label,
|
||||||
|
@ -225,13 +226,13 @@ module NewGestionnaire
|
||||||
"#{field['table']}/#{field['column']}"
|
"#{field['table']}/#{field['column']}"
|
||||||
end
|
end
|
||||||
|
|
||||||
procedure.fields_for_select.reject do |field|
|
procedure_presentation.fields_for_select.reject do |field|
|
||||||
current_filters_fields_ids.include?(field[1])
|
current_filters_fields_ids.include?(field[1])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def eager_load_displayed_fields
|
def eager_load_displayed_fields
|
||||||
@displayed_fields
|
procedure_presentation.displayed_fields
|
||||||
.reject { |field| field['table'] == 'self' }
|
.reject { |field| field['table'] == 'self' }
|
||||||
.group_by do |field|
|
.group_by do |field|
|
||||||
if ['type_de_champ', 'type_de_champ_private'].include?(field['table'])
|
if ['type_de_champ', 'type_de_champ_private'].include?(field['table'])
|
||||||
|
|
|
@ -296,16 +296,6 @@ class Procedure < ApplicationRecord
|
||||||
without_continuation_mail || Mails::WithoutContinuationMail.default_for_procedure(self)
|
without_continuation_mail || Mails::WithoutContinuationMail.default_for_procedure(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fields
|
|
||||||
DossierFieldService.new.fields(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def fields_for_select
|
|
||||||
fields.map do |field|
|
|
||||||
[field['label'], "#{field['table']}/#{field['column']}"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.default_sort
|
def self.default_sort
|
||||||
{
|
{
|
||||||
'table' => 'self',
|
'table' => 'self',
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class ProcedurePresentation < ApplicationRecord
|
class ProcedurePresentation < ApplicationRecord
|
||||||
EXTRA_SORT_COLUMNS = {
|
EXTRA_SORT_COLUMNS = {
|
||||||
'notifications' => Set['notifications'],
|
'notifications' => ['notifications'],
|
||||||
'self' => Set['id', 'state']
|
'self' => ['id', 'state']
|
||||||
}
|
}
|
||||||
|
|
||||||
belongs_to :assign_to
|
belongs_to :assign_to
|
||||||
|
@ -10,13 +10,137 @@ class ProcedurePresentation < ApplicationRecord
|
||||||
|
|
||||||
validate :check_allowed_displayed_fields
|
validate :check_allowed_displayed_fields
|
||||||
validate :check_allowed_sort_column
|
validate :check_allowed_sort_column
|
||||||
|
validate :check_allowed_sort_order
|
||||||
validate :check_allowed_filter_columns
|
validate :check_allowed_filter_columns
|
||||||
|
|
||||||
|
def fields
|
||||||
|
fields = [
|
||||||
|
field_hash('Créé le', 'self', 'created_at'),
|
||||||
|
field_hash('Mis à jour le', 'self', 'updated_at'),
|
||||||
|
field_hash('Demandeur', 'user', 'email')
|
||||||
|
]
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
def fields_for_select
|
||||||
|
fields.map do |field|
|
||||||
|
[field['label'], "#{field['table']}/#{field['column']}"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def displayed_field_values(dossier)
|
||||||
|
assert_matching_procedure(dossier)
|
||||||
|
displayed_fields.map { |field| get_value(dossier, field['table'], field['column']) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def sorted_ids(dossiers, gestionnaire)
|
||||||
|
dossiers.each { |dossier| assert_matching_procedure(dossier) }
|
||||||
|
table = sort['table']
|
||||||
|
column = sanitized_column(sort)
|
||||||
|
order = sort['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
|
||||||
|
|
||||||
|
def filtered_ids(dossiers, statut)
|
||||||
|
dossiers.each { |dossier| assert_matching_procedure(dossier) }
|
||||||
|
filters[statut].map do |filter|
|
||||||
|
table = filter['table']
|
||||||
|
column = sanitized_column(filter)
|
||||||
|
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
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def check_allowed_displayed_fields
|
def check_allowed_displayed_fields
|
||||||
displayed_fields.each do |field|
|
displayed_fields.each do |field|
|
||||||
table = field['table']
|
table = field['table']
|
||||||
column = field['column']
|
column = field['column']
|
||||||
if !dossier_field_service.valid_column?(procedure, table, column)
|
if !valid_column?(table, column)
|
||||||
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -25,30 +149,85 @@ class ProcedurePresentation < ApplicationRecord
|
||||||
def check_allowed_sort_column
|
def check_allowed_sort_column
|
||||||
table = sort['table']
|
table = sort['table']
|
||||||
column = sort['column']
|
column = sort['column']
|
||||||
if !valid_sort_column?(procedure, table, column)
|
if !valid_sort_column?(table, column)
|
||||||
errors.add(:sort, "#{table}.#{column} n’est pas une colonne permise")
|
errors.add(:sort, "#{table}.#{column} n’est pas une colonne permise")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def check_allowed_filter_columns
|
def check_allowed_filter_columns
|
||||||
filters.each do |_, columns|
|
filters.each do |_, columns|
|
||||||
columns.each do |column|
|
columns.each do |column|
|
||||||
table = column['table']
|
table = column['table']
|
||||||
column = column['column']
|
column = column['column']
|
||||||
if !dossier_field_service.valid_column?(procedure, table, column)
|
if !valid_column?(table, column)
|
||||||
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def assert_matching_procedure(dossier)
|
||||||
|
if dossier.procedure != procedure
|
||||||
|
raise "Procedure mismatch (expected #{procedure.id}, got #{dossier.procedure.id})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_value(dossier, table, column)
|
||||||
|
case table
|
||||||
|
when 'self'
|
||||||
|
dossier.send(column)
|
||||||
|
when 'user'
|
||||||
|
dossier.user.send(column)
|
||||||
|
when 'etablissement'
|
||||||
|
dossier.etablissement&.send(column)
|
||||||
|
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
|
||||||
|
|
||||||
|
def field_hash(label, table, column)
|
||||||
|
{
|
||||||
|
'label' => label,
|
||||||
|
'table' => table,
|
||||||
|
'column' => column
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def valid_column?(table, column)
|
||||||
|
valid_columns_for_table(table).include?(column)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
@column_whitelist[table] || []
|
||||||
|
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
|
||||||
|
|
||||||
def dossier_field_service
|
def dossier_field_service
|
||||||
@dossier_field_service ||= DossierFieldService.new
|
@dossier_field_service ||= DossierFieldService.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_sort_column?(procedure, table, column)
|
def valid_sort_column?(table, column)
|
||||||
dossier_field_service.valid_column?(procedure, table, column) || EXTRA_SORT_COLUMNS[table]&.include?(column)
|
valid_column?(table, column) || EXTRA_SORT_COLUMNS[table]&.include?(column)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,172 +0,0 @@
|
||||||
class DossierFieldService
|
|
||||||
def initialize
|
|
||||||
@column_whitelist = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def fields(procedure)
|
|
||||||
fields = [
|
|
||||||
field_hash('Créé le', 'self', 'created_at'),
|
|
||||||
field_hash('Mis à jour le', 'self', 'updated_at'),
|
|
||||||
field_hash('Demandeur', 'user', 'email')
|
|
||||||
]
|
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_value(dossier, table, column)
|
|
||||||
assert_valid_column(dossier.procedure, table, column)
|
|
||||||
|
|
||||||
case table
|
|
||||||
when 'self'
|
|
||||||
dossier.send(column)
|
|
||||||
when 'user'
|
|
||||||
dossier.user.send(column)
|
|
||||||
when 'etablissement'
|
|
||||||
dossier.etablissement&.send(column)
|
|
||||||
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
|
|
||||||
|
|
||||||
def assert_valid_column(procedure, table, column)
|
|
||||||
if !valid_column?(procedure, table, column)
|
|
||||||
raise "Invalid column #{table}.#{column}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def valid_column?(procedure, table, column)
|
|
||||||
valid_columns_for_table(procedure, table).include?(column)
|
|
||||||
end
|
|
||||||
|
|
||||||
def filtered_ids(dossiers, filters)
|
|
||||||
filters.map do |filter|
|
|
||||||
table = filter['table']
|
|
||||||
column = sanitized_column(filter)
|
|
||||||
case table
|
|
||||||
when 'self'
|
|
||||||
dossiers.where("? ILIKE ?", 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
|
|
||||||
|
|
||||||
def sorted_ids(dossiers, procedure_presentation, gestionnaire)
|
|
||||||
table = procedure_presentation.sort['table']
|
|
||||||
column = sanitized_column(procedure_presentation.sort)
|
|
||||||
order = procedure_presentation.sort['order']
|
|
||||||
assert_valid_order(order)
|
|
||||||
|
|
||||||
case table
|
|
||||||
when 'notifications'
|
|
||||||
procedure = procedure_presentation.assign_to.procedure
|
|
||||||
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 = #{procedure_presentation.sort['column'].to_i}")
|
|
||||||
.order("champs.value #{order}")
|
|
||||||
.pluck(:id)
|
|
||||||
else
|
|
||||||
return dossiers
|
|
||||||
.includes(table)
|
|
||||||
.order("#{column} #{order}")
|
|
||||||
.pluck(:id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def valid_columns_for_table(procedure, table)
|
|
||||||
if !@column_whitelist.key?(procedure.id)
|
|
||||||
@column_whitelist[procedure.id] = fields(procedure)
|
|
||||||
.group_by { |field| field['table'] }
|
|
||||||
.map { |table, fields| [table, Set.new(fields.map { |field| field['column'] }) ] }
|
|
||||||
.to_h
|
|
||||||
end
|
|
||||||
|
|
||||||
@column_whitelist[procedure.id][table] || []
|
|
||||||
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
|
|
||||||
|
|
||||||
def assert_valid_order(order)
|
|
||||||
if !["asc", "desc"].include?(order)
|
|
||||||
raise "Invalid order #{order}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def field_hash(label, table, column)
|
|
||||||
{
|
|
||||||
'label' => label,
|
|
||||||
'table' => table,
|
|
||||||
'column' => column
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -73,7 +73,7 @@
|
||||||
|
|
||||||
= render partial: "header_field", locals: { field: { "label" => "Nº dossier", "table" => "self", "column" => "id" }, classname: "number-col" }
|
= render partial: "header_field", locals: { field: { "label" => "Nº dossier", "table" => "self", "column" => "id" }, classname: "number-col" }
|
||||||
|
|
||||||
- @displayed_fields.each do |field|
|
- @procedure_presentation.displayed_fields.each do |field|
|
||||||
= render partial: "header_field", locals: { field: field, classname: "" }
|
= render partial: "header_field", locals: { field: field, classname: "" }
|
||||||
|
|
||||||
= render partial: "header_field", locals: { field: { "label" => "Statut", "table" => "self", "column" => "state" }, classname: "status-col" }
|
= render partial: "header_field", locals: { field: { "label" => "Statut", "table" => "self", "column" => "state" }, classname: "status-col" }
|
||||||
|
@ -85,14 +85,13 @@
|
||||||
.dropdown-content.fade-in-down
|
.dropdown-content.fade-in-down
|
||||||
= form_tag update_displayed_fields_gestionnaire_procedure_path(@procedure), method: :patch, class: 'dropdown-form columns-form' do
|
= form_tag update_displayed_fields_gestionnaire_procedure_path(@procedure), method: :patch, class: 'dropdown-form columns-form' do
|
||||||
= select_tag :values,
|
= select_tag :values,
|
||||||
options_for_select(@procedure.fields_for_select,
|
options_for_select(@procedure_presentation.fields_for_select,
|
||||||
selected: @displayed_fields_values),
|
selected: @displayed_fields_values),
|
||||||
multiple: true,
|
multiple: true,
|
||||||
class: 'select2-limited'
|
class: 'select2-limited'
|
||||||
= submit_tag "Enregistrer", class: 'button'
|
= submit_tag "Enregistrer", class: 'button'
|
||||||
|
|
||||||
%tbody
|
%tbody
|
||||||
- dossier_field_service = DossierFieldService.new
|
|
||||||
- @dossiers.each do |dossier|
|
- @dossiers.each do |dossier|
|
||||||
%tr
|
%tr
|
||||||
%td.folder-col
|
%td.folder-col
|
||||||
|
@ -105,10 +104,9 @@
|
||||||
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
|
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
|
||||||
= dossier.id
|
= dossier.id
|
||||||
|
|
||||||
- @displayed_fields.each do |field|
|
- @procedure_presentation.displayed_field_values(dossier).each do |value|
|
||||||
%td
|
%td
|
||||||
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
|
= link_to(value, gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link')
|
||||||
= dossier_field_service.get_value(dossier, field['table'], field['column'])
|
|
||||||
|
|
||||||
%td.status-col
|
%td.status-col
|
||||||
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
|
= link_to(gestionnaire_dossier_path(@procedure, dossier), class: 'cell-link') do
|
||||||
|
|
|
@ -1,26 +1,66 @@
|
||||||
{
|
{
|
||||||
"ignored_warnings": [
|
"ignored_warnings": [
|
||||||
{
|
{
|
||||||
"warning_type": "Redirect",
|
"warning_type": "SQL Injection",
|
||||||
"warning_code": 18,
|
"warning_code": 0,
|
||||||
"fingerprint": "a0a4cede6d50308b90bd747efd0a2ebd58947fbd5d100349ccd640c60413b1a9",
|
"fingerprint": "030551e51e29561702bcb9760bdeeed15e1936d4a9537f27e5d1d13a0ebb34ef",
|
||||||
"check_name": "Redirect",
|
"check_name": "SQL",
|
||||||
"message": "Possible unprotected redirect",
|
"message": "Possible SQL injection",
|
||||||
"file": "app/controllers/france_connect/particulier_controller.rb",
|
"file": "app/models/procedure_presentation.rb",
|
||||||
"line": 3,
|
"line": 130,
|
||||||
"link": "http://brakemanscanner.org/docs/warning_types/redirect/",
|
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
|
||||||
"code": "redirect_to(FranceConnectParticulierClient.new.authorization_uri)",
|
"code": "dossiers.includes(sort[\"table\"]).order(\"#{sanitized_column(sort)} #{sort[\"order\"]}\")",
|
||||||
"render_path": null,
|
"render_path": null,
|
||||||
"location": {
|
"location": {
|
||||||
"type": "method",
|
"type": "method",
|
||||||
"class": "FranceConnect::ParticulierController",
|
"class": "ProcedurePresentation",
|
||||||
"method": "login"
|
"method": "sorted_ids"
|
||||||
},
|
},
|
||||||
"user_input": "FranceConnectParticulierClient.new.authorization_uri",
|
"user_input": "sanitized_column(sort)",
|
||||||
"confidence": "High",
|
"confidence": "Weak",
|
||||||
"note": "We trust FC OpenId implem"
|
"note": "Not an injection because of `sanitized_column`"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"warning_type": "SQL Injection",
|
||||||
|
"warning_code": 0,
|
||||||
|
"fingerprint": "1840f5340630814ea86311e850ebd91b966e6bccd0b6856133528e7745c0695a",
|
||||||
|
"check_name": "SQL",
|
||||||
|
"message": "Possible SQL injection",
|
||||||
|
"file": "app/models/procedure_presentation.rb",
|
||||||
|
"line": 119,
|
||||||
|
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
|
||||||
|
"code": "dossiers.order(\"#{sanitized_column(sort)} #{sort[\"order\"]}\")",
|
||||||
|
"render_path": null,
|
||||||
|
"location": {
|
||||||
|
"type": "method",
|
||||||
|
"class": "ProcedurePresentation",
|
||||||
|
"method": "sorted_ids"
|
||||||
|
},
|
||||||
|
"user_input": "sanitized_column(sort)",
|
||||||
|
"confidence": "Weak",
|
||||||
|
"note": "Not an injection because of `sanitized_column`"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"warning_type": "SQL Injection",
|
||||||
|
"warning_code": 0,
|
||||||
|
"fingerprint": "b2feda5e5ae668cdbf0653f134c40bcb9e45499c1b607450e43a0166c4098364",
|
||||||
|
"check_name": "SQL",
|
||||||
|
"message": "Possible SQL injection",
|
||||||
|
"file": "app/models/procedure_presentation.rb",
|
||||||
|
"line": 125,
|
||||||
|
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
|
||||||
|
"code": "dossiers.includes(((\"type_de_champ\" == \"type_de_champ\") ? (:champs) : (:champs_private))).where(\"champs.type_de_champ_id = #{sort[\"column\"].to_i}\").order(\"champs.value #{sort[\"order\"]}\")",
|
||||||
|
"render_path": null,
|
||||||
|
"location": {
|
||||||
|
"type": "method",
|
||||||
|
"class": "ProcedurePresentation",
|
||||||
|
"method": "sorted_ids"
|
||||||
|
},
|
||||||
|
"user_input": "sort[\"order\"]",
|
||||||
|
"confidence": "Weak",
|
||||||
|
"note": "Not an injection because `sort[\"order\"]` has passed `check_allowed_sort_order`"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"updated": "2018-01-11 15:53:22 +0100",
|
"updated": "2018-10-05 16:12:32 +0200",
|
||||||
"brakeman_version": "3.7.0"
|
"brakeman_version": "4.3.1"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe ProcedurePresentation do
|
describe ProcedurePresentation do
|
||||||
let(:assign_to) { create(:assign_to, procedure: create(:procedure, :with_type_de_champ)) }
|
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
|
||||||
|
let(:assign_to) { create(:assign_to, procedure: procedure) }
|
||||||
let(:first_type_de_champ_id) { assign_to.procedure.types_de_champ.first.id.to_s }
|
let(:first_type_de_champ_id) { assign_to.procedure.types_de_champ.first.id.to_s }
|
||||||
let (:procedure_presentation_id) {
|
let (:procedure_presentation_id) {
|
||||||
ProcedurePresentation.create(
|
ProcedurePresentation.create(
|
||||||
|
@ -46,4 +47,280 @@ describe ProcedurePresentation do
|
||||||
it { expect(build(:procedure_presentation, filters: { "suivis" => [{ "table" => "user", "column" => "reset_password_token", "order" => "asc" }] })).to be_invalid }
|
it { expect(build(:procedure_presentation, filters: { "suivis" => [{ "table" => "user", "column" => "reset_password_token", "order" => "asc" }] })).to be_invalid }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#fields" do
|
||||||
|
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :types_de_champ_count => 4, :types_de_champ_private_count => 4) }
|
||||||
|
let(:tdc_1) { procedure.types_de_champ[0] }
|
||||||
|
let(:tdc_2) { procedure.types_de_champ[1] }
|
||||||
|
let(:tdc_private_1) { procedure.types_de_champ_private[0] }
|
||||||
|
let(:tdc_private_2) { procedure.types_de_champ_private[1] }
|
||||||
|
let(:expected) {
|
||||||
|
[
|
||||||
|
{ "label" => 'Créé le', "table" => 'self', "column" => 'created_at' },
|
||||||
|
{ "label" => 'Mis à jour le', "table" => 'self', "column" => 'updated_at' },
|
||||||
|
{ "label" => 'Demandeur', "table" => 'user', "column" => 'email' },
|
||||||
|
{ "label" => 'SIREN', "table" => 'etablissement', "column" => 'entreprise_siren' },
|
||||||
|
{ "label" => 'Forme juridique', "table" => 'etablissement', "column" => 'entreprise_forme_juridique' },
|
||||||
|
{ "label" => 'Nom commercial', "table" => 'etablissement', "column" => 'entreprise_nom_commercial' },
|
||||||
|
{ "label" => 'Raison sociale', "table" => 'etablissement', "column" => 'entreprise_raison_sociale' },
|
||||||
|
{ "label" => 'SIRET siège social', "table" => 'etablissement', "column" => 'entreprise_siret_siege_social' },
|
||||||
|
{ "label" => 'Date de création', "table" => 'etablissement', "column" => 'entreprise_date_creation' },
|
||||||
|
{ "label" => 'SIRET', "table" => 'etablissement', "column" => 'siret' },
|
||||||
|
{ "label" => 'Libellé NAF', "table" => 'etablissement', "column" => 'libelle_naf' },
|
||||||
|
{ "label" => 'Code postal', "table" => 'etablissement', "column" => 'code_postal' },
|
||||||
|
{ "label" => tdc_1.libelle, "table" => 'type_de_champ', "column" => tdc_1.id.to_s },
|
||||||
|
{ "label" => tdc_2.libelle, "table" => 'type_de_champ', "column" => tdc_2.id.to_s },
|
||||||
|
{ "label" => tdc_private_1.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_1.id.to_s },
|
||||||
|
{ "label" => tdc_private_2.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_2.id.to_s }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
before do
|
||||||
|
procedure.types_de_champ[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
|
||||||
|
procedure.types_de_champ[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
|
||||||
|
procedure.types_de_champ_private[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
|
||||||
|
procedure.types_de_champ_private[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
|
||||||
|
end
|
||||||
|
|
||||||
|
subject { create(:procedure_presentation, assign_to: create(:assign_to, procedure: procedure)) }
|
||||||
|
|
||||||
|
it { expect(subject.fields).to eq(expected) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#fields_for_select" do
|
||||||
|
subject { create(:procedure_presentation) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(subject).to receive(:fields).and_return([
|
||||||
|
{
|
||||||
|
"label" => "label1",
|
||||||
|
"table" => "table1",
|
||||||
|
"column" => "column1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label" => "label2",
|
||||||
|
"table" => "table2",
|
||||||
|
"column" => "column2"
|
||||||
|
}
|
||||||
|
])
|
||||||
|
end
|
||||||
|
|
||||||
|
it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#get_value' do
|
||||||
|
let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to, displayed_fields: [{ 'table' => table, 'column' => column }]) }
|
||||||
|
|
||||||
|
subject { procedure_presentation.displayed_field_values(dossier).first }
|
||||||
|
|
||||||
|
context 'for self table' do
|
||||||
|
let(:table) { 'self' }
|
||||||
|
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
|
||||||
|
|
||||||
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
|
||||||
|
before { dossier.touch(time: DateTime.new(2018, 9, 25)) }
|
||||||
|
|
||||||
|
it { is_expected.to eq(DateTime.new(2018, 9, 25)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for user table' do
|
||||||
|
let(:table) { 'user' }
|
||||||
|
let(:column) { 'email' }
|
||||||
|
|
||||||
|
let(:dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'bla@yopmail.com')) }
|
||||||
|
|
||||||
|
it { is_expected.to eq('bla@yopmail.com') }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for etablissement table' do
|
||||||
|
let(:table) { 'etablissement' }
|
||||||
|
let(:column) { 'code_postal' } # All other columns work the same, no extra test required
|
||||||
|
|
||||||
|
let!(:dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
|
||||||
|
|
||||||
|
it { is_expected.to eq('75008') }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for type_de_champ table' do
|
||||||
|
let(:table) { 'type_de_champ' }
|
||||||
|
let(:column) { procedure.types_de_champ.first.id.to_s }
|
||||||
|
|
||||||
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
|
||||||
|
before { dossier.champs.first.update(value: 'kale') }
|
||||||
|
|
||||||
|
it { is_expected.to eq('kale') }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for type_de_champ_private table' do
|
||||||
|
let(:table) { 'type_de_champ_private' }
|
||||||
|
let(:column) { procedure.types_de_champ_private.first.id.to_s }
|
||||||
|
|
||||||
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
|
||||||
|
before { dossier.champs_private.first.update(value: 'quinoa') }
|
||||||
|
|
||||||
|
it { is_expected.to eq('quinoa') }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#sorted_ids' do
|
||||||
|
let(:gestionnaire) { create(:gestionnaire) }
|
||||||
|
let(:assign_to) { create(:assign_to, procedure: procedure, gestionnaire: gestionnaire) }
|
||||||
|
let(:sort) { { 'table' => table, 'column' => column, 'order' => order } }
|
||||||
|
let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to, sort: sort) }
|
||||||
|
|
||||||
|
subject { procedure_presentation.sorted_ids(procedure.dossiers, gestionnaire) }
|
||||||
|
|
||||||
|
context 'for notifications table' do
|
||||||
|
let(:table) { 'notifications' }
|
||||||
|
let(:column) { 'notifications' }
|
||||||
|
|
||||||
|
let!(:notified_dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
||||||
|
let!(:recent_dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
||||||
|
let!(:older_dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
notified_dossier.champs.first.touch(time: DateTime.new(2018, 9, 20))
|
||||||
|
create(:follow, gestionnaire: gestionnaire, dossier: notified_dossier, demande_seen_at: DateTime.new(2018, 9, 10))
|
||||||
|
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
|
||||||
|
older_dossier.touch(time: DateTime.new(2018, 5, 13))
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'in ascending order' do
|
||||||
|
let(:order) { 'asc' }
|
||||||
|
|
||||||
|
it { is_expected.to eq([older_dossier, recent_dossier, notified_dossier].map(&:id)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'in descending order' do
|
||||||
|
let(:order) { 'desc' }
|
||||||
|
|
||||||
|
it { is_expected.to eq([notified_dossier, recent_dossier, older_dossier].map(&:id)) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for self table' do
|
||||||
|
let(:table) { 'self' }
|
||||||
|
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
|
||||||
|
let(:order) { 'asc' } # Desc works the same, no extra test required
|
||||||
|
|
||||||
|
let(:recent_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
let(:older_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
|
||||||
|
older_dossier.touch(time: DateTime.new(2018, 5, 13))
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for type_de_champ table' do
|
||||||
|
let(:table) { 'type_de_champ' }
|
||||||
|
let(:column) { procedure.types_de_champ.first.id.to_s }
|
||||||
|
let(:order) { 'desc' } # Asc works the same, no extra test required
|
||||||
|
|
||||||
|
let(:beurre_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
let(:tartine_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
beurre_dossier.champs.first.update(value: 'beurre')
|
||||||
|
tartine_dossier.champs.first.update(value: 'tartine')
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq([tartine_dossier, beurre_dossier].map(&:id)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for type_de_champ_private table' do
|
||||||
|
let(:table) { 'type_de_champ_private' }
|
||||||
|
let(:column) { procedure.types_de_champ_private.first.id.to_s }
|
||||||
|
let(:order) { 'asc' } # Desc works the same, no extra test required
|
||||||
|
|
||||||
|
let(:biere_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
let(:vin_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
biere_dossier.champs_private.first.update(value: 'biere')
|
||||||
|
vin_dossier.champs_private.first.update(value: 'vin')
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq([biere_dossier, vin_dossier].map(&:id)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for other tables' do
|
||||||
|
# All other columns and tables work the same so it’s ok to test only one
|
||||||
|
let(:table) { 'etablissement' }
|
||||||
|
let(:column) { 'code_postal' }
|
||||||
|
let(:order) { 'asc' } # Desc works the same, no extra test required
|
||||||
|
|
||||||
|
let!(:huitieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
|
||||||
|
let!(:vingtieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75020')) }
|
||||||
|
|
||||||
|
it { is_expected.to eq([huitieme_dossier, vingtieme_dossier].map(&:id)) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#filtered_ids' do
|
||||||
|
let(:procedure_presentation) { create(:procedure_presentation, assign_to: create(:assign_to, procedure: procedure), filters: { "suivis" => filter }) }
|
||||||
|
|
||||||
|
subject { procedure_presentation.filtered_ids(procedure.dossiers, 'suivis') }
|
||||||
|
|
||||||
|
context 'for type_de_champ table' do
|
||||||
|
let(:kept_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
let(:type_de_champ) { procedure.types_de_champ.first }
|
||||||
|
let(:filter) { [{ 'table' => 'type_de_champ', 'column' => type_de_champ.id.to_s, 'value' => 'keep' }] }
|
||||||
|
|
||||||
|
before do
|
||||||
|
type_de_champ.champ.create(dossier: kept_dossier, value: 'keep me')
|
||||||
|
type_de_champ.champ.create(dossier: discarded_dossier, value: 'discard me')
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to contain_exactly(kept_dossier.id) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for type_de_champ_private table' do
|
||||||
|
let(:kept_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
|
||||||
|
let(:type_de_champ_private) { procedure.types_de_champ_private.first }
|
||||||
|
let(:filter) { [{ 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.id.to_s, 'value' => 'keep' }] }
|
||||||
|
|
||||||
|
before do
|
||||||
|
type_de_champ_private.champ.create(dossier: kept_dossier, value: 'keep me')
|
||||||
|
type_de_champ_private.champ.create(dossier: discarded_dossier, value: 'discard me')
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to contain_exactly(kept_dossier.id) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for etablissement table' do
|
||||||
|
context 'for entreprise_date_creation column' do
|
||||||
|
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2018, 6, 21))) }
|
||||||
|
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2008, 6, 21))) }
|
||||||
|
let(:filter) { [{ 'table' => 'etablissement', 'column' => 'entreprise_date_creation', 'value' => '21/6/2018' }] }
|
||||||
|
|
||||||
|
it { is_expected.to contain_exactly(kept_dossier.id) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for code_postal column' do
|
||||||
|
# All columns except entreprise_date_creation work exacly the same, just testing one
|
||||||
|
|
||||||
|
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75017')) }
|
||||||
|
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '25000')) }
|
||||||
|
let(:filter) { [{ 'table' => 'etablissement', 'column' => 'code_postal', 'value' => '75017' }] }
|
||||||
|
|
||||||
|
it { is_expected.to contain_exactly(kept_dossier.id) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for user table' do
|
||||||
|
let!(:kept_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@keepmail.com')) }
|
||||||
|
let!(:discarded_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@discard.com')) }
|
||||||
|
let(:filter) { [{ 'table' => 'user', 'column' => 'email', 'value' => 'keepmail' }] }
|
||||||
|
|
||||||
|
it { is_expected.to contain_exactly(kept_dossier.id) }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -628,64 +628,6 @@ describe Procedure do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#fields" do
|
|
||||||
subject { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :types_de_champ_count => 4, :types_de_champ_private_count => 4) }
|
|
||||||
let(:tdc_1) { subject.types_de_champ[0] }
|
|
||||||
let(:tdc_2) { subject.types_de_champ[1] }
|
|
||||||
let(:tdc_private_1) { subject.types_de_champ_private[0] }
|
|
||||||
let(:tdc_private_2) { subject.types_de_champ_private[1] }
|
|
||||||
let(:expected) {
|
|
||||||
[
|
|
||||||
{ "label" => 'Créé le', "table" => 'self', "column" => 'created_at' },
|
|
||||||
{ "label" => 'Mis à jour le', "table" => 'self', "column" => 'updated_at' },
|
|
||||||
{ "label" => 'Demandeur', "table" => 'user', "column" => 'email' },
|
|
||||||
{ "label" => 'SIREN', "table" => 'etablissement', "column" => 'entreprise_siren' },
|
|
||||||
{ "label" => 'Forme juridique', "table" => 'etablissement', "column" => 'entreprise_forme_juridique' },
|
|
||||||
{ "label" => 'Nom commercial', "table" => 'etablissement', "column" => 'entreprise_nom_commercial' },
|
|
||||||
{ "label" => 'Raison sociale', "table" => 'etablissement', "column" => 'entreprise_raison_sociale' },
|
|
||||||
{ "label" => 'SIRET siège social', "table" => 'etablissement', "column" => 'entreprise_siret_siege_social' },
|
|
||||||
{ "label" => 'Date de création', "table" => 'etablissement', "column" => 'entreprise_date_creation' },
|
|
||||||
{ "label" => 'SIRET', "table" => 'etablissement', "column" => 'siret' },
|
|
||||||
{ "label" => 'Libellé NAF', "table" => 'etablissement', "column" => 'libelle_naf' },
|
|
||||||
{ "label" => 'Code postal', "table" => 'etablissement', "column" => 'code_postal' },
|
|
||||||
{ "label" => tdc_1.libelle, "table" => 'type_de_champ', "column" => tdc_1.id.to_s },
|
|
||||||
{ "label" => tdc_2.libelle, "table" => 'type_de_champ', "column" => tdc_2.id.to_s },
|
|
||||||
{ "label" => tdc_private_1.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_1.id.to_s },
|
|
||||||
{ "label" => tdc_private_2.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_2.id.to_s }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
before do
|
|
||||||
subject.types_de_champ[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
|
|
||||||
subject.types_de_champ[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
|
|
||||||
subject.types_de_champ_private[2].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:header_section))
|
|
||||||
subject.types_de_champ_private[3].update_attribute(:type_champ,TypeDeChamp.type_champs.fetch(:explication))
|
|
||||||
end
|
|
||||||
|
|
||||||
it { expect(subject.fields).to eq(expected) }
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#fields_for_select" do
|
|
||||||
subject { create(:procedure) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
allow(subject).to receive(:fields).and_return([
|
|
||||||
{
|
|
||||||
"label" => "label1",
|
|
||||||
"table" => "table1",
|
|
||||||
"column" => "column1"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label" => "label2",
|
|
||||||
"table" => "table2",
|
|
||||||
"column" => "column2"
|
|
||||||
}
|
|
||||||
])
|
|
||||||
end
|
|
||||||
|
|
||||||
it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) }
|
|
||||||
end
|
|
||||||
|
|
||||||
describe ".default_sort" do
|
describe ".default_sort" do
|
||||||
it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) }
|
it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,220 +0,0 @@
|
||||||
require 'spec_helper'
|
|
||||||
|
|
||||||
describe DossierFieldService do
|
|
||||||
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
|
|
||||||
|
|
||||||
describe '#filtered_ids' do
|
|
||||||
context 'for type_de_champ table' do
|
|
||||||
let(:kept_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:type_de_champ) { procedure.types_de_champ.first }
|
|
||||||
|
|
||||||
before do
|
|
||||||
type_de_champ.champ.create(dossier: kept_dossier, value: 'keep me')
|
|
||||||
type_de_champ.champ.create(dossier: discarded_dossier, value: 'discard me')
|
|
||||||
end
|
|
||||||
|
|
||||||
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'type_de_champ', 'column' => type_de_champ.id, 'value' => 'keep' }]) }
|
|
||||||
|
|
||||||
it { is_expected.to contain_exactly(kept_dossier.id) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for type_de_champ_private table' do
|
|
||||||
let(:kept_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:discarded_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:type_de_champ_private) { procedure.types_de_champ_private.first }
|
|
||||||
|
|
||||||
before do
|
|
||||||
type_de_champ_private.champ.create(dossier: kept_dossier, value: 'keep me')
|
|
||||||
type_de_champ_private.champ.create(dossier: discarded_dossier, value: 'discard me')
|
|
||||||
end
|
|
||||||
|
|
||||||
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.id, 'value' => 'keep' }]) }
|
|
||||||
|
|
||||||
it { is_expected.to contain_exactly(kept_dossier.id) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for etablissement table' do
|
|
||||||
context 'for entreprise_date_creation column' do
|
|
||||||
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2018, 6, 21))) }
|
|
||||||
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: DateTime.new(2008, 6, 21))) }
|
|
||||||
|
|
||||||
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'etablissement', 'column' => 'entreprise_date_creation', 'value' => '21/6/2018' }]) }
|
|
||||||
|
|
||||||
it { is_expected.to contain_exactly(kept_dossier.id) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for code_postal column' do
|
|
||||||
# All columns except entreprise_date_creation work exacly the same, just testing one
|
|
||||||
|
|
||||||
let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75017')) }
|
|
||||||
let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '25000')) }
|
|
||||||
|
|
||||||
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'etablissement', 'column' => 'code_postal', 'value' => '75017' }]) }
|
|
||||||
|
|
||||||
it { is_expected.to contain_exactly(kept_dossier.id) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for user table' do
|
|
||||||
let!(:kept_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@keepmail.com')) }
|
|
||||||
let!(:discarded_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'me@discard.com')) }
|
|
||||||
|
|
||||||
subject { described_class.new.filtered_ids(procedure.dossiers, [{ 'table' => 'user', 'column' => 'email', 'value' => 'keepmail' }]) }
|
|
||||||
|
|
||||||
it { is_expected.to contain_exactly(kept_dossier.id) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#sorted_ids' do
|
|
||||||
let(:gestionnaire) { create(:gestionnaire) }
|
|
||||||
let(:assign_to) { create(:assign_to, procedure: procedure, gestionnaire: gestionnaire) }
|
|
||||||
let(:sort) { { 'table' => table, 'column' => column, 'order' => order } }
|
|
||||||
let(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to, sort: sort) }
|
|
||||||
|
|
||||||
subject { described_class.new.sorted_ids(procedure.dossiers, procedure_presentation, gestionnaire) }
|
|
||||||
|
|
||||||
context 'for notifications table' do
|
|
||||||
let(:table) { 'notifications' }
|
|
||||||
let(:column) { 'notifications' }
|
|
||||||
|
|
||||||
let!(:notified_dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
|
||||||
let!(:recent_dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
|
||||||
let!(:older_dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
notified_dossier.champs.first.touch(time: DateTime.new(2018, 9, 20))
|
|
||||||
create(:follow, gestionnaire: gestionnaire, dossier: notified_dossier, demande_seen_at: DateTime.new(2018, 9, 10))
|
|
||||||
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
|
|
||||||
older_dossier.touch(time: DateTime.new(2018, 5, 13))
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'in ascending order' do
|
|
||||||
let(:order) { 'asc' }
|
|
||||||
|
|
||||||
it { is_expected.to eq([older_dossier, recent_dossier, notified_dossier].map(&:id)) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'in descending order' do
|
|
||||||
let(:order) { 'desc' }
|
|
||||||
|
|
||||||
it { is_expected.to eq([notified_dossier, recent_dossier, older_dossier].map(&:id)) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for self table' do
|
|
||||||
let(:table) { 'self' }
|
|
||||||
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
|
|
||||||
let(:order) { 'asc' } # Desc works the same, no extra test required
|
|
||||||
|
|
||||||
let(:recent_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:older_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
recent_dossier.touch(time: DateTime.new(2018, 9, 25))
|
|
||||||
older_dossier.touch(time: DateTime.new(2018, 5, 13))
|
|
||||||
end
|
|
||||||
|
|
||||||
it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for type_de_champ table' do
|
|
||||||
let(:table) { 'type_de_champ' }
|
|
||||||
let(:column) { procedure.types_de_champ.first.id.to_s }
|
|
||||||
let(:order) { 'desc' } # Asc works the same, no extra test required
|
|
||||||
|
|
||||||
let(:beurre_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:tartine_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
beurre_dossier.champs.first.update(value: 'beurre')
|
|
||||||
tartine_dossier.champs.first.update(value: 'tartine')
|
|
||||||
end
|
|
||||||
|
|
||||||
it { is_expected.to eq([tartine_dossier, beurre_dossier].map(&:id)) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for type_de_champ_private table' do
|
|
||||||
let(:table) { 'type_de_champ_private' }
|
|
||||||
let(:column) { procedure.types_de_champ_private.first.id.to_s }
|
|
||||||
let(:order) { 'asc' } # Desc works the same, no extra test required
|
|
||||||
|
|
||||||
let(:biere_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:vin_dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
biere_dossier.champs_private.first.update(value: 'biere')
|
|
||||||
vin_dossier.champs_private.first.update(value: 'vin')
|
|
||||||
end
|
|
||||||
|
|
||||||
it { is_expected.to eq([biere_dossier, vin_dossier].map(&:id)) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for other tables' do
|
|
||||||
# All other columns and tables work the same so it’s ok to test only one
|
|
||||||
let(:table) { 'etablissement' }
|
|
||||||
let(:column) { 'code_postal' }
|
|
||||||
let(:order) { 'asc' } # Desc works the same, no extra test required
|
|
||||||
|
|
||||||
let!(:huitieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
|
|
||||||
let!(:vingtieme_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75020')) }
|
|
||||||
|
|
||||||
it { is_expected.to eq([huitieme_dossier, vingtieme_dossier].map(&:id)) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#get_value' do
|
|
||||||
subject { described_class.new.get_value(dossier, table, column) }
|
|
||||||
|
|
||||||
context 'for self table' do
|
|
||||||
let(:table) { 'self' }
|
|
||||||
let(:column) { 'updated_at' } # All other columns work the same, no extra test required
|
|
||||||
|
|
||||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
|
|
||||||
before { dossier.touch(time: DateTime.new(2018, 9, 25)) }
|
|
||||||
|
|
||||||
it { is_expected.to eq(DateTime.new(2018, 9, 25)) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for user table' do
|
|
||||||
let(:table) { 'user' }
|
|
||||||
let(:column) { 'email' }
|
|
||||||
|
|
||||||
let(:dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'bla@yopmail.com')) }
|
|
||||||
|
|
||||||
it { is_expected.to eq('bla@yopmail.com') }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for etablissement table' do
|
|
||||||
let(:table) { 'etablissement' }
|
|
||||||
let(:column) { 'code_postal' } # All other columns work the same, no extra test required
|
|
||||||
|
|
||||||
let!(:dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '75008')) }
|
|
||||||
|
|
||||||
it { is_expected.to eq('75008') }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for type_de_champ table' do
|
|
||||||
let(:table) { 'type_de_champ' }
|
|
||||||
let(:column) { procedure.types_de_champ.first.id.to_s }
|
|
||||||
|
|
||||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
|
|
||||||
before { dossier.champs.first.update(value: 'kale') }
|
|
||||||
|
|
||||||
it { is_expected.to eq('kale') }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'for type_de_champ_private table' do
|
|
||||||
let(:table) { 'type_de_champ_private' }
|
|
||||||
let(:column) { procedure.types_de_champ_private.first.id.to_s }
|
|
||||||
|
|
||||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
|
|
||||||
before { dossier.champs_private.first.update(value: 'quinoa') }
|
|
||||||
|
|
||||||
it { is_expected.to eq('quinoa') }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in a new issue