Merge pull request #287 from sgmap/commentaire-refactor

Commentaire refactor
This commit is contained in:
gregoirenovel 2017-05-12 16:52:32 +02:00 committed by GitHub
commit 7f9c271938
5 changed files with 104 additions and 37 deletions

View file

@ -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

View file

@ -4,7 +4,7 @@ class Commentaire < ActiveRecord::Base
belongs_to :piece_justificative
after_save :internal_notification
after_create :notify
def header
"#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M')
@ -12,9 +12,32 @@ class Commentaire < ActiveRecord::Base
private
def internal_notification
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
NotificationMailer.new_answer(dossier).deliver_now!
end
end

View file

@ -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

View file

@ -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

View file

@ -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