Merge pull request #800 from sgmap/notification_that_works

Notification that works
This commit is contained in:
Mathieu Magnin 2017-11-10 16:45:58 +01:00 committed by GitHub
commit 6c71a65e8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 364 additions and 12 deletions

View file

@ -7,21 +7,25 @@ module NewGestionnaire
def show def show
@dossier = dossier @dossier = dossier
dossier.notifications.demande.mark_as_read dossier.notifications.demande.mark_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :demande)
end end
def messagerie def messagerie
@dossier = dossier @dossier = dossier
dossier.notifications.messagerie.mark_as_read dossier.notifications.messagerie.mark_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :messagerie)
end end
def annotations_privees def annotations_privees
@dossier = dossier @dossier = dossier
dossier.notifications.annotations_privees.mark_as_read dossier.notifications.annotations_privees.mark_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :annotations_privees)
end end
def avis def avis
@dossier = dossier @dossier = dossier
dossier.notifications.avis.mark_as_read dossier.notifications.avis.mark_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :avis)
end end
def follow def follow

View file

@ -12,6 +12,7 @@ class Avis < ApplicationRecord
scope :without_answer, -> { where(answer: nil) } scope :without_answer, -> { where(answer: nil) }
scope :for_dossier, ->(dossier_id) { where(dossier_id: dossier_id) } scope :for_dossier, ->(dossier_id) { where(dossier_id: dossier_id) }
scope :by_latest, -> { order(updated_at: :desc) } scope :by_latest, -> { order(updated_at: :desc) }
scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) }
def email_to_display def email_to_display
gestionnaire.try(:email) || email gestionnaire.try(:email) || email

View file

@ -11,6 +11,8 @@ class Champ < ActiveRecord::Base
after_save :internal_notification, if: Proc.new { !dossier.nil? } after_save :internal_notification, if: Proc.new { !dossier.nil? }
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
def mandatory? def mandatory?
mandatory mandatory
end end

View file

@ -5,6 +5,7 @@ class Commentaire < ActiveRecord::Base
belongs_to :piece_justificative belongs_to :piece_justificative
default_scope { order(created_at: :asc) } default_scope { order(created_at: :asc) }
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }
after_create :notify after_create :notify

View file

@ -62,6 +62,7 @@ class Dossier < ActiveRecord::Base
scope :en_cours, -> { not_archived.state_en_construction_ou_instruction } scope :en_cours, -> { not_archived.state_en_construction_ou_instruction }
scope :without_followers, -> { left_outer_joins(:follows).where(follows: { id: nil }) } scope :without_followers, -> { left_outer_joins(:follows).where(follows: { id: nil }) }
scope :with_unread_notifications, -> { where(notifications: { already_read: false }) } scope :with_unread_notifications, -> { where(notifications: { already_read: false }) }
scope :followed_by , -> (gestionnaire) { joins(:follows).where(follows: { gestionnaire: gestionnaire }) }
accepts_nested_attributes_for :individual accepts_nested_attributes_for :individual

View file

@ -3,4 +3,15 @@ class Follow < ActiveRecord::Base
belongs_to :dossier belongs_to :dossier
validates_uniqueness_of :gestionnaire_id, :scope => :dossier_id validates_uniqueness_of :gestionnaire_id, :scope => :dossier_id
before_create :set_default_date
private
def set_default_date
self.demande_seen_at = DateTime.now
self.annotations_privees_seen_at = DateTime.now
self.avis_seen_at = DateTime.now
self.messagerie_seen_at = DateTime.now
end
end end

View file

@ -131,6 +131,56 @@ class Gestionnaire < ActiveRecord::Base
assign_to.find_by(procedure_id: procedure_id).procedure_presentation_or_default assign_to.find_by(procedure_id: procedure_id).procedure_presentation_or_default
end end
def notifications_for_dossier(dossier)
follow = Follow
.includes(dossier: [:champs, :avis, :commentaires])
.find_by(gestionnaire: self, dossier: dossier)
if follow.present?
#retirer le seen_at.present? une fois la contrainte de presence en base (et les migrations ad hoc)
champs_publiques = follow.demande_seen_at.present? &&
follow.dossier.champs.updated_since?(follow.demande_seen_at).any?
pieces_justificatives = follow.demande_seen_at.present? &&
follow.dossier.pieces_justificatives.updated_since?(follow.demande_seen_at).any?
demande = champs_publiques || pieces_justificatives
annotations_privees = follow.annotations_privees_seen_at.present? &&
follow.dossier.champs_private.updated_since?(follow.annotations_privees_seen_at).any?
avis_notif = follow.avis_seen_at.present? &&
follow.dossier.avis.updated_since?(follow.avis_seen_at).any?
messagerie = follow.messagerie_seen_at.present? &&
dossier.commentaires
.where.not(email: 'contact@tps.apientreprise.fr')
.updated_since?(follow.messagerie_seen_at).any?
annotations_hash(demande, annotations_privees, avis_notif, messagerie)
else
annotations_hash(false, false, false, false)
end
end
def notifications_for_procedure(procedure)
dossiers = procedure.dossiers.en_cours.followed_by(self)
dossiers_id_with_notifications(dossiers)
end
def notifications_per_procedure
dossiers = Dossier.en_cours.followed_by(self)
Dossier.where(id: dossiers_id_with_notifications(dossiers)).group(:procedure_id).count
end
def mark_tab_as_seen(dossier, tab)
attributes = {}
attributes["#{tab}_seen_at"] = DateTime.now
Follow.where(gestionnaire: self, dossier: dossier).update_all(attributes)
end
private private
def valid_couple_table_attr? table, column def valid_couple_table_attr? table, column
@ -153,4 +203,42 @@ class Gestionnaire < ActiveRecord::Base
couples.include?({table: table, column: column}) couples.include?({table: table, column: column})
end end
def annotations_hash(demande, annotations_privees, avis, messagerie)
{
demande: demande,
annotations_privees: annotations_privees,
avis: avis,
messagerie: messagerie
}
end
def dossiers_id_with_notifications(dossiers)
updated_demandes = dossiers
.joins(:champs)
.where('champs.updated_at > follows.demande_seen_at')
updated_pieces_justificatives = dossiers
.joins(:pieces_justificatives)
.where('pieces_justificatives.updated_at > follows.demande_seen_at')
updated_annotations = dossiers
.joins(:champs_private)
.where('champs.updated_at > follows.annotations_privees_seen_at')
updated_avis = dossiers
.joins(:avis)
.where('avis.updated_at > follows.avis_seen_at')
updated_messagerie = dossiers
.joins(:commentaires)
.where('commentaires.updated_at > follows.messagerie_seen_at')
.where.not(commentaires: { email: 'contact@tps.apientreprise.fr' })
[updated_demandes,
updated_pieces_justificatives,
updated_annotations,
updated_avis,
updated_messagerie].map { |query| query.distinct.ids }.flatten.uniq
end
end end

View file

@ -15,6 +15,8 @@ class PieceJustificative < ActiveRecord::Base
after_save :internal_notification, if: Proc.new { !dossier.nil? } after_save :internal_notification, if: Proc.new { !dossier.nil? }
scope :updated_since?, -> (date) { where('pieces_justificatives.updated_at > ?', date) }
def empty? def empty?
content.blank? content.blank?
end end

View file

@ -12,7 +12,7 @@
= render partial: "new_gestionnaire/procedures/dossier_actions", locals: { procedure: dossier.procedure, dossier: dossier, dossier_is_followed: current_gestionnaire&.follow?(dossier) } = render partial: "new_gestionnaire/procedures/dossier_actions", locals: { procedure: dossier.procedure, dossier: dossier, dossier_is_followed: current_gestionnaire&.follow?(dossier) }
= render partial: "state_button", locals: { dossier: dossier } = render partial: "state_button", locals: { dossier: dossier }
%ul.tabs %ul.tabs
- notifications_summary = dossier.notifications_summary - notifications_summary = current_gestionnaire.notifications_for_dossier(dossier)
%li{ class: current_page?(dossier_path(dossier.procedure, dossier)) ? 'active' : nil } %li{ class: current_page?(dossier_path(dossier.procedure, dossier)) ? 'active' : nil }
- if notifications_summary[:demande] - if notifications_summary[:demande]
%span.notifications{ 'aria-label': 'notifications' } %span.notifications{ 'aria-label': 'notifications' }

View file

@ -25,7 +25,7 @@
%li %li
%object %object
= link_to(procedure_path(p, statut: 'suivis')) do = link_to(procedure_path(p, statut: 'suivis')) do
- if @notifications_count_per_procedure[p.id].present? - if current_gestionnaire.notifications_per_procedure[p.id].present?
%span.notifications{ 'aria-label': "notifications" } %span.notifications{ 'aria-label': "notifications" }
- followed_count = @followed_dossiers_count_per_procedure[p.id] || 0 - followed_count = @followed_dossiers_count_per_procedure[p.id] || 0
.stats-number .stats-number

View file

@ -15,7 +15,7 @@
%span.badge= @a_suivre_dossiers.count %span.badge= @a_suivre_dossiers.count
%li{ class: (@statut == 'suivis') ? 'active' : nil }> %li{ class: (@statut == 'suivis') ? 'active' : nil }>
- if @followed_dossiers.with_unread_notifications.present? - if current_gestionnaire.notifications_for_procedure(@procedure).present?
%span.notifications{ 'aria-label': 'notifications' } %span.notifications{ 'aria-label': 'notifications' }
= link_to(procedure_path(@procedure, statut: 'suivis')) do = link_to(procedure_path(@procedure, statut: 'suivis')) do
= t('pluralize.followed', count: @followed_dossiers.count) = t('pluralize.followed', count: @followed_dossiers.count)
@ -27,7 +27,7 @@
%span.badge= @termines_dossiers.count %span.badge= @termines_dossiers.count
%li{ class: (@statut == 'tous') ? 'active' : nil }> %li{ class: (@statut == 'tous') ? 'active' : nil }>
- if @followed_dossiers.with_unread_notifications.present? - if current_gestionnaire.notifications_for_procedure(@procedure).present?
%span.notifications{ 'aria-label': 'notifications' } %span.notifications{ 'aria-label': 'notifications' }
= link_to(procedure_path(@procedure, statut: 'tous')) do = link_to(procedure_path(@procedure, statut: 'tous')) do
tous les dossiers tous les dossiers
@ -99,7 +99,7 @@
%td.number-col %td.number-col
= link_to(dossier_path(@procedure, dossier), class: 'cell-link') do = link_to(dossier_path(@procedure, dossier), class: 'cell-link') do
.icon.folder .icon.folder
- if @followed_dossiers.with_unread_notifications.include?(dossier) - if current_gestionnaire.notifications_for_procedure(@procedure).include?(dossier.id)
%span.notifications{ 'aria-label': 'notifications' } %span.notifications{ 'aria-label': 'notifications' }
= dossier.id = dossier.id

View file

@ -0,0 +1,6 @@
class AddTimeStampToChamps < ActiveRecord::Migration[5.0]
def change
add_column :champs, :created_at, :datetime
add_column :champs, :updated_at, :datetime
end
end

View file

@ -0,0 +1,8 @@
class AddLastViewsAtToFollow < ActiveRecord::Migration[5.0]
def change
add_column :follows, :demande_seen_at, :datetime
add_column :follows, :annotations_privees_seen_at, :datetime
add_column :follows, :avis_seen_at, :datetime
add_column :follows, :messagerie_seen_at, :datetime
end
end

View file

@ -0,0 +1,5 @@
class AddColumnUpdatedAtToPieceJustificative < ActiveRecord::Migration[5.0]
def change
add_column :pieces_justificatives, :updated_at, :datetime
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171019113610) do ActiveRecord::Schema.define(version: 20171024135653) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -140,6 +140,8 @@ ActiveRecord::Schema.define(version: 20171019113610) do
t.integer "type_de_champ_id" t.integer "type_de_champ_id"
t.integer "dossier_id" t.integer "dossier_id"
t.string "type" t.string "type"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["dossier_id"], name: "index_champs_on_dossier_id", using: :btree t.index ["dossier_id"], name: "index_champs_on_dossier_id", using: :btree
t.index ["type_de_champ_id"], name: "index_champs_on_type_de_champ_id", using: :btree t.index ["type_de_champ_id"], name: "index_champs_on_type_de_champ_id", using: :btree
end end
@ -252,6 +254,10 @@ ActiveRecord::Schema.define(version: 20171019113610) do
create_table "follows", force: :cascade do |t| create_table "follows", force: :cascade do |t|
t.integer "gestionnaire_id", null: false t.integer "gestionnaire_id", null: false
t.integer "dossier_id", null: false t.integer "dossier_id", null: false
t.datetime "demande_seen_at"
t.datetime "annotations_privees_seen_at"
t.datetime "avis_seen_at"
t.datetime "messagerie_seen_at"
t.index ["dossier_id"], name: "index_follows_on_dossier_id", using: :btree t.index ["dossier_id"], name: "index_follows_on_dossier_id", using: :btree
t.index ["gestionnaire_id", "dossier_id"], name: "index_follows_on_gestionnaire_id_and_dossier_id", unique: true, using: :btree t.index ["gestionnaire_id", "dossier_id"], name: "index_follows_on_gestionnaire_id_and_dossier_id", unique: true, using: :btree
t.index ["gestionnaire_id"], name: "index_follows_on_gestionnaire_id", using: :btree t.index ["gestionnaire_id"], name: "index_follows_on_gestionnaire_id", using: :btree
@ -348,6 +354,7 @@ ActiveRecord::Schema.define(version: 20171019113610) do
t.integer "user_id" t.integer "user_id"
t.string "original_filename" t.string "original_filename"
t.string "content_secure_token" t.string "content_secure_token"
t.datetime "updated_at"
t.index ["dossier_id"], name: "index_pieces_justificatives_on_dossier_id", using: :btree t.index ["dossier_id"], name: "index_pieces_justificatives_on_dossier_id", using: :btree
t.index ["type_de_piece_justificative_id"], name: "index_pieces_justificatives_on_type_de_piece_justificative_id", using: :btree t.index ["type_de_piece_justificative_id"], name: "index_pieces_justificatives_on_type_de_piece_justificative_id", using: :btree
end end

View file

@ -0,0 +1,49 @@
namespace :'2017_10_06_set_follow_date' do
task set: :environment do
set_default_date_to_champs_and_pieces_justificatives
set_all_dossiers_as_read
apply_legacy_notification_to_new_system
end
def set_default_date_to_champs_and_pieces_justificatives
ActiveRecord::Base.connection
.execute('UPDATE champs SET created_at = dossiers.created_at, updated_at = dossiers.updated_at FROM dossiers where champs.dossier_id = dossiers.id')
PieceJustificative.includes(:dossier).where(created_at: nil).each do |piece_justificative|
piece_justificative.update_attribute('created_at', piece_justificative.dossier.created_at)
end
ActiveRecord::Base.connection
.execute('UPDATE pieces_justificatives SET updated_at = created_at')
end
def set_all_dossiers_as_read
Gestionnaire.includes(:follows).all.each do |gestionnaire|
gestionnaire.follows.update_all(
demande_seen_at: gestionnaire.current_sign_in_at,
annotations_privees_seen_at: gestionnaire.current_sign_in_at,
avis_seen_at: gestionnaire.current_sign_in_at,
messagerie_seen_at: gestionnaire.current_sign_in_at)
end
end
def apply_legacy_notification_to_new_system
Notification.joins(dossier: :follows).unread.each do |notification|
if notification.demande?
notification.dossier.follows.update_all(demande_seen_at: notification.created_at)
end
if notification.annotations_privees?
notification.dossier.follows.update_all(annotations_privees_seen_at: notification.created_at)
end
if notification.avis?
notification.dossier.follows.update_all(avis_seen_at: notification.created_at)
end
if notification.messagerie?
notification.dossier.follows.update_all(messagerie_seen_at: notification.created_at)
end
end
end
end

View file

@ -318,6 +318,8 @@ describe Gestionnaire, type: :model do
Timecop.freeze(friday) Timecop.freeze(friday)
end end
after { Timecop.return }
context 'when no procedure published was active last week' do context 'when no procedure published was active last week' do
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: Time.now) } let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: Time.now) }
context 'when the gestionnaire has no notifications' do context 'when the gestionnaire has no notifications' do
@ -404,4 +406,167 @@ describe Gestionnaire, type: :model do
it { expect(gestionnaire.procedure_presentation_for_procedure_id(procedure.id)).to eq(pp)} it { expect(gestionnaire.procedure_presentation_for_procedure_id(procedure.id)).to eq(pp)}
it { expect(gestionnaire.procedure_presentation_for_procedure_id(procedure_2.id).persisted?).to be_falsey} it { expect(gestionnaire.procedure_presentation_for_procedure_id(procedure_2.id).persisted?).to be_falsey}
end end
describe '#notifications_for_dossier' do
let!(:dossier) { create(:dossier, :followed, state: 'initiated') }
let(:gestionnaire) { dossier.follows.first.gestionnaire }
subject { gestionnaire.notifications_for_dossier(dossier) }
context 'when the gestionnaire has just followed the dossier' do
it { is_expected.to match({ demande: false, annotations_privees: false, avis: false, messagerie: false }) }
end
context 'when there is a modification on public champs' do
before { dossier.champs.first.update_attribute('value', 'toto') }
it { is_expected.to match({ demande: true, annotations_privees: false, avis: false, messagerie: false }) }
end
context 'when there is a modification on a piece jusitificative' do
before { dossier.pieces_justificatives << create(:piece_justificative, :contrat) }
it { is_expected.to match({ demande: true, annotations_privees: false, avis: false, messagerie: false }) }
end
context 'when there is a modification on private champs' do
before { dossier.champs_private.first.update_attribute('value', 'toto') }
it { is_expected.to match({ demande: false, annotations_privees: true, avis: false, messagerie: false }) }
end
context 'when there is a modification on avis' do
before { create(:avis, dossier: dossier) }
it { is_expected.to match({ demande: false, annotations_privees: false, avis: true, messagerie: false }) }
end
context 'messagerie' do
context 'when there is a new commentaire' do
before { create(:commentaire, dossier: dossier, email: 'a@b.com') }
it { is_expected.to match({ demande: false, annotations_privees: false, avis: false, messagerie: true }) }
end
context 'when there is a new commentaire issued by tps' do
before { create(:commentaire, dossier: dossier, email: 'contact@tps.apientreprise.fr') }
it { is_expected.to match({ demande: false, annotations_privees: false, avis: false, messagerie: false }) }
end
end
end
describe '#notification_for_procedure' do
let!(:dossier) { create(:dossier, :followed, state: 'initiated') }
let(:gestionnaire) { dossier.follows.first.gestionnaire }
let(:procedure) { dossier.procedure }
let!(:gestionnaire_2) { create(:gestionnaire, procedures: [procedure]) }
let!(:dossier_on_procedure_2) { create(:dossier, :followed, state: 'initiated') }
let!(:gestionnaire_on_procedure_2) { dossier_on_procedure_2.follows.first.gestionnaire }
before do
gestionnaire_2.followed_dossiers << dossier
end
subject { gestionnaire.notifications_for_procedure(procedure) }
context 'when the gestionnaire has just followed the dossier' do
it { is_expected.to match([]) }
end
context 'when there is a modification on public champs' do
before { dossier.champs.first.update_attribute('value', 'toto') }
it { is_expected.to match([dossier.id]) }
it { expect(gestionnaire_2.notifications_for_procedure(procedure)).to match([dossier.id]) }
it { expect(gestionnaire_on_procedure_2.notifications_for_procedure(procedure)).to match([]) }
context 'and there is a modification on private champs' do
before { dossier.champs_private.first.update_attribute('value', 'toto') }
it { is_expected.to match([dossier.id]) }
end
context 'when gestionnaire update it s public champs last seen' do
let(:follow) { gestionnaire.follows.find_by(dossier: dossier) }
before { follow.update_attribute('demande_seen_at', DateTime.now) }
it { is_expected.to match([]) }
it { expect(gestionnaire_2.notifications_for_procedure(procedure)).to match([dossier.id]) }
end
end
context 'when there is a modification on a piece justificative' do
before { dossier.pieces_justificatives << create(:piece_justificative, :contrat) }
it { is_expected.to match([dossier.id]) }
end
context 'when there is a modification on public champs on a followed dossier from another procedure' do
before { dossier_on_procedure_2.champs.first.update_attribute('value', 'toto') }
it { is_expected.to match([]) }
end
context 'when there is a modification on private champs' do
before { dossier.champs_private.first.update_attribute('value', 'toto') }
it { is_expected.to match([dossier.id]) }
end
context 'when there is a modification on avis' do
before { create(:avis, dossier: dossier) }
it { is_expected.to match([dossier.id]) }
end
context 'the messagerie' do
context 'when there is a new commentaire' do
before { create(:commentaire, dossier: dossier, email: 'a@b.com') }
it { is_expected.to match([dossier.id]) }
end
context 'when there is a new commentaire issued by tps' do
before { create(:commentaire, dossier: dossier, email: 'contact@tps.apientreprise.fr') }
it { is_expected.to match([]) }
end
end
end
describe '#notifications_per_procedure' do
let!(:dossier) { create(:dossier, :followed, state: 'initiated') }
let(:gestionnaire) { dossier.follows.first.gestionnaire }
let(:procedure) { dossier.procedure }
subject { gestionnaire.notifications_per_procedure }
context 'when there is a modification on public champs' do
before { dossier.champs.first.update_attribute('value', 'toto') }
it { is_expected.to match({ procedure.id => 1 }) }
end
end
describe '#mark_tab_as_seen' do
let!(:dossier) { create(:dossier, :followed, state: 'initiated') }
let(:gestionnaire) { dossier.follows.first.gestionnaire }
let(:freeze_date) { DateTime.parse('12/12/2012') }
context 'when demande is acknowledged' do
let(:follow) { gestionnaire.follows.find_by(dossier: dossier) }
before do
Timecop.freeze(freeze_date)
gestionnaire.mark_tab_as_seen(dossier, :demande)
end
it { expect(follow.demande_seen_at).to eq(freeze_date) }
after { Timecop.return }
end
end
end end

View file

@ -1,10 +1,12 @@
describe 'new_gestionnaire/dossiers/show.html.haml', type: :view do describe 'new_gestionnaire/dossiers/show.html.haml', type: :view do
let(:current_gestionnaire) { create(:gestionnaire) }
let(:individual) { nil } let(:individual) { nil }
let(:entreprise) { nil } let(:entreprise) { nil }
let(:dossier) { create(:dossier, :initiated, entreprise: entreprise, individual: individual) } let(:dossier) { create(:dossier, :initiated, entreprise: entreprise, individual: individual) }
before do before do
assign(:dossier, dossier) assign(:dossier, dossier)
view.stub(:current_gestionnaire).and_return(current_gestionnaire)
render render
end end