Merge pull request #3765 from betagouv/fix-3464-filter-by-followers_gestionnaires

Instructeur : permet de filtrer des dossiers par email de l’instructeur
This commit is contained in:
Pierre de La Morinerie 2019-04-10 17:48:03 +02:00 committed by GitHub
commit dda3dbd8ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 6 deletions

View file

@ -18,7 +18,8 @@ class ProcedurePresentation < ApplicationRecord
field_hash('Créé le', 'self', 'created_at'), field_hash('Créé le', 'self', 'created_at'),
field_hash('En construction le', 'self', 'en_construction_at'), field_hash('En construction le', 'self', 'en_construction_at'),
field_hash('Mis à jour le', 'self', 'updated_at'), field_hash('Mis à jour le', 'self', 'updated_at'),
field_hash('Demandeur', 'user', 'email') field_hash('Demandeur', 'user', 'email'),
field_hash('Email instructeur', 'followers_gestionnaires', 'email')
] ]
if procedure.for_individual if procedure.for_individual
@ -92,7 +93,7 @@ class ProcedurePresentation < ApplicationRecord
.where("champs.type_de_champ_id = #{column.to_i}") .where("champs.type_de_champ_id = #{column.to_i}")
.order("champs.value #{order}") .order("champs.value #{order}")
.pluck(:id) .pluck(:id)
when 'self', 'user', 'individual', 'etablissement' when 'self', 'user', 'individual', 'etablissement', 'followers_gestionnaires'
return (table == 'self' ? dossiers : dossiers.includes(table)) return (table == 'self' ? dossiers : dossiers.includes(table))
.order("#{self.class.sanitized_column(table, column)} #{order}") .order("#{self.class.sanitized_column(table, column)} #{order}")
.pluck(:id) .pluck(:id)
@ -114,7 +115,7 @@ class ProcedurePresentation < ApplicationRecord
dossiers dossiers
.includes(relation) .includes(relation)
.where("champs.type_de_champ_id = ?", column.to_i) .where("champs.type_de_champ_id = ?", column.to_i)
.filter_ilike(:champ, :value, values) .filter_ilike(relation, :value, values)
when 'etablissement' when 'etablissement'
if column == 'entreprise_date_creation' if column == 'entreprise_date_creation'
dates = values dates = values
@ -128,7 +129,7 @@ class ProcedurePresentation < ApplicationRecord
.includes(table) .includes(table)
.filter_ilike(table, column, values) .filter_ilike(table, column, values)
end end
when 'user', 'individual' when 'user', 'individual', 'followers_gestionnaires'
dossiers dossiers
.includes(table) .includes(table)
.filter_ilike(table, column, values) .filter_ilike(table, column, values)
@ -201,6 +202,8 @@ class ProcedurePresentation < ApplicationRecord
dossier.send(column)&.strftime('%d/%m/%Y') dossier.send(column)&.strftime('%d/%m/%Y')
when 'user', 'individual', 'etablissement' when 'user', 'individual', 'etablissement'
dossier.send(table)&.send(column) dossier.send(table)&.send(column)
when 'followers_gestionnaires'
dossier.send(table)&.map { |g| g.send(column) }&.join(', ')
when 'type_de_champ' when 'type_de_champ'
dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value
when 'type_de_champ_private' when 'type_de_champ_private'
@ -230,8 +233,14 @@ class ProcedurePresentation < ApplicationRecord
@column_whitelist[table] || [] @column_whitelist[table] || []
end end
def self.sanitized_column(table, column) def self.sanitized_column(association, column)
[(table == 'self' ? 'dossier' : table.to_s).pluralize, column] table = if association == 'self'
Dossier.table_name
else
Dossier.reflect_on_association(association).klass.table_name
end
[table, column]
.map { |name| ActiveRecord::Base.connection.quote_column_name(name) } .map { |name| ActiveRecord::Base.connection.quote_column_name(name) }
.join('.') .join('.')
end end

View file

@ -61,6 +61,7 @@ describe ProcedurePresentation do
{ "label" => 'En construction le', "table" => 'self', "column" => 'en_construction_at' }, { "label" => 'En construction le', "table" => 'self', "column" => 'en_construction_at' },
{ "label" => 'Mis à jour le', "table" => 'self', "column" => 'updated_at' }, { "label" => 'Mis à jour le', "table" => 'self', "column" => 'updated_at' },
{ "label" => 'Demandeur', "table" => 'user', "column" => 'email' }, { "label" => 'Demandeur', "table" => 'user', "column" => 'email' },
{ "label" => 'Email instructeur', "table" => 'followers_gestionnaires', "column" => 'email' },
{ "label" => 'SIREN', "table" => 'etablissement', "column" => 'entreprise_siren' }, { "label" => 'SIREN', "table" => 'etablissement', "column" => 'entreprise_siren' },
{ "label" => 'Forme juridique', "table" => 'etablissement', "column" => 'entreprise_forme_juridique' }, { "label" => 'Forme juridique', "table" => 'etablissement', "column" => 'entreprise_forme_juridique' },
{ "label" => 'Nom commercial', "table" => 'etablissement', "column" => 'entreprise_nom_commercial' }, { "label" => 'Nom commercial', "table" => 'etablissement', "column" => 'entreprise_nom_commercial' },
@ -196,6 +197,17 @@ describe ProcedurePresentation do
it { is_expected.to eq('75008') } it { is_expected.to eq('75008') }
end end
context 'for followers_gestionnaires table' do
let(:table) { 'followers_gestionnaires' }
let(:column) { 'email' }
let(:dossier) { create(:dossier, procedure: procedure) }
let!(:follow1) { create(:follow, dossier: dossier, gestionnaire: create(:gestionnaire, email: 'user1@host')) }
let!(:follow2) { create(:follow, dossier: dossier, gestionnaire: create(:gestionnaire, email: 'user2@host')) }
it { is_expected.to eq('user1@host, user2@host') }
end
context 'for type_de_champ table' do context 'for type_de_champ table' do
let(:table) { 'type_de_champ' } let(:table) { 'type_de_champ' }
let(:column) { procedure.types_de_champ.first.id.to_s } let(:column) { procedure.types_de_champ.first.id.to_s }
@ -631,6 +643,39 @@ describe ProcedurePresentation do
end end
end end
end end
context 'for followers_gestionnaires table' do
let(:filter) { [{ 'table' => 'followers_gestionnaires', 'column' => 'email', 'value' => 'keepmail' }] }
let!(:kept_dossier) { create(:dossier, procedure: procedure) }
let!(:discarded_dossier) { create(:dossier, procedure: procedure) }
before do
create(:follow, dossier: kept_dossier, gestionnaire: create(:gestionnaire, email: 'me@keepmail.com'))
create(:follow, dossier: discarded_dossier, gestionnaire: create(:gestionnaire, email: 'me@discard.com'))
end
it { is_expected.to contain_exactly(kept_dossier.id) }
context 'with multiple search values' do
let(:filter) do
[
{ 'table' => 'followers_gestionnaires', 'column' => 'email', 'value' => 'keepmail' },
{ 'table' => 'followers_gestionnaires', 'column' => 'email', 'value' => 'beta.gouv.fr' }
]
end
let(:other_kept_dossier) { create(:dossier, procedure: procedure) }
before do
create(:follow, dossier: other_kept_dossier, gestionnaire: create(:gestionnaire, 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
end end
describe '#eager_load_displayed_fields' do describe '#eager_load_displayed_fields' do
@ -650,6 +695,7 @@ describe ProcedurePresentation do
expect(displayed_dossier.association(:user)).not_to be_loaded expect(displayed_dossier.association(:user)).not_to be_loaded
expect(displayed_dossier.association(:individual)).not_to be_loaded expect(displayed_dossier.association(:individual)).not_to be_loaded
expect(displayed_dossier.association(:etablissement)).not_to be_loaded expect(displayed_dossier.association(:etablissement)).not_to be_loaded
expect(displayed_dossier.association(:followers_gestionnaires)).not_to be_loaded
end end
end end
@ -665,6 +711,7 @@ describe ProcedurePresentation do
expect(displayed_dossier.association(:user)).not_to be_loaded expect(displayed_dossier.association(:user)).not_to be_loaded
expect(displayed_dossier.association(:individual)).not_to be_loaded expect(displayed_dossier.association(:individual)).not_to be_loaded
expect(displayed_dossier.association(:etablissement)).not_to be_loaded expect(displayed_dossier.association(:etablissement)).not_to be_loaded
expect(displayed_dossier.association(:followers_gestionnaires)).not_to be_loaded
end end
end end
@ -678,6 +725,7 @@ describe ProcedurePresentation do
expect(displayed_dossier.association(:user)).to be_loaded expect(displayed_dossier.association(:user)).to be_loaded
expect(displayed_dossier.association(:individual)).not_to be_loaded expect(displayed_dossier.association(:individual)).not_to be_loaded
expect(displayed_dossier.association(:etablissement)).not_to be_loaded expect(displayed_dossier.association(:etablissement)).not_to be_loaded
expect(displayed_dossier.association(:followers_gestionnaires)).not_to be_loaded
end end
end end
@ -691,6 +739,7 @@ describe ProcedurePresentation do
expect(displayed_dossier.association(:user)).not_to be_loaded expect(displayed_dossier.association(:user)).not_to be_loaded
expect(displayed_dossier.association(:individual)).to be_loaded expect(displayed_dossier.association(:individual)).to be_loaded
expect(displayed_dossier.association(:etablissement)).not_to be_loaded expect(displayed_dossier.association(:etablissement)).not_to be_loaded
expect(displayed_dossier.association(:followers_gestionnaires)).not_to be_loaded
end end
end end
@ -704,6 +753,21 @@ describe ProcedurePresentation do
expect(displayed_dossier.association(:user)).not_to be_loaded expect(displayed_dossier.association(:user)).not_to be_loaded
expect(displayed_dossier.association(:individual)).not_to be_loaded expect(displayed_dossier.association(:individual)).not_to be_loaded
expect(displayed_dossier.association(:etablissement)).to be_loaded expect(displayed_dossier.association(:etablissement)).to be_loaded
expect(displayed_dossier.association(:followers_gestionnaires)).not_to be_loaded
end
end
context 'for followers_gestionnaires' do
let(:table) { 'followers_gestionnaires' }
let(:column) { 'email' }
it 'preloads the followers_gestionnaires relation' do
expect(displayed_dossier.association(:champs)).not_to be_loaded
expect(displayed_dossier.association(:champs_private)).not_to be_loaded
expect(displayed_dossier.association(:user)).not_to be_loaded
expect(displayed_dossier.association(:individual)).not_to be_loaded
expect(displayed_dossier.association(:etablissement)).not_to be_loaded
expect(displayed_dossier.association(:followers_gestionnaires)).to be_loaded
end end
end end
@ -717,6 +781,7 @@ describe ProcedurePresentation do
expect(displayed_dossier.association(:user)).not_to be_loaded expect(displayed_dossier.association(:user)).not_to be_loaded
expect(displayed_dossier.association(:individual)).not_to be_loaded expect(displayed_dossier.association(:individual)).not_to be_loaded
expect(displayed_dossier.association(:etablissement)).not_to be_loaded expect(displayed_dossier.association(:etablissement)).not_to be_loaded
expect(displayed_dossier.association(:followers_gestionnaires)).not_to be_loaded
end end
end end
end end