Gestionnaire: add last_week_overview method

This commit is contained in:
Simon Lehericey 2017-05-12 16:47:18 +02:00
parent e7d39ca650
commit e630548110
2 changed files with 68 additions and 0 deletions

View file

@ -92,6 +92,26 @@ class Gestionnaire < ActiveRecord::Base
notifications.pluck(:dossier_id).uniq.count
end
def last_week_overview
start_date = DateTime.now.beginning_of_week
active_procedure_overviews = procedures
.where(published: true)
.all
.map { |procedure| procedure.procedure_overview(start_date, dossiers_with_notifications_count_for_procedure(procedure)) }
.select(&:had_some_activities?)
if active_procedure_overviews.count == 0 && notifications.count == 0
nil
else
{
start_date: start_date,
procedure_overviews: active_procedure_overviews,
notifications: notifications
}
end
end
private
def valid_couple_table_attr? table, column

View file

@ -347,4 +347,52 @@ describe Gestionnaire, type: :model do
end
end
end
describe 'last_week_overview' do
let!(:gestionnaire2) { create(:gestionnaire) }
subject { gestionnaire2.last_week_overview }
let(:friday) { DateTime.new(2017, 5, 12) }
let(:monday) { DateTime.now.beginning_of_week }
before :each do
Timecop.freeze(friday)
end
context 'when no procedure published was active last week' do
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published: true) }
context 'when the gestionnaire has no notifications' do
it { is_expected.to eq(nil) }
end
context 'when the gestionnaire has one notification' do
before :each do
expect(gestionnaire2).to receive(:notifications).twice.and_return([1])
end
it { is_expected.to eq({ start_date: monday, procedure_overviews: [], notifications: [1] }) }
end
end
context 'when a procedure published was active' do
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published: true) }
let(:procedure_overview) { double('procedure_overview', 'had_some_activities?'.to_sym => true) }
before :each do
expect_any_instance_of(Procedure).to receive(:procedure_overview).and_return(procedure_overview)
end
it { expect(gestionnaire.last_week_overview[:procedure_overviews]).to match([procedure_overview]) }
end
context 'when a procedure not published was active with no notifications' do
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published: false) }
let(:procedure_overview) { double('procedure_overview', 'had_some_activities?'.to_sym => true) }
before :each do
allow_any_instance_of(Procedure).to receive(:procedure_overview).and_return(procedure_overview)
end
it { is_expected.to eq(nil) }
end
end
end