feat: Add CSAT Message Template Hooks (#2494)

This commit is contained in:
Sojan Jose 2021-06-23 19:29:27 +05:30 committed by GitHub
parent 640e028bde
commit cd11efea1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 115 additions and 7 deletions

View file

@ -67,11 +67,11 @@ class Inbox < ApplicationRecord
end
def facebook?
channel.class.name.to_s == 'Channel::FacebookPage'
channel_type == 'Channel::FacebookPage'
end
def web_widget?
channel.class.name.to_s == 'Channel::WebWidget'
channel_type == 'Channel::WebWidget'
end
def inbox_type

View file

@ -5,11 +5,7 @@ class MessageTemplates::HookExecutionService
return if inbox.agent_bot_inbox&.active?
return if conversation.campaign.present?
# TODO: let's see whether this is needed and remove this and related logic if not
# ::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message?
::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if inbox.enable_email_collect && should_send_email_collect?
trigger_templates
end
private
@ -17,6 +13,14 @@ class MessageTemplates::HookExecutionService
delegate :inbox, :conversation, to: :message
delegate :contact, to: :conversation
def trigger_templates
# TODO: let's see whether this is needed and remove this and related logic if not
# ::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message?
::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if inbox.enable_email_collect && should_send_email_collect?
::MessageTemplates::Template::CsatSurvey.new(conversation: conversation).perform if should_send_csat_survey?
end
def should_send_out_of_office_message?
inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present?
end
@ -41,4 +45,21 @@ class MessageTemplates::HookExecutionService
def contact_has_email?
contact.email
end
def csat_enabled_inbox?
# for now csat only available in web widget channel
return unless inbox.web_widget?
return unless inbox.csat_survey_enabled?
true
end
def should_send_csat_survey?
return unless conversation.resolved?
return unless csat_enabled_inbox?
# only send CSAT once in a conversation
return if conversation.messages.where(content_type: :input_csat).present?
true
end
end

View file

@ -0,0 +1,24 @@
class MessageTemplates::Template::CsatSurvey
pattr_initialize [:conversation!]
def perform
ActiveRecord::Base.transaction do
conversation.messages.create!(csat_survey_message_params)
end
end
private
delegate :contact, :account, to: :conversation
delegate :inbox, to: :message
def csat_survey_message_params
{
account_id: @conversation.account_id,
inbox_id: @conversation.inbox_id,
message_type: :template,
content_type: :input_csat,
content: I18n.t('conversations.templates.csat_input_message_body')
}
end
end

View file

@ -82,6 +82,7 @@ en:
greeting_message_body: "%{account_name} typically replies in a few hours."
ways_to_reach_you_message_body: "Give the team a way to reach you."
email_input_box_message_body: "Get notified by email"
csat_input_message_body: "Please rate the conversation"
reply:
email_subject: "New messages on this conversation"
transcript_subject: "Conversation Transcript"

View file

@ -87,6 +87,55 @@ describe ::MessageTemplates::HookExecutionService do
end
end
context 'when CSAT Survey' do
let(:csat_survey) { double }
let(:conversation) { create(:conversation) }
before do
allow(::MessageTemplates::Template::CsatSurvey).to receive(:new).and_return(csat_survey)
allow(csat_survey).to receive(:perform).and_return(true)
end
it 'calls ::MessageTemplates::Template::CsatSurvey when a conversation is resolved in an inbox with survey enabled' do
conversation.inbox.update(csat_survey_enabled: true)
conversation.resolved!
expect(::MessageTemplates::Template::CsatSurvey).to have_received(:new).with(conversation: conversation)
expect(csat_survey).to have_received(:perform)
end
it 'will not call ::MessageTemplates::Template::CsatSurvey when Csat is not enabled' do
conversation.inbox.update(csat_survey_enabled: false)
conversation.resolved!
expect(::MessageTemplates::Template::CsatSurvey).not_to have_received(:new).with(conversation: conversation)
expect(csat_survey).not_to have_received(:perform)
end
it 'will not call ::MessageTemplates::Template::CsatSurvey if its not a website widget' do
api_channel = create(:channel_api)
conversation = create(:conversation, inbox: create(:inbox, channel: api_channel))
conversation.inbox.update(csat_survey_enabled: true)
conversation.resolved!
expect(::MessageTemplates::Template::CsatSurvey).not_to have_received(:new).with(conversation: conversation)
expect(csat_survey).not_to have_received(:perform)
end
it 'will not call ::MessageTemplates::Template::CsatSurvey if another Csat was already sent' do
conversation.inbox.update(csat_survey_enabled: true)
conversation.messages.create!(message_type: 'outgoing', content_type: :input_csat, account: conversation.account, inbox: conversation.inbox)
conversation.resolved!
expect(::MessageTemplates::Template::CsatSurvey).not_to have_received(:new).with(conversation: conversation)
expect(csat_survey).not_to have_received(:perform)
end
end
# TODO: remove this if this hook is removed
# context 'when it is after working hours' do
# it 'calls ::MessageTemplates::Template::OutOfOffice' do

View file

@ -0,0 +1,13 @@
require 'rails_helper'
describe ::MessageTemplates::Template::CsatSurvey do
context 'when this hook is called' do
let(:conversation) { create(:conversation) }
it 'creates the out of office messages' do
described_class.new(conversation: conversation).perform
expect(conversation.messages.template.count).to eq(1)
expect(conversation.messages.template.first.content_type).to eq('input_csat')
end
end
end