ProcedureOverview: reboot model and layout

This commit is contained in:
Simon Lehericey 2017-06-27 18:00:05 +02:00
parent 689d0c2dd7
commit 603e2dd020
6 changed files with 138 additions and 138 deletions

View file

@ -106,16 +106,15 @@ class Gestionnaire < ActiveRecord::Base
active_procedure_overviews = procedures active_procedure_overviews = procedures
.where(published: true) .where(published: true)
.all .all
.map { |procedure| procedure.procedure_overview(start_date, dossiers_with_notifications_count_for_procedure(procedure)) } .map { |procedure| procedure.procedure_overview(start_date) }
.select(&:had_some_activities?) .select(&:had_some_activities?)
if active_procedure_overviews.count == 0 && notifications.count == 0 if active_procedure_overviews.count == 0
nil nil
else else
{ {
start_date: start_date, start_date: start_date,
procedure_overviews: active_procedure_overviews, procedure_overviews: active_procedure_overviews,
notifications: notifications
} }
end end
end end

View file

@ -135,8 +135,8 @@ class Procedure < ActiveRecord::Base
} }
end end
def procedure_overview(start_date, notifications_count) def procedure_overview(start_date)
ProcedureOverview.new(self, start_date, notifications_count) ProcedureOverview.new(self, start_date)
end end
def initiated_mail_template def initiated_mail_template

View file

@ -1,82 +1,72 @@
class ProcedureOverview class ProcedureOverview
include Rails.application.routes.url_helpers attr_accessor :procedure,
attr_accessor :libelle, :notifications_count, :received_dossiers_count, :created_dossiers_count, :processed_dossiers_count, :date :created_dossiers_count,
:dossiers_en_instruction_count,
:old_dossiers_en_instruction,
:dossiers_en_construction_count,
:old_dossiers_en_construction
def initialize(procedure, start_date, notifications_count) def initialize(procedure, start_date)
@libelle = procedure.libelle @start_date = start_date
@procedure_url = backoffice_dossiers_procedure_url(procedure) @procedure = procedure
@notifications_count = notifications_count
@received_dossiers_count = procedure.dossiers.where(state: :received).count @dossiers_en_instruction_count = procedure.dossiers.state_en_instruction.count
@created_dossiers_count = procedure.dossiers @old_dossiers_en_instruction = procedure
.dossiers
.state_en_instruction
.where('received_at < ?', 1.week.ago)
@dossiers_en_construction_count = procedure.dossiers.state_en_construction.count
@old_dossiers_en_construction = procedure
.dossiers
.state_en_construction
.where('initiated_at < ?', 1.week.ago)
@created_dossiers_count = procedure
.dossiers
.where(created_at: start_date..DateTime.now) .where(created_at: start_date..DateTime.now)
.where.not(state: :draft) .state_not_brouillon
.count .count
@processed_dossiers_count = procedure.dossiers.where(processed_at: start_date..DateTime.now).count
end end
def had_some_activities? def had_some_activities?
[received_dossiers_count, [@dossiers_en_instruction_count,
created_dossiers_count, @dossiers_en_construction_count,
processed_dossiers_count, @created_dossiers_count].reduce(:+) > 0
notifications_count].reduce(:+) > 0
end end
def to_html def dossiers_en_construction_description
[libelle_description, case @dossiers_en_construction_count
dossiers_en_instruction_description,
created_dossier_description,
processed_dossier_description,
notifications_description].compact.join('<br>')
end
private
def libelle_description
"<a href='#{@procedure_url}'><strong>#{libelle}</strong></a>"
end
def dossiers_en_instruction_description
case received_dossiers_count
when 0 when 0
nil nil
when 1 when 1
"1 dossier est en cours d'instruction" 'dossier suivi en construction'
else else
"#{received_dossiers_count} dossiers sont en cours d'instruction" 'dossiers suivis en construction'
end
end
def dossiers_en_instruction_description
case @dossiers_en_instruction_count
when 0
nil
when 1
"dossier est en cours d'instruction"
else
"dossiers sont en cours d'instruction"
end end
end end
def created_dossier_description def created_dossier_description
case created_dossiers_count formated_date = I18n.l(@start_date, format: '%d %B %Y')
when 0
nil
when 1
'1 nouveau dossier a été déposé'
else
"#{created_dossiers_count} nouveaux dossiers ont été déposés"
end
end
def processed_dossier_description case @created_dossiers_count
case processed_dossiers_count
when 0 when 0
nil nil
when 1 when 1
'1 dossier a été instruit' "nouveau dossier a été déposé depuis le #{formated_date}"
else else
"#{processed_dossiers_count} dossiers ont été instruits" "nouveaux dossiers ont été déposés depuis le #{formated_date}"
end
end
def notifications_description
case notifications_count
when 0
nil
when 1
'1 notification en attente sur les dossiers que vous suivez'
else
"#{notifications_count} notifications en attente sur les dossiers que vous suivez"
end end
end end
end end

View file

@ -1,26 +1,44 @@
%table{ align: 'center', border: '0', cellpadding: '0', cellspacing: '0', height: '100%', style: 'background-color: #fafafa', width: '100%' } - content_for(:title, 'Vos activités sur TPS')
%tbody
%tr
%td{ align: 'center', style: 'height: 100%; margin: 0; padding: 30px; width: 100%; border-top: 0', valign: 'top' }
%table{ border: '0', cellpadding: '0', cellspacing: '0', style: 'border-collapse: collapse; border: 0; max-width: 600px!important;', width: '100%' }
%tbody
%tr
%td{ style: 'background: #ffffff none no-repeat center/cover; background-color: #ffffff; background-image: none; background-repeat: no-repeat; background-position: center; background-size: cover; border-top: 0; padding-top: 0;', valign: 'top' }
%table{ border: '0', cellpadding: '0', cellspacing: '0', style: 'min-width: 100%; border-collapse: collapse', width: '100%' }
%tr
%td{ style: 'padding: 0 30px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; ', valign: 'top' }
%img{ align: 'middle', alt: 'Logo TPS', src: image_url('mailer/gestionnaire_mailer/logo.png'), style: 'max-width: 125px; padding: 30px 0; display: inline !important; vertical-align: bottom; border: 0; height: auto; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic;' }
%tr
%td{ style: 'padding: 0 30px 30px; word-break: break-word; color: #333333; font-family: Helvetica; font-size: 16px; line-height: 150%; text-align: left; border-bottom: 2px solid #4393F3;', valign: 'top' }
Bonjour, voici votre résumé de l'activité de la semaine du #{l(@args[:start_date], format: '%d %B')} au #{l(DateTime.now, format: '%d %B')}.
%br
%br
- @args[:procedure_overviews].each do |procedure_overview| - @args[:procedure_overviews].each_with_index do |procedure_overview, index|
= procedure_overview.to_html.html_safe
%br %h2{ style: 'font-size: 20px; font-weight: 300; margin: 25px 0 5px;' }
%br #{procedure_overview.procedure.libelle}
Bonne journée, = link_to 'voir', backoffice_dossiers_procedure_url(procedure_overview.procedure), style: 'color: #4393F3; font-size: 14px;'
%br %table{ cellpadding: '0', cellspacing: '0', style: 'width: 100%; padding-bottom: 20px;' }
%br %tbody
L'équipe Téléprocédures Simplifiées
- if procedure_overview.created_dossiers_count > 0
%tr
%td{ style: 'padding: 2px 0;' }
%span{ style: 'font-weight: bold;' }= procedure_overview.created_dossiers_count
= procedure_overview.created_dossier_description
- if procedure_overview.dossiers_en_construction_count > 0
%tr
%td{ style: 'padding: 2px 0;' }
%span{ style: 'font-weight: bold;' }= procedure_overview.dossiers_en_construction_count
= procedure_overview.dossiers_en_construction_description
- if procedure_overview.old_dossiers_en_construction.count > 0
dont <span style='font-weight: bold; color: #FF5D60; padding: 2px 0;' >#{procedure_overview.old_dossiers_en_construction.count}</span> depuis plus de 7 jours
- if procedure_overview.old_dossiers_en_construction.count < 6
\:
= procedure_overview.old_dossiers_en_construction.map do |old_dossier|
- link_to "nº #{old_dossier.id}", backoffice_dossier_url(old_dossier), style: 'color: #4393F3;'
- end.join(', ').html_safe
- if procedure_overview.dossiers_en_instruction_count > 0
%tr
%td{ style: 'padding: 2px 0;' }
%span{ style: 'font-weight: bold;' }= procedure_overview.dossiers_en_instruction_count
= procedure_overview.dossiers_en_instruction_description
- if procedure_overview.old_dossiers_en_instruction.count > 0
dont <span style='font-weight: bold; color: #FF5D60; padding: 2px 0;' >#{procedure_overview.old_dossiers_en_instruction.count}</span> depuis plus de 7 jours
- if procedure_overview.old_dossiers_en_instruction.count < 6
\:
= procedure_overview.old_dossiers_en_instruction.each do |old_dossier|
- link_to "nº #{old_dossier.id}", backoffice_dossier_url(old_dossier), style: 'color: #4393F3;'
- end.join(', ').html_safe
- if index != (@args[:procedure_overviews].count - 1)
.spacer{ style: 'border-bottom: 1px solid #CCC; margin: 25px 0 30px;' }

View file

@ -365,14 +365,6 @@ describe Gestionnaire, type: :model do
context 'when the gestionnaire has no notifications' do context 'when the gestionnaire has no notifications' do
it { is_expected.to eq(nil) } it { is_expected.to eq(nil) }
end 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 end
context 'when a procedure published was active' do context 'when a procedure published was active' do

View file

@ -9,14 +9,36 @@ describe ProcedureOverview, type: :model do
Timecop.freeze(friday) Timecop.freeze(friday)
end end
let(:procedure_overview) { ProcedureOverview.new(procedure, monday, 0) } let(:procedure_overview) { ProcedureOverview.new(procedure, monday) }
describe 'received_dossiers_count' do describe 'dossiers_en_instruction_count' do
let!(:received_dossier) do let!(:en_instruction_dossier) do
dossier = create(:dossier, procedure: procedure, state: :received, created_at: monday) create(:dossier, procedure: procedure, state: :received, created_at: monday)
end end
it { expect(procedure_overview.received_dossiers_count).to eq(1) } it { expect(procedure_overview.dossiers_en_instruction_count).to eq(1) }
end
describe 'old_dossiers_en_instruction' do
let!(:old_dossier_en_instruction) do
create(:dossier, procedure: procedure, state: :received, received_at: monday - 1.month)
end
let!(:dossier_en_instruction) do
create(:dossier, procedure: procedure, state: :received, received_at: monday)
end
it do
expect(procedure_overview.old_dossiers_en_instruction).to match([old_dossier_en_instruction])
end
end
describe 'dossiers_en_construction_count' do
let!(:dossier_en_construction) do
create(:dossier, procedure: procedure, state: :initiated, created_at: monday)
end
it { expect(procedure_overview.dossiers_en_construction_count).to eq(1) }
end end
describe 'created_dossiers_count' do describe 'created_dossiers_count' do
@ -35,53 +57,32 @@ describe ProcedureOverview, type: :model do
it { expect(procedure_overview.created_dossiers_count).to eq(1) } it { expect(procedure_overview.created_dossiers_count).to eq(1) }
end end
describe 'processed_dossiers_count' do describe 'had_some_activities?' do
let!(:processed_dossier_during_the_week) do subject { procedure_overview.had_some_activities? }
create(:dossier, procedure: procedure, created_at: monday, processed_at: monday)
end
let!(:processed_dossier_before_the_week) do
create(:dossier, procedure: procedure, created_at: (monday - 1.week), processed_at: (monday - 1.week))
end
it { expect(procedure_overview.processed_dossiers_count).to eq(1) }
end
describe 'to_html' do
subject { procedure_overview.to_html }
context 'when the different count are equal to 0' do
it { is_expected.to match(/^<a href='http:\/\/.+'><strong>libelle<\/strong><\/a>$/) }
end
context 'when the different counts are equal to 1' do
before :each do before :each do
procedure_overview.notifications_count = 1 procedure_overview.dossiers_en_instruction_count = 0
procedure_overview.received_dossiers_count = 1 procedure_overview.dossiers_en_construction_count = 0
procedure_overview.created_dossiers_count = 1 procedure_overview.created_dossiers_count = 0
procedure_overview.processed_dossiers_count = 1
end end
it { is_expected.to match(/^<a href='.+'><strong>libelle<\/strong><\/a>/) } context 'when there are no activities' do
it { is_expected.to include("1 dossier est en cours d'instruction") } it { is_expected.to be false }
it { is_expected.to include('1 nouveau dossier a été déposé') }
it { is_expected.to include('1 dossier a été instruit') }
it { is_expected.to include('1 notification en attente sur les dossiers que vous suivez') }
end end
context 'when the different counts are equal to 2' do context 'when there are some dossiers en instruction' do
before :each do before { procedure_overview.dossiers_en_instruction_count = 2 }
procedure_overview.notifications_count = 2 it { is_expected.to be true }
procedure_overview.received_dossiers_count = 3
procedure_overview.created_dossiers_count = 4
procedure_overview.processed_dossiers_count = 5
end end
it { is_expected.to match(/^<a href='.+'><strong>libelle<\/strong><\/a>/) } context 'when there are some dossiers en construction' do
it { is_expected.to include("3 dossiers sont en cours d'instruction") } before { procedure_overview.dossiers_en_construction_count = 2 }
it { is_expected.to include('4 nouveaux dossiers ont été déposés') } it { is_expected.to be true }
it { is_expected.to include('5 dossiers ont été instruits') } end
it { is_expected.to include('2 notifications en attente sur les dossiers que vous suivez') }
context 'when there are some created dossiers' do
before { procedure_overview.created_dossiers_count = 2 }
it { is_expected.to be true }
end end
end end
end end