diff --git a/app/models/concerns/dossier_filtering_concern.rb b/app/models/concerns/dossier_filtering_concern.rb index e102c1239..a1470bbda 100644 --- a/app/models/concerns/dossier_filtering_concern.rb +++ b/app/models/concerns/dossier_filtering_concern.rb @@ -29,13 +29,13 @@ module DossierFilteringConcern } scope :filter_ilike, lambda { |table, column, values| - table_column = ProcedurePresentation.sanitized_column(table, column) + table_column = DossierFilterService.sanitized_column(table, column) q = Array.new(values.count, "(#{table_column} ILIKE ?)").join(' OR ') where(q, *(values.map { |value| "%#{value}%" })) } scope :filter_enum, lambda { |table, column, values| - table_column = ProcedurePresentation.sanitized_column(table, column) + table_column = DossierFilterService.sanitized_column(table, column) q = Array.new(values.count, "(#{table_column} = ?)").join(' OR ') where(q, *(values)) } diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 37247174a..42345edd9 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -42,17 +42,6 @@ class ProcedurePresentation < ApplicationRecord ] end - def filtered_sorted_ids(dossiers, statut, count: nil) - dossiers_by_statut = dossiers.by_statut(statut, instructeur) - dossiers_sorted_ids = self.sorted_ids(dossiers_by_statut, count || dossiers_by_statut.size) - - if filters_for(statut).present? - dossiers_sorted_ids.intersection(filtered_ids(dossiers_by_statut, statut)) - else - dossiers_sorted_ids - end - end - 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) @@ -90,120 +79,6 @@ class ProcedurePresentation < ApplicationRecord private - def sorted_ids(dossiers, count) - table = sorted_column.column.table - column = sorted_column.column.column - order = sorted_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_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) - else - case table - when 'self' - if filtered_column.type == :date - dates = values - .filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil } - - dossiers.filter_by_datetimes(column, dates) - elsif filtered_column.column == "state" && values.include?("pending_correction") - dossiers.joins(:corrections).where(corrections: DossierCorrection.pending) - elsif filtered_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 - if filtered_column.type == :enum - 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 - 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) - dossiers - .includes(:followers_instructeurs) - .joins('INNER JOIN users instructeurs_users ON instructeurs_users.id = instructeurs.user_id') - .filter_ilike('instructeurs_users', :email, values) # ilike OK, user may want to search by *@domain - when 'user', 'individual' # user_columns: [email], individual_columns: ['nom', 'prenom', 'gender'] - dossiers - .includes(table) - .filter_ilike(table, column, values) # ilike or where column == 'value' are both valid, we opted for ilike - when 'groupe_instructeur' - assert_supported_column(table, column) - - dossiers - .joins(:groupe_instructeur) - .where(groupe_instructeur_id: values) - end.pluck(:id) - end - end.reduce(:&) - end - def find_type_de_champ(column) TypeDeChamp .joins(:revision_types_de_champ) @@ -211,29 +86,4 @@ class ProcedurePresentation < ApplicationRecord .order(created_at: :desc) .find_by(stable_id: column) end - - def self.sanitized_column(association, column) - table = if association == 'self' - Dossier.table_name - elsif (association_reflection = Dossier.reflect_on_association(association)) - association_reflection.klass.table_name - else - # Allow filtering on a joined table alias (which doesn’t exist - # in the ActiveRecord domain). - association - end - - [table, column] - .map { |name| ActiveRecord::Base.connection.quote_column_name(name) } - .join('.') - end - - def assert_supported_column(table, column) - if table == 'followers_instructeurs' && column != 'email' - raise ArgumentError, 'Table `followers_instructeurs` only supports the `email` column.' - end - if table == 'groupe_instructeur' && (column != 'label' && column != 'id') - raise ArgumentError, 'Table `groupe_instructeur` only supports the `label` or `id` column.' - end - end end diff --git a/app/services/dossier_filter_service.rb b/app/services/dossier_filter_service.rb new file mode 100644 index 000000000..586213451 --- /dev/null +++ b/app/services/dossier_filter_service.rb @@ -0,0 +1,157 @@ +# frozen_string_literal: true + +class DossierFilterService + TYPE_DE_CHAMP = 'type_de_champ' + + def self.filtered_sorted_ids(dossiers, statut, filters, sorted_column, instructeur, count: nil) + dossiers_by_statut = dossiers.by_statut(statut, instructeur) + dossiers_sorted_ids = self.sorted_ids(dossiers_by_statut, sorted_column, instructeur, count || dossiers_by_statut.size) + + if filters.present? + dossiers_sorted_ids.intersection(filtered_ids(dossiers_by_statut, filters)) + else + dossiers_sorted_ids + end + end + + private + + def self.sorted_ids(dossiers, sorted_column, instructeur, count) + table = sorted_column.column.table + column = sorted_column.column.column + order = sorted_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("#{sanitized_column(table, column)} #{order}") + .pluck(:id) + .uniq + when 'self', 'user', 'individual', 'etablissement', 'groupe_instructeur' + (table == 'self' ? dossiers : dossiers.includes(table)) + .order("#{sanitized_column(table, column)} #{order}") + .pluck(:id) + end + end + + def self.filtered_ids(dossiers, filters) + filters + .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) + else + case table + when 'self' + if filtered_column.type == :date + dates = values + .filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil } + + dossiers.filter_by_datetimes(column, dates) + elsif filtered_column.column == "state" && values.include?("pending_correction") + dossiers.joins(:corrections).where(corrections: DossierCorrection.pending) + elsif filtered_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 + if filtered_column.type == :enum + 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 + 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) + dossiers + .includes(:followers_instructeurs) + .joins('INNER JOIN users instructeurs_users ON instructeurs_users.id = instructeurs.user_id') + .filter_ilike('instructeurs_users', :email, values) # ilike OK, user may want to search by *@domain + when 'user', 'individual' # user_columns: [email], individual_columns: ['nom', 'prenom', 'gender'] + dossiers + .includes(table) + .filter_ilike(table, column, values) # ilike or where column == 'value' are both valid, we opted for ilike + when 'groupe_instructeur' + assert_supported_column(table, column) + + dossiers + .joins(:groupe_instructeur) + .where(groupe_instructeur_id: values) + end.pluck(:id) + end + end.reduce(:&) + end + + def self.sanitized_column(association, column) + table = if association == 'self' + Dossier.table_name + elsif (association_reflection = Dossier.reflect_on_association(association)) + association_reflection.klass.table_name + else + # Allow filtering on a joined table alias (which doesn’t exist + # in the ActiveRecord domain). + association + end + + [table, column] + .map { |name| ActiveRecord::Base.connection.quote_column_name(name) } + .join('.') + end + + def self.assert_supported_column(table, column) + if table == 'followers_instructeurs' && column != 'email' + raise ArgumentError, 'Table `followers_instructeurs` only supports the `email` column.' + end + if table == 'groupe_instructeur' && (column != 'label' && column != 'id') + raise ArgumentError, 'Table `groupe_instructeur` only supports the `label` or `id` column.' + end + end +end diff --git a/config/brakeman.ignore b/config/brakeman.ignore index 4cb15c70d..69ad55629 100644 --- a/config/brakeman.ignore +++ b/config/brakeman.ignore @@ -44,29 +44,6 @@ ], "note": "" }, - { - "warning_type": "SQL Injection", - "warning_code": 0, - "fingerprint": "31693060072e27c02ca4f884f2a07f4f1c1247b7a6f5cc5c724e88e6ca9b4873", - "check_name": "SQL", - "message": "Possible SQL injection", - "file": "app/models/concerns/dossier_filtering_concern.rb", - "line": 40, - "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", - "code": "where(\"#{values.count} OR #{\"(#{ProcedurePresentation.sanitized_column(table, column)} = ?)\"}\", *values)", - "render_path": null, - "location": { - "type": "method", - "class": "DossierFilteringConcern", - "method": null - }, - "user_input": "values.count", - "confidence": "Medium", - "cwe_id": [ - 89 - ], - "note": "filtered by rails query params where(something: ?, values)" - }, { "warning_type": "SQL Injection", "warning_code": 0, @@ -136,6 +113,29 @@ ], "note": "" }, + { + "warning_type": "SQL Injection", + "warning_code": 0, + "fingerprint": "91ff8031e7c639c95fe6c244867349a72078ef456d8b3507deaf2bdb9bf62fe2", + "check_name": "SQL", + "message": "Possible SQL injection", + "file": "app/models/concerns/dossier_filtering_concern.rb", + "line": 34, + "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", + "code": "where(\"#{values.count} OR #{\"(#{DossierFilterService.sanitized_column(table, column)} ILIKE ?)\"}\", *values.map do\n \"%#{value}%\"\n end)", + "render_path": null, + "location": { + "type": "method", + "class": "DossierFilteringConcern", + "method": null + }, + "user_input": "values.count", + "confidence": "Medium", + "cwe_id": [ + 89 + ], + "note": "filtered by rails query params where(something: ?, values)" + }, { "warning_type": "Cross-Site Scripting", "warning_code": 2, @@ -196,13 +196,13 @@ { "warning_type": "SQL Injection", "warning_code": 0, - "fingerprint": "bd1df30f95135357b646e21a03d95498874faffa32e3804fc643e9b6b957ee14", + "fingerprint": "aaff41afa7bd5a551cd2e3a385071090cb53c95caa40fad3785cd3d68c9b939c", "check_name": "SQL", "message": "Possible SQL injection", "file": "app/models/concerns/dossier_filtering_concern.rb", - "line": 34, + "line": 40, "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", - "code": "where(\"#{values.count} OR #{\"(#{ProcedurePresentation.sanitized_column(table, column)} ILIKE ?)\"}\", *values.map do\n \"%#{value}%\"\n end)", + "code": "where(\"#{values.count} OR #{\"(#{DossierFilterService.sanitized_column(table, column)} = ?)\"}\", *values)", "render_path": null, "location": { "type": "method", @@ -272,6 +272,6 @@ "note": "Current is not a model" } ], - "updated": "2024-09-24 20:56:24 +0200", + "updated": "2024-10-15 15:57:27 +0200", "brakeman_version": "6.1.2" } diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index ecf427de9..ac30ea06e 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -55,655 +55,6 @@ describe ProcedurePresentation do end end - describe '#sorted_ids' do - let(:instructeur) { create(:instructeur) } - let(:assign_to) { create(:assign_to, procedure:, instructeur:) } - let(:sorted_column) { SortedColumn.new(column:, order:) } - let(:procedure_presentation) { create(:procedure_presentation, assign_to:, sorted_column:) } - - subject { procedure_presentation.send(:sorted_ids, procedure.dossiers, procedure.dossiers.count) } - - context 'for notifications table' do - let(:column) { procedure.notifications_column } - - let!(:notified_dossier) { create(:dossier, :en_construction, procedure:) } - let!(:recent_dossier) { create(:dossier, :en_construction, procedure:) } - let!(:older_dossier) { create(:dossier, :en_construction, procedure:) } - - before do - notified_dossier.update!(last_champ_updated_at: Time.zone.local(2018, 9, 20)) - create(:follow, instructeur: instructeur, dossier: notified_dossier, demande_seen_at: Time.zone.local(2018, 9, 10)) - notified_dossier.touch(time: Time.zone.local(2018, 9, 20)) - recent_dossier.touch(time: Time.zone.local(2018, 9, 25)) - older_dossier.touch(time: Time.zone.local(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 - - context 'with a dossier terminé' do - let!(:notified_dossier) { create(:dossier, :accepte, procedure:) } - let(:order) { 'desc' } - - it { is_expected.to eq([notified_dossier, recent_dossier, older_dossier].map(&:id)) } - end - end - - context 'for self table' do - let(:order) { 'asc' } # Desc works the same, no extra test required - - context 'for created_at column' do - let!(:column) { procedure.find_column(label: 'Créé le') } - let!(:recent_dossier) { Timecop.freeze(Time.zone.local(2018, 10, 17)) { create(:dossier, procedure: procedure) } } - let!(:older_dossier) { Timecop.freeze(Time.zone.local(2003, 11, 11)) { create(:dossier, procedure: procedure) } } - - it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) } - end - - context 'for en_construction_at column' do - let!(:column) { procedure.find_column(label: 'En construction le') } - let!(:recent_dossier) { create(:dossier, :en_construction, procedure: procedure, en_construction_at: Time.zone.local(2018, 10, 17)) } - let!(:older_dossier) { create(:dossier, :en_construction, procedure: procedure, en_construction_at: Time.zone.local(2013, 1, 1)) } - - it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) } - end - - context 'for updated_at column' do - let(:column) { procedure.find_column(label: 'Mis à jour le') } - let(:recent_dossier) { create(:dossier, procedure: procedure) } - let(:older_dossier) { create(:dossier, procedure: procedure) } - - before do - recent_dossier.touch(time: Time.zone.local(2018, 9, 25)) - older_dossier.touch(time: Time.zone.local(2018, 5, 13)) - end - - it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) } - end - end - - context 'for type_de_champ table' do - context 'with no revisions' do - let(:column) { procedure.find_column(label: first_type_de_champ.libelle) } - - let(:beurre_dossier) { create(:dossier, procedure:) } - let(:tartine_dossier) { create(:dossier, procedure:) } - - before do - beurre_dossier.project_champs_public.first.update(value: 'beurre') - tartine_dossier.project_champs_public.first.update(value: 'tartine') - end - - context 'asc' do - let(:order) { 'asc' } - - it { is_expected.to eq([beurre_dossier, tartine_dossier].map(&:id)) } - end - - context 'desc' do - let(:order) { 'desc' } - - it { is_expected.to eq([tartine_dossier, beurre_dossier].map(&:id)) } - end - end - - context 'with a revision adding a new type_de_champ' do - let!(:tdc) { { type_champ: :text, libelle: 'nouveau champ' } } - let(:column) { procedure.find_column(label: 'nouveau champ') } - - let!(:nothing_dossier) { create(:dossier, procedure:) } - let!(:beurre_dossier) { create(:dossier, procedure:) } - let!(:tartine_dossier) { create(:dossier, procedure:) } - - before do - nothing_dossier - procedure.draft_revision.add_type_de_champ(tdc) - procedure.publish_revision! - beurre_dossier.project_champs_public.last.update(value: 'beurre') - tartine_dossier.project_champs_public.last.update(value: 'tartine') - end - - context 'asc' do - let(:order) { 'asc' } - it { is_expected.to eq([nothing_dossier, beurre_dossier, tartine_dossier].map(&:id)) } - end - - context 'desc' do - let(:order) { 'desc' } - it { is_expected.to eq([tartine_dossier, beurre_dossier, nothing_dossier].map(&:id)) } - end - end - end - - context 'for type_de_champ_private table' do - context 'with no revisions' do - let(:column) { procedure.find_column(label: procedure.active_revision.types_de_champ_private.first.libelle) } - - let(:biere_dossier) { create(:dossier, procedure: procedure) } - let(:vin_dossier) { create(:dossier, procedure: procedure) } - - before do - biere_dossier.project_champs_private.first.update(value: 'biere') - vin_dossier.project_champs_private.first.update(value: 'vin') - end - - context 'asc' do - let(:order) { 'asc' } - - it { is_expected.to eq([biere_dossier, vin_dossier].map(&:id)) } - end - - context 'desc' do - let(:order) { 'desc' } - - it { is_expected.to eq([vin_dossier, biere_dossier].map(&:id)) } - end - end - end - - context 'for individual table' do - let(:order) { 'asc' } # Desc works the same, no extra test required - - let(:procedure) { create(:procedure, :for_individual) } - - let!(:first_dossier) { create(:dossier, procedure: procedure, individual: build(:individual, gender: 'M', prenom: 'Alain', nom: 'Antonelli')) } - let!(:last_dossier) { create(:dossier, procedure: procedure, individual: build(:individual, gender: 'Mme', prenom: 'Zora', nom: 'Zemmour')) } - - context 'for gender column' do - let(:column) { procedure.find_column(label: 'Civilité') } - - it { is_expected.to eq([first_dossier, last_dossier].map(&:id)) } - end - - context 'for prenom column' do - let(:column) { procedure.find_column(label: 'Prénom') } - - it { is_expected.to eq([first_dossier, last_dossier].map(&:id)) } - end - - context 'for nom column' do - let(:column) { procedure.find_column(label: 'Nom') } - - it { is_expected.to eq([first_dossier, last_dossier].map(&:id)) } - end - end - - context 'for followers_instructeurs table' do - let(:order) { 'asc' } # Desc works the same, no extra test required - - let!(:dossier_z) { create(:dossier, :en_construction, procedure: procedure) } - let!(:dossier_a) { create(:dossier, :en_construction, procedure: procedure) } - let!(:dossier_without_instructeur) { create(:dossier, :en_construction, procedure: procedure) } - - before do - create(:follow, dossier: dossier_z, instructeur: create(:instructeur, email: 'zythum@exemple.fr')) - create(:follow, dossier: dossier_a, instructeur: create(:instructeur, email: 'abaca@exemple.fr')) - create(:follow, dossier: dossier_a, instructeur: create(:instructeur, email: 'abaca2@exemple.fr')) - end - - context 'for email column' do - let(:column) { procedure.find_column(label: 'Email instructeur') } - - it { is_expected.to eq([dossier_a, dossier_z, dossier_without_instructeur].map(&:id)) } - end - end - - context 'for avis table' do - let(:column) { procedure.find_column(label: 'Avis oui/non') } - let(:order) { 'asc' } - - let!(:dossier_yes) { create(:dossier, procedure:) } - let!(:dossier_no) { create(:dossier, procedure:) } - - before do - create_list(:avis, 2, dossier: dossier_yes, question_answer: true) - create(:avis, dossier: dossier_no, question_answer: true) - create(:avis, dossier: dossier_no, question_answer: false) - end - - it { is_expected.to eq([dossier_no, dossier_yes].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(:column) { procedure.find_column(label: '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:, suivis_filters: filtered_columns) } - let(:filtered_columns) { filters.map { to_filter(_1) } } - let(:filters) { [filter] } - - subject { procedure_presentation.send(:filtered_ids, procedure.dossiers.joins(:user), 'suivis') } - - context 'for self table' do - context 'for created_at column' do - let(:filter) { ['Créé le', '18/9/2018'] } - - let!(:kept_dossier) { create(:dossier, procedure: procedure, created_at: Time.zone.local(2018, 9, 18, 14, 28)) } - let!(:discarded_dossier) { create(:dossier, procedure: procedure, created_at: Time.zone.local(2018, 9, 17, 23, 59)) } - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'for en_construction_at column' do - let(:filter) { ['En construction le', '17/10/2018'] } - - let!(:kept_dossier) { create(:dossier, :en_construction, procedure: procedure, en_construction_at: Time.zone.local(2018, 10, 17)) } - let!(:discarded_dossier) { create(:dossier, :en_construction, procedure: procedure, en_construction_at: Time.zone.local(2013, 1, 1)) } - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'for updated_at column' do - let(:filter) { ['Mis à jour le', '18/9/2018'] } - - let(:kept_dossier) { create(:dossier, procedure: procedure) } - let(:discarded_dossier) { create(:dossier, procedure: procedure) } - - before do - kept_dossier.touch(time: Time.zone.local(2018, 9, 18, 14, 28)) - discarded_dossier.touch(time: Time.zone.local(2018, 9, 17, 23, 59)) - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'for updated_since column' do - let(:filter) { ['Mis à jour depuis', '18/9/2018'] } - - let(:kept_dossier) { create(:dossier, procedure: procedure) } - let(:later_dossier) { create(:dossier, procedure: procedure) } - let(:discarded_dossier) { create(:dossier, procedure: procedure) } - - before do - kept_dossier.touch(time: Time.zone.local(2018, 9, 18, 14, 28)) - later_dossier.touch(time: Time.zone.local(2018, 9, 19, 14, 28)) - discarded_dossier.touch(time: Time.zone.local(2018, 9, 17, 14, 28)) - end - - it { is_expected.to match_array([kept_dossier.id, later_dossier.id]) } - end - - context 'for sva_svr_decision_before column' do - before do - travel_to Time.zone.local(2023, 6, 10, 10) - end - - let(:procedure) { create(:procedure, :published, :sva, types_de_champ_public: [{}], types_de_champ_private: [{}]) } - let(:filter) { ['Date décision SVA avant', '15/06/2023'] } - - let!(:kept_dossier) { create(:dossier, :en_instruction, procedure:, sva_svr_decision_on: Date.current) } - let!(:later_dossier) { create(:dossier, :en_instruction, procedure:, sva_svr_decision_on: Date.current + 2.days) } - let!(:discarded_dossier) { create(:dossier, :en_instruction, procedure:, sva_svr_decision_on: Date.current + 10.days) } - let!(:en_construction_dossier) { create(:dossier, :en_construction, procedure:, sva_svr_decision_on: Date.current + 2.days) } - let!(:accepte_dossier) { create(:dossier, :accepte, procedure:, sva_svr_decision_on: Date.current + 2.days) } - - it { is_expected.to match_array([kept_dossier.id, later_dossier.id, en_construction_dossier.id]) } - end - - context 'ignore time of day' do - let(:filter) { ['En construction le', '17/10/2018 19:30'] } - - let!(:kept_dossier) { create(:dossier, :en_construction, procedure: procedure, en_construction_at: Time.zone.local(2018, 10, 17, 15, 56)) } - let!(:discarded_dossier) { create(:dossier, :en_construction, procedure: procedure, en_construction_at: Time.zone.local(2018, 10, 18, 5, 42)) } - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'for a malformed date' do - context 'when its a string' do - let(:filter) { ['Mis à jour le', 'malformed date'] } - - it { is_expected.to match([]) } - end - - context 'when its a number' do - let(:filter) { ['Mis à jour le', '177500'] } - - it { is_expected.to match([]) } - end - end - - context 'with multiple search values' do - let(:filters) { [['En construction le', '17/10/2018'], ['En construction le', '19/10/2018']] } - - let!(:kept_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 17)) } - let!(:other_kept_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 19)) } - let!(:discarded_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2013, 1, 1)) } - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - - context 'with multiple state filters' do - let(:filters) { [['Statut', 'en_construction'], ['Statut', 'en_instruction']] } - - let!(:kept_dossier) { create(:dossier, :en_construction, procedure:) } - let!(:other_kept_dossier) { create(:dossier, :en_instruction, procedure:) } - let!(:discarded_dossier) { create(:dossier, :accepte, procedure:) } - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - - context 'with en_construction state filters' do - let(:filter) { ['Statut', 'en_construction'] } - - let!(:en_construction) { create(:dossier, :en_construction, procedure:) } - let!(:en_construction_with_correction) { create(:dossier, :en_construction, procedure:) } - let!(:correction) { create(:dossier_correction, dossier: en_construction_with_correction) } - it 'excludes dossier en construction with pending correction' do - is_expected.to contain_exactly(en_construction.id) - end - end - end - - context 'for type_de_champ table' do - let(:filter) { [type_de_champ.libelle, 'keep'] } - - let(:kept_dossier) { create(:dossier, procedure: procedure) } - let(:discarded_dossier) { create(:dossier, procedure: procedure) } - let(:type_de_champ) { procedure.active_revision.types_de_champ_public.first } - - context 'with single value' do - before do - kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'keep me') - discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'discard me') - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'with multiple search values' do - let(:filters) { [[type_de_champ.libelle, 'keep'], [type_de_champ.libelle, 'and']] } - let(:other_kept_dossier) { create(:dossier, procedure: procedure) } - - before do - kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'keep me') - discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'discard me') - other_kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'and me too') - end - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - - context 'with yes_no type_de_champ' do - let(:filter) { [type_de_champ.libelle, 'true'] } - let(:types_de_champ_public) { [{ type: :yes_no }] } - - before do - kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'true') - discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'false') - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'with departement type_de_champ' do - let(:filter) { [type_de_champ.libelle, '13'] } - let(:types_de_champ_public) { [{ type: :departements }] } - - before do - kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(external_id: '13') - discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(external_id: '69') - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'with enum type_de_champ' do - let(:filter) { [type_de_champ.libelle, 'Favorable'] } - let(:types_de_champ_public) { [{ type: :drop_down_list, options: ['Favorable', 'Defavorable'] }] } - - before do - kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'Favorable') - discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(external_id: 'Defavorable') - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - end - - context 'for type_de_champ_private table' do - let(:filter) { [type_de_champ_private.libelle, 'keep'] } - - let(:kept_dossier) { create(:dossier, procedure: procedure) } - let(:discarded_dossier) { create(:dossier, procedure: procedure) } - let(:type_de_champ_private) { procedure.active_revision.types_de_champ_private.first } - - before do - kept_dossier.champs.find_by(stable_id: type_de_champ_private.stable_id).update(value: 'keep me') - discarded_dossier.champs.find_by(stable_id: type_de_champ_private.stable_id).update(value: 'discard me') - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'for type_de_champ using AddressableColumnConcern' do - let(:column) { filtered_columns.first.column } - let(:types_de_champ_public) { [{ type: :rna, stable_id: 1, libelle: 'rna' }] } - let(:type_de_champ) { procedure.active_revision.types_de_champ.first } - let(:kept_dossier) { create(:dossier, procedure: procedure) } - - context "when searching by postal_code (text)" do - let(:value) { "60580" } - let(:filter) { ["rna – code postal (5 chiffres)", value] } - - before do - kept_dossier.project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "postal_code" => value }) - create(:dossier, procedure: procedure).project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "postal_code" => "unknown" }) - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - - it 'describes column' do - expect(column.type).to eq(:text) - expect(column.options_for_select).to eq([]) - end - end - - context "when searching by departement_code (enum)" do - let(:value) { "99" } - let(:filter) { ["rna – département", value] } - - before do - kept_dossier.project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "departement_code" => value }) - create(:dossier, procedure: procedure).project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "departement_code" => "unknown" }) - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - - it 'describes column' do - expect(column.type).to eq(:enum) - expect(column.options_for_select.first).to eq(["99 – Etranger", "99"]) - end - end - - context "when searching by region_name" do - let(:value) { "60" } - let(:filter) { ["rna – region", value] } - - before do - kept_dossier.project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "region_name" => value }) - create(:dossier, procedure: procedure).project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "region_name" => "unknown" }) - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - - it 'describes column' do - expect(column.type).to eq(:enum) - expect(column.options_for_select.first).to eq(["Auvergne-Rhône-Alpes", "Auvergne-Rhône-Alpes"]) - end - end - end - - context 'for etablissement table' do - context 'for entreprise_date_creation column' do - let(:filter) { ['Date de création', '21/6/2018'] } - - let!(:kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: Time.zone.local(2018, 6, 21))) } - let!(:discarded_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: Time.zone.local(2008, 6, 21))) } - - it { is_expected.to contain_exactly(kept_dossier.id) } - - context 'with multiple search values' do - let(:filters) { [['Date de création', '21/6/2016'], ['Date de création', '21/6/2018']] } - - let!(:other_kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, entreprise_date_creation: Time.zone.local(2016, 6, 21))) } - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - end - - context 'for code_postal column' do - # All columns except entreprise_date_creation work exacly the same, just testing one - - let(:filter) { ['Code postal', '75017'] } - - 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')) } - - it { is_expected.to contain_exactly(kept_dossier.id) } - - context 'with multiple search values' do - let(:filters) { [['Code postal', '75017'], ['Code postal', '88100']] } - - let!(:other_kept_dossier) { create(:dossier, procedure: procedure, etablissement: create(:etablissement, code_postal: '88100')) } - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - end - end - - context 'for user table' do - let(:filter) { ['Demandeur', 'keepmail'] } - - 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')) } - - it { is_expected.to contain_exactly(kept_dossier.id) } - - context 'with multiple search values' do - let(:filters) { [['Demandeur', 'keepmail'], ['Demandeur', 'beta.gouv.fr']] } - - let!(:other_kept_dossier) { create(:dossier, procedure: procedure, user: create(:user, email: 'bazinga@beta.gouv.fr')) } - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - end - - context 'for individual table' do - let(:procedure) { create(:procedure, :for_individual) } - let!(:kept_dossier) { create(:dossier, procedure: procedure, individual: build(:individual, gender: 'Mme', prenom: 'Josephine', nom: 'Baker')) } - let!(:discarded_dossier) { create(:dossier, procedure: procedure, individual: build(:individual, gender: 'M', prenom: 'Jean', nom: 'Tremblay')) } - - context 'for gender column' do - let(:filter) { ['Civilité', 'Mme'] } - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'for prenom column' do - let(:filter) { ['Prénom', 'Josephine'] } - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'for nom column' do - let(:filter) { ['Nom', 'Baker'] } - - it { is_expected.to contain_exactly(kept_dossier.id) } - end - - context 'with multiple search values' do - let(:filters) { [['Prénom', 'Josephine'], ['Prénom', 'Romuald']] } - - let!(:other_kept_dossier) { create(:dossier, procedure: procedure, individual: build(:individual, gender: 'M', prenom: 'Romuald', nom: 'Pistis')) } - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - end - - context 'for followers_instructeurs table' do - let(:filter) { ['Email instructeur', 'keepmail'] } - - let!(:kept_dossier) { create(:dossier, procedure: procedure) } - let!(:discarded_dossier) { create(:dossier, procedure: procedure) } - - before do - create(:follow, dossier: kept_dossier, instructeur: create(:instructeur, email: 'me@keepmail.com')) - create(:follow, dossier: discarded_dossier, instructeur: create(:instructeur, email: 'me@discard.com')) - end - - it { is_expected.to contain_exactly(kept_dossier.id) } - - context 'with multiple search values' do - let(:filters) { [['Email instructeur', 'keepmail'], ['Email instructeur', 'beta.gouv.fr']] } - - let(:other_kept_dossier) { create(:dossier, procedure:) } - - before do - create(:follow, dossier: other_kept_dossier, instructeur: create(:instructeur, email: 'bazinga@beta.gouv.fr')) - end - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - end - - context 'for groupe_instructeur table' do - let(:filter) { ['Groupe instructeur', procedure.defaut_groupe_instructeur.id.to_s] } - - let!(:gi_2) { create(:groupe_instructeur, label: 'gi2', procedure:) } - let!(:gi_3) { create(:groupe_instructeur, label: 'gi3', procedure:) } - - let!(:kept_dossier) { create(:dossier, :en_construction, procedure:) } - let!(:discarded_dossier) { create(:dossier, :en_construction, procedure:, groupe_instructeur: gi_2) } - - it { is_expected.to contain_exactly(kept_dossier.id) } - - context 'with multiple search values' do - let(:filters) { [['Groupe instructeur', procedure.defaut_groupe_instructeur.id.to_s], ['Groupe instructeur', gi_3.id.to_s]] } - - let!(:other_kept_dossier) { create(:dossier, procedure:, groupe_instructeur: gi_3) } - - it 'returns every dossier that matches any of the search criteria for a given column' do - is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) - end - end - end - end - describe "#human_value_for_filter" do let(:filtered_column) { to_filter([first_type_de_champ.libelle, "true"]) } @@ -742,54 +93,6 @@ describe ProcedurePresentation do end end - describe '#filtered_sorted_ids' do - let(:procedure_presentation) { create(:procedure_presentation, assign_to:) } - - subject { procedure_presentation.filtered_sorted_ids(dossiers, statut) } - - context 'with no filters' do - let(:statut) { 'suivis' } - let(:dossiers) { procedure.dossiers } - - before do - create(:follow, dossier: en_construction_dossier, instructeur: procedure_presentation.instructeur) - create(:follow, dossier: accepte_dossier, instructeur: procedure_presentation.instructeur) - end - - let(:en_construction_dossier) { create(:dossier, :en_construction, procedure:) } - let(:accepte_dossier) { create(:dossier, :accepte, procedure:) } - - it { is_expected.to contain_exactly(en_construction_dossier.id) } - end - - context 'with mocked sorted_ids' do - let(:dossier_1) { create(:dossier) } - let(:dossier_2) { create(:dossier) } - let(:dossier_3) { create(:dossier) } - let(:dossiers) { Dossier.where(id: [dossier_1, dossier_2, dossier_3].map(&:id)) } - - let(:sorted_ids) { [dossier_2, dossier_3, dossier_1].map(&:id) } - let(:statut) { 'tous' } - - before do - expect(procedure_presentation).to receive(:sorted_ids).and_return(sorted_ids) - end - - it { is_expected.to eq(sorted_ids) } - - context 'when a filter is present' do - let(:filtered_ids) { [dossier_1, dossier_2, dossier_3].map(&:id) } - - before do - procedure_presentation.tous_filters = [to_filter(['Statut', 'en_construction'])] - expect(procedure_presentation).to receive(:filtered_ids).and_return(filtered_ids) - end - - it { is_expected.to eq(sorted_ids) } - end - end - end - describe '#update_displayed_fields' do let(:en_construction_column) { procedure.find_column(label: 'En construction le') } let(:mise_a_jour_column) { procedure.find_column(label: 'Mis à jour le') } diff --git a/spec/services/dossier_filter_service_spec.rb b/spec/services/dossier_filter_service_spec.rb new file mode 100644 index 000000000..95737dd25 --- /dev/null +++ b/spec/services/dossier_filter_service_spec.rb @@ -0,0 +1,709 @@ +# frozen_string_literal: true + +describe DossierFilterService do + def to_filter((label, filter)) = FilteredColumn.new(column: procedure.find_column(label:), filter:) + + describe '.filtered_sorted_ids' do + let(:procedure) { create(:procedure) } + let(:instructeur) { create(:instructeur) } + let(:dossiers) { procedure.dossiers } + let(:statut) { 'suivis' } + let(:filters) { [] } + let(:sorted_columns) { procedure.default_sorted_column } + + subject { described_class.filtered_sorted_ids(dossiers, statut, filters, sorted_columns, instructeur) } + + context 'with no filters' do + let(:en_construction_dossier) { create(:dossier, :en_construction, procedure:) } + let(:accepte_dossier) { create(:dossier, :accepte, procedure:) } + + before do + create(:follow, dossier: en_construction_dossier, instructeur:) + create(:follow, dossier: accepte_dossier, instructeur:) + end + + it { is_expected.to contain_exactly(en_construction_dossier.id) } + end + + context 'with mocked sorted_ids' do + let(:dossier_1) { create(:dossier) } + let(:dossier_2) { create(:dossier) } + let(:dossier_3) { create(:dossier) } + let(:dossiers) { Dossier.where(id: [dossier_1, dossier_2, dossier_3].map(&:id)) } + + let(:sorted_ids) { [dossier_2, dossier_3, dossier_1].map(&:id) } + + before do + expect(described_class).to receive(:sorted_ids).and_return(sorted_ids) + end + + it { is_expected.to eq(sorted_ids) } + + context 'when a filter is present' do + let(:filtered_ids) { [dossier_1, dossier_2, dossier_3].map(&:id) } + let(:filters) { [to_filter(['Statut', 'en_construction'])] } + + before do + expect(described_class).to receive(:filtered_ids).and_return(filtered_ids) + end + + it { is_expected.to eq(sorted_ids) } + end + end + end + + describe '#sorted_ids' do + let(:procedure) { create(:procedure, :published, types_de_champ_public:, types_de_champ_private: [{}]) } + let(:types_de_champ_public) { [{}] } + let(:first_type_de_champ) { assign_to.procedure.active_revision.types_de_champ_public.first } + let(:dossiers) { procedure.dossiers } + let(:instructeur) { create(:instructeur) } + let(:assign_to) { create(:assign_to, procedure:, instructeur:) } + let(:sorted_column) { SortedColumn.new(column:, order:) } + + subject { described_class.send(:sorted_ids, dossiers, sorted_column, instructeur, dossiers.count) } + + context 'for notifications table' do + let(:column) { procedure.notifications_column } + + let!(:notified_dossier) { create(:dossier, :en_construction, procedure:) } + let!(:recent_dossier) { create(:dossier, :en_construction, procedure:) } + let!(:older_dossier) { create(:dossier, :en_construction, procedure:) } + + before do + notified_dossier.update!(last_champ_updated_at: Time.zone.local(2018, 9, 20)) + create(:follow, instructeur: instructeur, dossier: notified_dossier, demande_seen_at: Time.zone.local(2018, 9, 10)) + notified_dossier.touch(time: Time.zone.local(2018, 9, 20)) + recent_dossier.touch(time: Time.zone.local(2018, 9, 25)) + older_dossier.touch(time: Time.zone.local(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 + + context 'with a dossier terminé' do + let!(:notified_dossier) { create(:dossier, :accepte, procedure:) } + let(:order) { 'desc' } + + it { is_expected.to eq([notified_dossier, recent_dossier, older_dossier].map(&:id)) } + end + end + + context 'for self table' do + let(:order) { 'asc' } # Desc works the same, no extra test required + + context 'for created_at column' do + let!(:column) { procedure.find_column(label: 'Créé le') } + let!(:recent_dossier) { Timecop.freeze(Time.zone.local(2018, 10, 17)) { create(:dossier, procedure:) } } + let!(:older_dossier) { Timecop.freeze(Time.zone.local(2003, 11, 11)) { create(:dossier, procedure:) } } + + it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) } + end + + context 'for en_construction_at column' do + let!(:column) { procedure.find_column(label: 'En construction le') } + let!(:recent_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 17)) } + let!(:older_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2013, 1, 1)) } + + it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) } + end + + context 'for updated_at column' do + let(:column) { procedure.find_column(label: 'Mis à jour le') } + let(:recent_dossier) { create(:dossier, procedure:) } + let(:older_dossier) { create(:dossier, procedure:) } + + before do + recent_dossier.touch(time: Time.zone.local(2018, 9, 25)) + older_dossier.touch(time: Time.zone.local(2018, 5, 13)) + end + + it { is_expected.to eq([older_dossier, recent_dossier].map(&:id)) } + end + end + + context 'for type_de_champ table' do + context 'with no revisions' do + let(:column) { procedure.find_column(label: first_type_de_champ.libelle) } + + let(:beurre_dossier) { create(:dossier, procedure:) } + let(:tartine_dossier) { create(:dossier, procedure:) } + + before do + beurre_dossier.project_champs_public.first.update(value: 'beurre') + tartine_dossier.project_champs_public.first.update(value: 'tartine') + end + + context 'asc' do + let(:order) { 'asc' } + + it { is_expected.to eq([beurre_dossier, tartine_dossier].map(&:id)) } + end + + context 'desc' do + let(:order) { 'desc' } + + it { is_expected.to eq([tartine_dossier, beurre_dossier].map(&:id)) } + end + end + + context 'with a revision adding a new type_de_champ' do + let!(:tdc) { { type_champ: :text, libelle: 'nouveau champ' } } + let(:column) { procedure.find_column(label: 'nouveau champ') } + + let!(:nothing_dossier) { create(:dossier, procedure:) } + let!(:beurre_dossier) { create(:dossier, procedure:) } + let!(:tartine_dossier) { create(:dossier, procedure:) } + + before do + nothing_dossier + procedure.draft_revision.add_type_de_champ(tdc) + procedure.publish_revision! + beurre_dossier.project_champs_public.last.update(value: 'beurre') + tartine_dossier.project_champs_public.last.update(value: 'tartine') + end + + context 'asc' do + let(:order) { 'asc' } + it { is_expected.to eq([nothing_dossier, beurre_dossier, tartine_dossier].map(&:id)) } + end + + context 'desc' do + let(:order) { 'desc' } + it { is_expected.to eq([tartine_dossier, beurre_dossier, nothing_dossier].map(&:id)) } + end + end + end + + context 'for type_de_champ_private table' do + context 'with no revisions' do + let(:column) { procedure.find_column(label: procedure.active_revision.types_de_champ_private.first.libelle) } + + let(:biere_dossier) { create(:dossier, procedure:) } + let(:vin_dossier) { create(:dossier, procedure:) } + + before do + biere_dossier.project_champs_private.first.update(value: 'biere') + vin_dossier.project_champs_private.first.update(value: 'vin') + end + + context 'asc' do + let(:order) { 'asc' } + + it { is_expected.to eq([biere_dossier, vin_dossier].map(&:id)) } + end + + context 'desc' do + let(:order) { 'desc' } + + it { is_expected.to eq([vin_dossier, biere_dossier].map(&:id)) } + end + end + end + + context 'for individual table' do + let(:order) { 'asc' } # Desc works the same, no extra test required + + let(:procedure) { create(:procedure, :for_individual) } + + let!(:first_dossier) { create(:dossier, procedure:, individual: build(:individual, gender: 'M', prenom: 'Alain', nom: 'Antonelli')) } + let!(:last_dossier) { create(:dossier, procedure:, individual: build(:individual, gender: 'Mme', prenom: 'Zora', nom: 'Zemmour')) } + + context 'for gender column' do + let(:column) { procedure.find_column(label: 'Civilité') } + + it { is_expected.to eq([first_dossier, last_dossier].map(&:id)) } + end + + context 'for prenom column' do + let(:column) { procedure.find_column(label: 'Prénom') } + + it { is_expected.to eq([first_dossier, last_dossier].map(&:id)) } + end + + context 'for nom column' do + let(:column) { procedure.find_column(label: 'Nom') } + + it { is_expected.to eq([first_dossier, last_dossier].map(&:id)) } + end + end + + context 'for followers_instructeurs table' do + let(:order) { 'asc' } # Desc works the same, no extra test required + + let!(:dossier_z) { create(:dossier, :en_construction, procedure:) } + let!(:dossier_a) { create(:dossier, :en_construction, procedure:) } + let!(:dossier_without_instructeur) { create(:dossier, :en_construction, procedure:) } + + before do + create(:follow, dossier: dossier_z, instructeur: create(:instructeur, email: 'zythum@exemple.fr')) + create(:follow, dossier: dossier_a, instructeur: create(:instructeur, email: 'abaca@exemple.fr')) + create(:follow, dossier: dossier_a, instructeur: create(:instructeur, email: 'abaca2@exemple.fr')) + end + + context 'for email column' do + let(:column) { procedure.find_column(label: 'Email instructeur') } + + it { is_expected.to eq([dossier_a, dossier_z, dossier_without_instructeur].map(&:id)) } + end + end + + context 'for avis table' do + let(:column) { procedure.find_column(label: 'Avis oui/non') } + let(:order) { 'asc' } + + let!(:dossier_yes) { create(:dossier, procedure:) } + let!(:dossier_no) { create(:dossier, procedure:) } + + before do + create_list(:avis, 2, dossier: dossier_yes, question_answer: true) + create(:avis, dossier: dossier_no, question_answer: true) + create(:avis, dossier: dossier_no, question_answer: false) + end + + it { is_expected.to eq([dossier_no, dossier_yes].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(:column) { procedure.find_column(label: 'Code postal') } + let(:order) { 'asc' } # Desc works the same, no extra test required + + let!(:huitieme_dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, code_postal: '75008')) } + let!(:vingtieme_dossier) { create(:dossier, 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) { create(:procedure, types_de_champ_public:, types_de_champ_private:) } + let(:types_de_champ_public) { [{}] } + let(:types_de_champ_private) { [{}] } + let(:dossiers) { procedure.dossiers } + let(:filtered_columns) { filters.map { to_filter(_1) } } + let(:filters) { [filter] } + + subject { described_class.send(:filtered_ids, dossiers.joins(:user), filtered_columns) } + + context 'for self table' do + context 'for created_at column' do + let(:filter) { ['Créé le', '18/9/2018'] } + + let!(:kept_dossier) { create(:dossier, procedure:, created_at: Time.zone.local(2018, 9, 18, 14, 28)) } + let!(:discarded_dossier) { create(:dossier, procedure:, created_at: Time.zone.local(2018, 9, 17, 23, 59)) } + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'for en_construction_at column' do + let(:filter) { ['En construction le', '17/10/2018'] } + + let!(:kept_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 17)) } + let!(:discarded_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2013, 1, 1)) } + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'for updated_at column' do + let(:filter) { ['Mis à jour le', '18/9/2018'] } + + let(:kept_dossier) { create(:dossier, procedure:) } + let(:discarded_dossier) { create(:dossier, procedure:) } + + before do + kept_dossier.touch(time: Time.zone.local(2018, 9, 18, 14, 28)) + discarded_dossier.touch(time: Time.zone.local(2018, 9, 17, 23, 59)) + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'for updated_since column' do + let(:filter) { ['Mis à jour depuis', '18/9/2018'] } + + let(:kept_dossier) { create(:dossier, procedure:) } + let(:later_dossier) { create(:dossier, procedure:) } + let(:discarded_dossier) { create(:dossier, procedure:) } + + before do + kept_dossier.touch(time: Time.zone.local(2018, 9, 18, 14, 28)) + later_dossier.touch(time: Time.zone.local(2018, 9, 19, 14, 28)) + discarded_dossier.touch(time: Time.zone.local(2018, 9, 17, 14, 28)) + end + + it { is_expected.to match_array([kept_dossier.id, later_dossier.id]) } + end + + context 'for sva_svr_decision_before column' do + before do + travel_to Time.zone.local(2023, 6, 10, 10) + end + + let(:procedure) { create(:procedure, :published, :sva, types_de_champ_public: [{}], types_de_champ_private: [{}]) } + let(:filter) { ['Date décision SVA avant', '15/06/2023'] } + + let!(:kept_dossier) { create(:dossier, :en_instruction, procedure:, sva_svr_decision_on: Date.current) } + let!(:later_dossier) { create(:dossier, :en_instruction, procedure:, sva_svr_decision_on: Date.current + 2.days) } + let!(:discarded_dossier) { create(:dossier, :en_instruction, procedure:, sva_svr_decision_on: Date.current + 10.days) } + let!(:en_construction_dossier) { create(:dossier, :en_construction, procedure:, sva_svr_decision_on: Date.current + 2.days) } + let!(:accepte_dossier) { create(:dossier, :accepte, procedure:, sva_svr_decision_on: Date.current + 2.days) } + + it { is_expected.to match_array([kept_dossier.id, later_dossier.id, en_construction_dossier.id]) } + end + + context 'ignore time of day' do + let(:filter) { ['En construction le', '17/10/2018 19:30'] } + + let!(:kept_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 17, 15, 56)) } + let!(:discarded_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 18, 5, 42)) } + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'for a malformed date' do + context 'when its a string' do + let(:filter) { ['Mis à jour le', 'malformed date'] } + + it { is_expected.to match([]) } + end + + context 'when its a number' do + let(:filter) { ['Mis à jour le', '177500'] } + + it { is_expected.to match([]) } + end + end + + context 'with multiple search values' do + let(:filters) { [['En construction le', '17/10/2018'], ['En construction le', '19/10/2018']] } + + let!(:kept_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 17)) } + let!(:other_kept_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2018, 10, 19)) } + let!(:discarded_dossier) { create(:dossier, :en_construction, procedure:, en_construction_at: Time.zone.local(2013, 1, 1)) } + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + + context 'with multiple state filters' do + let(:filters) { [['Statut', 'en_construction'], ['Statut', 'en_instruction']] } + + let!(:kept_dossier) { create(:dossier, :en_construction, procedure:) } + let!(:other_kept_dossier) { create(:dossier, :en_instruction, procedure:) } + let!(:discarded_dossier) { create(:dossier, :accepte, procedure:) } + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + + context 'with en_construction state filters' do + let(:filter) { ['Statut', 'en_construction'] } + + let!(:en_construction) { create(:dossier, :en_construction, procedure:) } + let!(:en_construction_with_correction) { create(:dossier, :en_construction, procedure:) } + let!(:correction) { create(:dossier_correction, dossier: en_construction_with_correction) } + it 'excludes dossier en construction with pending correction' do + is_expected.to contain_exactly(en_construction.id) + end + end + end + + context 'for type_de_champ table' do + let(:filter) { [type_de_champ.libelle, 'keep'] } + + let(:kept_dossier) { create(:dossier, procedure:) } + let(:discarded_dossier) { create(:dossier, procedure:) } + let(:type_de_champ) { procedure.active_revision.types_de_champ_public.first } + + context 'with single value' do + before do + kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'keep me') + discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'discard me') + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'with multiple search values' do + let(:filters) { [[type_de_champ.libelle, 'keep'], [type_de_champ.libelle, 'and']] } + let(:other_kept_dossier) { create(:dossier, procedure:) } + + before do + kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'keep me') + discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'discard me') + other_kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'and me too') + end + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + + context 'with yes_no type_de_champ' do + let(:filter) { [type_de_champ.libelle, 'true'] } + let(:types_de_champ_public) { [{ type: :yes_no }] } + + before do + kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'true') + discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'false') + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'with departement type_de_champ' do + let(:filter) { [type_de_champ.libelle, '13'] } + let(:types_de_champ_public) { [{ type: :departements }] } + + before do + kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(external_id: '13') + discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(external_id: '69') + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'with enum type_de_champ' do + let(:filter) { [type_de_champ.libelle, 'Favorable'] } + let(:types_de_champ_public) { [{ type: :drop_down_list, options: ['Favorable', 'Defavorable'] }] } + + before do + kept_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(value: 'Favorable') + discarded_dossier.champs.find_by(stable_id: type_de_champ.stable_id).update(external_id: 'Defavorable') + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + end + + context 'for type_de_champ_private table' do + let(:filter) { [type_de_champ_private.libelle, 'keep'] } + + let(:kept_dossier) { create(:dossier, procedure:) } + let(:discarded_dossier) { create(:dossier, procedure:) } + let(:type_de_champ_private) { procedure.active_revision.types_de_champ_private.first } + + before do + kept_dossier.champs.find_by(stable_id: type_de_champ_private.stable_id).update(value: 'keep me') + discarded_dossier.champs.find_by(stable_id: type_de_champ_private.stable_id).update(value: 'discard me') + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'for type_de_champ using AddressableColumnConcern' do + let(:column) { filtered_columns.first.column } + let(:types_de_champ_public) { [{ type: :rna, stable_id: 1, libelle: 'rna' }] } + let(:type_de_champ) { procedure.active_revision.types_de_champ.first } + let(:kept_dossier) { create(:dossier, procedure:) } + + context "when searching by postal_code (text)" do + let(:value) { "60580" } + let(:filter) { ["rna – code postal (5 chiffres)", value] } + + before do + kept_dossier.project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "postal_code" => value }) + create(:dossier, procedure:).project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "postal_code" => "unknown" }) + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + + it 'describes column' do + expect(column.type).to eq(:text) + expect(column.options_for_select).to eq([]) + end + end + + context "when searching by departement_code (enum)" do + let(:value) { "99" } + let(:filter) { ["rna – département", value] } + + before do + kept_dossier.project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "departement_code" => value }) + create(:dossier, procedure:).project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "departement_code" => "unknown" }) + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + + it 'describes column' do + expect(column.type).to eq(:enum) + expect(column.options_for_select.first).to eq(["99 – Etranger", "99"]) + end + end + + context "when searching by region_name" do + let(:value) { "60" } + let(:filter) { ["rna – region", value] } + + before do + kept_dossier.project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "region_name" => value }) + create(:dossier, procedure:).project_champs_public.find { _1.stable_id == 1 }.update(value_json: { "region_name" => "unknown" }) + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + + it 'describes column' do + expect(column.type).to eq(:enum) + expect(column.options_for_select.first).to eq(["Auvergne-Rhône-Alpes", "Auvergne-Rhône-Alpes"]) + end + end + end + + context 'for etablissement table' do + context 'for entreprise_date_creation column' do + let(:filter) { ['Date de création', '21/6/2018'] } + + let!(:kept_dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, entreprise_date_creation: Time.zone.local(2018, 6, 21))) } + let!(:discarded_dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, entreprise_date_creation: Time.zone.local(2008, 6, 21))) } + + it { is_expected.to contain_exactly(kept_dossier.id) } + + context 'with multiple search values' do + let(:filters) { [['Date de création', '21/6/2016'], ['Date de création', '21/6/2018']] } + + let!(:other_kept_dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, entreprise_date_creation: Time.zone.local(2016, 6, 21))) } + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + end + + context 'for code_postal column' do + # All columns except entreprise_date_creation work exacly the same, just testing one + + let(:filter) { ['Code postal', '75017'] } + + let!(:kept_dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, code_postal: '75017')) } + let!(:discarded_dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, code_postal: '25000')) } + + it { is_expected.to contain_exactly(kept_dossier.id) } + + context 'with multiple search values' do + let(:filters) { [['Code postal', '75017'], ['Code postal', '88100']] } + + let!(:other_kept_dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, code_postal: '88100')) } + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + end + end + + context 'for user table' do + let(:filter) { ['Demandeur', 'keepmail'] } + + let!(:kept_dossier) { create(:dossier, procedure:, user: create(:user, email: 'me@keepmail.com')) } + let!(:discarded_dossier) { create(:dossier, procedure:, user: create(:user, email: 'me@discard.com')) } + + it { is_expected.to contain_exactly(kept_dossier.id) } + + context 'with multiple search values' do + let(:filters) { [['Demandeur', 'keepmail'], ['Demandeur', 'beta.gouv.fr']] } + + let!(:other_kept_dossier) { create(:dossier, procedure:, user: create(:user, email: 'bazinga@beta.gouv.fr')) } + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + end + + context 'for individual table' do + let(:procedure) { create(:procedure, :for_individual) } + let!(:kept_dossier) { create(:dossier, procedure:, individual: build(:individual, gender: 'Mme', prenom: 'Josephine', nom: 'Baker')) } + let!(:discarded_dossier) { create(:dossier, procedure:, individual: build(:individual, gender: 'M', prenom: 'Jean', nom: 'Tremblay')) } + + context 'for gender column' do + let(:filter) { ['Civilité', 'Mme'] } + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'for prenom column' do + let(:filter) { ['Prénom', 'Josephine'] } + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'for nom column' do + let(:filter) { ['Nom', 'Baker'] } + + it { is_expected.to contain_exactly(kept_dossier.id) } + end + + context 'with multiple search values' do + let(:filters) { [['Prénom', 'Josephine'], ['Prénom', 'Romuald']] } + + let!(:other_kept_dossier) { create(:dossier, procedure:, individual: build(:individual, gender: 'M', prenom: 'Romuald', nom: 'Pistis')) } + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + end + + context 'for followers_instructeurs table' do + let(:filter) { ['Email instructeur', 'keepmail'] } + + let!(:kept_dossier) { create(:dossier, procedure:) } + let!(:discarded_dossier) { create(:dossier, procedure:) } + + before do + create(:follow, dossier: kept_dossier, instructeur: create(:instructeur, email: 'me@keepmail.com')) + create(:follow, dossier: discarded_dossier, instructeur: create(:instructeur, email: 'me@discard.com')) + end + + it { is_expected.to contain_exactly(kept_dossier.id) } + + context 'with multiple search values' do + let(:filters) { [['Email instructeur', 'keepmail'], ['Email instructeur', 'beta.gouv.fr']] } + + let(:other_kept_dossier) { create(:dossier, procedure:) } + + before do + create(:follow, dossier: other_kept_dossier, instructeur: create(:instructeur, email: 'bazinga@beta.gouv.fr')) + end + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + end + + context 'for groupe_instructeur table' do + let(:filter) { ['Groupe instructeur', procedure.defaut_groupe_instructeur.id.to_s] } + + let!(:gi_2) { create(:groupe_instructeur, label: 'gi2', procedure:) } + let!(:gi_3) { create(:groupe_instructeur, label: 'gi3', procedure:) } + + let!(:kept_dossier) { create(:dossier, :en_construction, procedure:) } + let!(:discarded_dossier) { create(:dossier, :en_construction, procedure:, groupe_instructeur: gi_2) } + + it { is_expected.to contain_exactly(kept_dossier.id) } + + context 'with multiple search values' do + let(:filters) { [['Groupe instructeur', procedure.defaut_groupe_instructeur.id.to_s], ['Groupe instructeur', gi_3.id.to_s]] } + + let!(:other_kept_dossier) { create(:dossier, procedure:, groupe_instructeur: gi_3) } + + it 'returns every dossier that matches any of the search criteria for a given column' do + is_expected.to contain_exactly(kept_dossier.id, other_kept_dossier.id) + end + end + end + end +end