From 8e67e5c057e483b1b2e3b25401d5b5b6df45973e Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 5 Dec 2017 17:20:10 +0100 Subject: [PATCH] [Fix #1016] display notifications for new messages --- .../javascripts/new_design/messagerie.js | 18 +++++++++++- .../new_gestionnaire/dossiers_controller.rb | 9 ++++-- .../avis/messagerie.html.haml | 2 +- .../dossiers/messagerie.html.haml | 2 +- .../commentaires/_commentaire.html.haml | 5 ++-- spec/helpers/dossier_helper_spec.rb | 28 +++++++++++++++++++ .../commentaire.html.haml_spec.rb | 22 +++++++++++++++ 7 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 spec/helpers/dossier_helper_spec.rb create mode 100644 spec/views/new_gestionnaire/shared/commentaires/commentaire.html.haml_spec.rb diff --git a/app/assets/javascripts/new_design/messagerie.js b/app/assets/javascripts/new_design/messagerie.js index 1b8d1d324..5f7c67a08 100644 --- a/app/assets/javascripts/new_design/messagerie.js +++ b/app/assets/javascripts/new_design/messagerie.js @@ -1,7 +1,23 @@ TPS.scrollMessagerie = function () { + var scrollTo = function ($container, $scrollTo) { + $container.scrollTop( + $scrollTo.offset().top - $container.offset().top + $container.scrollTop() + ); + } + + var scrollToBottom = function ($container) { + $container.scrollTop($container.prop('scrollHeight')); + } + var $ul = $(".messagerie ul").first(); if($ul.length) { - $ul.scrollTop($ul.prop('scrollHeight')); + var $elementToScroll = $('.date.highlighted').first(); + + if ($elementToScroll.length != 0) { + scrollTo($ul, $elementToScroll); + } else { + scrollToBottom($ul); + } } }; diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index c17279606..0e786d04c 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -4,6 +4,7 @@ module NewGestionnaire include ActionView::Helpers::TextHelper after_action :mark_demande_as_read, only: :show + after_action :mark_messagerie_as_read, only: [:messagerie, :create_commentaire] def attestation send_data(dossier.attestation.pdf.read, filename: 'attestation.pdf', type: 'application/pdf') @@ -14,9 +15,8 @@ module NewGestionnaire end def messagerie - dossier.notifications.messagerie.mark_as_read - current_gestionnaire.mark_tab_as_seen(dossier, :messagerie) @commentaire = Commentaire.new + @messagerie_seen_at = current_gestionnaire.follows.find_by(dossier: dossier)&.messagerie_seen_at end def annotations_privees @@ -198,5 +198,10 @@ module NewGestionnaire dossier.notifications.demande.mark_as_read current_gestionnaire.mark_tab_as_seen(dossier, :demande) end + + def mark_messagerie_as_read + dossier.notifications.messagerie.mark_as_read + current_gestionnaire.mark_tab_as_seen(dossier, :messagerie) + end end end diff --git a/app/views/new_gestionnaire/avis/messagerie.html.haml b/app/views/new_gestionnaire/avis/messagerie.html.haml index cf022f20e..d6b71b69b 100644 --- a/app/views/new_gestionnaire/avis/messagerie.html.haml +++ b/app/views/new_gestionnaire/avis/messagerie.html.haml @@ -6,6 +6,6 @@ %ul.messages-list - @dossier.commentaires.each do |commentaire| %li - = render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire } + = render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire, messagerie_seen_at: nil } = render partial: "new_gestionnaire/shared/commentaires/form", locals: { commentaire: @commentaire, form_url: commentaire_avis_path(@avis) } diff --git a/app/views/new_gestionnaire/dossiers/messagerie.html.haml b/app/views/new_gestionnaire/dossiers/messagerie.html.haml index b7423effc..5dac5390a 100644 --- a/app/views/new_gestionnaire/dossiers/messagerie.html.haml +++ b/app/views/new_gestionnaire/dossiers/messagerie.html.haml @@ -6,6 +6,6 @@ %ul.messages-list - @dossier.commentaires.each do |commentaire| %li - = render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire } + = render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire, messagerie_seen_at: @messagerie_seen_at } = render partial: "new_gestionnaire/shared/commentaires/form", locals: { commentaire: @commentaire, form_url: commentaire_dossier_path(@dossier.procedure, @dossier) } diff --git a/app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml b/app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml index d3cbb9610..5de924c4f 100644 --- a/app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml +++ b/app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml @@ -4,9 +4,10 @@ %h2 %span.mail = render partial: 'new_gestionnaire/shared/commentaires/commentaire_issuer', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire } - - if ![current_gestionnaire.email, @dossier.user.email, 'contact@tps.apientreprise.fr'].include?(commentaire.email) + - if ![current_gestionnaire.email, commentaire.dossier.user.email, 'contact@tps.apientreprise.fr'].include?(commentaire.email) %span.guest Invité - %span.date= I18n.l(commentaire.created_at.localtime, format: '%H:%M le %d/%m/%Y') + %span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) } + = I18n.l(commentaire.created_at.localtime, format: '%d/%m/%Y à %H:%M ') .rich-text= sanitize(commentaire.body) - if commentaire.piece_justificative diff --git a/spec/helpers/dossier_helper_spec.rb b/spec/helpers/dossier_helper_spec.rb new file mode 100644 index 000000000..0ceec6d96 --- /dev/null +++ b/spec/helpers/dossier_helper_spec.rb @@ -0,0 +1,28 @@ +require 'rails_helper' + +RSpec.describe DossierHelper, type: :helper do + describe ".highlight_if_unseen_class" do + let(:seen_at) { DateTime.now } + + subject { highlight_if_unseen_class(seen_at, updated_at) } + + context "when commentaire date is created before last seen datetime" do + let(:updated_at) { seen_at - 2.days } + + it { is_expected.to eq nil } + end + + context "when commentaire date is created after last seen datetime" do + let(:updated_at) { seen_at + 2.hours } + + it { is_expected.to eq "highlighted" } + end + + context "when there is no last seen datetime" do + let(:updated_at) { DateTime.now } + let(:seen_at) { nil } + + it { is_expected.to eq nil } + end + end +end diff --git a/spec/views/new_gestionnaire/shared/commentaires/commentaire.html.haml_spec.rb b/spec/views/new_gestionnaire/shared/commentaires/commentaire.html.haml_spec.rb new file mode 100644 index 000000000..e866a356d --- /dev/null +++ b/spec/views/new_gestionnaire/shared/commentaires/commentaire.html.haml_spec.rb @@ -0,0 +1,22 @@ +describe 'new_gestionnaire/shared/commentaires/commentaire.html.haml', type: :view do + before { view.extend DossierHelper } + + subject { render 'new_gestionnaire/shared/commentaires/commentaire.html.haml', commentaire: commentaire, messagerie_seen_at: seen_at, current_gestionnaire: current_gestionnaire } + + let(:dossier) { create(:dossier) } + let(:commentaire) { create(:commentaire, dossier: dossier) } + let(:current_gestionnaire) { create(:gestionnaire) } + let(:seen_at) { commentaire.created_at + 1.hour } + + context "with a seen_at after commentaire created_at" do + let(:seen_at) { commentaire.created_at + 1.hour } + + it { is_expected.not_to have_css(".highlighted") } + end + + context "with a seen_at after commentaire created_at" do + let(:seen_at) { commentaire.created_at - 1.hour } + + it { is_expected.to have_css(".highlighted") } + end +end