diff --git a/app/models/export.rb b/app/models/export.rb index c6c3bb805..b8c72020a 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -51,6 +51,7 @@ class Export < ApplicationRecord end def compute + self.dossiers_count = dossiers_for_export.count load_snapshot! file.attach(blob.signed_id) # attaching a blob directly might run identify/virus scanner and wipe it @@ -109,9 +110,10 @@ class Export < ApplicationRecord end def count - if procedure_presentation_id.present? - dossiers_for_export.count - end + return dossiers_count if !dossiers_count.nil? # export generated + return dossiers_for_export.count if procedure_presentation_id.present? + + nil end def procedure diff --git a/spec/components/dossiers/export_link_component_spec.rb b/spec/components/dossiers/export_link_component_spec.rb index c413ec276..af26ed8ba 100644 --- a/spec/components/dossiers/export_link_component_spec.rb +++ b/spec/components/dossiers/export_link_component_spec.rb @@ -1,17 +1,19 @@ RSpec.describe Dossiers::ExportLinkComponent, type: :component do let(:procedure) { create(:procedure) } - let(:groupe_instructeur) { create(:groupe_instructeur, procedure: procedure) } - let(:export) { create(:export, groupe_instructeurs: [groupe_instructeur], updated_at: 5.minutes.ago, created_at: 10.minutes.ago) } + let(:groupe_instructeur) { create(:groupe_instructeur, procedure: procedure, instructeurs: [build(:instructeur)]) } let(:export_url) { double("ExportUrl", call: "/some/fake/path") } + let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: groupe_instructeur.instructeurs.first) } + let(:procedure_presentation) { create(:procedure_presentation, procedure: procedure, assign_to: assign_to) } + let(:component) { described_class.new(procedure:, exports: [export], export_url:) } describe "rendering" do subject { render_inline(component).to_html } context "when the export is available" do + let(:export) { create(:export, :generated, groupe_instructeurs: [groupe_instructeur], updated_at: 5.minutes.ago, created_at: 10.minutes.ago) } before do - allow(export).to receive(:available?).and_return(true) attachment = ActiveStorage::Attachment.new(name: "export", record: export, blob: ActiveStorage::Blob.new(byte_size: 10.kilobytes, content_type: "text/csv", filename: "export.csv")) allow(export).to receive(:file).and_return(attachment) end @@ -25,12 +27,29 @@ RSpec.describe Dossiers::ExportLinkComponent, type: :component do expect(subject).to include("CSV") expect(subject).to include("10 ko") end + + context 'when export is for everything' do + it 'not display the exact dossiers count' do + expect(subject).to include("tous les dossiers") + end + end + + context 'when export is for a presentation' do + before do + export.update!(procedure_presentation: procedure_presentation) + end + + it 'display the persisted dossiers count' do + expect(subject).to include("10 dossiers") + end + end end context "when the export is not available" do + let(:export) { create(:export, :pending, groupe_instructeurs: [groupe_instructeur], procedure_presentation: procedure_presentation, created_at: 10.minutes.ago) } + before do - allow(export).to receive(:available?).and_return(false) - allow(export).to receive(:failed?).and_return(false) + create_list(:dossier, 3, :en_construction, procedure: procedure, groupe_instructeur: groupe_instructeur) end it "displays the pending label" do @@ -40,12 +59,14 @@ RSpec.describe Dossiers::ExportLinkComponent, type: :component do it "displays a refresh page button" do expect(subject).to include("Recharger") end + + it 'displays the current dossiers count' do + expect(subject).to include("3 dossiers") + end end context "when the export has failed" do - before do - allow(export).to receive(:failed?).and_return(true) - end + let(:export) { create(:export, :failed) } it "displays the refresh old export button" do expect(subject).to include("Regénérer") diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb index 36abef09d..4c508f244 100644 --- a/spec/models/export_spec.rb +++ b/spec/models/export_spec.rb @@ -198,4 +198,31 @@ RSpec.describe Export, type: :model do expect(results.count).to eq(1) end end + + describe '.dossiers_count' do + let(:export) { create(:export, :pending) } + + before do + blob_double = instance_double("ActiveStorage::Blob", signed_id: "some_signed_id_value") + attachment_double = instance_double("ActiveStorage::Attached::One", attach: true) + + allow(export).to receive(:blob).and_return(blob_double) + allow(export).to receive(:file).and_return(attachment_double) + + create_list(:dossier, 3, :en_construction, groupe_instructeur: export.groupe_instructeurs.first) + end + + it 'is not set until generation' do + expect(export.dossiers_count).to be_nil + end + + it 'is persisted after generation' do + export.compute_with_safe_stale_for_purge do + export.compute + end + + export.reload + expect(export.dossiers_count).to eq(3) + end + end end