feat: Add APIs to create custom views on the dashboard (#2498)
This commit is contained in:
50
app/controllers/api/v1/accounts/custom_filters_controller.rb
Normal file
50
app/controllers/api/v1/accounts/custom_filters_controller.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
class Api::V1::Accounts::CustomFiltersController < Api::V1::Accounts::BaseController
|
||||
protect_from_forgery with: :null_session
|
||||
before_action :fetch_custom_filters, except: [:create]
|
||||
before_action :fetch_custom_filter, only: [:show, :update, :destroy]
|
||||
DEFAULT_FILTER_TYPE = 'conversation'.freeze
|
||||
|
||||
def index; end
|
||||
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@custom_filter = current_user.custom_filters.create!(
|
||||
permitted_payload.merge(account_id: Current.account.id)
|
||||
)
|
||||
end
|
||||
|
||||
def update
|
||||
@custom_filter.update!(permitted_payload)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@custom_filter.destroy
|
||||
head :no_content
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_custom_filters
|
||||
@custom_filters = current_user.custom_filters.where(
|
||||
account_id: Current.account.id,
|
||||
filter_type: permitted_params[:filter_type] || DEFAULT_FILTER_TYPE
|
||||
)
|
||||
end
|
||||
|
||||
def fetch_custom_filter
|
||||
@custom_filter = @custom_filters.find(permitted_params[:id])
|
||||
end
|
||||
|
||||
def permitted_payload
|
||||
params.require(:custom_filter).permit(
|
||||
:name,
|
||||
:filter_type,
|
||||
query: {}
|
||||
)
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.permit(:id, :filter_type)
|
||||
end
|
||||
end
|
||||
@@ -59,6 +59,7 @@ class Account < ApplicationRecord
|
||||
has_many :kbase_categories, dependent: :destroy, class_name: '::Kbase::Category'
|
||||
has_many :kbase_articles, dependent: :destroy, class_name: '::Kbase::Article'
|
||||
has_many :teams, dependent: :destroy
|
||||
has_many :custom_filters, dependent: :destroy
|
||||
has_flags ACCOUNT_SETTINGS_FLAGS.merge(column: 'settings_flags').merge(DEFAULT_QUERY_SETTING)
|
||||
|
||||
enum locale: LANGUAGES_CONFIG.map { |key, val| [val[:iso_639_1_code], key] }.to_h
|
||||
|
||||
24
app/models/custom_filter.rb
Normal file
24
app/models/custom_filter.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: custom_filters
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# filter_type :integer default("conversation"), not null
|
||||
# name :string not null
|
||||
# query :jsonb not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint not null
|
||||
# user_id :bigint not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_custom_filters_on_account_id (account_id)
|
||||
# index_custom_filters_on_user_id (user_id)
|
||||
#
|
||||
class CustomFilter < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :account
|
||||
|
||||
enum filter_type: { conversation: 0, contact: 1, report: 2 }
|
||||
end
|
||||
@@ -83,6 +83,7 @@ class User < ApplicationRecord
|
||||
has_many :team_members, dependent: :destroy
|
||||
has_many :teams, through: :team_members
|
||||
has_many :notes, dependent: :nullify
|
||||
has_many :custom_filters, dependent: :destroy
|
||||
|
||||
before_validation :set_password_and_uid, on: :create
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/custom_filter.json.jbuilder', resource: @custom_filter
|
||||
@@ -0,0 +1,3 @@
|
||||
json.array! @custom_filters do |custom_filter|
|
||||
json.partial! 'api/v1/models/custom_filter.json.jbuilder', resource: custom_filter
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/custom_filter.json.jbuilder', resource: @custom_filter
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/custom_filter.json.jbuilder', resource: @custom_filter
|
||||
6
app/views/api/v1/models/_custom_filter.json.jbuilder
Normal file
6
app/views/api/v1/models/_custom_filter.json.jbuilder
Normal file
@@ -0,0 +1,6 @@
|
||||
json.id resource.id
|
||||
json.name resource.name
|
||||
json.filter_type resource.filter_type
|
||||
json.query resource.query
|
||||
json.created_at resource.created_at
|
||||
json.updated_at resource.updated_at
|
||||
Reference in New Issue
Block a user