From cae3e1f420877e3bf97e73ef06d2cbe546f22f9d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 12 May 2017 10:03:21 +0200 Subject: [PATCH 1/5] Commentaire#internal_notification => #notify_gestionnaires --- app/models/commentaire.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 0e39e5eba..fb9a2f509 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -4,7 +4,7 @@ class Commentaire < ActiveRecord::Base belongs_to :piece_justificative - after_save :internal_notification + after_save :notify_gestionnaires def header "#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M') @@ -12,7 +12,7 @@ class Commentaire < ActiveRecord::Base private - def internal_notification + def notify_gestionnaires if email == dossier.user.email || dossier.invites_user.pluck(:email).to_a.include?(email) NotificationService.new('commentaire', self.dossier.id).notify end From 9c3cde2b2eaf6f7c64ebc43e20d62415afa7787d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 12 May 2017 13:40:32 +0200 Subject: [PATCH 2/5] Move CommentairesController#notify_user_user_with_mail to Commentaire --- app/controllers/commentaires_controller.rb | 11 +-------- app/models/commentaire.rb | 5 ++++ .../dossiers/commentaires_controller_spec.rb | 23 ------------------- 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/app/controllers/commentaires_controller.rb b/app/controllers/commentaires_controller.rb index 920a64155..b22ada7eb 100644 --- a/app/controllers/commentaires_controller.rb +++ b/app/controllers/commentaires_controller.rb @@ -35,15 +35,12 @@ class CommentairesController < ApplicationController end @commentaire.body = params['texte_commentaire'] - saved = false unless @commentaire.body.blank? && @commentaire.piece_justificative.nil? - saved = @commentaire.save unless flash.alert + @commentaire.save unless flash.alert else flash.alert = "Veuillez rédiger un message ou ajouter une pièce jointe." end - notify_user_with_mail(@commentaire) if saved - if is_gestionnaire? unless current_gestionnaire.follow? @commentaire.dossier current_gestionnaire.toggle_follow_dossier @commentaire.dossier @@ -63,10 +60,4 @@ class CommentairesController < ApplicationController def is_gestionnaire? false end - - private - - def notify_user_with_mail(commentaire) - NotificationMailer.new_answer(commentaire.dossier).deliver_now! unless current_user.try(:email) == commentaire.dossier.user.email - end end diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index fb9a2f509..8956831bd 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -5,6 +5,7 @@ class Commentaire < ActiveRecord::Base belongs_to :piece_justificative after_save :notify_gestionnaires + after_save :notify_user_with_mail def header "#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M') @@ -17,4 +18,8 @@ class Commentaire < ActiveRecord::Base NotificationService.new('commentaire', self.dossier.id).notify end end + + def notify_user_with_mail + NotificationMailer.new_answer(dossier).deliver_now! unless (current_user.try(:email) == dossier.user.email || email == 'contact@tps.apientreprise.fr') + end end diff --git a/spec/controllers/users/dossiers/commentaires_controller_spec.rb b/spec/controllers/users/dossiers/commentaires_controller_spec.rb index d56a18d9b..f83943979 100644 --- a/spec/controllers/users/dossiers/commentaires_controller_spec.rb +++ b/spec/controllers/users/dossiers/commentaires_controller_spec.rb @@ -45,27 +45,4 @@ describe Users::Dossiers::CommentairesController, type: :controller do end end - - describe '#notify_user_with_mail' do - let(:commentaire){create(:commentaire)} - - context 'when usager is writing a commentaire on dossier' do - before { sign_in commentaire.dossier.user } - - it { - expect(NotificationMailer).to_not receive(:new_answer) - subject.send(:notify_user_with_mail, commentaire) - } - end - - context 'when anybody else but usager is writing a commentaire' do - before { sign_in create(:user, email: 'administrateur@test.fr') } - - it { - expect(NotificationMailer).to receive(:new_answer).and_return(NotificationMailer) - expect(NotificationMailer).to receive(:deliver_now!) - subject.send(:notify_user_with_mail, commentaire) - } - end - end end From c07b83b26601270e6f3c3e3cd7f4df8f9fce8891 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 10 May 2017 10:56:30 +0200 Subject: [PATCH 3/5] Commentaire#notify_user_with_mail -> #notify_user --- app/models/commentaire.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 8956831bd..e0f6598cb 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -5,7 +5,7 @@ class Commentaire < ActiveRecord::Base belongs_to :piece_justificative after_save :notify_gestionnaires - after_save :notify_user_with_mail + after_save :notify_user def header "#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M') @@ -19,7 +19,7 @@ class Commentaire < ActiveRecord::Base end end - def notify_user_with_mail + def notify_user NotificationMailer.new_answer(dossier).deliver_now! unless (current_user.try(:email) == dossier.user.email || email == 'contact@tps.apientreprise.fr') end end From 1235fce34ab9433cdabef20f208de204884236c6 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 12 May 2017 13:44:40 +0200 Subject: [PATCH 4/5] Refactor Commentaire#notify_user --- app/models/commentaire.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index e0f6598cb..1617e36c7 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -20,6 +20,8 @@ class Commentaire < ActiveRecord::Base end def notify_user - NotificationMailer.new_answer(dossier).deliver_now! unless (current_user.try(:email) == dossier.user.email || email == 'contact@tps.apientreprise.fr') + if email != dossier.user.email && email != 'contact@tps.apientreprise.fr' + NotificationMailer.new_answer(dossier).deliver_now! + end end end From 8a5420301c2caa5ba0482ed9cd84b6fa6d4aa9b3 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 12 May 2017 13:32:48 +0200 Subject: [PATCH 5/5] Add Commentaire#notify to dispatch notifications --- app/models/commentaire.rb | 32 +++++++++++++++----- spec/factories/invite_user.rb | 24 +++++++++++++++ spec/models/commentaire_spec.rb | 52 +++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 spec/factories/invite_user.rb diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 1617e36c7..001976ab3 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -4,8 +4,7 @@ class Commentaire < ActiveRecord::Base belongs_to :piece_justificative - after_save :notify_gestionnaires - after_save :notify_user + after_create :notify def header "#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M') @@ -13,15 +12,32 @@ class Commentaire < ActiveRecord::Base private - def notify_gestionnaires - if email == dossier.user.email || dossier.invites_user.pluck(:email).to_a.include?(email) - NotificationService.new('commentaire', self.dossier.id).notify + def notify + dossier_user_email = dossier.user.email + invited_users_emails = dossier.invites_user.pluck(:email).to_a + + case email + when "contact@tps.apientreprise.fr" + # The commentaire is a copy of an automated notification email + # we sent to a user, so do nothing + when dossier_user_email, *invited_users_emails + # A user or an inved user posted a commentaire, + # we need to notify the gestionnaires + + notify_gestionnaires + else + # A gestionnaire posted a commentaire, + # we need to notify the user + + notify_user end end + def notify_gestionnaires + NotificationService.new('commentaire', self.dossier.id).notify + end + def notify_user - if email != dossier.user.email && email != 'contact@tps.apientreprise.fr' - NotificationMailer.new_answer(dossier).deliver_now! - end + NotificationMailer.new_answer(dossier).deliver_now! end end diff --git a/spec/factories/invite_user.rb b/spec/factories/invite_user.rb new file mode 100644 index 000000000..c4f7dcdbd --- /dev/null +++ b/spec/factories/invite_user.rb @@ -0,0 +1,24 @@ +FactoryGirl.define do + factory :invite_user do + email 'plop@octo.com' + + after(:build) do |invite, _evaluator| + if invite.dossier.nil? + invite.dossier = create(:dossier) + end + + unless invite.user.nil? + invite.email = invite.user.email + end + end + + trait :with_user do + after(:build) do |invite, _evaluator| + if invite.user.nil? + invite.user = create(:user) + invite.email = invite.user.email + end + end + end + end +end diff --git a/spec/models/commentaire_spec.rb b/spec/models/commentaire_spec.rb index efcf9bff1..8decaf4ae 100644 --- a/spec/models/commentaire_spec.rb +++ b/spec/models/commentaire_spec.rb @@ -8,4 +8,56 @@ describe Commentaire do it { is_expected.to belong_to(:dossier) } it { is_expected.to belong_to(:piece_justificative) } + + describe "#notify" do + let(:procedure) { create(:procedure) } + let(:gestionnaire) { create(:gestionnaire) } + let(:assign_to) { create(:assign_to, gestionnaire: gestionnaire, procedure: procedure) } + let(:user) { create(:user) } + let(:dossier) { create(:dossier, procedure: procedure, user: user) } + let(:commentaire) { Commentaire.new(dossier: dossier) } + + context "with a commentaire created by a user" do + it "calls notify_gestionnaires" do + expect(commentaire).to receive(:notify_gestionnaires) + + commentaire.email = user.email + commentaire.save + end + end + + context "with a commentaire created by an invited user" do + let(:user_invite) { create(:user) } + + before do + FactoryGirl.create(:invite_user, email: "invite@tps.apientreprise.fr", dossier: dossier, user: user_invite) + end + + it "calls notify_gestionnaires" do + expect(commentaire).to receive(:notify_gestionnaires) + + commentaire.email = user_invite.email + commentaire.save + end + end + + context "with a commentaire created by a gestionnaire" do + it "calls notify_user" do + expect(commentaire).to receive(:notify_user) + + commentaire.email = gestionnaire.email + commentaire.save + end + end + + context "with a commentaire automatically created (notification)" do + it "does not call notify_user or notify_gestionnaires" do + expect(commentaire).not_to receive(:notify_user) + expect(commentaire).not_to receive(:notify_gestionnaires) + + commentaire.email = "contact@tps.apientreprise.fr" + commentaire.save + end + end + end end