Feature: Add new notification settings for user (#569)

Added new notification settings API for user 

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Sony Mathew 2020-02-29 20:41:09 +05:30 committed by GitHub
parent bbd9968d4b
commit 7f26b34b15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 418 additions and 224 deletions

View file

@ -78,6 +78,9 @@ gem 'sentry-raven'
##-- background job processing --## ##-- background job processing --##
gem 'sidekiq' gem 'sidekiq'
##-- used for single column multiple binary flags in notification settings/feature flagging --##
gem 'flag_shih_tzu'
group :development do group :development do
gem 'annotate' gem 'annotate'
gem 'bullet' gem 'bullet'

View file

@ -177,6 +177,7 @@ GEM
faraday_middleware (0.14.0) faraday_middleware (0.14.0)
faraday (>= 0.7.4, < 1.0) faraday (>= 0.7.4, < 1.0)
ffi (1.12.2) ffi (1.12.2)
flag_shih_tzu (0.3.23)
foreman (0.87.0) foreman (0.87.0)
globalid (0.4.2) globalid (0.4.2)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
@ -502,6 +503,7 @@ DEPENDENCIES
facebook-messenger facebook-messenger
factory_bot_rails factory_bot_rails
faker faker
flag_shih_tzu
foreman foreman
google-cloud-storage google-cloud-storage
haikunator haikunator

View file

@ -44,7 +44,7 @@ class Api::V1::CallbacksController < Api::BaseController
end end
def update_fb_page(fb_page_id, access_token) def update_fb_page(fb_page_id, access_token)
get_fb_page(fb_page_id)&.update_attributes!( get_fb_page(fb_page_id)&.update!(
user_access_token: @user_access_token, page_access_token: access_token user_access_token: @user_access_token, page_access_token: access_token
) )
end end

View file

@ -0,0 +1,29 @@
class Api::V1::User::NotificationSettingsController < Api::BaseController
before_action :set_user, :load_notification_setting
def show; end
def update
update_flags
@notification_setting.save!
render action: 'show'
end
private
def set_user
@user = current_user
end
def load_notification_setting
@notification_setting = @user.notification_settings.find_by(account_id: current_account.id)
end
def notification_setting_params
params.require(:notification_settings).permit(selected_email_flags: [])
end
def update_flags
@notification_setting.selected_email_flags = notification_setting_params[:selected_email_flags]
end
end

View file

@ -24,6 +24,7 @@ class Account < ApplicationRecord
has_many :canned_responses, dependent: :destroy has_many :canned_responses, dependent: :destroy
has_many :webhooks, dependent: :destroy has_many :webhooks, dependent: :destroy
has_one :subscription, dependent: :destroy has_one :subscription, dependent: :destroy
has_many :notification_settings, dependent: :destroy
after_create :create_subscription after_create :create_subscription
after_create :notify_creation after_create :notify_creation

View file

@ -0,0 +1,34 @@
# == Schema Information
#
# Table name: notification_settings
#
# id :bigint not null, primary key
# email_flags :integer default(0), not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer
# user_id :integer
#
# Indexes
#
# by_account_user (account_id,user_id) UNIQUE
#
class NotificationSetting < ApplicationRecord
# used for single column multi flags
include FlagShihTzu
belongs_to :account
belongs_to :user
DEFAULT_QUERY_SETTING = {
flag_query_mode: :bit_operator
}.freeze
EMAIL_NOTIFCATION_FLAGS = {
1 => :conversation_creation,
2 => :conversation_assignment
}.freeze
has_flags EMAIL_NOTIFCATION_FLAGS.merge(column: 'email_flags').merge(DEFAULT_QUERY_SETTING)
end

View file

@ -74,12 +74,13 @@ class User < ApplicationRecord
has_many :inbox_members, dependent: :destroy has_many :inbox_members, dependent: :destroy
has_many :assigned_inboxes, through: :inbox_members, source: :inbox has_many :assigned_inboxes, through: :inbox_members, source: :inbox
has_many :messages has_many :messages
has_many :notification_settings, dependent: :destroy
before_validation :set_password_and_uid, on: :create before_validation :set_password_and_uid, on: :create
accepts_nested_attributes_for :account accepts_nested_attributes_for :account
after_create :notify_creation after_create :notify_creation, :create_notification_setting
after_destroy :notify_deletion after_destroy :notify_deletion
def send_devise_notification(notification, *args) def send_devise_notification(notification, *args)
@ -100,6 +101,12 @@ class User < ApplicationRecord
Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: account) Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: account)
end end
def create_notification_setting
setting = notification_settings.new(account_id: account_id)
setting.selected_email_flags = [:conversation_assignment]
setting.save!
end
def notify_deletion def notify_deletion
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account) Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
end end

View file

@ -0,0 +1,5 @@
json.id @notification_setting.id
json.user_id @notification_setting.user_id
json.account_id @notification_setting.account_id
json.all_email_flags @notification_setting.all_email_flags
json.selected_email_flags @notification_setting.selected_email_flags

View file

@ -102,6 +102,10 @@ Rails.application.routes.draw do
post :chargebee post :chargebee
end end
end end
namespace :user do
resource :notification_settings, only: [:show, :update]
end
end end
end end

View file

@ -0,0 +1,22 @@
class AddNotificationSettings < ActiveRecord::Migration[6.0]
def change
create_table :notification_settings do |t|
t.integer :account_id
t.integer :user_id
t.integer :email_flags, null: false, default: 0
t.timestamps
end
add_index :notification_settings, [:account_id, :user_id], unique: true, name: 'by_account_user'
::User.find_in_batches do |users_batch|
users_batch.each do |user|
user_notification_setting = user.notification_settings.new(account_id: user.account_id)
user_notification_setting.conversation_creation = false
user_notification_setting.conversation_assignment = true
user_notification_setting.save!
end
end
end
end

View file

@ -10,268 +10,276 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_02_25_162150) do ActiveRecord::Schema.define(version: 20_200_226_194_012) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension 'plpgsql'
create_table "accounts", id: :serial, force: :cascade do |t| create_table 'accounts', id: :serial, force: :cascade do |t|
t.string "name", null: false t.string 'name', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
end end
create_table "active_storage_attachments", force: :cascade do |t| create_table 'active_storage_attachments', force: :cascade do |t|
t.string "name", null: false t.string 'name', null: false
t.string "record_type", null: false t.string 'record_type', null: false
t.bigint "record_id", null: false t.bigint 'record_id', null: false
t.bigint "blob_id", null: false t.bigint 'blob_id', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" t.index ['blob_id'], name: 'index_active_storage_attachments_on_blob_id'
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true t.index %w[record_type record_id name blob_id], name: 'index_active_storage_attachments_uniqueness', unique: true
end end
create_table "active_storage_blobs", force: :cascade do |t| create_table 'active_storage_blobs', force: :cascade do |t|
t.string "key", null: false t.string 'key', null: false
t.string "filename", null: false t.string 'filename', null: false
t.string "content_type" t.string 'content_type'
t.text "metadata" t.text 'metadata'
t.bigint "byte_size", null: false t.bigint 'byte_size', null: false
t.string "checksum", null: false t.string 'checksum', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true t.index ['key'], name: 'index_active_storage_blobs_on_key', unique: true
end end
create_table "attachments", id: :serial, force: :cascade do |t| create_table 'attachments', id: :serial, force: :cascade do |t|
t.integer "file_type", default: 0 t.integer 'file_type', default: 0
t.string "external_url" t.string 'external_url'
t.float "coordinates_lat", default: 0.0 t.float 'coordinates_lat', default: 0.0
t.float "coordinates_long", default: 0.0 t.float 'coordinates_long', default: 0.0
t.integer "message_id", null: false t.integer 'message_id', null: false
t.integer "account_id", null: false t.integer 'account_id', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.string "fallback_title" t.string 'fallback_title'
t.string "extension" t.string 'extension'
end end
create_table "canned_responses", id: :serial, force: :cascade do |t| create_table 'canned_responses', id: :serial, force: :cascade do |t|
t.integer "account_id", null: false t.integer 'account_id', null: false
t.string "short_code" t.string 'short_code'
t.text "content" t.text 'content'
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
end end
create_table "channel_facebook_pages", id: :serial, force: :cascade do |t| create_table 'channel_facebook_pages', id: :serial, force: :cascade do |t|
t.string "name", null: false t.string 'name', null: false
t.string "page_id", null: false t.string 'page_id', null: false
t.string "user_access_token", null: false t.string 'user_access_token', null: false
t.string "page_access_token", null: false t.string 'page_access_token', null: false
t.integer "account_id", null: false t.integer 'account_id', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.index ["page_id", "account_id"], name: "index_channel_facebook_pages_on_page_id_and_account_id", unique: true t.index %w[page_id account_id], name: 'index_channel_facebook_pages_on_page_id_and_account_id', unique: true
t.index ["page_id"], name: "index_channel_facebook_pages_on_page_id" t.index ['page_id'], name: 'index_channel_facebook_pages_on_page_id'
end end
create_table "channel_twitter_profiles", force: :cascade do |t| create_table 'channel_twitter_profiles', force: :cascade do |t|
t.string "name" t.string 'name'
t.string "profile_id", null: false t.string 'profile_id', null: false
t.string "twitter_access_token", null: false t.string 'twitter_access_token', null: false
t.string "twitter_access_token_secret", null: false t.string 'twitter_access_token_secret', null: false
t.integer "account_id", null: false t.integer 'account_id', null: false
t.datetime "created_at", precision: 6, null: false t.datetime 'created_at', precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime 'updated_at', precision: 6, null: false
end end
create_table "channel_web_widgets", id: :serial, force: :cascade do |t| create_table 'channel_web_widgets', id: :serial, force: :cascade do |t|
t.string "website_name" t.string 'website_name'
t.string "website_url" t.string 'website_url'
t.integer "account_id" t.integer 'account_id'
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.string "website_token" t.string 'website_token'
t.string "widget_color", default: "#1f93ff" t.string 'widget_color', default: '#1f93ff'
t.index ["website_token"], name: "index_channel_web_widgets_on_website_token", unique: true t.index ['website_token'], name: 'index_channel_web_widgets_on_website_token', unique: true
end end
create_table "contact_inboxes", force: :cascade do |t| create_table 'contact_inboxes', force: :cascade do |t|
t.bigint "contact_id" t.bigint 'contact_id'
t.bigint "inbox_id" t.bigint 'inbox_id'
t.string "source_id", null: false t.string 'source_id', null: false
t.datetime "created_at", precision: 6, null: false t.datetime 'created_at', precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime 'updated_at', precision: 6, null: false
t.index ["contact_id"], name: "index_contact_inboxes_on_contact_id" t.index ['contact_id'], name: 'index_contact_inboxes_on_contact_id'
t.index ["inbox_id", "source_id"], name: "index_contact_inboxes_on_inbox_id_and_source_id", unique: true t.index %w[inbox_id source_id], name: 'index_contact_inboxes_on_inbox_id_and_source_id', unique: true
t.index ["inbox_id"], name: "index_contact_inboxes_on_inbox_id" t.index ['inbox_id'], name: 'index_contact_inboxes_on_inbox_id'
t.index ["source_id"], name: "index_contact_inboxes_on_source_id" t.index ['source_id'], name: 'index_contact_inboxes_on_source_id'
end end
create_table "contacts", id: :serial, force: :cascade do |t| create_table 'contacts', id: :serial, force: :cascade do |t|
t.string "name" t.string 'name'
t.string "email" t.string 'email'
t.string "phone_number" t.string 'phone_number'
t.integer "account_id", null: false t.integer 'account_id', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.string "pubsub_token" t.string 'pubsub_token'
t.jsonb "additional_attributes" t.jsonb 'additional_attributes'
t.index ["account_id"], name: "index_contacts_on_account_id" t.index ['account_id'], name: 'index_contacts_on_account_id'
t.index ["pubsub_token"], name: "index_contacts_on_pubsub_token", unique: true t.index ['pubsub_token'], name: 'index_contacts_on_pubsub_token', unique: true
end end
create_table "conversations", id: :serial, force: :cascade do |t| create_table 'conversations', id: :serial, force: :cascade do |t|
t.integer "account_id", null: false t.integer 'account_id', null: false
t.integer "inbox_id", null: false t.integer 'inbox_id', null: false
t.integer "status", default: 0, null: false t.integer 'status', default: 0, null: false
t.integer "assignee_id" t.integer 'assignee_id'
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.bigint "contact_id" t.bigint 'contact_id'
t.integer "display_id", null: false t.integer 'display_id', null: false
t.datetime "user_last_seen_at" t.datetime 'user_last_seen_at'
t.datetime "agent_last_seen_at" t.datetime 'agent_last_seen_at'
t.boolean "locked", default: false t.boolean 'locked', default: false
t.jsonb "additional_attributes" t.jsonb 'additional_attributes'
t.bigint "contact_inbox_id" t.bigint 'contact_inbox_id'
t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true t.index %w[account_id display_id], name: 'index_conversations_on_account_id_and_display_id', unique: true
t.index ["account_id"], name: "index_conversations_on_account_id" t.index ['account_id'], name: 'index_conversations_on_account_id'
t.index ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id" t.index ['contact_inbox_id'], name: 'index_conversations_on_contact_inbox_id'
end end
create_table "inbox_members", id: :serial, force: :cascade do |t| create_table 'inbox_members', id: :serial, force: :cascade do |t|
t.integer "user_id", null: false t.integer 'user_id', null: false
t.integer "inbox_id", null: false t.integer 'inbox_id', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.index ["inbox_id"], name: "index_inbox_members_on_inbox_id" t.index ['inbox_id'], name: 'index_inbox_members_on_inbox_id'
end end
create_table "inboxes", id: :serial, force: :cascade do |t| create_table 'inboxes', id: :serial, force: :cascade do |t|
t.integer "channel_id", null: false t.integer 'channel_id', null: false
t.integer "account_id", null: false t.integer 'account_id', null: false
t.string "name", null: false t.string 'name', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.string "channel_type" t.string 'channel_type'
t.boolean "enable_auto_assignment", default: true t.boolean 'enable_auto_assignment', default: true
t.index ["account_id"], name: "index_inboxes_on_account_id" t.index ['account_id'], name: 'index_inboxes_on_account_id'
end end
create_table "messages", id: :serial, force: :cascade do |t| create_table 'messages', id: :serial, force: :cascade do |t|
t.text "content" t.text 'content'
t.integer "account_id", null: false t.integer 'account_id', null: false
t.integer "inbox_id", null: false t.integer 'inbox_id', null: false
t.integer "conversation_id", null: false t.integer 'conversation_id', null: false
t.integer "message_type", null: false t.integer 'message_type', null: false
t.datetime "created_at", null: false t.datetime 'created_at', null: false
t.datetime "updated_at", null: false t.datetime 'updated_at', null: false
t.boolean "private", default: false t.boolean 'private', default: false
t.integer "user_id" t.integer 'user_id'
t.integer "status", default: 0 t.integer 'status', default: 0
t.string "source_id" t.string 'source_id'
t.integer "content_type", default: 0 t.integer 'content_type', default: 0
t.json "content_attributes", default: {} t.json 'content_attributes', default: {}
t.bigint "contact_id" t.bigint 'contact_id'
t.index ["contact_id"], name: "index_messages_on_contact_id" t.index ['contact_id'], name: 'index_messages_on_contact_id'
t.index ["conversation_id"], name: "index_messages_on_conversation_id" t.index ['conversation_id'], name: 'index_messages_on_conversation_id'
t.index ["source_id"], name: "index_messages_on_source_id" t.index ['source_id'], name: 'index_messages_on_source_id'
end end
create_table "subscriptions", id: :serial, force: :cascade do |t| create_table 'notification_settings', force: :cascade do |t|
t.string "pricing_version" t.integer 'account_id'
t.integer "account_id" t.integer 'user_id'
t.datetime "expiry" t.integer 'email_flags', default: 0, null: false
t.string "billing_plan", default: "trial" t.datetime 'created_at', precision: 6, null: false
t.string "stripe_customer_id" t.datetime 'updated_at', precision: 6, null: false
t.datetime "created_at", null: false t.index %w[account_id user_id], name: 'by_account_user', unique: true
t.datetime "updated_at", null: false
t.integer "state", default: 0
t.boolean "payment_source_added", default: false
end end
create_table "taggings", id: :serial, force: :cascade do |t| create_table 'subscriptions', id: :serial, force: :cascade do |t|
t.integer "tag_id" t.string 'pricing_version'
t.string "taggable_type" t.integer 'account_id'
t.integer "taggable_id" t.datetime 'expiry'
t.string "tagger_type" t.string 'billing_plan', default: 'trial'
t.integer "tagger_id" t.string 'stripe_customer_id'
t.string "context", limit: 128 t.datetime 'created_at', null: false
t.datetime "created_at" t.datetime 'updated_at', null: false
t.index ["context"], name: "index_taggings_on_context" t.integer 'state', default: 0
t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true t.boolean 'payment_source_added', default: false
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id"
t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
t.index ["tagger_type", "tagger_id"], name: "index_taggings_on_tagger_type_and_tagger_id"
end end
create_table "tags", id: :serial, force: :cascade do |t| create_table 'taggings', id: :serial, force: :cascade do |t|
t.string "name" t.integer 'tag_id'
t.integer "taggings_count", default: 0 t.string 'taggable_type'
t.index ["name"], name: "index_tags_on_name", unique: true t.integer 'taggable_id'
t.string 'tagger_type'
t.integer 'tagger_id'
t.string 'context', limit: 128
t.datetime 'created_at'
t.index ['context'], name: 'index_taggings_on_context'
t.index %w[tag_id taggable_id taggable_type context tagger_id tagger_type], name: 'taggings_idx', unique: true
t.index ['tag_id'], name: 'index_taggings_on_tag_id'
t.index %w[taggable_id taggable_type context], name: 'index_taggings_on_taggable_id_and_taggable_type_and_context'
t.index %w[taggable_id taggable_type tagger_id context], name: 'taggings_idy'
t.index ['taggable_id'], name: 'index_taggings_on_taggable_id'
t.index %w[taggable_type taggable_id], name: 'index_taggings_on_taggable_type_and_taggable_id'
t.index ['taggable_type'], name: 'index_taggings_on_taggable_type'
t.index %w[tagger_id tagger_type], name: 'index_taggings_on_tagger_id_and_tagger_type'
t.index ['tagger_id'], name: 'index_taggings_on_tagger_id'
t.index %w[tagger_type tagger_id], name: 'index_taggings_on_tagger_type_and_tagger_id'
end end
create_table "telegram_bots", id: :serial, force: :cascade do |t| create_table 'tags', id: :serial, force: :cascade do |t|
t.string "name" t.string 'name'
t.string "auth_key" t.integer 'taggings_count', default: 0
t.integer "account_id" t.index ['name'], name: 'index_tags_on_name', unique: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end end
create_table "users", id: :serial, force: :cascade do |t| create_table 'telegram_bots', id: :serial, force: :cascade do |t|
t.string "provider", default: "email", null: false t.string 'name'
t.string "uid", default: "", null: false t.string 'auth_key'
t.string "encrypted_password", default: "", null: false t.integer 'account_id'
t.string "reset_password_token" t.datetime 'created_at', null: false
t.datetime "reset_password_sent_at" t.datetime 'updated_at', null: false
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.string "name", null: false
t.string "nickname"
t.string "email"
t.json "tokens"
t.integer "account_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "pubsub_token"
t.integer "role", default: 0
t.bigint "inviter_id"
t.index ["email"], name: "index_users_on_email"
t.index ["inviter_id"], name: "index_users_on_inviter_id"
t.index ["pubsub_token"], name: "index_users_on_pubsub_token", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true
end end
create_table "webhooks", force: :cascade do |t| create_table 'users', id: :serial, force: :cascade do |t|
t.integer "account_id" t.string 'provider', default: 'email', null: false
t.integer "inbox_id" t.string 'uid', default: '', null: false
t.string "url" t.string 'encrypted_password', default: '', null: false
t.datetime "created_at", precision: 6, null: false t.string 'reset_password_token'
t.datetime "updated_at", precision: 6, null: false t.datetime 'reset_password_sent_at'
t.integer "webhook_type", default: 0 t.datetime 'remember_created_at'
t.integer 'sign_in_count', default: 0, null: false
t.datetime 'current_sign_in_at'
t.datetime 'last_sign_in_at'
t.string 'current_sign_in_ip'
t.string 'last_sign_in_ip'
t.string 'confirmation_token'
t.datetime 'confirmed_at'
t.datetime 'confirmation_sent_at'
t.string 'unconfirmed_email'
t.string 'name', null: false
t.string 'nickname'
t.string 'email'
t.json 'tokens'
t.integer 'account_id', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.string 'pubsub_token'
t.integer 'role', default: 0
t.bigint 'inviter_id'
t.index ['email'], name: 'index_users_on_email'
t.index ['inviter_id'], name: 'index_users_on_inviter_id'
t.index ['pubsub_token'], name: 'index_users_on_pubsub_token', unique: true
t.index ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true
t.index %w[uid provider], name: 'index_users_on_uid_and_provider', unique: true
end end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" create_table 'webhooks', force: :cascade do |t|
add_foreign_key "contact_inboxes", "contacts" t.integer 'account_id'
add_foreign_key "contact_inboxes", "inboxes" t.integer 'inbox_id'
add_foreign_key "conversations", "contact_inboxes" t.string 'url'
add_foreign_key "messages", "contacts" t.datetime 'created_at', precision: 6, null: false
add_foreign_key "users", "users", column: "inviter_id", on_delete: :nullify t.datetime 'updated_at', precision: 6, null: false
t.integer 'webhook_type', default: 0
end
add_foreign_key 'active_storage_attachments', 'active_storage_blobs', column: 'blob_id'
add_foreign_key 'contact_inboxes', 'contacts'
add_foreign_key 'contact_inboxes', 'inboxes'
add_foreign_key 'conversations', 'contact_inboxes'
add_foreign_key 'messages', 'contacts'
add_foreign_key 'users', 'users', column: 'inviter_id', on_delete: :nullify
end end

View file

@ -0,0 +1,58 @@
require 'rails_helper'
RSpec.describe 'Notification Settings API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/user/notification_settings' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get '/api/v1/user/notification_settings'
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns current user notification settings' do
get '/api/v1/user/notification_settings',
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['user_id']).to eq(agent.id)
expect(json_response['account_id']).to eq(account.id)
end
end
end
describe 'PUT /api/v1/user/notification_settings' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put '/api/v1/user/notification_settings'
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
it 'updates the email related notification flags' do
put '/api/v1/user/notification_settings',
params: { notification_settings: { selected_email_flags: ['conversation_assignment'] } },
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
agent.reload
expect(json_response['user_id']).to eq(agent.id)
expect(json_response['account_id']).to eq(account.id)
expect(json_response['selected_email_flags']).to eq(['conversation_assignment'])
end
end
end
end

View file

@ -15,4 +15,5 @@ RSpec.describe Account do
it { is_expected.to have_many(:web_widgets).class_name('::Channel::WebWidget').dependent(:destroy) } it { is_expected.to have_many(:web_widgets).class_name('::Channel::WebWidget').dependent(:destroy) }
it { is_expected.to have_one(:subscription).dependent(:destroy) } it { is_expected.to have_one(:subscription).dependent(:destroy) }
it { is_expected.to have_many(:webhooks).dependent(:destroy) } it { is_expected.to have_many(:webhooks).dependent(:destroy) }
it { is_expected.to have_many(:notification_settings).dependent(:destroy) }
end end

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe NotificationSetting do
context 'with associations' do
it { is_expected.to belong_to(:account) }
it { is_expected.to belong_to(:user) }
end
end

View file

@ -16,6 +16,7 @@ RSpec.describe User do
it { is_expected.to belong_to(:inviter).class_name('User').required(false) } it { is_expected.to belong_to(:inviter).class_name('User').required(false) }
it { is_expected.to have_many(:assigned_conversations).class_name('Conversation').dependent(:nullify) } it { is_expected.to have_many(:assigned_conversations).class_name('Conversation').dependent(:nullify) }
it { is_expected.to have_many(:inbox_members).dependent(:destroy) } it { is_expected.to have_many(:inbox_members).dependent(:destroy) }
it { is_expected.to have_many(:notification_settings).dependent(:destroy) }
it { is_expected.to have_many(:assigned_inboxes).through(:inbox_members) } it { is_expected.to have_many(:assigned_inboxes).through(:inbox_members) }
it { is_expected.to have_many(:messages) } it { is_expected.to have_many(:messages) }
end end
@ -26,4 +27,13 @@ RSpec.describe User do
it { expect(user.pubsub_token).not_to eq(nil) } it { expect(user.pubsub_token).not_to eq(nil) }
it { expect(user.saved_changes.keys).not_to eq('pubsub_token') } it { expect(user.saved_changes.keys).not_to eq('pubsub_token') }
end end
describe 'notification_settings' do
it 'gets created with the right default settings' do
expect(user.notification_settings).not_to eq(nil)
expect(user.notification_settings.first.conversation_creation?).to eq(false)
expect(user.notification_settings.first.conversation_assignment?).to eq(true)
end
end
end end