Instructeur: only export its dossiers
This commit is contained in:
parent
f4d4a896a0
commit
8660d4af30
7 changed files with 29 additions and 19 deletions
|
@ -187,17 +187,19 @@ module Instructeurs
|
||||||
def download_dossiers
|
def download_dossiers
|
||||||
options = params.permit(:version, :limit, :since, tables: [])
|
options = params.permit(:version, :limit, :since, tables: [])
|
||||||
|
|
||||||
|
dossiers = current_instructeur.dossiers.for_procedure(procedure)
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.csv do
|
format.csv do
|
||||||
send_data(procedure.to_csv(options),
|
send_data(procedure.to_csv(dossiers, options),
|
||||||
filename: procedure.export_filename(:csv))
|
filename: procedure.export_filename(:csv))
|
||||||
end
|
end
|
||||||
format.xlsx do
|
format.xlsx do
|
||||||
send_data(procedure.to_xlsx(options),
|
send_data(procedure.to_xlsx(dossiers, options),
|
||||||
filename: procedure.export_filename(:xlsx))
|
filename: procedure.export_filename(:xlsx))
|
||||||
end
|
end
|
||||||
format.ods do
|
format.ods do
|
||||||
send_data(procedure.to_ods(options),
|
send_data(procedure.to_ods(dossiers, options),
|
||||||
filename: procedure.export_filename(:ods))
|
filename: procedure.export_filename(:ods))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -334,26 +334,26 @@ class Procedure < ApplicationRecord
|
||||||
"dossiers_#{procedure_identifier}_#{Time.zone.now.strftime('%Y-%m-%d_%H-%M')}.#{format}"
|
"dossiers_#{procedure_identifier}_#{Time.zone.now.strftime('%Y-%m-%d_%H-%M')}.#{format}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def export(options = {})
|
def export(dossiers, options = {})
|
||||||
version = options.delete(:version)
|
version = options.delete(:version)
|
||||||
if version == 'v2'
|
if version == 'v2'
|
||||||
options.delete(:tables)
|
options.delete(:tables)
|
||||||
ProcedureExportV2Service.new(self, **options.to_h.symbolize_keys)
|
ProcedureExportV2Service.new(self, dossiers, **options.to_h.symbolize_keys)
|
||||||
else
|
else
|
||||||
ProcedureExportService.new(self, **options.to_h.symbolize_keys)
|
ProcedureExportService.new(self, dossiers, **options.to_h.symbolize_keys)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_csv(options = {})
|
def to_csv(dossiers, options = {})
|
||||||
export(options).to_csv
|
export(dossiers, options).to_csv
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_xlsx(options = {})
|
def to_xlsx(dossiers, options = {})
|
||||||
export(options).to_xlsx
|
export(dossiers, options).to_xlsx
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_ods(options = {})
|
def to_ods(dossiers, options = {})
|
||||||
export(options).to_ods
|
export(dossiers, options).to_ods
|
||||||
end
|
end
|
||||||
|
|
||||||
def procedure_overview(start_date)
|
def procedure_overview(start_date)
|
||||||
|
|
|
@ -49,9 +49,9 @@ class ProcedureExportService
|
||||||
:prenom
|
:prenom
|
||||||
]
|
]
|
||||||
|
|
||||||
def initialize(procedure, tables: [], ids: nil, since: nil, limit: nil)
|
def initialize(procedure, dossiers, tables: [], ids: nil, since: nil, limit: nil)
|
||||||
@procedure = procedure
|
@procedure = procedure
|
||||||
@dossiers = procedure.dossiers.downloadable_sorted
|
@dossiers = dossiers.downloadable_sorted
|
||||||
if ids
|
if ids
|
||||||
@dossiers = @dossiers.where(id: ids)
|
@dossiers = @dossiers.where(id: ids)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
class ProcedureExportV2Service
|
class ProcedureExportV2Service
|
||||||
attr_reader :dossiers
|
attr_reader :dossiers
|
||||||
|
|
||||||
def initialize(procedure, ids: nil, since: nil, limit: nil)
|
def initialize(procedure, dossiers, ids: nil, since: nil, limit: nil)
|
||||||
@procedure = procedure
|
@procedure = procedure
|
||||||
@dossiers = procedure.dossiers.downloadable_sorted
|
@dossiers = dossiers.downloadable_sorted
|
||||||
if ids
|
if ids
|
||||||
@dossiers = @dossiers.where(id: ids)
|
@dossiers = @dossiers.where(id: ids)
|
||||||
end
|
end
|
||||||
|
|
|
@ -414,6 +414,9 @@ describe Instructeurs::ProceduresController, type: :controller do
|
||||||
describe "#download_dossiers" do
|
describe "#download_dossiers" do
|
||||||
let(:instructeur) { create(:instructeur) }
|
let(:instructeur) { create(:instructeur) }
|
||||||
let!(:procedure) { create(:procedure, instructeurs: [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
|
context "when logged in" do
|
||||||
before do
|
before do
|
||||||
|
@ -421,7 +424,12 @@ describe Instructeurs::ProceduresController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "csv" do
|
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) }
|
it { expect(response).to have_http_status(:ok) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,7 @@ describe ProcedureExportService do
|
||||||
describe 'to_data' do
|
describe 'to_data' do
|
||||||
let(:procedure) { create(:procedure, :published, :with_all_champs) }
|
let(:procedure) { create(:procedure, :published, :with_all_champs) }
|
||||||
let(:table) { :dossiers }
|
let(:table) { :dossiers }
|
||||||
subject { ProcedureExportService.new(procedure).to_data(table) }
|
subject { ProcedureExportService.new(procedure, procedure.dossiers).to_data(table) }
|
||||||
|
|
||||||
let(:headers) { subject[:headers] }
|
let(:headers) { subject[:headers] }
|
||||||
let(:data) { subject[:data] }
|
let(:data) { subject[:data] }
|
||||||
|
|
|
@ -5,7 +5,7 @@ describe ProcedureExportV2Service do
|
||||||
let(:procedure) { create(:procedure, :published, :with_all_champs) }
|
let(:procedure) { create(:procedure, :published, :with_all_champs) }
|
||||||
subject do
|
subject do
|
||||||
Tempfile.create do |f|
|
Tempfile.create do |f|
|
||||||
f << ProcedureExportV2Service.new(procedure).to_xlsx
|
f << ProcedureExportV2Service.new(procedure, procedure.dossiers).to_xlsx
|
||||||
f.rewind
|
f.rewind
|
||||||
SimpleXlsxReader.open(f.path)
|
SimpleXlsxReader.open(f.path)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue