Feature: API Channel (#1052)

This commit is contained in:
Sojan Jose
2020-07-21 12:15:24 +05:30
committed by GitHub
parent fa04098c20
commit 8079bf50a0
40 changed files with 735 additions and 246 deletions

View File

@@ -11,9 +11,11 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
def show; end
def create
@contact = Current.account.contacts.new(contact_create_params)
@contact.save!
render json: @contact
ActiveRecord::Base.transaction do
@contact = Current.account.contacts.new(contact_create_params)
@contact.save!
@contact_inbox = build_contact_inbox
end
end
def update
@@ -26,6 +28,14 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
authorize(Contact)
end
def build_contact_inbox
return if params[:inbox_id].blank?
inbox = Inbox.find(params[:inbox_id])
source_id = params[:source_id] || SecureRandom.uuid
ContactInbox.create(contact: @contact, inbox: inbox, source_id: source_id)
end
def contact_params
params.require(:contact).permit(:name, :email, :phone_number)
end

View File

@@ -5,8 +5,10 @@ class Api::V1::Accounts::Conversations::MessagesController < Api::V1::Accounts::
def create
user = current_user || @resource
mb = Messages::Outgoing::NormalBuilder.new(user, @conversation, params)
mb = Messages::MessageBuilder.new(user, @conversation, params)
@message = mb.perform
rescue StandardError => e
render_could_not_create_error(e.message)
end
private

View File

@@ -4,12 +4,12 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
before_action :check_authorization
def index
@inboxes = policy_scope(Current.account.inboxes)
@inboxes = policy_scope(Current.account.inboxes.includes(:channel, :avatar_attachment))
end
def create
ActiveRecord::Base.transaction do
channel = web_widgets.create!(permitted_params[:channel].except(:type)) if permitted_params[:channel][:type] == 'web_widget'
channel = create_channel
@inbox = Current.account.inboxes.build(
name: permitted_params[:name],
greeting_message: permitted_params[:greeting_message],
@@ -52,21 +52,28 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
@agent_bot = AgentBot.find(params[:agent_bot]) if params[:agent_bot]
end
def web_widgets
Current.account.web_widgets
end
def check_authorization
authorize(Inbox)
end
def create_channel
case permitted_params[:channel][:type]
when 'web_widget'
Current.account.web_widgets.create!(permitted_params[:channel].except(:type))
when 'api'
Current.account.api_channels.create!(permitted_params[:channel].except(:type))
when 'email'
Current.account.email_channels.create!(permitted_params[:channel].except(:type))
end
end
def permitted_params
params.permit(:id, :avatar, :name, :greeting_message, :greeting_enabled, channel:
[:type, :website_url, :widget_color, :welcome_title, :welcome_tagline])
[:type, :website_url, :widget_color, :welcome_title, :welcome_tagline, :webhook_url, :email])
end
def inbox_update_params
params.permit(:enable_auto_assignment, :name, :avatar, :greeting_message, :greeting_enabled,
channel: [:website_url, :widget_color, :welcome_title, :welcome_tagline])
channel: [:website_url, :widget_color, :welcome_title, :welcome_tagline, :webhook_url, :email])
end
end