diff --git a/app/models/notification.rb b/app/models/notification.rb index 75396be4b..edd0ab23d 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -5,7 +5,8 @@ class Notification < ActiveRecord::Base enum type_notif: { commentaire: 'commentaire', - cerfa: 'cerfa' + cerfa: 'cerfa', + piece_justificative: 'piece_justificative' } # def broadcast_notification diff --git a/app/models/piece_justificative.rb b/app/models/piece_justificative.rb index e1a0df9c8..405871a38 100644 --- a/app/models/piece_justificative.rb +++ b/app/models/piece_justificative.rb @@ -13,6 +13,8 @@ class PieceJustificative < ActiveRecord::Base validates :content, :file_size => {:maximum => 20.megabytes} validates :content, presence: true, allow_blank: false, allow_nil: false + after_save :internal_notification + def empty? content.blank? end @@ -43,4 +45,10 @@ class PieceJustificative < ActiveRecord::Base image/jpeg " end + + private + + def internal_notification + NotificationService.new('piece_justificative', self.dossier.id, self.libelle).notify + end end diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index d4f672a18..fe46c0cc3 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -1,10 +1,11 @@ class NotificationService - def initialize type_notif, dossier_id + def initialize type_notif, dossier_id, attribut_change='' @type_notif = type_notif @dossier_id = dossier_id - notification.liste.push text_for_notif + notification.liste.push text_for_notif attribut_change + notification.liste = notification.liste.uniq self end @@ -22,12 +23,14 @@ class NotificationService end end - def text_for_notif + def text_for_notif attribut='' case @type_notif when 'commentaire' "#{notification.liste.size + 1} nouveau(x) commentaire(s) déposé(s)." when 'cerfa' "Un nouveau formulaire a été déposé." + when 'piece_justificative' + attribut else 'Notification par défaut' end diff --git a/spec/controllers/users/description_controller_shared_example.rb b/spec/controllers/users/description_controller_shared_example.rb index f74f8e04b..c08c8bbd2 100644 --- a/spec/controllers/users/description_controller_shared_example.rb +++ b/spec/controllers/users/description_controller_shared_example.rb @@ -299,6 +299,10 @@ shared_examples 'description_controller_spec' do sign_in guest end + it 'Notification interne is create' do + expect { subject }.to change(Notification, :count).by (1) + end + context 'when PJ have no documents' do it { expect(dossier.pieces_justificatives.size).to eq 0 }