feat: Add backend changes for whatsapp csat template (#12984)

This PR add the backend changes for the feature [sending CSAT surveys
via WhatsApp message templates
](https://github.com/chatwoot/chatwoot/pull/12787)

---------

Co-authored-by: Vinay Keerthi <11478411+stonecharioteer@users.noreply.github.com>
This commit is contained in:
Muhsin Keloth
2025-12-11 16:36:37 +05:30
committed by GitHub
parent 1de8d3e56d
commit 2bd8e76886
10 changed files with 1331 additions and 18 deletions

View File

@@ -312,4 +312,92 @@ describe Whatsapp::Providers::WhatsappCloudService do
end
end
end
describe 'CSAT template methods' do
let(:mock_csat_template_service) { instance_double(Whatsapp::CsatTemplateService) }
let(:expected_template_name) { "customer_satisfaction_survey_#{whatsapp_channel.inbox.id}" }
let(:template_config) do
{
name: expected_template_name,
language: 'en',
category: 'UTILITY'
}
end
before do
allow(Whatsapp::CsatTemplateService).to receive(:new)
.with(whatsapp_channel)
.and_return(mock_csat_template_service)
end
describe '#create_csat_template' do
it 'delegates to csat_template_service with correct config' do
allow(mock_csat_template_service).to receive(:create_template)
.with(template_config)
.and_return({ success: true, template_id: '123' })
result = service.create_csat_template(template_config)
expect(mock_csat_template_service).to have_received(:create_template).with(template_config)
expect(result).to eq({ success: true, template_id: '123' })
end
end
describe '#delete_csat_template' do
it 'delegates to csat_template_service with default template name' do
allow(mock_csat_template_service).to receive(:delete_template)
.with(expected_template_name)
.and_return({ success: true })
result = service.delete_csat_template
expect(mock_csat_template_service).to have_received(:delete_template).with(expected_template_name)
expect(result).to eq({ success: true })
end
it 'delegates to csat_template_service with custom template name' do
custom_template_name = 'custom_csat_template'
allow(mock_csat_template_service).to receive(:delete_template)
.with(custom_template_name)
.and_return({ success: true })
result = service.delete_csat_template(custom_template_name)
expect(mock_csat_template_service).to have_received(:delete_template).with(custom_template_name)
expect(result).to eq({ success: true })
end
end
describe '#get_template_status' do
it 'delegates to csat_template_service with template name' do
template_name = 'customer_survey_template'
expected_response = { success: true, template: { status: 'APPROVED' } }
allow(mock_csat_template_service).to receive(:get_template_status)
.with(template_name)
.and_return(expected_response)
result = service.get_template_status(template_name)
expect(mock_csat_template_service).to have_received(:get_template_status).with(template_name)
expect(result).to eq(expected_response)
end
end
describe 'csat_template_service memoization' do
it 'creates and memoizes the csat_template_service instance' do
allow(Whatsapp::CsatTemplateService).to receive(:new)
.with(whatsapp_channel)
.and_return(mock_csat_template_service)
allow(mock_csat_template_service).to receive(:get_template_status)
.and_return({ success: true })
# Call multiple methods that use the service
service.get_template_status('test1')
service.get_template_status('test2')
# Verify the service was only instantiated once
expect(Whatsapp::CsatTemplateService).to have_received(:new).once
end
end
end
end