feat(ExportedColumn): use dedicated formatter for exported column

This commit is contained in:
mfo 2024-11-04 17:01:36 +01:00
parent ed3b1bc046
commit 3b5acaad4c
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
4 changed files with 88 additions and 32 deletions

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Columns::ChampColumn < Column
attr_reader :stable_id
attr_reader :stable_id, :tdc_type
def initialize(procedure_id:, label:, stable_id:, tdc_type:, displayable: true, filterable: true, type: :text, options_for_select: [])
@stable_id = stable_id

View file

@ -11,6 +11,6 @@ class ExportedColumn
def id = { id: column.id, libelle: }.to_json
def libelle_with_value(champ_or_dossier)
[libelle, column.value(champ_or_dossier)]
[libelle, ExportedColumnFormatter.format(column:, champ_or_dossier:)]
end
end

View file

@ -0,0 +1,42 @@
# frozen_string_literal: true
class ExportedColumnFormatter
def self.format(column:, champ_or_dossier:)
return if champ_or_dossier.nil?
raw_value = column.value(champ_or_dossier)
case column.type
when :attachements
format_attachments(column:, raw_value:)
when :enum
format_enum(column:, raw_value:)
when :enums
format_enums(column:, raw_values: raw_value)
else
raw_value
end
end
private
def self.format_attachments(column:, raw_value:)
case column.tdc_type
when TypeDeChamp.type_champs[:titre_identite]
raw_value.present? ? 'présent' : 'absent'
when TypeDeChamp.type_champs[:piece_justificative]
raw_value.map { _1.blob.filename }.join(", ")
end
end
def self.format_enums(column:, raw_values:)
raw_values.map { format_enum(column:, raw_value: _1) }.join(', ')
end
def self.format_enum(column:, raw_value:)
# options for select store ["trad", :enum_value]
selected_option = column.options_for_select.find { _1[1].to_s == raw_value }
selected_option ? selected_option.first : raw_value
end
end

View file

@ -44,6 +44,7 @@ describe ProcedureExportService do
end
describe 'Dossiers sheet' do
context 'multiple columns' do
let(:exported_columns) do
[
ExportedColumn.new(libelle: 'Date du dernier évènement', column: procedure.find_column(label: 'Date du dernier évènement')),
@ -69,10 +70,16 @@ describe ProcedureExportService do
expect(dossiers_sheet.data).to match_array([[anything, dossier.user_email_for_display, "défaut", "En instruction", "text", "60172", "toto.txt"]])
end
end
context 'with a procedure routee' do
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:) }
before { create(:groupe_instructeur, label: '2', procedure:) }
context 'with a procedure having multiple groupe instructeur' do
let(:exported_columns) { [ExportedColumn.new(libelle: 'Groupe instructeur', column: procedure.find_column(label: 'Groupe instructeur'))] }
let(:types_de_champ_public) { [] }
before do
create(:groupe_instructeur, label: '2', procedure:)
create(:dossier, :en_instruction, procedure:)
end
it 'find groupe instructeur data' do
expect(dossiers_sheet.headers).to include('Groupe instructeur')
@ -81,17 +88,24 @@ describe ProcedureExportService do
end
context 'with a dossier having multiple pjs' do
let(:procedure) { create(:procedure, :published, :for_individual, types_de_champ_public:) }
let!(:dossier) { create(:dossier, :en_instruction, :with_populated_champs, :with_individual, procedure:) }
let!(:dossier_2) { create(:dossier, :en_instruction, :with_populated_champs, :with_individual, procedure:) }
let(:types_de_champ_public) { [{ type: :piece_justificative, libelle: "PJ" }] }
let(:exported_columns) { [ExportedColumn.new(libelle: 'PJ', column: procedure.find_column(label: 'PJ'))] }
before do
dossier_2.filled_champs_public
dossier = create(:dossier, :en_instruction, :with_populated_champs, procedure:)
dossier.filled_champs_public
.find { _1.is_a? Champs::PieceJustificativeChamp }
.piece_justificative_file
.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
end
it { expect(dossiers_sheet.data.last.last).to eq "toto.txt, toto.txt" }
end
context 'with a dossier TypeDeChamp::MutlipleDropDownList' do
let(:types_de_champ_public) { [{ type: :multiple_drop_down_list, libelle: "multiple_drop_down_list", mandatory: true }] }
let(:exported_columns) { [ExportedColumn.new(libelle: 'Date du dernier évènement', column: procedure.find_column(label: 'multiple_drop_down_list'))] }
before { create(:dossier, :with_populated_champs, procedure:) }
it { expect(dossiers_sheet.data.last.last).to eq "val1, val2" }
end
end
describe 'Etablissement sheet' do