ajout de procedure.dossiers_count_for_instructeur

This commit is contained in:
clemkeirua 2020-10-14 15:40:28 +02:00 committed by LeSim (Rebase PR Action)
parent 5d7680150b
commit b4910c557d
2 changed files with 161 additions and 0 deletions

View file

@ -630,6 +630,45 @@ class Procedure < ApplicationRecord
draft_revision.deep_clone(include: [:revision_types_de_champ, :revision_types_de_champ_private]) draft_revision.deep_clone(include: [:revision_types_de_champ, :revision_types_de_champ_private])
end end
def dossiers_count_for_instructeur(instructeur)
query = <<-EOF
SELECT
COUNT(*) FILTER ( WHERE "dossiers"."state" in ('en_construction', 'en_instruction') and "follows"."id" IS NULL and not "dossiers"."archived" ) AS a_suivre,
COUNT(*) FILTER ( WHERE "dossiers"."state" in ('en_construction', 'en_instruction') and "follows"."instructeur_id" = :instructeur_id and not "dossiers"."archived" ) AS suivis,
COUNT(*) FILTER ( WHERE "dossiers"."state" in ('accepte', 'refuse', 'sans_suite') and not "dossiers"."archived" ) AS termines,
COUNT(*) FILTER ( WHERE "dossiers"."state" != 'brouillon' and not "dossiers"."archived" ) AS total,
COUNT(*) FILTER ( WHERE "dossiers"."archived" ) AS archived
FROM
"dossiers"
INNER JOIN
"groupe_instructeurs"
ON "dossiers"."groupe_instructeur_id" = "groupe_instructeurs"."id"
INNER JOIN
"assign_tos"
ON "groupe_instructeurs"."id" = "assign_tos"."groupe_instructeur_id"
INNER JOIN
"procedures"
ON "groupe_instructeurs"."procedure_id" = "procedures"."id"
LEFT OUTER JOIN
"follows"
ON "follows"."dossier_id" = "dossiers"."id"
WHERE
"dossiers"."hidden_at" IS NULL
AND "assign_tos"."instructeur_id" = :instructeur_id
AND "procedures"."id" = :procedure_id
GROUP BY
groupe_instructeurs.procedure_id, procedures.libelle
EOF
sanitized_query = ActiveRecord::Base.sanitize_sql([
query,
instructeur_id: instructeur.id,
procedure_id: self.id
])
Procedure.connection.select_all(sanitized_query).first || { "a_suivre" => 0, "suivis" => 0, "termines" => 0, "total" => 0, "archived" => 0 }
end
private private
def before_publish def before_publish

View file

@ -1023,4 +1023,126 @@ describe Procedure do
it { is_expected.to be false } it { is_expected.to be false }
end end
end end
describe "#dossiers_count_for_instructeur" do
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, instructeurs: [instructeur]) }
let(:gi_2) { procedure.groupe_instructeurs.create(label: '2') }
let(:gi_3) { procedure.groupe_instructeurs.create(label: '3') }
subject do
procedure.dossiers_count_for_instructeur(instructeur)
end
context "when logged in, and belonging to gi_1, gi_2" do
before do
instructeur.groupe_instructeurs << gi_2
end
context "without any dossier" do
it { expect(subject['a_suivre']).to eq(0) }
it { expect(subject['suivis']).to eq(0) }
it { expect(subject['termines']).to eq(0) }
it { expect(subject['total']).to eq(0) }
it { expect(subject['archived']).to eq(0) }
end
context 'with a new brouillon dossier' do
let!(:brouillon_dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:brouillon)) }
it { expect(subject['a_suivre']).to eq(0) }
it { expect(subject['suivis']).to eq(0) }
it { expect(subject['termines']).to eq(0) }
it { expect(subject['total']).to eq(0) }
it { expect(subject['archived']).to eq(0) }
end
context 'with a new dossier without follower' do
let!(:new_unfollow_dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_instruction)) }
it { expect(subject['a_suivre']).to eq(1) }
it { expect(subject['suivis']).to eq(0) }
it { expect(subject['termines']).to eq(0) }
it { expect(subject['total']).to eq(1) }
it { expect(subject['archived']).to eq(0) }
context 'and dossiers without follower on each of the others groups' do
let!(:new_unfollow_dossier_on_gi_2) { create(:dossier, groupe_instructeur: gi_2, state: Dossier.states.fetch(:en_instruction)) }
let!(:new_unfollow_dossier_on_gi_3) { create(:dossier, groupe_instructeur: gi_3, state: Dossier.states.fetch(:en_instruction)) }
before { subject }
it { expect(subject['a_suivre']).to eq(2) }
it { expect(subject['total']).to eq(2) }
end
end
context 'with a new dossier with a follower' do
let!(:new_followed_dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_instruction)) }
before do
instructeur.followed_dossiers << new_followed_dossier
end
it { expect(subject['a_suivre']).to eq(0) }
it { expect(subject['suivis']).to eq(1) }
it { expect(subject['termines']).to eq(0) }
it { expect(subject['total']).to eq(1) }
it { expect(subject['archived']).to eq(0) }
context 'and dossier with a follower on each of the others groups' do
let!(:new_follow_dossier_on_gi_2) { create(:dossier, groupe_instructeur: gi_2, state: Dossier.states.fetch(:en_instruction)) }
let!(:new_follow_dossier_on_gi_3) { create(:dossier, groupe_instructeur: gi_3, state: Dossier.states.fetch(:en_instruction)) }
before do
instructeur.followed_dossiers << new_follow_dossier_on_gi_2 << new_follow_dossier_on_gi_3
end
# followed dossiers on another groupe should not be displayed
it { expect(subject['suivis']).to eq(2) }
it { expect(subject['total']).to eq(2) }
end
end
context 'with a termine dossier' do
let!(:termine_dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:accepte)) }
it { expect(subject['a_suivre']).to eq(0) }
it { expect(subject['suivis']).to eq(0) }
it { expect(subject['termines']).to eq(1) }
it { expect(subject['total']).to eq(1) }
it { expect(subject['archived']).to eq(0) }
context 'and terminer dossiers on each of the others groups' do
let!(:termine_dossier_on_gi_2) { create(:dossier, groupe_instructeur: gi_2, state: Dossier.states.fetch(:accepte)) }
let!(:termine_dossier_on_gi_3) { create(:dossier, groupe_instructeur: gi_3, state: Dossier.states.fetch(:accepte)) }
before { subject }
it { expect(subject['a_suivre']).to eq(0) }
it { expect(subject['suivis']).to eq(0) }
it { expect(subject['termines']).to eq(2) }
it { expect(subject['total']).to eq(2) }
it { expect(subject['archived']).to eq(0) }
end
end
context 'with an archived dossier' do
let!(:archived_dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_instruction), archived: true) }
it { expect(subject['a_suivre']).to eq(0) }
it { expect(subject['suivis']).to eq(0) }
it { expect(subject['termines']).to eq(0) }
it { expect(subject['total']).to eq(0) }
it { expect(subject['archived']).to eq(1) }
context 'and terminer dossiers on each of the others groups' do
let!(:archived_dossier_on_gi_2) { create(:dossier, groupe_instructeur: gi_2, state: Dossier.states.fetch(:en_instruction), archived: true) }
let!(:archived_dossier_on_gi_3) { create(:dossier, groupe_instructeur: gi_3, state: Dossier.states.fetch(:en_instruction), archived: true) }
it { expect(subject['archived']).to eq(2) }
end
end
end
end
end end