diff --git a/app/controllers/administrateurs/exports_controller.rb b/app/controllers/administrateurs/exports_controller.rb index 28f84778b..1d9b0938a 100644 --- a/app/controllers/administrateurs/exports_controller.rb +++ b/app/controllers/administrateurs/exports_controller.rb @@ -4,7 +4,7 @@ module Administrateurs before_action :ensure_not_super_admin! def download - export = Export.find_or_create_fresh_export(export_format, all_groupe_instructeurs, **export_options) + export = Export.find_or_create_fresh_export(export_format, all_groupe_instructeurs, current_administrateur.instructeur, **export_options) @dossiers_count = export.count if export.available? diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 686f07f10..dbfc84bd5 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -175,7 +175,7 @@ module Instructeurs .visible_by_administration .exists?(groupe_instructeur_id: groupe_instructeur_ids) && !instructeur_as_manager? - export = Export.find_or_create_fresh_export(export_format, groupe_instructeurs, **export_options) + export = Export.find_or_create_fresh_export(export_format, groupe_instructeurs, current_instructeur, **export_options) @procedure = procedure @statut = export_options[:statut] diff --git a/app/models/export.rb b/app/models/export.rb index 4280442c3..c6c3bb805 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -64,7 +64,7 @@ class Export < ApplicationRecord procedure_presentation_id.present? end - def self.find_or_create_fresh_export(format, groupe_instructeurs, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil) + def self.find_or_create_fresh_export(format, groupe_instructeurs, instructeur, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil) attributes = { format:, time_span_type:, @@ -80,6 +80,7 @@ class Export < ApplicationRecord return recent_export if recent_export.present? create!(**attributes, groupe_instructeurs:, + instructeur:, procedure_presentation:, procedure_presentation_snapshot: procedure_presentation&.snapshot) end diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb index f53d38195..36abef09d 100644 --- a/spec/models/export_spec.rb +++ b/spec/models/export_spec.rb @@ -81,28 +81,29 @@ RSpec.describe Export, type: :model do describe '.find_or_create_fresh_export' do let!(:procedure) { create(:procedure) } - let!(:gi_1) { create(:groupe_instructeur, procedure: procedure, instructeurs: [create(:instructeur)]) } + let(:instructeur) { create(:instructeur) } + let!(:gi_1) { create(:groupe_instructeur, procedure: procedure, instructeurs: [instructeur]) } let!(:pp) { gi_1.instructeurs.first.procedure_presentation_and_errors_for_procedure_id(procedure.id).first } before { pp.add_filter('tous', 'self/created_at', '10/12/2021') } context 'with procedure_presentation having different filters' do it 'works once' do - expect { Export.find_or_create_fresh_export(:zip, [gi_1], time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) } + expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) } .to change { Export.count }.by(1) end it 'works once, changes procedure_presentation, recreate a new' do - expect { Export.find_or_create_fresh_export(:zip, [gi_1], time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) } + expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) } .to change { Export.count }.by(1) pp.add_filter('tous', 'self/updated_at', '10/12/2021') - expect { Export.find_or_create_fresh_export(:zip, [gi_1], time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) } + expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) } .to change { Export.count }.by(1) end end context 'with existing matching export' do def find_or_create = - Export.find_or_create_fresh_export(:zip, [gi_1], time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) + Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) context 'freshly generate export' do before { find_or_create.update!(job_status: :generated, updated_at: 1.second.ago) }