diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 7d55e97df..f13fb26db 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -108,7 +108,24 @@ class Dossier < ApplicationRecord scope :en_construction, -> { not_archived.state_en_construction } scope :en_instruction, -> { not_archived.state_en_instruction } scope :termine, -> { not_archived.state_termine } - scope :downloadable_sorted, -> { state_not_brouillon.includes(:etablissement, :user, :individual, :followers_gestionnaires, :avis, champs: { etablissement: [:champ], type_de_champ: :drop_down_list }, champs_private: { etablissement: [:champ], type_de_champ: :drop_down_list }).order(en_construction_at: 'asc') } + scope :downloadable_sorted, -> { + state_not_brouillon + .includes( + :user, + :individual, + :followers_gestionnaires, + :avis, + etablissement: :champ, + champs: { + etablissement: :champ, + type_de_champ: :drop_down_list + }, + champs_private: { + etablissement: :champ, + type_de_champ: :drop_down_list + } + ).order(en_construction_at: 'asc') + } scope :en_cours, -> { not_archived.state_en_construction_ou_instruction } scope :without_followers, -> { left_outer_joins(:follows).where(follows: { id: nil }) } scope :followed_by, -> (gestionnaire) { joins(:follows).where(follows: { gestionnaire: gestionnaire }) } diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 2dc07760a..98584e672 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -120,6 +120,6 @@ class Etablissement < ApplicationRecord end def libelle_for_export - champ&.libelle + champ&.libelle || 'Dossier' end end diff --git a/app/services/procedure_export_v2_service.rb b/app/services/procedure_export_v2_service.rb index 2252136dd..be5bf8ff0 100644 --- a/app/services/procedure_export_v2_service.rb +++ b/app/services/procedure_export_v2_service.rb @@ -53,15 +53,17 @@ class ProcedureExportV2Service [dossier.champs, dossier.champs_private] .flatten .select { |champ| champ.is_a?(Champs::RepetitionChamp) } - end + end.group_by(&:libelle) end def champs_repetables_options - champs_repetables.map do |champ| + champs_repetables.map do |libelle, champs| [ - champ.libelle, - champ.rows.each_with_index.map do |champs, index| - Champs::RepetitionChamp::Row.new(index: index + 1, dossier_id: champ.dossier_id.to_s, champs: champs) + libelle, + champs.flat_map do |champ| + champ.rows.each_with_index.map do |champs, index| + Champs::RepetitionChamp::Row.new(index: index + 1, dossier_id: champ.dossier_id.to_s, champs: champs) + end end ] end diff --git a/spec/services/procedure_export_v2_service_spec.rb b/spec/services/procedure_export_v2_service_spec.rb index 5790307bb..d0dfa759a 100644 --- a/spec/services/procedure_export_v2_service_spec.rb +++ b/spec/services/procedure_export_v2_service_spec.rb @@ -93,6 +93,9 @@ describe ProcedureExportV2Service do context 'with etablissement' do let!(:dossier) { create(:dossier, :en_instruction, :with_all_champs, :with_entreprise, procedure: procedure) } + let(:dossier_etablissement) { etablissements_sheet.data[1] } + let(:champ_etablissement) { etablissements_sheet.data[0] } + it 'should have headers' do expect(etablissements_sheet.headers).to eq([ "Dossier ID", @@ -132,6 +135,8 @@ describe ProcedureExportV2Service do it 'should have data' do expect(etablissements_sheet.data.size).to eq(2) + expect(dossier_etablissement[1]).to eq("Dossier") + expect(champ_etablissement[1]).to eq("siret") end end @@ -155,8 +160,13 @@ describe ProcedureExportV2Service do end context 'with repetitions' do - let!(:dossier) { create(:dossier, :en_instruction, :with_all_champs, :for_individual, procedure: procedure) } - let(:champ_repetition) { dossier.champs.find { |champ| champ.type_champ == 'repetition' } } + let!(:dossiers) do + [ + create(:dossier, :en_instruction, :with_all_champs, :for_individual, procedure: procedure), + create(:dossier, :en_instruction, :with_all_champs, :for_individual, procedure: procedure) + ] + end + let(:champ_repetition) { dossiers.first.champs.find { |champ| champ.type_champ == 'repetition' } } it 'should have sheets' do expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', champ_repetition.libelle]) @@ -172,7 +182,7 @@ describe ProcedureExportV2Service do end it 'should have data' do - expect(repetition_sheet.data.size).to eq(2) + expect(repetition_sheet.data.size).to eq(4) end end end