fix: Send CSAT survey only when agent can reply in conversation (#11637)

This commit is contained in:
Muhsin Keloth
2025-06-11 22:45:32 +05:30
committed by GitHub
parent 459c22054a
commit 7a67799f35
7 changed files with 180 additions and 82 deletions

View File

@@ -1,4 +1,12 @@
class CsatSurveyListener < BaseListener
def conversation_status_changed(event)
conversation = extract_conversation_and_account(event)[0]
return unless conversation.resolved?
CsatSurveyService.new(conversation: conversation).perform
end
def message_updated(event)
message = extract_message_and_account(event)[0]
return unless message.input_csat?

View File

@@ -0,0 +1,48 @@
class CsatSurveyService
pattr_initialize [:conversation!]
def perform
return unless should_send_csat_survey?
if within_messaging_window?
::MessageTemplates::Template::CsatSurvey.new(conversation: conversation).perform
else
create_csat_not_sent_activity_message
end
end
private
delegate :inbox, :contact, to: :conversation
def should_send_csat_survey?
conversation_allows_csat? && csat_enabled? && !csat_already_sent?
end
def conversation_allows_csat?
conversation.resolved? && !conversation.tweet?
end
def csat_enabled?
inbox.csat_survey_enabled?
end
def csat_already_sent?
conversation.messages.where(content_type: :input_csat).present?
end
def within_messaging_window?
conversation.can_reply?
end
def create_csat_not_sent_activity_message
content = I18n.t('conversations.activity.csat.not_sent_due_to_messaging_window')
activity_message_params = {
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
message_type: :activity,
content: content
}
::Conversations::ActivityMessageJob.perform_later(conversation, activity_message_params) if content
end
end

View File

@@ -17,7 +17,6 @@ class MessageTemplates::HookExecutionService
::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?
@@ -55,23 +54,5 @@ class MessageTemplates::HookExecutionService
def contact_has_email?
contact.email
end
def csat_enabled_conversation?
return false unless conversation.resolved?
# should not sent since the link will be public
return false if conversation.tweet?
return false unless inbox.csat_survey_enabled?
true
end
def should_send_csat_survey?
return unless csat_enabled_conversation?
# only send CSAT once in a conversation
return if conversation.messages.where(content_type: :input_csat).present?
true
end
end
MessageTemplates::HookExecutionService.prepend_mod_with('MessageTemplates::HookExecutionService')