Add notifications when user post a comment

This commit is contained in:
Xavier J 2016-12-22 20:40:23 +01:00
parent ce5b75a0b9
commit 54fbe367c0
6 changed files with 94 additions and 7 deletions

View file

@ -1,9 +1,9 @@
class CommentairesController < ApplicationController
def index
@facade = DossierFacades.new(
params[:dossier_id],
(current_gestionnaire || current_user).email,
params[:champs_id]
params[:dossier_id],
(current_gestionnaire || current_user).email,
params[:champs_id]
)
render layout: false
rescue ActiveRecord::RecordNotFound
@ -48,12 +48,17 @@ class CommentairesController < ApplicationController
end
NotificationMailer.new_answer(@commentaire.dossier).deliver_now! if saved
redirect_to url_for(controller: 'backoffice/dossiers', action: :show, id: params['dossier_id'])
elsif current_user.email != @commentaire.dossier.user.email
invite = Invite.where(dossier: @commentaire.dossier, user: current_user).first
redirect_to url_for(controller: 'users/dossiers/invites', action: :show, id: invite.id)
else
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: params['dossier_id'])
NotificationService.new('commentaire', @commentaire.dossier.id).notify if saved
if current_user.email != @commentaire.dossier.user.email
invite = Invite.where(dossier: @commentaire.dossier, user: current_user).first
redirect_to url_for(controller: 'users/dossiers/invites', action: :show, id: invite.id)
else
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: params['dossier_id'])
end
end
end

View file

@ -1,4 +1,8 @@
class Notification < ActiveRecord::Base
belongs_to :dossier
enum type_notif: {
commentaire: 'commentaire'
}
end

View file

@ -0,0 +1,33 @@
class NotificationService
def initialize type_notif, dossier_id
@type_notif = type_notif
@dossier_id = dossier_id
notification.liste.push text_for_notif
self
end
def notify
notification.save
end
def notification
@notification ||=
begin
Notification.find_by! dossier_id: @dossier_id, already_read: false, type_notif: @type_notif
rescue ActiveRecord::RecordNotFound
Notification.new dossier_id: @dossier_id, type_notif: @type_notif, liste: []
end
end
def text_for_notif
case @type_notif
when 'commentaire'
"#{notification.liste.size + 1} nouveau(x) commentaire(s) déposé(s)."
else
'Notification par défaut'
end
end
end

View file

@ -28,6 +28,10 @@ describe Users::CommentairesController, type: :controller do
subject
end
it 'Notification interne is create' do
expect { subject }.to change(Notification, :count).by (1)
end
end
context 'when document is upload whith a commentaire', vcr: {cassette_name: 'controllers_sers_commentaires_controller_upload_doc'} do

View file

@ -0,0 +1,12 @@
FactoryGirl.define do
factory :notification do
type_notif 'commentaire'
liste []
after(:create) do |notification, _evaluator|
unless notification.dossier
notification.dossier = create :dossier
end
end
end
end

View file

@ -0,0 +1,29 @@
require 'spec_helper'
describe NotificationService do
describe '.notify' do
let(:dossier) { create :dossier }
let(:service) { described_class.new type_notif, dossier.id }
subject { service.notify }
context 'when is the first notification for dossier_id and type_notif and alread_read is false' do
let(:type_notif) { 'commentaire' }
it { expect { subject }.to change(Notification, :count).by (1) }
context 'when is not the first notification' do
before do
create :notification, dossier: dossier, type_notif: type_notif
end
it { expect { subject }.to change(Notification, :count).by (0) }
end
end
end
describe 'text_for_notif' do
pending
end
end