feat: Add APIs for custom attribute definitions (#2689)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
class Api::V1::Accounts::CustomAttributeDefinitionsController < Api::V1::Accounts::BaseController
|
||||
before_action :fetch_custom_attributes_definitions, except: [:create]
|
||||
before_action :fetch_custom_attribute_definition, only: [:show, :update, :destroy]
|
||||
DEFAULT_ATTRIBUTE_MODEL = 'conversation_attribute'.freeze
|
||||
|
||||
def index; end
|
||||
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@custom_attribute_definition = Current.account.custom_attribute_definitions.create!(
|
||||
permitted_payload
|
||||
)
|
||||
end
|
||||
|
||||
def update
|
||||
@custom_attribute_definition.update!(permitted_payload)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@custom_attribute_definition.destroy
|
||||
head :no_content
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_custom_attributes_definitions
|
||||
@custom_attribute_definitions = Current.account.custom_attribute_definitions.where(
|
||||
attribute_model: permitted_params[:attribute_model] || DEFAULT_ATTRIBUTE_MODEL
|
||||
)
|
||||
end
|
||||
|
||||
def fetch_custom_attribute_definition
|
||||
@custom_attribute_definition = @custom_attribute_definitions.find(permitted_params[:id])
|
||||
end
|
||||
|
||||
def permitted_payload
|
||||
params.require(:custom_attribute_definition).permit(
|
||||
:attribute_display_name,
|
||||
:attribute_display_type,
|
||||
:attribute_key,
|
||||
:attribute_model,
|
||||
:default_value
|
||||
)
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.permit(:id, :filter_type)
|
||||
end
|
||||
end
|
||||
@@ -61,6 +61,8 @@ class Account < ApplicationRecord
|
||||
has_many :kbase_articles, dependent: :destroy, class_name: '::Kbase::Article'
|
||||
has_many :teams, dependent: :destroy
|
||||
has_many :custom_filters, dependent: :destroy
|
||||
has_many :custom_attribute_definitions, 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
|
||||
|
||||
34
app/models/custom_attribute_definition.rb
Normal file
34
app/models/custom_attribute_definition.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: custom_attribute_definitions
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# attribute_display_name :string
|
||||
# attribute_display_type :integer default("text")
|
||||
# attribute_key :string
|
||||
# attribute_model :integer default("conversation_attribute")
|
||||
# default_value :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# attribute_key_model_index (attribute_key,attribute_model) UNIQUE
|
||||
# index_custom_attribute_definitions_on_account_id (account_id)
|
||||
#
|
||||
class CustomAttributeDefinition < ApplicationRecord
|
||||
validates :attribute_display_name, presence: true
|
||||
|
||||
validates :attribute_key,
|
||||
presence: true,
|
||||
uniqueness: { scope: :attribute_model }
|
||||
|
||||
validates :attribute_display_type, presence: true
|
||||
validates :attribute_model, presence: true
|
||||
|
||||
enum attribute_model: { conversation_attribute: 0, contact_attribute: 1 }
|
||||
enum attribute_display_type: { text: 0, number: 1, currency: 2, percent: 3, link: 4 }
|
||||
|
||||
belongs_to :account
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/custom_attribute_definition.json.jbuilder', resource: @custom_attribute_definition
|
||||
@@ -0,0 +1,3 @@
|
||||
json.array! @custom_attribute_definitions do |custom_attribute_definition|
|
||||
json.partial! 'api/v1/models/custom_attribute_definition.json.jbuilder', resource: custom_attribute_definition
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/custom_attribute_definition.json.jbuilder', resource: @custom_attribute_definition
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/custom_attribute_definition.json.jbuilder', resource: @custom_attribute_definition
|
||||
@@ -0,0 +1,7 @@
|
||||
json.attribute_display_name resource.attribute_display_name
|
||||
json.attribute_display_type resource.attribute_display_type
|
||||
json.attribute_key resource.attribute_key
|
||||
json.attribute_model resource.attribute_model
|
||||
json.default_value resource.default_value
|
||||
json.created_at resource.created_at
|
||||
json.updated_at resource.updated_at
|
||||
Reference in New Issue
Block a user