Files
chatwoot/spec/services/whatsapp/oneoff_campaign_service_spec.rb
Sojan JoseandGitHub f27bbef73b feat: show processing status for one-off campaigns (#14592)
## Summary

One-off SMS and WhatsApp campaigns now show a `Processing` state while
the audience send is in progress. The campaign moves to `Completed`
after processing finishes, and already-processing campaigns are skipped
by the scheduler to avoid duplicate sends.

## Closes

- [CW-6037: feat: Introduce an in-progress status for
campaigns](https://linear.app/chatwoot/issue/CW-6037/feat-introduce-an-in-progress-status-for-campaigns)

## Screenshot

SMS campaign card showing the new `Processing` status.

<img width="3840" height="2160" alt="framed-campaign-processing-status"
src="https://github.com/user-attachments/assets/de7913b5-65fb-4121-9034-24a568eb0382"
/>

## What changed

- Added `processing` as a campaign status.
- Mark one-off campaigns as `processing` under a row lock before the
send service runs.
- Complete SMS, Twilio SMS, and WhatsApp one-off campaigns after
audience processing finishes.
- Keep campaigns in `processing` if an unexpected service error escapes,
so the scheduler does not automatically resend the audience.
- Added the `Processing` label for SMS and WhatsApp campaign cards.

## Known operational behavior

If a worker is interrupted or an unexpected service error escapes after
a campaign is marked `processing`, the campaign can remain in
`processing`. This is intentional for now to avoid automatic
full-audience resends. Installation admins can decide whether to mark
the campaign completed or restart it manually from the Rails console
after checking what was sent.

## How to test

- Create a one-off SMS or WhatsApp campaign scheduled for now.
- Run the scheduled job or trigger the campaign job.
- Confirm the campaign card shows `Processing` while the audience is
being processed. For small audiences, refresh during processing or use a
larger audience so the state is observable.
- Confirm the campaign moves to `Completed` after audience processing
finishes.
- Confirm an already-processing campaign is not enqueued again by the
scheduled job.
2026-06-01 16:47:17 +05:30

261 lines
11 KiB
Ruby

require 'rails_helper'
describe Whatsapp::OneoffCampaignService do
let(:account) { create(:account) }
let!(:whatsapp_channel) do
create(:channel_whatsapp, account: account, provider: 'whatsapp_cloud', validate_provider_config: false, sync_templates: false)
end
let!(:whatsapp_inbox) { whatsapp_channel.inbox }
let(:label1) { create(:label, account: account) }
let(:label2) { create(:label, account: account) }
let!(:campaign) do
create(:campaign, inbox: whatsapp_inbox, account: account,
audience: [{ type: 'Label', id: label1.id }, { type: 'Label', id: label2.id }],
template_params: template_params)
end
let(:template_params) do
{
'name' => 'ticket_status_updated',
'namespace' => '23423423_2342423_324234234_2343224',
'category' => 'UTILITY',
'language' => 'en',
'processed_params' => { 'body' => { 'name' => 'John', 'ticket_id' => '2332' } }
}
end
before do
# Stub HTTP requests to WhatsApp API
stub_request(:post, /graph\.facebook\.com.*messages/)
.to_return(status: 200, body: { messages: [{ id: 'message_id_123' }] }.to_json, headers: { 'Content-Type' => 'application/json' })
# Ensure the service uses our mocked channel object by stubbing the whole delegation chain
# Using allow_any_instance_of here because the service is instantiated within individual tests
# and we need to mock the delegated channel method for proper test isolation
allow_any_instance_of(described_class).to receive(:channel).and_return(whatsapp_channel) # rubocop:disable RSpec/AnyInstance
end
describe '#perform' do
before do
# Enable WhatsApp campaigns feature flag for all tests
account.enable_features!(:whatsapp_campaign)
end
context 'when campaign validation fails' do
it 'raises error if campaign is completed' do
campaign.completed!
expect { described_class.new(campaign: campaign).perform }.to raise_error 'Completed Campaign'
end
it 'raises error when campaign is not a WhatsApp campaign' do
sms_channel = create(:channel_sms, account: account)
sms_inbox = create(:inbox, channel: sms_channel, account: account)
invalid_campaign = create(:campaign, inbox: sms_inbox, account: account)
expect { described_class.new(campaign: invalid_campaign).perform }
.to raise_error "Invalid campaign #{invalid_campaign.id}"
end
it 'raises error when campaign is not oneoff' do
allow(campaign).to receive(:one_off?).and_return(false)
expect { described_class.new(campaign: campaign).perform }.to raise_error "Invalid campaign #{campaign.id}"
end
it 'raises error when channel provider is not whatsapp_cloud' do
whatsapp_channel.update!(provider: 'default')
expect { described_class.new(campaign: campaign).perform }.to raise_error 'WhatsApp Cloud provider required'
end
it 'raises error when WhatsApp campaigns feature is not enabled' do
account.disable_features!(:whatsapp_campaign)
expect { described_class.new(campaign: campaign).perform }.to raise_error 'WhatsApp campaigns feature not enabled'
end
end
context 'when campaign is valid' do
it 'marks campaign as completed' do
described_class.new(campaign: campaign).perform
expect(campaign.reload.completed?).to be true
end
it 'marks the campaign completed after processing the audience' do
contact = create(:contact, :with_phone_number, account: account)
contact.update_labels([label1.title])
expect(whatsapp_channel).to receive(:send_template) do
expect(campaign.reload.completed?).to be false
end
described_class.new(campaign: campaign).perform
expect(campaign.reload.completed?).to be true
end
it 'processes contacts with matching labels' do
contact_with_label1, contact_with_label2, contact_with_both_labels =
create_list(:contact, 3, :with_phone_number, account: account)
contact_with_label1.update_labels([label1.title])
contact_with_label2.update_labels([label2.title])
contact_with_both_labels.update_labels([label1.title, label2.title])
expect(whatsapp_channel).to receive(:send_template).exactly(3).times
described_class.new(campaign: campaign).perform
end
it 'skips contacts without phone numbers' do
contact_without_phone = create(:contact, account: account, phone_number: nil)
contact_without_phone.update_labels([label1.title])
expect(whatsapp_channel).not_to receive(:send_template)
described_class.new(campaign: campaign).perform
end
it 'uses template processor service to process templates' do
contact = create(:contact, :with_phone_number, account: account)
contact.update_labels([label1.title])
expect(Whatsapp::TemplateProcessorService).to receive(:new)
.with(channel: whatsapp_channel, template_params: template_params)
.and_call_original
described_class.new(campaign: campaign).perform
end
it 'sends template message with correct parameters' do
contact = create(:contact, :with_phone_number, account: account)
contact.update_labels([label1.title])
expect(whatsapp_channel).to receive(:send_template).with(
contact.phone_number,
hash_including(
name: 'ticket_status_updated',
namespace: '23423423_2342423_324234234_2343224',
lang_code: 'en',
parameters: array_including(
hash_including(
type: 'body',
parameters: array_including(
hash_including(type: 'text', parameter_name: 'name', text: 'John'),
hash_including(type: 'text', parameter_name: 'ticket_id', text: '2332')
)
)
)
),
nil
)
described_class.new(campaign: campaign).perform
end
it 'processes liquid variables in template parameters' do
contact = create(:contact, :with_phone_number, account: account, name: 'Jane Smith', email: 'jane@example.com')
contact.update_labels([label1.title])
campaign_with_liquid = create(:campaign, inbox: whatsapp_inbox, account: account,
audience: [{ type: 'Label', id: label1.id }],
template_params: {
'name' => 'ticket_status_updated',
'namespace' => '23423423_2342423_324234234_2343224',
'category' => 'UTILITY',
'language' => 'en',
'processed_params' => {
'body' => {
'name' => '{{contact.name}}',
'ticket_id' => '{{contact.email}}'
}
}
})
contact_drop_name = ContactDrop.new(contact).name
expect(whatsapp_channel).to receive(:send_template).with(
contact.phone_number,
hash_including(
name: 'ticket_status_updated',
namespace: '23423423_2342423_324234234_2343224',
lang_code: 'en',
parameters: array_including(
hash_including(
type: 'body',
parameters: array_including(
hash_including(type: 'text', parameter_name: 'name', text: contact_drop_name),
hash_including(type: 'text', parameter_name: 'ticket_id', text: contact.email)
)
)
)
),
nil
)
described_class.new(campaign: campaign_with_liquid).perform
end
it 'skips contacts when liquid variables resolve to blank values' do
contact = create(:contact, :with_phone_number, account: account, name: 'Jane', email: nil)
contact.update_labels([label1.title])
campaign_with_blank_liquid = create(:campaign, inbox: whatsapp_inbox, account: account,
audience: [{ type: 'Label', id: label1.id }],
template_params: {
'name' => 'test_template',
'namespace' => 'test_namespace',
'language' => 'en',
'processed_params' => {
'body' => {
'email' => '{{contact.email}}'
}
}
})
expect(whatsapp_channel).not_to receive(:send_template)
expect(Rails.logger).to receive(:info).with("Skipping contact #{contact.name} - liquid variables resolved to blank values")
allow(Rails.logger).to receive(:info)
described_class.new(campaign: campaign_with_blank_liquid).perform
end
end
context 'when template_params is missing' do
let(:template_params) { nil }
it 'skips contacts and logs error' do
contact = create(:contact, :with_phone_number, account: account)
contact.update_labels([label1.title])
expect(Rails.logger).to receive(:error)
.with("Skipping contact #{contact.name} - no template_params found for WhatsApp campaign")
expect(whatsapp_channel).not_to receive(:send_template)
described_class.new(campaign: campaign).perform
end
end
context 'when send_template raises an error' do
it 'logs error and continues processing remaining contacts' do
contact_error, contact_success = create_list(:contact, 2, :with_phone_number, account: account)
contact_error.update_labels([label1.title])
contact_success.update_labels([label1.title])
error_message = 'WhatsApp API error'
allow(whatsapp_channel).to receive(:send_template).and_return(nil)
expect(whatsapp_channel).to receive(:send_template).with(contact_error.phone_number, anything, nil).and_raise(StandardError, error_message)
expect(whatsapp_channel).to receive(:send_template).with(contact_success.phone_number, anything, nil).once
expect(Rails.logger).to receive(:error)
.with("Failed to send WhatsApp template message to #{contact_error.phone_number}: #{error_message}")
expect(Rails.logger).to receive(:error).with(/Backtrace:/)
described_class.new(campaign: campaign).perform
expect(campaign.reload.completed?).to be true
end
end
end
end