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:
Muhsin Keloth
2026-01-16 10:16:00 +04:00
committed by GitHub
parent d451615811
commit 96b5780ea7
4 changed files with 88 additions and 105 deletions

View File

@@ -88,6 +88,25 @@ describe CsatSurveyService do
expect(MessageTemplates::Template::CsatSurvey).not_to have_received(:new)
expect(Conversations::ActivityMessageJob).not_to have_received(:perform_later)
end
context 'when survey rules block sending' do
before do
inbox.update(csat_config: {
'survey_rules' => {
'operator' => 'does_not_contain',
'values' => ['bot-detectado']
}
})
conversation.update(label_list: ['bot-detectado'])
end
it 'does not send CSAT' do
service.perform
expect(MessageTemplates::Template::CsatSurvey).not_to have_received(:new)
expect(conversation.messages.where(content_type: :input_csat)).to be_empty
end
end
end
context 'when it is a WhatsApp channel' do
@@ -306,6 +325,29 @@ describe CsatSurveyService do
expect(MessageTemplates::Template::CsatSurvey).not_to have_received(:new)
end
end
context 'when survey rules block sending' do
before do
whatsapp_inbox.update(csat_config: {
'template' => { 'name' => 'customer_survey_template', 'language' => 'en' },
'message' => 'Please rate your experience',
'survey_rules' => {
'operator' => 'does_not_contain',
'values' => ['bot-detectado']
}
})
whatsapp_conversation.update(label_list: ['bot-detectado'])
end
it 'does not call WhatsApp template or create a CSAT message' do
expect(mock_provider_service).not_to receive(:get_template_status)
expect(mock_provider_service).not_to receive(:send_template)
whatsapp_service.perform
expect(whatsapp_conversation.messages.where(content_type: :input_csat)).to be_empty
end
end
end
end

View File

@@ -17,83 +17,24 @@ describe MessageTemplates::Template::CsatSurvey do
expect(conversation.messages.template.first.content_type).to eq('input_csat')
end
end
end
describe '#perform with contains operator' do
let(:csat_config) do
{
'display_type' => 'emoji',
'message' => 'Please rate your experience',
'survey_rules' => {
'operator' => 'contains',
'values' => %w[support help]
context 'when csat config is provided' do
let(:csat_config) do
{
'display_type' => 'star',
'message' => 'Please rate your experience'
}
}
end
end
before do
inbox.update(csat_config: csat_config)
end
context 'when conversation has matching labels' do
it 'creates a CSAT survey message' do
conversation.update(label_list: %w[support urgent])
before { inbox.update(csat_config: csat_config) }
it 'creates a CSAT message with configured attributes' do
service.perform
expect(conversation.messages.template.count).to eq(1)
message = conversation.messages.template.first
message = conversation.messages.template.last
expect(message.content_type).to eq('input_csat')
expect(message.content).to eq('Please rate your experience')
expect(message.content_attributes['display_type']).to eq('emoji')
end
end
context 'when conversation has no matching labels' do
it 'does not create a CSAT survey message' do
conversation.update(label_list: %w[billing-support payment])
service.perform
expect(conversation.messages.template.count).to eq(0)
end
end
end
describe '#perform with does_not_contain operator' do
let(:csat_config) do
{
'display_type' => 'emoji',
'message' => 'Please rate your experience',
'survey_rules' => {
'operator' => 'does_not_contain',
'values' => %w[support help]
}
}
end
before do
inbox.update(csat_config: csat_config)
end
context 'when conversation does not have matching labels' do
it 'creates a CSAT survey message' do
conversation.update(label_list: %w[billing payment])
service.perform
expect(conversation.messages.template.count).to eq(1)
expect(conversation.messages.template.first.content_type).to eq('input_csat')
end
end
context 'when conversation has matching labels' do
it 'does not create a CSAT survey message' do
conversation.update(label_list: %w[support urgent])
service.perform
expect(conversation.messages.template.count).to eq(0)
expect(message.content_attributes['display_type']).to eq('star')
end
end
end