Merge pull request #2646 from betagouv/frederic/fix_2613-filtrage_date_creation_entreprise

fix filtrage date creation entreprise
This commit is contained in:
gregoirenovel 2018-09-25 08:01:03 +02:00 committed by GitHub
commit 4bb00e9875
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 9 deletions

View file

@ -51,8 +51,6 @@ class DossierFieldService
dossier.user.send(column)
when 'france_connect_information'
dossier.user.france_connect_information&.send(column)
when 'entreprise'
dossier.etablissement&.send(:"entreprise_#{column}")
when 'etablissement'
dossier.etablissement&.send(column)
when 'type_de_champ'
@ -79,19 +77,19 @@ class DossierFieldService
.includes(relation)
.where("champs.type_de_champ_id = ?", filter['column'].to_i)
.where("champs.value ILIKE ?", "%#{filter['value']}%")
when 'entreprise'
table = 'etablissement'
if filter['column'] == 'date_creation'
when 'etablissement'
table = filter['table']
if filter['column'] == 'entreprise_date_creation'
date = filter['value'].to_date rescue nil
dossiers
.includes(table)
.where("#{table.pluralize}.entreprise_#{filter['column']} = ?", date)
.where("#{table.pluralize}.#{filter['column']} = ?", date)
else
dossiers
.includes(table)
.where("#{table.pluralize}.entreprise_#{filter['column']} ILIKE ?", "%#{filter['value']}%")
.where("#{table.pluralize}.#{filter['column']} ILIKE ?", "%#{filter['value']}%")
end
when 'user', 'etablissement'
when 'user'
dossiers
.includes(filter['table'])
.where("#{filter['table'].pluralize}.#{filter['column']} ILIKE ?", "%#{filter['value']}%")

View file

@ -0,0 +1,51 @@
namespace :'2018_09_20_procedure_presentation_entreprise' do
task run: :environment do
Class.new do
def run
fix_displayed_fields
fix_sort
fix_filters
end
def fix_displayed_fields
ProcedurePresentation.where(%q`displayed_fields @> '[{"table": "entreprise"}]'`).each do |procedure_presentation|
procedure_presentation.displayed_fields.each { |field| entreprise_to_etablissement(field) }
procedure_presentation.save
end
end
def fix_sort
ProcedurePresentation.where(%q`sort @> '{"table": "entreprise"}'`).each do |procedure_presentation|
entreprise_to_etablissement(procedure_presentation['sort'])
procedure_presentation.save
end
end
def fix_filters
ProcedurePresentation.find_by_sql(
<<~SQL
SELECT procedure_presentations.*, array_agg(key) as keys
FROM procedure_presentations, LATERAL jsonb_each_text(filters)
WHERE value::jsonb @> '[{"table": "entreprise"}]'
GROUP BY id;
SQL
).each do |procedure_presentation|
procedure_presentation.keys.each do |key|
procedure_presentation.filters[key].each { |filter| entreprise_to_etablissement(filter) }
end
procedure_presentation.save
end
end
def entreprise_to_etablissement(field)
if field['table'] == 'entreprise'
field['table'] = 'etablissement'
field['column'] = "entreprise_#{field['column']}"
end
end
end.new.run
end
end

View file

@ -750,7 +750,7 @@ describe Dossier do
it { expect(dossier.get_value('self', 'created_at')).to eq(dossier.created_at) }
it { expect(dossier.get_value('user', 'email')).to eq(user.email) }
it { expect(dossier.get_value('france_connect_information', 'gender')).to eq(user.france_connect_information.gender) }
it { expect(dossier.get_value('entreprise', 'siren')).to eq(dossier.etablissement.entreprise_siren) }
it { expect(dossier.get_value('etablissement', 'entreprise_siren')).to eq(dossier.etablissement.entreprise_siren) }
it { expect(dossier.get_value('etablissement', 'siret')).to eq(dossier.etablissement.siret) }
it { expect(dossier.get_value('type_de_champ', @champ_public.type_de_champ.id.to_s)).to eq(dossier.champs.first.value) }
it { expect(dossier.get_value('type_de_champ_private', @champ_private.type_de_champ.id.to_s)).to eq(dossier.champs_private.first.value) }

View file

@ -0,0 +1,29 @@
require 'spec_helper'
describe DossierFieldService do
describe '#filtered_ids' do
let(:procedure) { create(:procedure) }
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.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.filtered_ids(procedure.dossiers, [{ 'table' => 'etablissement', 'column' => 'code_postal', 'value' => '75017' }]) }
it { is_expected.to contain_exactly(kept_dossier.id) }
end
end
end
end