From 8bec63bfc9e3f3d39d7385890e4e36f89c515f94 Mon Sep 17 00:00:00 2001 From: Javier Torres Date: Mon, 27 Mar 2017 13:43:23 +0200 Subject: [PATCH] Add API to create and send notifications --- .../notifications/notification.rb | 16 ++++++ .../notifications/notifications_service.rb | 49 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 gears/carto_gears_api/lib/carto_gears_api/notifications/notification.rb create mode 100644 gears/carto_gears_api/lib/carto_gears_api/notifications/notifications_service.rb diff --git a/gears/carto_gears_api/lib/carto_gears_api/notifications/notification.rb b/gears/carto_gears_api/lib/carto_gears_api/notifications/notification.rb new file mode 100644 index 0000000000..c99708fed2 --- /dev/null +++ b/gears/carto_gears_api/lib/carto_gears_api/notifications/notification.rb @@ -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 diff --git a/gears/carto_gears_api/lib/carto_gears_api/notifications/notifications_service.rb b/gears/carto_gears_api/lib/carto_gears_api/notifications/notifications_service.rb new file mode 100644 index 0000000000..1f448476cd --- /dev/null +++ b/gears/carto_gears_api/lib/carto_gears_api/notifications/notifications_service.rb @@ -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