feat(champ siret): etablissement columns filterable & displayable

This commit is contained in:
Colin Darie 2024-11-20 16:35:42 +01:00
parent 5226f67cb3
commit 90a8888ade
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
8 changed files with 93 additions and 6 deletions

View file

@ -50,6 +50,7 @@ class Column
def dossier_column? = false
def champ_column? = false
def filterable? = filterable
def label_for_value(value)
if options_for_select.present?

View file

@ -3,7 +3,7 @@
class Columns::JSONPathColumn < Columns::ChampColumn
attr_reader :jsonpath
def initialize(procedure_id:, label:, stable_id:, tdc_type:, jsonpath:, options_for_select: [], displayable:, type: :text)
def initialize(procedure_id:, label:, stable_id:, tdc_type:, jsonpath:, options_for_select: [], displayable:, filterable: true, type: :text)
@jsonpath = quote_string(jsonpath)
super(
@ -12,6 +12,7 @@ class Columns::JSONPathColumn < Columns::ChampColumn
stable_id:,
tdc_type:,
displayable:,
filterable:,
type:,
options_for_select:
)

View file

@ -21,6 +21,17 @@ class Etablissement < ApplicationRecord
after_commit -> { dossier&.index_search_terms_later }
# See https://github.com/demarches-simplifiees/demarches-simplifiees.fr/pull/10591#discussion_r1819399688
# SIRET is already exposed as base column.
DISPLAYABLE_COLUMNS = {
"entreprise_raison_sociale" => { type: :text },
"entreprise_siren" => { type: :text },
"entreprise_nom_commercial" => { type: :text },
"entreprise_forme_juridique" => { type: :text },
"entreprise_date_creation" => { type: :date, filterable: false },
"libelle_naf" => { type: :text }
}.freeze
def entreprise_raison_sociale
read_attribute(:entreprise_raison_sociale).presence || raison_sociale_for_ei
end

View file

@ -10,6 +10,27 @@ class TypesDeChamp::SiretTypeDeChamp < TypesDeChamp::TypeDeChampBase
def champ_blank_or_invalid?(champ) = Siret.new(siret: champ.value).invalid?
def columns(procedure:, displayable: true, prefix: nil)
super.concat(addressable_columns(procedure:, displayable:, prefix:))
super
.concat(etablissement_columns(procedure:, displayable:, prefix:))
.concat(addressable_columns(procedure:, displayable:, prefix:))
end
private
def etablissement_columns(procedure:, displayable:, prefix:)
i18n_scope = [:activerecord, :attributes, :procedure_presentation, :fields, :etablissement]
Etablissement::DISPLAYABLE_COLUMNS.map do |(column, attributes)|
Columns::JSONPathColumn.new(
procedure_id: procedure.id,
stable_id:,
tdc_type: type_champ,
label: [prefix, libelle, I18n.t(column, scope: i18n_scope)].compact.join(' '),
type: attributes[:type],
jsonpath: "$.#{column}",
displayable: true,
filterable: attributes.fetch(:filterable, true)
)
end
end
end

View file

@ -165,7 +165,7 @@ FactoryBot.define do
factory :champ_do_not_use_siret, class: 'Champs::SiretChamp' do
association :etablissement, factory: [:etablissement]
value { '44011762001530' }
value_json { AddressProxy::ADDRESS_PARTS.index_by(&:itself) }
value_json { etablissement.champ_value_json }
end
factory :champ_do_not_use_rna, class: 'Champs::RNAChamp' do

View file

@ -19,7 +19,21 @@ describe Columns::ChampColumn do
expect_type_de_champ_values('pays', eq(['France']))
expect_type_de_champ_values('epci', eq([nil]))
expect_type_de_champ_values('iban', eq([nil]))
expect_type_de_champ_values('siret', eq(["44011762001530", "postal_code", "city_name", "departement_code", "region_name"]))
expect_type_de_champ_values('siret', match_array(
[
"44011762001530",
"SA à conseil d'administration (s.a.i.)",
"440117620",
"GRTGAZ",
"GRTGAZ",
"1990-04-24",
"Transports par conduites",
"92270",
"Bois-Colombes",
"92",
"Île-de-France"
]
))
expect_type_de_champ_values('text', eq(['text']))
expect_type_de_champ_values('textarea', eq(['textarea']))
expect_type_de_champ_values('number', eq(['42']))

View file

@ -123,11 +123,20 @@ describe ExportTemplate do
context 'when procedure has a TypeDeChamp::Siret' do
let(:types_de_champ_public) do
[
{ type: :siret, libelle: 'siret', stable_id: 20 }
{ type: :siret, libelle: 'SIRET', stable_id: 20 }
]
end
it 'is able to resolve stable_id' do
expect(export_template.columns_for_stable_id(20).size).to eq(5)
columns = export_template.columns_for_stable_id(20)
expect(columns.find { _1.libelle == "SIRET" }).to be_present
%w[
$.entreprise_nom_commercial
$.entreprise_raison_sociale
].each do |jsonpath|
expect(columns.find { _1.column.respond_to?(:jsonpath) && _1.column.jsonpath == jsonpath }).to be_present
end
end
end
context 'when procedure has a TypeDeChamp::Text' do

View file

@ -0,0 +1,30 @@
# frozen_string_literal: true
describe TypesDeChamp::SiretTypeDeChamp do
let(:tdc_siret) { build(:type_de_champ_siret, libelle: 'Numéro SIRET') }
let(:procedure) { build(:procedure) }
describe "#columns" do
subject(:columns) { tdc_siret.columns(procedure: procedure) }
it "includes required jsonpaths" do
expected_paths = [
"$.entreprise_raison_sociale",
"$.entreprise_siren"
]
json_columns = columns.filter { _1.is_a?(Columns::JSONPathColumn) }
expect(json_columns.map(&:jsonpath)).to include(*expected_paths)
end
it "includes address columns" do
address_columns = columns.filter { _1.is_a?(Columns::JSONPathColumn) && _1.jsonpath.match?(/adresse|postal_code/) }
expect(address_columns).not_to be_empty
end
it "does not include jsonpath SIRET column" do
expect(columns.find { |c| c.is_a?(Columns::JSONPathColumn) && c.jsonpath == "$.siret" }).to be_nil
end
end
end