2019-10-29 07:20:54 +00:00
|
|
|
class WidgetsController < ActionController::Base
|
|
|
|
before_action :set_web_widget
|
2019-10-30 05:13:11 +00:00
|
|
|
before_action :set_token
|
2019-10-29 07:20:54 +00:00
|
|
|
before_action :set_contact
|
|
|
|
before_action :build_contact
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def index
|
|
|
|
render
|
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
private
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def set_web_widget
|
|
|
|
@web_widget = ::Channel::WebWidget.find_by!(website_token: permitted_params[:website_token])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_token
|
|
|
|
@token = permitted_params[:cw_conversation]
|
|
|
|
@auth_token_params = if @token.present?
|
|
|
|
::Widget::TokenService.new(token: @token).decode_token
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def set_contact
|
2020-01-09 07:36:40 +00:00
|
|
|
return if @auth_token_params[:source_id].nil?
|
2019-10-29 07:20:54 +00:00
|
|
|
|
|
|
|
contact_inbox = ::ContactInbox.find_by(
|
|
|
|
inbox_id: @web_widget.inbox.id,
|
2020-01-09 07:36:40 +00:00
|
|
|
source_id: @auth_token_params[:source_id]
|
2019-10-29 07:20:54 +00:00
|
|
|
)
|
|
|
|
|
2019-11-21 06:32:10 +00:00
|
|
|
@contact = contact_inbox ? contact_inbox.contact : nil
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_contact
|
|
|
|
return if @contact.present?
|
|
|
|
|
|
|
|
contact_inbox = @web_widget.create_contact_inbox
|
|
|
|
@contact = contact_inbox.contact
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
payload = { source_id: contact_inbox.source_id, inbox_id: @web_widget.inbox.id }
|
|
|
|
@token = ::Widget::TokenService.new(payload: payload).generate_token
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_params
|
2019-10-30 05:13:11 +00:00
|
|
|
params.permit(:website_token, :cw_conversation)
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
end
|