From a298c48e8f79316a3f44c27f58bbf0b7ae2d41f1 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Wed, 30 Aug 2017 15:31:36 +0200 Subject: [PATCH] Dossier: add notification icon --- .../new_gestionnaire/dossiers_controller.rb | 3 +++ app/models/dossier.rb | 10 +++++++ app/models/notification.rb | 22 +++++++++++++++- .../dossiers/_header.html.haml | 7 +++++ .../dossiers_controller_spec.rb | 26 ++++++++++++++++--- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index e76364587..cc35adbd3 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -6,14 +6,17 @@ module NewGestionnaire def show @dossier = dossier + dossier.notifications.demande.mark_as_read end def messagerie @dossier = dossier + dossier.notifications.messagerie.mark_as_read end def instruction @dossier = dossier + dossier.notifications.instruction.mark_as_read end def follow diff --git a/app/models/dossier.rb b/app/models/dossier.rb index b8e7530f4..0c30adc50 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -104,6 +104,16 @@ class Dossier < ActiveRecord::Base pieces_justificatives.where(type_de_piece_justificative_id: type_id).count > 0 end + def notifications_summary + unread_notifications = notifications.unread + + { + demande: unread_notifications.select(&:demande?).present?, + instruction: unread_notifications.select(&:instruction?).present?, + messagerie: unread_notifications.select(&:messagerie?).present? + } + end + def retrieve_last_piece_justificative_by_type(type) pieces_justificatives.where(type_de_piece_justificative_id: type).last end diff --git a/app/models/notification.rb b/app/models/notification.rb index 53d8af02d..b2cd552fe 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -8,7 +8,27 @@ class Notification < ActiveRecord::Base avis: 'avis' } + DEMANDE = %w(cerfa piece_justificative champs submitted) + INSTRUCTION = %w(avis) + MESSAGERIE = %w(commentaire) + belongs_to :dossier - scope :unread, -> { where(already_read: false) } + scope :unread, -> { where(already_read: false) } + scope :demande, -> { where(type_notif: DEMANDE) } + scope :instruction, -> { where(type_notif: INSTRUCTION) } + scope :messagerie, -> { where(type_notif: MESSAGERIE) } + scope :mark_as_read, -> { update_all(already_read: true) } + + def demande? + Notification::DEMANDE.include?(type_notif) + end + + def instruction? + Notification::INSTRUCTION.include?(type_notif) + end + + def messagerie? + Notification::MESSAGERIE.include?(type_notif) + end end diff --git a/app/views/new_gestionnaire/dossiers/_header.html.haml b/app/views/new_gestionnaire/dossiers/_header.html.haml index 01d750f9e..914c46e1e 100644 --- a/app/views/new_gestionnaire/dossiers/_header.html.haml +++ b/app/views/new_gestionnaire/dossiers/_header.html.haml @@ -6,11 +6,18 @@ %li = "Dossier n° #{dossier.id}" %ul.tabs + - notifications_summary = dossier.notifications_summary %li{ class: current_page?(dossier_path(dossier.procedure, dossier)) ? 'active' : nil } + - if notifications_summary[:demande] + %span.notifications{ 'aria-label': 'notifications' } = link_to "Demande", dossier_path(dossier.procedure, dossier) %li{ class: current_page?(instruction_dossier_path(dossier.procedure, dossier)) ? 'active' : nil } + - if notifications_summary[:instruction] + %span.notifications{ 'aria-label': 'notifications' } = link_to "Instruction", instruction_dossier_path(dossier.procedure, dossier) %li{ class: current_page?(messagerie_dossier_path(dossier.procedure, dossier)) ? 'active' : nil } + - if notifications_summary[:messagerie] + %span.notifications{ 'aria-label': 'notifications' } = link_to "Messagerie", messagerie_dossier_path(dossier.procedure, dossier) %li = link_to "Historique", "#" diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 032cc83e9..084a52ed2 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -72,10 +72,30 @@ describe NewGestionnaire::DossiersController, type: :controller do it { expect(response).to redirect_to(procedures_url) } end - describe "#show" do - before { get :show, params: { procedure_id: procedure.id, dossier_id: dossier.id } } + describe '#show #messagerie #instruction' do + before do + dossier.notifications = %w(champs avis commentaire).map{ |type| Notification.create!(type_notif: type) } + get method, params: { procedure_id: procedure.id, dossier_id: dossier.id } + dossier.notifications.each(&:reload) + end - it { expect(response).to have_http_status(:success) } + context '#show' do + let(:method) { :show } + it { expect(dossier.notifications.map(&:already_read)).to match([true, false, false]) } + it { expect(response).to have_http_status(:success) } + end + + context '#instruction' do + let(:method) { :instruction } + it { expect(dossier.notifications.map(&:already_read)).to match([false, true, false]) } + it { expect(response).to have_http_status(:success) } + end + + context '#messagerie' do + let(:method) { :messagerie } + it { expect(dossier.notifications.map(&:already_read)).to match([false, false, true]) } + it { expect(response).to have_http_status(:success) } + end end describe "#create_commentaire" do