fix counter for dossiers suivis/à suivre when unfollow

This commit is contained in:
clemkeirua 2020-10-15 14:25:17 +02:00 committed by LeSim (Rebase PR Action)
parent 640984f9b6
commit 89316c0689
2 changed files with 14 additions and 3 deletions

View file

@ -633,8 +633,8 @@ class Procedure < ApplicationRecord
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 ('en_construction', 'en_instruction') and (("follows"."id" IS NULL) or ("follows"."instructeur_id" = :instructeur_id and "follows"."unfollowed_at" < :now)) 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" and "follows"."unfollowed_at" IS NULL) 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
@ -663,7 +663,8 @@ class Procedure < ApplicationRecord
sanitized_query = ActiveRecord::Base.sanitize_sql([
query,
instructeur_id: instructeur.id,
procedure_id: self.id
procedure_id: self.id,
now: Time.zone.now
])
Procedure.connection.select_all(sanitized_query).first || { "a_suivre" => 0, "suivis" => 0, "termines" => 0, "total" => 0, "archived" => 0 }

View file

@ -1102,6 +1102,16 @@ describe Procedure do
it { expect(subject['suivis']).to eq(2) }
it { expect(subject['total']).to eq(2) }
end
context 'and dossier with a follower is unfollowed' do
before do
instructeur.unfollow(new_followed_dossier)
end
it { expect(subject['a_suivre']).to eq(1) }
it { expect(subject['suivis']).to eq(0) }
it { expect(subject['total']).to eq(1) }
end
end
context 'with a termine dossier' do