parent
255b439723
commit
8bec63bfc9
@ -0,0 +1,16 @@
|
|||||||
|
require 'values'
|
||||||
|
|
||||||
|
module CartoGearsApi
|
||||||
|
module Notifications
|
||||||
|
# Notification information.
|
||||||
|
#
|
||||||
|
# @attr_reader [String] id notification id
|
||||||
|
# @attr_reader [String] icon name of the icon shown next to the notification
|
||||||
|
# @attr_reader [String] body text of the notification, markdown formatted
|
||||||
|
# @attr_reader [Organization] organization organization if this is an org-notification, +nil+ otherwise
|
||||||
|
# @attr_reader [String] recipients +builders+, +viewers+, +all+ if this is an org-notification, nil otherwise
|
||||||
|
# @attr_reader [DateTime] created_at date this notification was created at
|
||||||
|
class Notification < Value.new(:id, :icon, :body, :organization, :recipients, :created_at)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,49 @@
|
|||||||
|
require_dependency 'carto_gears_api/notifications/notification'
|
||||||
|
require_dependency 'carto_gears_api/errors'
|
||||||
|
|
||||||
|
module CartoGearsApi
|
||||||
|
module Notifications
|
||||||
|
class NotificationsService
|
||||||
|
# Creates a new notification. Does not send it to any user.
|
||||||
|
# @see send_notification
|
||||||
|
#
|
||||||
|
# @param [String] body markdown body of the notification
|
||||||
|
# @param [String] icon name of the icon (defaults to success)
|
||||||
|
# @return [Notification] the created notification
|
||||||
|
# @raise ValidationFailed if there were some invalid parameters
|
||||||
|
def create_notification(body:, icon: Carto::Notification::ICON_SUCCESS)
|
||||||
|
gears_notification(Carto::Notification.create!(body: body, icon: icon))
|
||||||
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
|
raise Errors::ValidationFailed.new(e.record.errors.messages)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Send a notification to a user
|
||||||
|
#
|
||||||
|
# @param [String] notification_id
|
||||||
|
# @param [String] user_id
|
||||||
|
# @raise RecordNotFound if the user or notification does not exist in the database
|
||||||
|
def send_notification(notification_id, user_id)
|
||||||
|
notification = Carto::Notification.where(id: notification_id).first
|
||||||
|
raise Errors::RecordNotFound.new('Notification', notification_id) unless notification
|
||||||
|
user = Carto::User.where(id: user_id).first
|
||||||
|
raise Errors::RecordNotFound.new('User', user_id) unless user
|
||||||
|
|
||||||
|
user.received_notifications.create!(notification: notification, received_at: DateTime.now)
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def gears_notification(notification)
|
||||||
|
Notification.with(
|
||||||
|
id: notification.id,
|
||||||
|
body: notification.body,
|
||||||
|
icon: notification.icon,
|
||||||
|
organization: notification.organization,
|
||||||
|
recipients: notification.recipients,
|
||||||
|
created_at: notification.created_at
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue