fix: Respect survey label rules for WhatsApp CSAT template (#13285)
Ensure CSAT survey label rules are evaluated once in CsatSurveyService before any channel-specific sending (including WhatsApp/Twilio templates), remove the duplicated rule check from the template builder, and cover the blocking-label scenario in service specs while simplifying the template specs accordingly. Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -20,7 +20,7 @@ class CsatSurveyService
|
||||
delegate :inbox, :contact, to: :conversation
|
||||
|
||||
def should_send_csat_survey?
|
||||
conversation_allows_csat? && csat_enabled? && !csat_already_sent?
|
||||
conversation_allows_csat? && csat_enabled? && !csat_already_sent? && csat_allowed_by_survey_rules?
|
||||
end
|
||||
|
||||
def conversation_allows_csat?
|
||||
@@ -39,6 +39,37 @@ class CsatSurveyService
|
||||
conversation.can_reply?
|
||||
end
|
||||
|
||||
def csat_allowed_by_survey_rules?
|
||||
return true unless survey_rules_configured?
|
||||
|
||||
labels = conversation.label_list
|
||||
return true if rule_values.empty?
|
||||
|
||||
case rule_operator
|
||||
when 'contains'
|
||||
rule_values.any? { |label| labels.include?(label) }
|
||||
when 'does_not_contain'
|
||||
rule_values.none? { |label| labels.include?(label) }
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def survey_rules_configured?
|
||||
return false if csat_config.blank?
|
||||
return false if csat_config['survey_rules'].blank?
|
||||
|
||||
rule_values.any?
|
||||
end
|
||||
|
||||
def rule_operator
|
||||
csat_config.dig('survey_rules', 'operator') || 'contains'
|
||||
end
|
||||
|
||||
def rule_values
|
||||
csat_config.dig('survey_rules', 'values') || []
|
||||
end
|
||||
|
||||
def whatsapp_channel?
|
||||
inbox.channel_type == 'Channel::Whatsapp'
|
||||
end
|
||||
@@ -113,6 +144,10 @@ class CsatSurveyService
|
||||
)
|
||||
end
|
||||
|
||||
def csat_config
|
||||
inbox.csat_config || {}
|
||||
end
|
||||
|
||||
def send_twilio_whatsapp_template_survey
|
||||
template_config = inbox.csat_config&.dig('template')
|
||||
content_sid = template_config['content_sid']
|
||||
|
||||
@@ -2,8 +2,6 @@ class MessageTemplates::Template::CsatSurvey
|
||||
pattr_initialize [:conversation!]
|
||||
|
||||
def perform
|
||||
return unless should_send_csat_survey?
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
conversation.messages.create!(csat_survey_message_params)
|
||||
end
|
||||
@@ -13,39 +11,6 @@ class MessageTemplates::Template::CsatSurvey
|
||||
|
||||
delegate :contact, :account, :inbox, to: :conversation
|
||||
|
||||
def should_send_csat_survey?
|
||||
return true unless survey_rules_configured?
|
||||
|
||||
labels = conversation.label_list
|
||||
|
||||
return true if rule_values.empty?
|
||||
|
||||
case rule_operator
|
||||
when 'contains'
|
||||
rule_values.any? { |label| labels.include?(label) }
|
||||
when 'does_not_contain'
|
||||
rule_values.none? { |label| labels.include?(label) }
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def survey_rules_configured?
|
||||
return false if csat_config.blank?
|
||||
return false if csat_config['survey_rules'].blank?
|
||||
return false if rule_values.empty?
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def rule_operator
|
||||
csat_config.dig('survey_rules', 'operator') || 'contains'
|
||||
end
|
||||
|
||||
def rule_values
|
||||
csat_config.dig('survey_rules', 'values') || []
|
||||
end
|
||||
|
||||
def message_content
|
||||
return I18n.t('conversations.templates.csat_input_message_body') if csat_config.blank? || csat_config['message'].blank?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user