feat(ExportedColumn): use dedicated formatter for exported column
This commit is contained in:
parent
ed3b1bc046
commit
3b5acaad4c
4 changed files with 88 additions and 32 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
42
app/services/exported_column_formatter.rb
Normal file
42
app/services/exported_column_formatter.rb
Normal 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
|
|
@ -44,35 +44,42 @@ describe ProcedureExportService do
|
|||
end
|
||||
|
||||
describe 'Dossiers sheet' do
|
||||
let(:exported_columns) do
|
||||
[
|
||||
ExportedColumn.new(libelle: 'Date du dernier évènement', column: procedure.find_column(label: 'Date du dernier évènement')),
|
||||
ExportedColumn.new(libelle: 'Email', column: procedure.find_column(label: 'Email')),
|
||||
ExportedColumn.new(libelle: 'Groupe instructeur', column: procedure.find_column(label: 'Groupe instructeur')),
|
||||
ExportedColumn.new(libelle: 'État du dossier', column: procedure.dossier_state_column),
|
||||
ExportedColumn.new(libelle: 'first champ', column: procedure.find_column(label: 'first champ')),
|
||||
ExportedColumn.new(libelle: 'Commune (Code INSEE)', column: procedure.find_column(label: 'Commune (Code INSEE)')),
|
||||
ExportedColumn.new(libelle: 'PJ', column: procedure.find_column(label: 'PJ'))
|
||||
]
|
||||
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')),
|
||||
ExportedColumn.new(libelle: 'Email', column: procedure.find_column(label: 'Email')),
|
||||
ExportedColumn.new(libelle: 'Groupe instructeur', column: procedure.find_column(label: 'Groupe instructeur')),
|
||||
ExportedColumn.new(libelle: 'État du dossier', column: procedure.dossier_state_column),
|
||||
ExportedColumn.new(libelle: 'first champ', column: procedure.find_column(label: 'first champ')),
|
||||
ExportedColumn.new(libelle: 'Commune (Code INSEE)', column: procedure.find_column(label: 'Commune (Code INSEE)')),
|
||||
ExportedColumn.new(libelle: 'PJ', column: procedure.find_column(label: 'PJ'))
|
||||
]
|
||||
end
|
||||
|
||||
let!(:dossier) { create(:dossier, :en_instruction, :with_populated_champs, :with_individual, procedure: procedure) }
|
||||
let(:selected_headers) { ["Email", "first champ", "Commune (Code INSEE)", "Groupe instructeur", "Date du dernier évènement", "État du dossier", "PJ"] }
|
||||
|
||||
it 'should have only headers from export template' do
|
||||
expect(dossiers_sheet.headers).to match_array(selected_headers)
|
||||
end
|
||||
|
||||
it 'should have data' do
|
||||
expect(procedure.dossiers.count).to eq 1
|
||||
expect(dossiers_sheet.data.size).to eq 1
|
||||
|
||||
expect(dossiers_sheet.data).to match_array([[anything, dossier.user_email_for_display, "défaut", "En instruction", "text", "60172", "toto.txt"]])
|
||||
end
|
||||
end
|
||||
|
||||
let!(:dossier) { create(:dossier, :en_instruction, :with_populated_champs, :with_individual, procedure: procedure) }
|
||||
let(:selected_headers) { ["Email", "first champ", "Commune (Code INSEE)", "Groupe instructeur", "Date du dernier évènement", "État du dossier", "PJ"] }
|
||||
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) { [] }
|
||||
|
||||
it 'should have only headers from export template' do
|
||||
expect(dossiers_sheet.headers).to match_array(selected_headers)
|
||||
end
|
||||
|
||||
it 'should have data' do
|
||||
expect(procedure.dossiers.count).to eq 1
|
||||
expect(dossiers_sheet.data.size).to eq 1
|
||||
|
||||
expect(dossiers_sheet.data).to match_array([[anything, dossier.user_email_for_display, "défaut", "En instruction", "text", "60172", "toto.txt"]])
|
||||
end
|
||||
|
||||
context 'with a procedure routee' do
|
||||
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:) }
|
||||
before { create(:groupe_instructeur, label: '2', procedure:) }
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue