From 05aaa885a1f90335324b14273b3156c6b6edad38 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 30 Mar 2023 09:54:59 +0200 Subject: [PATCH] (feat) do no notify on filling-invoice creation --- app/services/notification_center.rb | 41 +++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/app/services/notification_center.rb b/app/services/notification_center.rb index dc9710763..b4b9e2dc9 100644 --- a/app/services/notification_center.rb +++ b/app/services/notification_center.rb @@ -2,18 +2,37 @@ # send notification to one or several receiver with a type, an attached object and an optional meta data class NotificationCenter - def self.call(type: nil, receiver: nil, attached_object: nil, meta_data: {}) - receiver = [receiver] unless receiver.respond_to?(:each) - notification_type = NotificationType.find_by(name: type) + class << self + def call(type: nil, receiver: nil, attached_object: nil) + return if prevent_notify?(type: type, attached_object: attached_object) - receiver.each do |user| - Notification.new( - meta_data: meta_data, - attached_object: attached_object, - receiver: user, - notification_type: notification_type - ) - .deliver_with_preferences(user, notification_type) + receiver = [receiver] unless receiver.respond_to?(:each) + notification_type = NotificationType.find_by(name: type) + + receiver.each do |user| + Notification.new( + meta_data: meta_data, + attached_object: attached_object, + receiver: user, + notification_type: notification_type + ) + .deliver_with_preferences(user, notification_type) + end + end + + private + + # In some very special cases, we do not want the notification to be created at all + # @param type [String] + # @param attached_object [ApplicationRecord] + # @return [Boolean] + def prevent_notify?(type: nil, attached_object: nil) + if type == 'notify_user_when_invoice_ready' + item = attached_object.main_item + return true if item.object_type == 'Error' && item.object_id == 1 + end + + false end end end