diff --git a/app/controllers/administrateurs/archives_controller.rb b/app/controllers/administrateurs/archives_controller.rb index e93015927..2fdd834f3 100644 --- a/app/controllers/administrateurs/archives_controller.rb +++ b/app/controllers/administrateurs/archives_controller.rb @@ -8,7 +8,7 @@ module Administrateurs def index @exports = Export.find_for_groupe_instructeurs(all_groupe_instructeurs.map(&:id), nil) @average_dossier_weight = @procedure.average_dossier_weight - @count_dossiers_termines_by_month = Traitement.count_dossiers_termines_by_month(all_groupe_instructeurs) + @count_dossiers_termines_by_month = @procedure.dossiers.processed_by_month(all_groupe_instructeurs).count @archives = Archive.for_groupe_instructeur(all_groupe_instructeurs).to_a end diff --git a/app/controllers/instructeurs/archives_controller.rb b/app/controllers/instructeurs/archives_controller.rb index e059c5cd4..44e6d71fc 100644 --- a/app/controllers/instructeurs/archives_controller.rb +++ b/app/controllers/instructeurs/archives_controller.rb @@ -7,7 +7,7 @@ module Instructeurs def index @average_dossier_weight = @procedure.average_dossier_weight - @count_dossiers_termines_by_month = Traitement.count_dossiers_termines_by_month(groupe_instructeurs) + @count_dossiers_termines_by_month = @procedure.dossiers.processed_by_month(groupe_instructeurs).count @archives = Archive.for_groupe_instructeur(groupe_instructeurs).to_a end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 477e10c15..579624d4c 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -235,11 +235,16 @@ class Dossier < ApplicationRecord scope :en_instruction, -> { not_archived.state_en_instruction } scope :termine, -> { not_archived.state_termine } + scope :processed_by_month, -> (all_groupe_instructeurs) { + state_termine + .where(groupe_instructeurs: all_groupe_instructeurs) + .group_by_period(:month, :processed_at, reverse: true) + } + scope :processed_in_month, -> (date) do date = date.to_datetime state_termine - .joins(:traitements) - .where(traitements: { processed_at: date.beginning_of_month..date.end_of_month }) + .where(processed_at: date.beginning_of_month..date.end_of_month) end scope :ordered_for_export, -> { order(depose_at: 'asc') diff --git a/app/models/traitement.rb b/app/models/traitement.rb index 4975bbcc1..a4f882810 100644 --- a/app/models/traitement.rb +++ b/app/models/traitement.rb @@ -25,30 +25,4 @@ class Traitement < ApplicationRecord .where.not('dossiers.depose_at' => nil, processed_at: nil) .order(:processed_at) end - - scope :termine_close_to_expiration, -> do - joins(dossier: :procedure) - .termine - .where(process_expired: true) - .where('dossiers.state' => Dossier::TERMINE) - .where("traitements.processed_at + (procedures.duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: Dossier::INTERVAL_BEFORE_EXPIRATION }) - end - - def self.count_dossiers_termines_by_month(groupe_instructeurs) - last_traitements_per_dossier = Traitement - .select('max(traitements.processed_at) as processed_at') - .termine - .where(dossier: Dossier.state_termine.where(groupe_instructeur: groupe_instructeurs)) - .group(:dossier_id) - .to_sql - - sql = <<~EOF - select date_trunc('month', r1.processed_at::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.formatted_offset}'::INTERVAL) as month, count(r1.processed_at) - from (#{last_traitements_per_dossier}) as r1 - group by date_trunc('month', r1.processed_at::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.formatted_offset}'::INTERVAL) - order by month desc - EOF - - ActiveRecord::Base.connection.execute(sql) - end end diff --git a/app/views/shared/archives/_table.html.haml b/app/views/shared/archives/_table.html.haml index 178b0c407..f63d6aa8d 100644 --- a/app/views/shared/archives/_table.html.haml +++ b/app/views/shared/archives/_table.html.haml @@ -7,17 +7,15 @@ %th.center Télécharger %tbody - - count_dossiers_termines_by_month.each do |count_by_month| - - month = count_by_month["month"].to_date - - nb_dossiers_termines = count_by_month["count"] - - matching_archive = archives.find { |archive| archive.time_span_type == 'monthly' && archive.month == month } - - weight = estimate_weight(matching_archive, nb_dossiers_termines, average_dossier_weight) + - count_dossiers_termines_by_month.each do |date, count| + - matching_archive = archives.find { |archive| archive.time_span_type == 'monthly' && archive.month == date.month } + - weight = estimate_weight(matching_archive, count, average_dossier_weight) %tr %td - = I18n.l(month, format: "%B %Y").capitalize + = I18n.l(date, format: "%B %Y").capitalize %td.text-right - = nb_dossiers_termines + = count %td.text-right = number_to_human_size(weight) %td.center @@ -30,7 +28,7 @@ %span.icon.retry = t(:archive_pending_html, scope: [:instructeurs, :procedure], created_period: time_ago_in_words(matching_archive.created_at)) - elsif weight.nil? || weight < Archive::MAX_SIZE - = link_to create_archive_url(procedure, month), method: :post, class: "button" do + = link_to create_archive_url(procedure, date), method: :post, class: "button" do %span.icon.new-folder Demander la création - else diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index beeac792e..30d468ca6 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1709,4 +1709,50 @@ describe Dossier do end end end + + describe '#processed_by_month' do + let(:procedure) { create(:procedure, :published, groupe_instructeurs: [groupe_instructeurs]) } + let(:groupe_instructeurs) { create(:groupe_instructeur) } + + before do + create_dossier_for_month(procedure, 2021, 3) + create_dossier_for_month(procedure, 2021, 3) + create_archived_dossier_for_month(procedure, 2021, 3) + create_dossier_for_month(procedure, 2021, 2) + end + + subject do + Timecop.freeze(Time.zone.local(2021, 3, 5)) do + Dossier.processed_by_month(groupe_instructeurs).count + end + end + + it 'count dossiers_termines by month' do + expect(count_for_month(subject, 3)).to eq 3 + expect(count_for_month(subject, 2)).to eq 1 + end + + it 'returns descending order by month' do + expect(subject.keys.first.month).to eq 3 + expect(subject.keys.last.month).to eq 2 + end + end + + private + + def count_for_month(processed_by_month, month) + processed_by_month.find { |date, _count| date.month == month }[1] + end + + def create_dossier_for_month(procedure, year, month) + Timecop.freeze(Time.zone.local(year, month, 5)) do + create(:dossier, :accepte, :with_attestation, procedure: procedure) + end + end + + def create_archived_dossier_for_month(procedure, year, month) + Timecop.freeze(Time.zone.local(year, month, 5)) do + create(:dossier, :accepte, :archived, :with_attestation, procedure: procedure) + end + end end diff --git a/spec/models/traitement_spec.rb b/spec/models/traitement_spec.rb deleted file mode 100644 index d84bdeedc..000000000 --- a/spec/models/traitement_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -describe Traitement do - describe '#count_dossiers_termines_by_month' do - let(:procedure) { create(:procedure, :published, groupe_instructeurs: [groupe_instructeurs]) } - let(:groupe_instructeurs) { create(:groupe_instructeur) } - - before do - create_dossier_for_month(procedure, 2021, 3) - create_dossier_for_month(procedure, 2021, 3) - create_archived_dossier_for_month(procedure, 2021, 3) - create_dossier_for_month(procedure, 2021, 2) - end - - subject do - Timecop.freeze(Time.zone.local(2021, 3, 5)) do - Traitement.count_dossiers_termines_by_month(groupe_instructeurs) - end - end - - it 'count dossiers_termines by month' do - expect(count_for_month(subject, 3)).to eq 3 - expect(count_for_month(subject, 2)).to eq 1 - end - - it 'returns descending order by month' do - expect(subject[0]["month"].to_date.month).to eq 3 - expect(subject[1]["month"].to_date.month).to eq 2 - end - end - - private - - def count_for_month(count_dossiers_termines_by_month, month) - count_dossiers_termines_by_month.find do |count_by_month| - count_by_month["month"].to_date.month == month - end["count"] - end - - def create_dossier_for_month(procedure, year, month) - Timecop.freeze(Time.zone.local(year, month, 5)) do - create(:dossier, :accepte, :with_attestation, procedure: procedure) - end - end - - def create_archived_dossier_for_month(procedure, year, month) - Timecop.freeze(Time.zone.local(year, month, 5)) do - create(:dossier, :accepte, :archived, :with_attestation, procedure: procedure) - end - end -end diff --git a/spec/views/shared/archives/_table.html.haml_spec.rb b/spec/views/shared/archives/_table.html.haml_spec.rb index 688e3a760..298603b34 100644 --- a/spec/views/shared/archives/_table.html.haml_spec.rb +++ b/spec/views/shared/archives/_table.html.haml_spec.rb @@ -3,21 +3,21 @@ RSpec.describe 'shared/archives/_table.html.haml', type: :view do let(:procedure) { create(:procedure) } let(:all_archives) { create_list(:archive, 2) } - let(:month_date) { "2022-01-01T00:00:00+00:00" } + let(:month_date) { DateTime.parse("2022-01-01T00:00:00+00:00") } let(:average_dossier_weight) { 1024 } before do allow(view).to receive(:create_archive_url).and_return("/archive/created/stubed") end - subject { render 'shared/archives/table.html.haml', count_dossiers_termines_by_month: [{ "month" => month_date, "count" => 5 }], archives: all_archives + [monthly_archive].compact, average_dossier_weight: average_dossier_weight, procedure: procedure } + subject { render 'shared/archives/table.html.haml', count_dossiers_termines_by_month: { month_date => 5 }, archives: all_archives + [monthly_archive].compact, average_dossier_weight: average_dossier_weight, procedure: procedure } context "when archive is available" do let(:monthly_archive) { create(:archive, time_span_type: "monthly", month: month_date, job_status: :generated, file: Rack::Test::UploadedFile.new('spec/fixtures/files/RIB.pdf', 'application/pdf')) } it 'renders archive by month with estimate_weight' do expect(subject).to have_text("Janvier 2022") expect(subject).to have_text("Télécharger") - expect(subject).to have_text("338 ko") + expect(subject).to have_text("5 ko") end end