diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index ada358189..d646d7177 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -98,6 +98,10 @@ class Gestionnaire < ActiveRecord::Base 0 end + def dossiers_with_notifications_count + notifications.pluck(:dossier_id).uniq.count + end + private def valid_couple_table_attr? table, column diff --git a/app/models/notification.rb b/app/models/notification.rb index 2d93de8b3..af36da06e 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,8 +1,5 @@ class Notification < ActiveRecord::Base belongs_to :dossier - - # after_save :broadcast_notification - enum type_notif: { commentaire: 'commentaire', cerfa: 'cerfa', @@ -10,10 +7,5 @@ class Notification < ActiveRecord::Base champs: 'champs', submitted: 'submitted' } - - # def broadcast_notification - # ActionCable.server.broadcast 'notifications', - # message: "Dossier nº #{self.dossier.id} : #{self.liste.last}", - # dossier: {id: self.dossier.id} - # end + scope :unread, -> { where(already_read: false) } end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 7d3624dca..2e910acd3 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -3,6 +3,7 @@ class Procedure < ActiveRecord::Base has_many :types_de_champ, class_name: 'TypeDeChampPublic', dependent: :destroy has_many :types_de_champ_private, dependent: :destroy has_many :dossiers + has_many :notifications, through: :dossiers has_one :procedure_path, dependent: :destroy @@ -47,6 +48,8 @@ class Procedure < ActiveRecord::Base end scope :not_archived, -> { where(archived: false) } + scope :by_libelle, -> { order(libelle: :asc) } + def path procedure_path.path unless procedure_path.nil? diff --git a/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml b/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml index c990c6836..5d6e6c9d7 100644 --- a/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml +++ b/app/views/layouts/left_panels/_left_panel_backoffice_dossierscontroller_index.html.haml @@ -1,42 +1,42 @@ -%div#first-block +#first-block .dossiers-en-cours - .count= @facade_data_view.total_dossier_follow + .count= current_gestionnaire.dossiers_follow.count .text SUIVIS .nouveaux-dossiers - .count= @facade_data_view.total_new_dossier + .count= current_gestionnaire.dossiers.nouveaux.count .text NOUVEAUX .nouvelles-notifications - .count= @facade_data_view.dossiers_with_unread_notifications.count + .count= current_gestionnaire.dossiers_with_notifications_count .text MODIFIÉS -%div#action-block +#action-block -%div#menu-block +#menu-block .split-hr-left #switch-buttons #switch-procedures.active Procédures #switch-notifications Notifications -%div#infos-block +#infos-block .split-hr-left #procedure-list - - @facade_data_view.gestionnaire_procedures_name_and_id_list.each do |procedure| - - = link_to backoffice_dossiers_procedure_path(procedure[:id]), { title: procedure[:libelle] } do - .procedure-list-element{ class: ('active' if procedure[:id] == @facade_data_view.procedure.id rescue '') } - = truncate(procedure[:libelle], length: 50) - - total_new = @facade_data_view.new_dossier_number procedure[:id] + - current_gestionnaire.procedures.by_libelle.each do |procedure| + = link_to backoffice_dossiers_procedure_path(procedure.id), { title: procedure.libelle } do + .procedure-list-element{ class: ('active' if procedure.id.to_s == params[:id]) } + = procedure.libelle.truncate(50) + - total_new = procedure.dossiers.nouveaux.count - if total_new > 0 .badge.progress-bar-success{ title: 'Nouveaux dossiers' } = total_new - - if procedure[:unread_notifications] > 0 + - unread_notif_count = procedure.notifications.unread.count + - if unread_notif_count > 0 .badge.progress-bar-warning{ title: 'Notifications' } - = procedure[:unread_notifications] + = unread_notif_count #notifications-list.hidden - - if @facade_data_view.dossiers_with_unread_notifications.empty? + - if current_gestionnaire.notifications.empty? .no-notification Aucune notification pour le moment. - else - - @facade_data_view.dossiers_with_unread_notifications.each do |dossier| + - current_gestionnaire.notifications.includes(:dossier).map(&:dossier).uniq.each do |dossier| = link_to backoffice_dossier_path(dossier.id) do .notification .dossier-index= "Dossier nº #{dossier.id}" diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index 1d1ed1320..a9c88fa92 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -272,6 +272,37 @@ describe Gestionnaire, type: :model do it { is_expected.to be_nil } end end + end + describe '#dossiers_with_notifications_count' do + subject { gestionnaire.dossiers_with_notifications_count } + + context 'when there is no notifications' do + it { is_expected.to eq(0) } + end + + context 'when there is one notification for one dossier' do + let(:notification){ create(:notification, already_read: false) } + let!(:follow){ create(:follow, dossier: notification.dossier, gestionnaire: gestionnaire) } + + it { is_expected.to eq(1) } + end + + context 'when there are many notifications for one dossier' do + let(:notification){ create(:notification, already_read: false) } + let(:notification2){ create(:notification, already_read: false, dossier: notification.dossier) } + let!(:follow){ create(:follow, dossier: notification.dossier, gestionnaire: gestionnaire) } + + it { is_expected.to eq(1) } + end + + context 'when there are many notifications for many dossiers' do + let(:notification){ create(:notification, already_read: false) } + let(:notification2){ create(:notification, already_read: false) } + let!(:follow){ create(:follow, dossier: notification.dossier, gestionnaire: gestionnaire) } + let!(:follow2){ create(:follow, dossier: notification2.dossier, gestionnaire: gestionnaire) } + + it { is_expected.to eq(2) } + end end end