parent
2f58bfbe30
commit
944c72191d
@ -0,0 +1,41 @@
|
|||||||
|
require_dependency 'carto/uuidhelper'
|
||||||
|
|
||||||
|
module Carto
|
||||||
|
module Api
|
||||||
|
class EmailNotificationsController < ::Api::ApplicationController
|
||||||
|
|
||||||
|
ssl_required :update
|
||||||
|
before_action :load_notifications, only: [:show, :update]
|
||||||
|
|
||||||
|
rescue_from StandardError, with: :rescue_from_standard_error
|
||||||
|
rescue_from Carto::LoadError, with: :rescue_from_carto_error
|
||||||
|
|
||||||
|
def show
|
||||||
|
render_jsonp({ notifications: decorate_notifications }, 200)
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
notifications = params.require(:notifications)
|
||||||
|
@carto_user.email_notifications = notifications
|
||||||
|
|
||||||
|
render_jsonp({}, 204)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def load_notifications
|
||||||
|
@carto_user = Carto::User.find(current_user.id)
|
||||||
|
@notifications = @carto_user.email_notifications
|
||||||
|
end
|
||||||
|
|
||||||
|
def decorate_notifications
|
||||||
|
payload = {}
|
||||||
|
@notifications.each do |notification|
|
||||||
|
payload[notification.notification] = notification.enabled
|
||||||
|
end
|
||||||
|
payload
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,27 @@
|
|||||||
|
module Carto
|
||||||
|
class UserEmailNotification < ActiveRecord::Base
|
||||||
|
|
||||||
|
belongs_to :user, class_name: 'Carto::User', inverse_of: :email_notifications
|
||||||
|
|
||||||
|
validates :user, presence: true
|
||||||
|
validate :valid_notification
|
||||||
|
|
||||||
|
NOTIFICATION_DO_SUBSCRIPTIONS = 'do_subscriptions'.freeze
|
||||||
|
VALID_NOTIFICATIONS = [NOTIFICATION_DO_SUBSCRIPTIONS].freeze
|
||||||
|
|
||||||
|
def self.new_do_subscriptions(user_id)
|
||||||
|
Carto::UserEmailNotification.create!(
|
||||||
|
user_id: user_id,
|
||||||
|
notification: NOTIFICATION_DO_SUBSCRIPTIONS,
|
||||||
|
enabled: true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def valid_notification
|
||||||
|
errors.add(:notification, 'Invalid notification') unless VALID_NOTIFICATIONS.include?(notification)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,20 @@
|
|||||||
|
require 'carto/db/migration_helper'
|
||||||
|
|
||||||
|
include Carto::Db::MigrationHelper
|
||||||
|
|
||||||
|
migration(
|
||||||
|
proc do
|
||||||
|
create_table :user_email_notifications do
|
||||||
|
Uuid :id, primary_key: true, default: Sequel.lit('uuid_generate_v4()')
|
||||||
|
foreign_key :user_id, :users, type: :uuid, null: false, on_delete: :cascade
|
||||||
|
String :notification, null: false
|
||||||
|
Boolean :enabled
|
||||||
|
DateTime :created_at, null: false
|
||||||
|
DateTime :updated_at, null: false
|
||||||
|
index [:user_id, :notification], unique: true
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
proc do
|
||||||
|
drop_table :user_email_notifications
|
||||||
|
end
|
||||||
|
)
|
@ -0,0 +1,52 @@
|
|||||||
|
require 'spec_helper_min'
|
||||||
|
require 'support/helpers'
|
||||||
|
|
||||||
|
describe Carto::Api::EmailNotificationsController do
|
||||||
|
include HelperMethods
|
||||||
|
|
||||||
|
before(:all) do
|
||||||
|
@carto_user = FactoryGirl.create(:carto_user)
|
||||||
|
@carto_user.email_notifications = {
|
||||||
|
do_subscriptions: true
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:auth_params) do
|
||||||
|
{ user_domain: @carto_user.username, api_key: @carto_user.api_key }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#show' do
|
||||||
|
it 'list the current notifications' do
|
||||||
|
get_json(api_v3_email_notifications_show_url(auth_params)) do |response|
|
||||||
|
response.status.should eq 200
|
||||||
|
response.body.should eq({ notifications: { do_subscriptions: true } })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'return error if unauthenticated' do
|
||||||
|
get_json(api_v3_email_notifications_show_url({})) do |response|
|
||||||
|
response.status.should eq 401
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#update' do
|
||||||
|
it 'return error if unauthenticated' do
|
||||||
|
put_json(api_v3_email_notifications_update_url({})) do |response|
|
||||||
|
response.status.should eq 401
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns error if an invalid notification is provided' do
|
||||||
|
put_json(api_v3_email_notifications_update_url(auth_params), { notifications: { invalid: true } }) do |response|
|
||||||
|
response.status.should eq 500
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'successfully updates notifications' do
|
||||||
|
put_json(api_v3_email_notifications_update_url(auth_params), { notifications: { do_subscriptions: false } }) do |response|
|
||||||
|
response.status.should eq 204
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue