From bd1b17baf3209e04431f1b800537aba8b3bc2ec4 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Fri, 25 Jun 2021 13:37:51 +0530 Subject: [PATCH] feat: Show the CSAT response to the agent (#2511) --- .../api/v1/widget/messages_controller.rb | 2 +- .../widgets/conversation/Message.vue | 8 +++- .../helpers/botMessageContentHelper.js | 48 +++++++++++++++---- .../specs/botMessageContentHelper.spec.js | 24 ++++++++++ .../i18n/locale/en/conversation.json | 2 + .../widget/components/AgentMessageBubble.vue | 2 +- 6 files changed, 74 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/widget/messages_controller.rb b/app/controllers/api/v1/widget/messages_controller.rb index c5492eff9..f0286ecd2 100644 --- a/app/controllers/api/v1/widget/messages_controller.rb +++ b/app/controllers/api/v1/widget/messages_controller.rb @@ -54,7 +54,7 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController end def message_update_params - params.permit(message: [{ submitted_values: [:name, :title, :value, { csat_survey_response: [:feedback_text, :rating] }] }]) + params.permit(message: [{ submitted_values: [:name, :title, :value, { csat_survey_response: [:feedback, :rating] }] }]) end def permitted_params diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index b866ef4e7..e43b8e3e5 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -100,7 +100,13 @@ export default { const botMessageContent = generateBotMessageContent( this.contentType, this.contentAttributes, - this.$t('CONVERSATION.NO_RESPONSE') + { + noResponseText: this.$t('CONVERSATION.NO_RESPONSE'), + csat: { + ratingTitle: this.$t('CONVERSATION.RATING_TITLE'), + feedbackTitle: this.$t('CONVERSATION.FEEDBACK_TITLE'), + }, + } ); const { diff --git a/app/javascript/dashboard/components/widgets/conversation/helpers/botMessageContentHelper.js b/app/javascript/dashboard/components/widgets/conversation/helpers/botMessageContentHelper.js index 42e59a161..b7e105abf 100644 --- a/app/javascript/dashboard/components/widgets/conversation/helpers/botMessageContentHelper.js +++ b/app/javascript/dashboard/components/widgets/conversation/helpers/botMessageContentHelper.js @@ -1,4 +1,6 @@ -const generateInputSelectContent = (contentType, contentAttributes) => { +import { CSAT_RATINGS } from '../../../../../shared/constants/messages'; + +const generateInputSelectContent = contentAttributes => { const { submitted_values: submittedValues = [] } = contentAttributes; const [selectedOption] = submittedValues; @@ -8,7 +10,7 @@ const generateInputSelectContent = (contentType, contentAttributes) => { return ''; }; -const generateInputEmailContent = (contentType, contentAttributes) => { +const generateInputEmailContent = contentAttributes => { const { submitted_email: submittedEmail = '' } = contentAttributes; if (submittedEmail) { return `${submittedEmail}`; @@ -16,11 +18,7 @@ const generateInputEmailContent = (contentType, contentAttributes) => { return ''; }; -const generateFormContent = ( - contentType, - contentAttributes, - noResponseText -) => { +const generateFormContent = (contentAttributes, { noResponseText }) => { const { items, submitted_values: submittedValues = [] } = contentAttributes; if (submittedValues.length) { const submittedObject = submittedValues.reduce((acc, keyValuePair) => { @@ -38,20 +36,52 @@ const generateFormContent = ( return ''; }; +const generateCSATContent = ( + contentAttributes, + { ratingTitle, feedbackTitle } +) => { + const { + submitted_values: { csat_survey_response: surveyResponse = {} } = {}, + } = contentAttributes; + const { rating, feedback } = surveyResponse || {}; + + let messageContent = ''; + if (rating) { + const [ratingObject = {}] = CSAT_RATINGS.filter( + csatRating => csatRating.value === rating + ); + messageContent += `
${ratingTitle}
`; + messageContent += `

${ratingObject.emoji}

`; + } + if (feedback) { + messageContent += `
${feedbackTitle}
`; + messageContent += `

${feedback}

`; + } + return messageContent; +}; + export const generateBotMessageContent = ( contentType, contentAttributes, - noResponseText = 'No response' + { + noResponseText = 'No response', + csat: { ratingTitle = 'Rating', feedbackTitle = 'Feedback' } = {}, + } = {} ) => { const contentTypeMethods = { input_select: generateInputSelectContent, input_email: generateInputEmailContent, form: generateFormContent, + input_csat: generateCSATContent, }; const contentTypeMethod = contentTypeMethods[contentType]; if (contentTypeMethod && typeof contentTypeMethod === 'function') { - return contentTypeMethod(contentType, contentAttributes, noResponseText); + return contentTypeMethod(contentAttributes, { + noResponseText, + ratingTitle, + feedbackTitle, + }); } return ''; }; diff --git a/app/javascript/dashboard/components/widgets/conversation/helpers/specs/botMessageContentHelper.spec.js b/app/javascript/dashboard/components/widgets/conversation/helpers/specs/botMessageContentHelper.spec.js index 716fdd65b..9268b4559 100644 --- a/app/javascript/dashboard/components/widgets/conversation/helpers/specs/botMessageContentHelper.spec.js +++ b/app/javascript/dashboard/components/widgets/conversation/helpers/specs/botMessageContentHelper.spec.js @@ -17,6 +17,30 @@ describe('#generateBotMessageContent', () => { ).toEqual('hello@chatwoot.com'); }); + it('return correct input_csat content', () => { + expect( + generateBotMessageContent('input_csat', { + submitted_values: { + csat_survey_response: { rating: 5, feedback: 'Great Service' }, + }, + }) + ).toEqual( + '
Rating

๐Ÿ˜

Feedback

Great Service

' + ); + + expect( + generateBotMessageContent( + 'input_csat', + { + submitted_values: { + csat_survey_response: { rating: 1, feedback: '' }, + }, + }, + { csat: { ratingTitle: 'เดฑเต‡เดฑเตเดฑเดฟเด‚เด—เต', feedbackTitle: 'เดชเตเดฐเดคเดฟเด•เดฐเดฃเด‚' } } + ) + ).toEqual('
เดฑเต‡เดฑเตเดฑเดฟเด‚เด—เต

๐Ÿ˜ž

'); + }); + it('return correct form content', () => { expect( generateBotMessageContent('form', { diff --git a/app/javascript/dashboard/i18n/locale/en/conversation.json b/app/javascript/dashboard/i18n/locale/en/conversation.json index 976d1395a..587530e74 100644 --- a/app/javascript/dashboard/i18n/locale/en/conversation.json +++ b/app/javascript/dashboard/i18n/locale/en/conversation.json @@ -28,6 +28,8 @@ "DOWNLOAD": "Download", "UPLOADING_ATTACHMENTS": "Uploading attachments...", "NO_RESPONSE": "No response", + "RATING_TITLE": "Rating", + "FEEDBACK_TITLE": "Feedback", "HEADER": { "RESOLVE_ACTION": "Resolve", "REOPEN_ACTION": "Reopen", diff --git a/app/javascript/widget/components/AgentMessageBubble.vue b/app/javascript/widget/components/AgentMessageBubble.vue index 95921e867..071a37d26 100755 --- a/app/javascript/widget/components/AgentMessageBubble.vue +++ b/app/javascript/widget/components/AgentMessageBubble.vue @@ -130,7 +130,7 @@ export default { submittedValues: { csat_survey_response: { rating, - feedback_text: feedback, + feedback, }, }, messageId: this.messageId,