2023-10-17 18:02:55 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BaseNotification < Noticed::Base
|
|
|
|
deliver_by :database, if: :database_notification?
|
2023-11-29 18:09:10 +08:00
|
|
|
deliver_by :email, mailer: 'AppMailer', method: :general_notification, if: :email_notification?
|
2023-10-17 18:02:55 +08:00
|
|
|
|
|
|
|
def self.send_notifications(params, later: false)
|
2023-10-19 17:33:40 +08:00
|
|
|
recipients_class =
|
2023-11-24 21:58:46 +08:00
|
|
|
"Recipients::#{NotificationExtends::NOTIFICATIONS_TYPES[subtype || params[:type]][:recipients_module]}".constantize
|
2023-10-17 18:02:55 +08:00
|
|
|
recipients_class.new(params).recipients.each do |recipient|
|
|
|
|
if later
|
|
|
|
with(params).deliver_later(recipient)
|
|
|
|
else
|
|
|
|
with(params).deliver(recipient)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-24 21:58:46 +08:00
|
|
|
def self.subtype; end
|
|
|
|
|
2023-11-21 21:21:55 +08:00
|
|
|
def subtype
|
2023-11-24 21:58:46 +08:00
|
|
|
self.class.subtype || params[:type]
|
2023-10-19 17:33:40 +08:00
|
|
|
end
|
|
|
|
|
2023-10-17 18:02:55 +08:00
|
|
|
def subject; end
|
|
|
|
|
2023-12-11 21:32:22 +08:00
|
|
|
def message
|
|
|
|
params[:message]
|
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
|
|
|
params[:title]
|
|
|
|
end
|
|
|
|
|
2023-10-17 18:02:55 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def database_notification?
|
|
|
|
recipient.notifications_settings.dig(notification_subgroup.to_s, 'in_app')
|
|
|
|
end
|
|
|
|
|
2023-11-21 21:21:55 +08:00
|
|
|
def email_notification?
|
|
|
|
recipient.notifications_settings.dig(notification_subgroup.to_s, 'email')
|
|
|
|
end
|
|
|
|
|
2023-10-17 18:02:55 +08:00
|
|
|
def notification_subgroup
|
2023-10-19 17:33:40 +08:00
|
|
|
NotificationExtends::NOTIFICATIONS_GROUPS.values.reduce({}, :merge).find do |_sg, n|
|
2023-11-21 21:21:55 +08:00
|
|
|
n.include?(subtype.to_sym)
|
2023-10-19 17:33:40 +08:00
|
|
|
end[0]
|
2023-10-17 18:02:55 +08:00
|
|
|
end
|
|
|
|
end
|