Merge pull request #8369 from demarches-simplifiees/8354-dossiers-count

ETQ admin, je peux voir le nb de dossiers par démarche dans la liste de toutes les démarches
This commit is contained in:
krichtof 2023-02-21 10:14:00 +01:00 committed by GitHub
commit 6e2845893b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 68 additions and 4 deletions

View file

@ -418,7 +418,7 @@ module Administrateurs
procedures_result = procedures_result.where('unaccent(libelle) ILIKE unaccent(?)', "%#{filter.libelle}%") if filter.libelle.present? procedures_result = procedures_result.where('unaccent(libelle) ILIKE unaccent(?)', "%#{filter.libelle}%") if filter.libelle.present?
procedures_sql = procedures_result.to_sql procedures_sql = procedures_result.to_sql
sql = "select id, libelle, published_at, aasm_state, count(administrateurs_procedures.administrateur_id) as admin_count from administrateurs_procedures inner join procedures on procedures.id = administrateurs_procedures.procedure_id where procedures.id in (#{procedures_sql}) group by procedures.id order by published_at desc" sql = "select id, libelle, published_at, aasm_state, estimated_dossiers_count, count(administrateurs_procedures.administrateur_id) as admin_count from administrateurs_procedures inner join procedures on procedures.id = administrateurs_procedures.procedure_id where procedures.id in (#{procedures_sql}) group by procedures.id order by published_at desc"
ActiveRecord::Base.connection.execute(sql) ActiveRecord::Base.connection.execute(sql)
end end

View file

@ -874,6 +874,7 @@ class Dossier < ApplicationRecord
.passer_en_construction .passer_en_construction
.processed_at .processed_at
save! save!
procedure.compute_dossiers_count
end end
def after_passer_en_instruction(h) def after_passer_en_instruction(h)

View file

@ -15,10 +15,12 @@
# closed_at :datetime # closed_at :datetime
# declarative_with_state :string # declarative_with_state :string
# description :string # description :string
# dossiers_count_computed_at :datetime
# duree_conservation_dossiers_dans_ds :integer # duree_conservation_dossiers_dans_ds :integer
# duree_conservation_etendue_par_ds :boolean default(FALSE) # duree_conservation_etendue_par_ds :boolean default(FALSE)
# encrypted_api_particulier_token :string # encrypted_api_particulier_token :string
# estimated_duration_visible :boolean default(TRUE), not null # estimated_duration_visible :boolean default(TRUE), not null
# estimated_dossiers_count :integer
# euro_flag :boolean default(FALSE) # euro_flag :boolean default(FALSE)
# experts_require_administrateur_invitation :boolean default(FALSE) # experts_require_administrateur_invitation :boolean default(FALSE)
# for_individual :boolean default(FALSE) # for_individual :boolean default(FALSE)
@ -71,6 +73,8 @@ class Procedure < ApplicationRecord
MIN_WEIGHT = 350000 MIN_WEIGHT = 350000
DOSSIERS_COUNT_EXPIRING = 1.hour
attr_encrypted :api_particulier_token attr_encrypted :api_particulier_token
has_many :revisions, -> { order(:id) }, class_name: 'ProcedureRevision', inverse_of: :procedure has_many :revisions, -> { order(:id) }, class_name: 'ProcedureRevision', inverse_of: :procedure
@ -836,7 +840,13 @@ class Procedure < ApplicationRecord
self.connection.query(query.to_sql).flatten self.connection.query(query.to_sql).flatten
end end
private def compute_dossiers_count
now = Time.zone.now
if now > (self.dossiers_count_computed_at || self.created_at) + DOSSIERS_COUNT_EXPIRING
self.update(estimated_dossiers_count: self.dossiers.visible_by_administration.count,
dossiers_count_computed_at: now)
end
end
def move_new_children_to_new_parent_coordinate(new_draft) def move_new_children_to_new_parent_coordinate(new_draft)
children = new_draft.revision_types_de_champ children = new_draft.revision_types_de_champ

View file

@ -8,6 +8,7 @@
%td= procedure.libelle %td= procedure.libelle
%td= procedure.id %td= procedure.id
%td= procedure.estimated_dossiers_count
%td= procedure.administrateurs.count %td= procedure.administrateurs.count
%td= t procedure.aasm_state, scope: 'activerecord.attributes.procedure.aasm_state' %td= t procedure.aasm_state, scope: 'activerecord.attributes.procedure.aasm_state'
%td= l(procedure.published_at, format: :message_date_without_time) %td= l(procedure.published_at, format: :message_date_without_time)
@ -16,7 +17,7 @@
- if show_detail - if show_detail
%tr.procedure{ id: "procedure_detail_#{procedure.id}" } %tr.procedure{ id: "procedure_detail_#{procedure.id}" }
%td.fr-highlight--beige-gris-galet{ colspan: '7' } %td.fr-highlight--beige-gris-galet{ colspan: '8' }
.fr-container .fr-container
.fr-grid-row .fr-grid-row
.fr-col-6 .fr-col-6

View file

@ -41,6 +41,7 @@
%th{ scope: 'col' } %th{ scope: 'col' }
%th{ scope: 'col' } Démarche %th{ scope: 'col' } Démarche
%th{ scope: 'col' } № %th{ scope: 'col' } №
%th{ scope: 'col' } Dossiers
%th{ scope: 'col' } Administrateurs %th{ scope: 'col' } Administrateurs
%th{ scope: 'col' } Statut %th{ scope: 'col' } Statut
%th{ scope: 'col' } Date %th{ scope: 'col' } Date

View file

@ -0,0 +1,6 @@
class AddEstimatedDossiersCountAndDossiersCountComputedAtToProcedures < ActiveRecord::Migration[6.1]
def change
add_column :procedures, :estimated_dossiers_count, :integer
add_column :procedures, :dossiers_count_computed_at, :datetime
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2023_02_16_141558) do ActiveRecord::Schema.define(version: 2023_02_17_094119) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto" enable_extension "pgcrypto"
@ -703,12 +703,14 @@ ActiveRecord::Schema.define(version: 2023_02_16_141558) do
t.string "declarative_with_state" t.string "declarative_with_state"
t.string "description" t.string "description"
t.string "direction" t.string "direction"
t.datetime "dossiers_count_computed_at"
t.bigint "draft_revision_id" t.bigint "draft_revision_id"
t.integer "duree_conservation_dossiers_dans_ds" t.integer "duree_conservation_dossiers_dans_ds"
t.boolean "duree_conservation_etendue_par_ds", default: false t.boolean "duree_conservation_etendue_par_ds", default: false
t.boolean "durees_conservation_required", default: true t.boolean "durees_conservation_required", default: true
t.string "encrypted_api_particulier_token" t.string "encrypted_api_particulier_token"
t.boolean "estimated_duration_visible", default: true, null: false t.boolean "estimated_duration_visible", default: true, null: false
t.integer "estimated_dossiers_count"
t.boolean "euro_flag", default: false t.boolean "euro_flag", default: false
t.boolean "experts_require_administrateur_invitation", default: false t.boolean "experts_require_administrateur_invitation", default: false
t.boolean "for_individual", default: false t.boolean "for_individual", default: false

View file

@ -0,0 +1,20 @@
namespace :after_party do
desc 'Deployment task: update_procedure_dossiers_count'
task update_procedure_dossiers_count: :environment do
puts "Running deploy task 'update_procedure_dossiers_count'"
progress = ProgressReport.new(Procedure.count)
Procedure.find_each do |p|
progress.inc
begin
p.update_columns(estimated_dossiers_count: p.dossiers.visible_by_administration.count, dossiers_count_computed_at: Time.zone.now)
rescue => e
Sentry.capture_exception(e, extra: { procedure_id: p.id })
end
end
progress.finish
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -2080,6 +2080,15 @@ describe Dossier do
end end
end end
describe 'update procedure dossiers count' do
let(:dossier) { create(:dossier, :brouillon, :with_individual) }
it 'update procedure dossiers count when passing to construction' do
expect(dossier.procedure).to receive(:compute_dossiers_count)
dossier.passer_en_construction!
end
end
private private
def count_for_month(processed_by_month, month) def count_for_month(processed_by_month, month)

View file

@ -9,6 +9,20 @@ describe Procedure do
it { expect(subject.without_continuation_mail_template).to be_a(Mails::WithoutContinuationMail) } it { expect(subject.without_continuation_mail_template).to be_a(Mails::WithoutContinuationMail) }
end end
describe 'compute_dossiers_count' do
let(:procedure) { create(:procedure_with_dossiers, dossiers_count: 2, dossiers_count_computed_at: Time.zone.now - Procedure::DOSSIERS_COUNT_EXPIRING) }
it 'caches estimated_dossiers_count' do
procedure.dossiers.each(&:passer_en_construction!)
expect { procedure.compute_dossiers_count }.to change(procedure, :estimated_dossiers_count).from(nil).to(2)
expect { create(:dossier, procedure: procedure).passer_en_construction! }.not_to change(procedure, :estimated_dossiers_count)
Timecop.freeze(Time.zone.now + Procedure::DOSSIERS_COUNT_EXPIRING)
expect { procedure.compute_dossiers_count }.to change(procedure, :estimated_dossiers_count).from(2).to(3)
Timecop.return
end
end
describe 'initiated_mail' do describe 'initiated_mail' do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }