Merge pull request #4345 from betagouv/export_and_groupe_instructeur

Prépare l'export des dossiers au routage
This commit is contained in:
LeSim 2019-09-19 12:29:23 +02:00 committed by GitHub
commit fb631fb019
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 19 deletions

View file

@ -187,17 +187,19 @@ module Instructeurs
def download_dossiers
options = params.permit(:version, :limit, :since, tables: [])
dossiers = current_instructeur.dossiers.for_procedure(procedure)
respond_to do |format|
format.csv do
send_data(procedure.to_csv(options),
send_data(procedure.to_csv(dossiers, options),
filename: procedure.export_filename(:csv))
end
format.xlsx do
send_data(procedure.to_xlsx(options),
send_data(procedure.to_xlsx(dossiers, options),
filename: procedure.export_filename(:xlsx))
end
format.ods do
send_data(procedure.to_ods(options),
send_data(procedure.to_ods(dossiers, options),
filename: procedure.export_filename(:ods))
end
end

View file

@ -334,26 +334,26 @@ class Procedure < ApplicationRecord
"dossiers_#{procedure_identifier}_#{Time.zone.now.strftime('%Y-%m-%d_%H-%M')}.#{format}"
end
def export(options = {})
def export(dossiers, options = {})
version = options.delete(:version)
if version == 'v2'
options.delete(:tables)
ProcedureExportV2Service.new(self, **options.to_h.symbolize_keys)
ProcedureExportV2Service.new(self, dossiers, **options.to_h.symbolize_keys)
else
ProcedureExportService.new(self, **options.to_h.symbolize_keys)
ProcedureExportService.new(self, dossiers, **options.to_h.symbolize_keys)
end
end
def to_csv(options = {})
export(options).to_csv
def to_csv(dossiers, options = {})
export(dossiers, options).to_csv
end
def to_xlsx(options = {})
export(options).to_xlsx
def to_xlsx(dossiers, options = {})
export(dossiers, options).to_xlsx
end
def to_ods(options = {})
export(options).to_ods
def to_ods(dossiers, options = {})
export(dossiers, options).to_ods
end
def procedure_overview(start_date)

View file

@ -49,9 +49,9 @@ class ProcedureExportService
:prenom
]
def initialize(procedure, tables: [], ids: nil, since: nil, limit: nil)
def initialize(procedure, dossiers, tables: [], ids: nil, since: nil, limit: nil)
@procedure = procedure
@dossiers = procedure.dossiers.downloadable_sorted
@dossiers = dossiers.downloadable_sorted
if ids
@dossiers = @dossiers.where(id: ids)
end

View file

@ -1,9 +1,9 @@
class ProcedureExportV2Service
attr_reader :dossiers
def initialize(procedure, ids: nil, since: nil, limit: nil)
def initialize(procedure, dossiers, ids: nil, since: nil, limit: nil)
@procedure = procedure
@dossiers = procedure.dossiers.downloadable_sorted
@dossiers = dossiers.downloadable_sorted
if ids
@dossiers = @dossiers.where(id: ids)
end

View file

@ -414,6 +414,9 @@ describe Instructeurs::ProceduresController, type: :controller do
describe "#download_dossiers" do
let(:instructeur) { create(:instructeur) }
let!(:procedure) { create(:procedure, instructeurs: [instructeur]) }
let!(:gi_2) { procedure.groupe_instructeurs.create(label: '2') }
let!(:dossier_1) { create(:dossier, procedure: procedure) }
let!(:dossier_2) { create(:dossier, groupe_instructeur: gi_2) }
context "when logged in" do
before do
@ -421,7 +424,12 @@ describe Instructeurs::ProceduresController, type: :controller do
end
context "csv" do
before { get :download_dossiers, params: { procedure_id: procedure.id }, format: 'csv' }
before do
expect_any_instance_of(Procedure).to receive(:to_csv)
.with(instructeur.dossiers.for_procedure(procedure), {})
get :download_dossiers, params: { procedure_id: procedure.id }, format: 'csv'
end
it { expect(response).to have_http_status(:ok) }
end

View file

@ -4,7 +4,7 @@ describe ProcedureExportService do
describe 'to_data' do
let(:procedure) { create(:procedure, :published, :with_all_champs) }
let(:table) { :dossiers }
subject { ProcedureExportService.new(procedure).to_data(table) }
subject { ProcedureExportService.new(procedure, procedure.dossiers).to_data(table) }
let(:headers) { subject[:headers] }
let(:data) { subject[:data] }

View file

@ -5,7 +5,7 @@ describe ProcedureExportV2Service do
let(:procedure) { create(:procedure, :published, :with_all_champs) }
subject do
Tempfile.create do |f|
f << ProcedureExportV2Service.new(procedure).to_xlsx
f << ProcedureExportV2Service.new(procedure, procedure.dossiers).to_xlsx
f.rewind
SimpleXlsxReader.open(f.path)
end