feat: APIs for Integration Hooks (#2250)
- Introduces JSON Schema validations via JSONSchemer - Add CRUD APIs for integration hooks
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
class Api::V1::Accounts::Integrations::HooksController < Api::V1::Accounts::BaseController
|
||||
before_action :fetch_hook, only: [:update, :destroy]
|
||||
before_action :check_authorization
|
||||
|
||||
def create
|
||||
@hook = Current.account.hooks.create!(permitted_params)
|
||||
end
|
||||
|
||||
def update
|
||||
@hook.update!(permitted_params.slice(:status, :settings))
|
||||
end
|
||||
|
||||
def destroy
|
||||
@hook.destroy
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_hook
|
||||
@hook = Current.account.hooks.find(params[:id])
|
||||
end
|
||||
|
||||
def check_authorization
|
||||
authorize(:hook)
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.require(:hook).permit(:app_id, :inbox_id, :status, settings: {})
|
||||
end
|
||||
end
|
||||
@@ -17,8 +17,13 @@
|
||||
class Integrations::Hook < ApplicationRecord
|
||||
include Reauthorizable
|
||||
|
||||
attr_readonly :app_id, :account_id, :inbox_id, :hook_type
|
||||
before_validation :ensure_hook_type
|
||||
validates :account_id, presence: true
|
||||
validates :app_id, presence: true
|
||||
validates :inbox_id, presence: true, if: -> { hook_type == 'inbox' }
|
||||
validate :validate_settings_json_schema
|
||||
validates :app_id, uniqueness: { scope: [:account_id], unless: -> { app.present? && app.params[:allow_multiple_hooks].present? } }
|
||||
|
||||
enum status: { disabled: 0, enabled: 1 }
|
||||
|
||||
@@ -39,4 +44,16 @@ class Integrations::Hook < ApplicationRecord
|
||||
def disable
|
||||
update(status: 'disabled')
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_hook_type
|
||||
self.hook_type = app.params[:hook_type] if app.present?
|
||||
end
|
||||
|
||||
def validate_settings_json_schema
|
||||
return if app.blank? || app.params[:settings_json_schema].blank?
|
||||
|
||||
errors.add(:settings, ': Invalid settings data') unless JSONSchemer.schema(app.params[:settings_json_schema]).valid?(settings)
|
||||
end
|
||||
end
|
||||
|
||||
13
app/policies/hook_policy.rb
Normal file
13
app/policies/hook_policy.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class HookPolicy < ApplicationPolicy
|
||||
def create?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def update?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def destroy?
|
||||
@account_user.administrator?
|
||||
end
|
||||
end
|
||||
@@ -1,10 +1,5 @@
|
||||
json.payload do
|
||||
json.array! @apps do |app|
|
||||
json.id app.id
|
||||
json.name app.name
|
||||
json.description app.description
|
||||
json.logo app.logo
|
||||
json.enabled app.enabled?(@current_account)
|
||||
json.action app.action
|
||||
json.partial! 'api/v1/models/app.json.jbuilder', resource: app
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1 @@
|
||||
json.id @app.id
|
||||
json.name @app.name
|
||||
json.logo @app.logo
|
||||
json.description @app.description
|
||||
json.fields @app.fields
|
||||
json.enabled @app.enabled?(@current_account)
|
||||
json.button @app.action
|
||||
json.partial! 'api/v1/models/app.json.jbuilder', resource: @app
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/hook.json.jbuilder', resource: @hook
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/hook.json.jbuilder', resource: @hook
|
||||
6
app/views/api/v1/models/_app.json.jbuilder
Normal file
6
app/views/api/v1/models/_app.json.jbuilder
Normal file
@@ -0,0 +1,6 @@
|
||||
json.call(resource.params, *resource.params.keys)
|
||||
json.name resource.name
|
||||
json.description resource.description
|
||||
json.enabled resource.enabled?(@current_account)
|
||||
json.button resource.action
|
||||
json.hooks @current_account.hooks.where(app_id: resource.id)
|
||||
4
app/views/api/v1/models/_hook.json.jbuilder
Normal file
4
app/views/api/v1/models/_hook.json.jbuilder
Normal file
@@ -0,0 +1,4 @@
|
||||
json.id resource.id
|
||||
json.app resource.app.params.to_h
|
||||
json.enabled resource.enabled?
|
||||
json.inbox_id resource.inbox_id
|
||||
Reference in New Issue
Block a user