From 7f26b34b157cf3a3a12fa2b13d45f5c2c8a66459 Mon Sep 17 00:00:00 2001 From: Sony Mathew Date: Sat, 29 Feb 2020 20:41:09 +0530 Subject: [PATCH] Feature: Add new notification settings for user (#569) Added new notification settings API for user Co-authored-by: Sojan Jose --- Gemfile | 3 + Gemfile.lock | 2 + .../api/v1/callbacks_controller.rb | 2 +- .../user/notification_settings_controller.rb | 29 ++ app/models/account.rb | 1 + app/models/notification_setting.rb | 34 ++ app/models/user.rb | 9 +- .../notification_settings/show.json.jbuilder | 5 + config/routes.rb | 4 + ...0200226194012_add_notification_settings.rb | 22 + db/schema.rb | 452 +++++++++--------- .../notification_settings_controller_spec.rb | 58 +++ spec/models/account_spec.rb | 1 + spec/models/notification_setting_spec.rb | 10 + spec/models/user_spec.rb | 10 + 15 files changed, 418 insertions(+), 224 deletions(-) create mode 100644 app/controllers/api/v1/user/notification_settings_controller.rb create mode 100644 app/models/notification_setting.rb create mode 100644 app/views/api/v1/user/notification_settings/show.json.jbuilder create mode 100644 db/migrate/20200226194012_add_notification_settings.rb create mode 100644 spec/controllers/api/v1/user/notification_settings_controller_spec.rb create mode 100644 spec/models/notification_setting_spec.rb diff --git a/Gemfile b/Gemfile index 7a225f023..b3f1a2995 100644 --- a/Gemfile +++ b/Gemfile @@ -78,6 +78,9 @@ gem 'sentry-raven' ##-- background job processing --## gem 'sidekiq' +##-- used for single column multiple binary flags in notification settings/feature flagging --## +gem 'flag_shih_tzu' + group :development do gem 'annotate' gem 'bullet' diff --git a/Gemfile.lock b/Gemfile.lock index d74e3e888..7131f36e3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -177,6 +177,7 @@ GEM faraday_middleware (0.14.0) faraday (>= 0.7.4, < 1.0) ffi (1.12.2) + flag_shih_tzu (0.3.23) foreman (0.87.0) globalid (0.4.2) activesupport (>= 4.2.0) @@ -502,6 +503,7 @@ DEPENDENCIES facebook-messenger factory_bot_rails faker + flag_shih_tzu foreman google-cloud-storage haikunator diff --git a/app/controllers/api/v1/callbacks_controller.rb b/app/controllers/api/v1/callbacks_controller.rb index 5c770f826..031d5accc 100644 --- a/app/controllers/api/v1/callbacks_controller.rb +++ b/app/controllers/api/v1/callbacks_controller.rb @@ -44,7 +44,7 @@ class Api::V1::CallbacksController < Api::BaseController end 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 ) end diff --git a/app/controllers/api/v1/user/notification_settings_controller.rb b/app/controllers/api/v1/user/notification_settings_controller.rb new file mode 100644 index 000000000..78c5dc720 --- /dev/null +++ b/app/controllers/api/v1/user/notification_settings_controller.rb @@ -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 diff --git a/app/models/account.rb b/app/models/account.rb index 30987d041..77b102847 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -24,6 +24,7 @@ class Account < ApplicationRecord has_many :canned_responses, dependent: :destroy has_many :webhooks, dependent: :destroy has_one :subscription, dependent: :destroy + has_many :notification_settings, dependent: :destroy after_create :create_subscription after_create :notify_creation diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rb new file mode 100644 index 000000000..e5b26ca96 --- /dev/null +++ b/app/models/notification_setting.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 26567854b..2aa47a011 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -74,12 +74,13 @@ class User < ApplicationRecord has_many :inbox_members, dependent: :destroy has_many :assigned_inboxes, through: :inbox_members, source: :inbox has_many :messages + has_many :notification_settings, dependent: :destroy before_validation :set_password_and_uid, on: :create accepts_nested_attributes_for :account - after_create :notify_creation + after_create :notify_creation, :create_notification_setting after_destroy :notify_deletion def send_devise_notification(notification, *args) @@ -100,6 +101,12 @@ class User < ApplicationRecord Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: account) 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 Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account) end diff --git a/app/views/api/v1/user/notification_settings/show.json.jbuilder b/app/views/api/v1/user/notification_settings/show.json.jbuilder new file mode 100644 index 000000000..4ef421e65 --- /dev/null +++ b/app/views/api/v1/user/notification_settings/show.json.jbuilder @@ -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 diff --git a/config/routes.rb b/config/routes.rb index f5336a1e9..4a4e875a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -102,6 +102,10 @@ Rails.application.routes.draw do post :chargebee end end + + namespace :user do + resource :notification_settings, only: [:show, :update] + end end end diff --git a/db/migrate/20200226194012_add_notification_settings.rb b/db/migrate/20200226194012_add_notification_settings.rb new file mode 100644 index 000000000..af3ab1300 --- /dev/null +++ b/db/migrate/20200226194012_add_notification_settings.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 603df4afd..563501515 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,268 +10,276 @@ # # 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 - enable_extension "plpgsql" + enable_extension 'plpgsql' - create_table "accounts", id: :serial, force: :cascade do |t| - t.string "name", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'accounts', id: :serial, force: :cascade do |t| + t.string 'name', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "active_storage_attachments", force: :cascade do |t| - t.string "name", null: false - t.string "record_type", null: false - t.bigint "record_id", null: false - t.bigint "blob_id", null: false - t.datetime "created_at", null: false - 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 + create_table 'active_storage_attachments', force: :cascade do |t| + t.string 'name', null: false + t.string 'record_type', null: false + t.bigint 'record_id', null: false + t.bigint 'blob_id', null: false + t.datetime 'created_at', null: false + t.index ['blob_id'], name: 'index_active_storage_attachments_on_blob_id' + t.index %w[record_type record_id name blob_id], name: 'index_active_storage_attachments_uniqueness', unique: true end - create_table "active_storage_blobs", force: :cascade do |t| - t.string "key", null: false - t.string "filename", null: false - t.string "content_type" - t.text "metadata" - t.bigint "byte_size", null: false - t.string "checksum", null: false - t.datetime "created_at", null: false - t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true + create_table 'active_storage_blobs', force: :cascade do |t| + t.string 'key', null: false + t.string 'filename', null: false + t.string 'content_type' + t.text 'metadata' + t.bigint 'byte_size', null: false + t.string 'checksum', null: false + t.datetime 'created_at', null: false + t.index ['key'], name: 'index_active_storage_blobs_on_key', unique: true end - create_table "attachments", id: :serial, force: :cascade do |t| - t.integer "file_type", default: 0 - t.string "external_url" - t.float "coordinates_lat", default: 0.0 - t.float "coordinates_long", default: 0.0 - t.integer "message_id", null: false - t.integer "account_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "fallback_title" - t.string "extension" + create_table 'attachments', id: :serial, force: :cascade do |t| + t.integer 'file_type', default: 0 + t.string 'external_url' + t.float 'coordinates_lat', default: 0.0 + t.float 'coordinates_long', default: 0.0 + t.integer 'message_id', null: false + t.integer 'account_id', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'fallback_title' + t.string 'extension' end - create_table "canned_responses", id: :serial, force: :cascade do |t| - t.integer "account_id", null: false - t.string "short_code" - t.text "content" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'canned_responses', id: :serial, force: :cascade do |t| + t.integer 'account_id', null: false + t.string 'short_code' + t.text 'content' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "channel_facebook_pages", id: :serial, force: :cascade do |t| - t.string "name", null: false - t.string "page_id", null: false - t.string "user_access_token", null: false - t.string "page_access_token", null: false - t.integer "account_id", null: false - t.datetime "created_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 ["page_id"], name: "index_channel_facebook_pages_on_page_id" + create_table 'channel_facebook_pages', id: :serial, force: :cascade do |t| + t.string 'name', null: false + t.string 'page_id', null: false + t.string 'user_access_token', null: false + t.string 'page_access_token', null: false + t.integer 'account_id', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + 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' end - create_table "channel_twitter_profiles", force: :cascade do |t| - t.string "name" - t.string "profile_id", null: false - t.string "twitter_access_token", null: false - t.string "twitter_access_token_secret", null: false - t.integer "account_id", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false + create_table 'channel_twitter_profiles', force: :cascade do |t| + t.string 'name' + t.string 'profile_id', null: false + t.string 'twitter_access_token', null: false + t.string 'twitter_access_token_secret', null: false + t.integer 'account_id', null: false + t.datetime 'created_at', precision: 6, null: false + t.datetime 'updated_at', precision: 6, null: false end - create_table "channel_web_widgets", id: :serial, force: :cascade do |t| - t.string "website_name" - t.string "website_url" - t.integer "account_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "website_token" - t.string "widget_color", default: "#1f93ff" - t.index ["website_token"], name: "index_channel_web_widgets_on_website_token", unique: true + create_table 'channel_web_widgets', id: :serial, force: :cascade do |t| + t.string 'website_name' + t.string 'website_url' + t.integer 'account_id' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'website_token' + t.string 'widget_color', default: '#1f93ff' + t.index ['website_token'], name: 'index_channel_web_widgets_on_website_token', unique: true end - create_table "contact_inboxes", force: :cascade do |t| - t.bigint "contact_id" - t.bigint "inbox_id" - t.string "source_id", null: false - t.datetime "created_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 ["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 ["source_id"], name: "index_contact_inboxes_on_source_id" + create_table 'contact_inboxes', force: :cascade do |t| + t.bigint 'contact_id' + t.bigint 'inbox_id' + t.string 'source_id', null: false + t.datetime 'created_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 %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 ['source_id'], name: 'index_contact_inboxes_on_source_id' end - create_table "contacts", id: :serial, force: :cascade do |t| - t.string "name" - t.string "email" - t.string "phone_number" - t.integer "account_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "pubsub_token" - t.jsonb "additional_attributes" - t.index ["account_id"], name: "index_contacts_on_account_id" - t.index ["pubsub_token"], name: "index_contacts_on_pubsub_token", unique: true + create_table 'contacts', id: :serial, force: :cascade do |t| + t.string 'name' + t.string 'email' + t.string 'phone_number' + t.integer 'account_id', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'pubsub_token' + t.jsonb 'additional_attributes' + t.index ['account_id'], name: 'index_contacts_on_account_id' + t.index ['pubsub_token'], name: 'index_contacts_on_pubsub_token', unique: true end - create_table "conversations", id: :serial, force: :cascade do |t| - t.integer "account_id", null: false - t.integer "inbox_id", null: false - t.integer "status", default: 0, null: false - t.integer "assignee_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.bigint "contact_id" - t.integer "display_id", null: false - t.datetime "user_last_seen_at" - t.datetime "agent_last_seen_at" - t.boolean "locked", default: false - t.jsonb "additional_attributes" - t.bigint "contact_inbox_id" - t.index ["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 ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id" + create_table 'conversations', id: :serial, force: :cascade do |t| + t.integer 'account_id', null: false + t.integer 'inbox_id', null: false + t.integer 'status', default: 0, null: false + t.integer 'assignee_id' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.bigint 'contact_id' + t.integer 'display_id', null: false + t.datetime 'user_last_seen_at' + t.datetime 'agent_last_seen_at' + t.boolean 'locked', default: false + t.jsonb 'additional_attributes' + t.bigint 'contact_inbox_id' + 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 ['contact_inbox_id'], name: 'index_conversations_on_contact_inbox_id' end - create_table "inbox_members", id: :serial, force: :cascade do |t| - t.integer "user_id", null: false - t.integer "inbox_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["inbox_id"], name: "index_inbox_members_on_inbox_id" + create_table 'inbox_members', id: :serial, force: :cascade do |t| + t.integer 'user_id', null: false + t.integer 'inbox_id', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.index ['inbox_id'], name: 'index_inbox_members_on_inbox_id' end - create_table "inboxes", id: :serial, force: :cascade do |t| - t.integer "channel_id", null: false - t.integer "account_id", null: false - t.string "name", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "channel_type" - t.boolean "enable_auto_assignment", default: true - t.index ["account_id"], name: "index_inboxes_on_account_id" + create_table 'inboxes', id: :serial, force: :cascade do |t| + t.integer 'channel_id', null: false + t.integer 'account_id', null: false + t.string 'name', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.string 'channel_type' + t.boolean 'enable_auto_assignment', default: true + t.index ['account_id'], name: 'index_inboxes_on_account_id' end - create_table "messages", id: :serial, force: :cascade do |t| - t.text "content" - t.integer "account_id", null: false - t.integer "inbox_id", null: false - t.integer "conversation_id", null: false - t.integer "message_type", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.boolean "private", default: false - t.integer "user_id" - t.integer "status", default: 0 - t.string "source_id" - t.integer "content_type", default: 0 - t.json "content_attributes", default: {} - t.bigint "contact_id" - t.index ["contact_id"], name: "index_messages_on_contact_id" - t.index ["conversation_id"], name: "index_messages_on_conversation_id" - t.index ["source_id"], name: "index_messages_on_source_id" + create_table 'messages', id: :serial, force: :cascade do |t| + t.text 'content' + t.integer 'account_id', null: false + t.integer 'inbox_id', null: false + t.integer 'conversation_id', null: false + t.integer 'message_type', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.boolean 'private', default: false + t.integer 'user_id' + t.integer 'status', default: 0 + t.string 'source_id' + t.integer 'content_type', default: 0 + t.json 'content_attributes', default: {} + t.bigint 'contact_id' + t.index ['contact_id'], name: 'index_messages_on_contact_id' + t.index ['conversation_id'], name: 'index_messages_on_conversation_id' + t.index ['source_id'], name: 'index_messages_on_source_id' end - create_table "subscriptions", id: :serial, force: :cascade do |t| - t.string "pricing_version" - t.integer "account_id" - t.datetime "expiry" - t.string "billing_plan", default: "trial" - t.string "stripe_customer_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "state", default: 0 - t.boolean "payment_source_added", default: false + create_table 'notification_settings', force: :cascade do |t| + t.integer 'account_id' + t.integer 'user_id' + t.integer 'email_flags', default: 0, null: false + t.datetime 'created_at', precision: 6, null: false + t.datetime 'updated_at', precision: 6, null: false + t.index %w[account_id user_id], name: 'by_account_user', unique: true end - create_table "taggings", id: :serial, force: :cascade do |t| - t.integer "tag_id" - t.string "taggable_type" - 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 ["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 ["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" + create_table 'subscriptions', id: :serial, force: :cascade do |t| + t.string 'pricing_version' + t.integer 'account_id' + t.datetime 'expiry' + t.string 'billing_plan', default: 'trial' + t.string 'stripe_customer_id' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false + t.integer 'state', default: 0 + t.boolean 'payment_source_added', default: false end - create_table "tags", id: :serial, force: :cascade do |t| - t.string "name" - t.integer "taggings_count", default: 0 - t.index ["name"], name: "index_tags_on_name", unique: true + create_table 'taggings', id: :serial, force: :cascade do |t| + t.integer 'tag_id' + t.string 'taggable_type' + 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 - create_table "telegram_bots", id: :serial, force: :cascade do |t| - t.string "name" - t.string "auth_key" - t.integer "account_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + create_table 'tags', id: :serial, force: :cascade do |t| + t.string 'name' + t.integer 'taggings_count', default: 0 + t.index ['name'], name: 'index_tags_on_name', unique: true end - create_table "users", id: :serial, force: :cascade do |t| - t.string "provider", default: "email", null: false - t.string "uid", default: "", null: false - t.string "encrypted_password", default: "", null: false - t.string "reset_password_token" - t.datetime "reset_password_sent_at" - 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 + create_table 'telegram_bots', id: :serial, force: :cascade do |t| + t.string 'name' + t.string 'auth_key' + t.integer 'account_id' + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end - create_table "webhooks", force: :cascade do |t| - t.integer "account_id" - t.integer "inbox_id" - t.string "url" - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.integer "webhook_type", default: 0 + create_table 'users', id: :serial, force: :cascade do |t| + t.string 'provider', default: 'email', null: false + t.string 'uid', default: '', null: false + t.string 'encrypted_password', default: '', null: false + t.string 'reset_password_token' + t.datetime 'reset_password_sent_at' + 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 - 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 + create_table 'webhooks', force: :cascade do |t| + t.integer 'account_id' + t.integer 'inbox_id' + t.string 'url' + t.datetime 'created_at', precision: 6, null: false + 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 diff --git a/spec/controllers/api/v1/user/notification_settings_controller_spec.rb b/spec/controllers/api/v1/user/notification_settings_controller_spec.rb new file mode 100644 index 000000000..a803dafad --- /dev/null +++ b/spec/controllers/api/v1/user/notification_settings_controller_spec.rb @@ -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 diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 84dcf2677..10d2b4651 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -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_one(:subscription).dependent(:destroy) } it { is_expected.to have_many(:webhooks).dependent(:destroy) } + it { is_expected.to have_many(:notification_settings).dependent(:destroy) } end diff --git a/spec/models/notification_setting_spec.rb b/spec/models/notification_setting_spec.rb new file mode 100644 index 000000000..c4c064a50 --- /dev/null +++ b/spec/models/notification_setting_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 71d89faa8..58858850e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -16,6 +16,7 @@ RSpec.describe User do 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(: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(:messages) } end @@ -26,4 +27,13 @@ RSpec.describe User do it { expect(user.pubsub_token).not_to eq(nil) } it { expect(user.saved_changes.keys).not_to eq('pubsub_token') } 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