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
co-authored by Sojan Jose
parent bbd9968d4b
commit 7f26b34b15
15 changed files with 418 additions and 224 deletions
@@ -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
@@ -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
+1
View File
@@ -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
+34
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
+8 -1
View File
@@ -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
@@ -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