fix: Disable processing events if account is suspended (#6849)
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
class Webhooks::WhatsappEventsJob < ApplicationJob
|
class Webhooks::WhatsappEventsJob < ApplicationJob
|
||||||
queue_as :default
|
queue_as :low
|
||||||
|
|
||||||
def perform(params = {})
|
def perform(params = {})
|
||||||
channel = find_channel_from_whatsapp_business_payload(params) || find_channel(params)
|
channel = find_channel_from_whatsapp_business_payload(params) || find_channel(params)
|
||||||
return if channel.blank?
|
return if channel_is_inactive?(channel)
|
||||||
return if channel.reauthorization_required?
|
|
||||||
|
|
||||||
case channel.provider
|
case channel.provider
|
||||||
when 'whatsapp_cloud'
|
when 'whatsapp_cloud'
|
||||||
@@ -16,6 +15,14 @@ class Webhooks::WhatsappEventsJob < ApplicationJob
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def channel_is_inactive?(channel)
|
||||||
|
return true if channel.blank?
|
||||||
|
return true if channel.reauthorization_required?
|
||||||
|
return true unless channel.account.active?
|
||||||
|
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def find_channel(params)
|
def find_channel(params)
|
||||||
return unless params[:phone_number]
|
return unless params[:phone_number]
|
||||||
|
|
||||||
|
|||||||
@@ -14,26 +14,46 @@ RSpec.describe Webhooks::WhatsappEventsJob, type: :job do
|
|||||||
it 'enqueues the job' do
|
it 'enqueues the job' do
|
||||||
expect { job.perform_later(params) }.to have_enqueued_job(described_class)
|
expect { job.perform_later(params) }.to have_enqueued_job(described_class)
|
||||||
.with(params)
|
.with(params)
|
||||||
.on_queue('default')
|
.on_queue('low')
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when whatsapp_cloud provider' do
|
context 'when whatsapp_cloud provider' do
|
||||||
it 'enques Whatsapp::IncomingMessageWhatsappCloudService' do
|
it 'enqueue Whatsapp::IncomingMessageWhatsappCloudService' do
|
||||||
allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service)
|
allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service)
|
||||||
expect(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new)
|
expect(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new)
|
||||||
job.perform_now(params)
|
job.perform_now(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will not enques Whatsapp::IncomingMessageWhatsappCloudService if channel reauthorization required' do
|
it 'will not enqueue Whatsapp::IncomingMessageWhatsappCloudService if channel reauthorization required' do
|
||||||
channel.prompt_reauthorization!
|
channel.prompt_reauthorization!
|
||||||
allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service)
|
allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service)
|
||||||
expect(Whatsapp::IncomingMessageWhatsappCloudService).not_to receive(:new)
|
expect(Whatsapp::IncomingMessageWhatsappCloudService).not_to receive(:new)
|
||||||
job.perform_now(params)
|
job.perform_now(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'will not enqueue if channel is not present' do
|
||||||
|
allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service)
|
||||||
|
allow(Whatsapp::IncomingMessageService).to receive(:new).and_return(process_service)
|
||||||
|
|
||||||
|
expect(Whatsapp::IncomingMessageWhatsappCloudService).not_to receive(:new)
|
||||||
|
expect(Whatsapp::IncomingMessageService).not_to receive(:new)
|
||||||
|
job.perform_now(phone_number: 'random_phone_number')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will not enqueue Whatsapp::IncomingMessageWhatsappCloudService if account is suspended' do
|
||||||
|
account = channel.account
|
||||||
|
account.update!(status: :suspended)
|
||||||
|
allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service)
|
||||||
|
allow(Whatsapp::IncomingMessageService).to receive(:new).and_return(process_service)
|
||||||
|
|
||||||
|
expect(Whatsapp::IncomingMessageWhatsappCloudService).not_to receive(:new)
|
||||||
|
expect(Whatsapp::IncomingMessageService).not_to receive(:new)
|
||||||
|
job.perform_now(params)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when default provider' do
|
context 'when default provider' do
|
||||||
it 'enques Whatsapp::IncomingMessageService' do
|
it 'enqueue Whatsapp::IncomingMessageService' do
|
||||||
stub_request(:post, 'https://waba.360dialog.io/v1/configs/webhook')
|
stub_request(:post, 'https://waba.360dialog.io/v1/configs/webhook')
|
||||||
channel.update(provider: 'default')
|
channel.update(provider: 'default')
|
||||||
allow(Whatsapp::IncomingMessageService).to receive(:new).and_return(process_service)
|
allow(Whatsapp::IncomingMessageService).to receive(:new).and_return(process_service)
|
||||||
@@ -43,7 +63,7 @@ RSpec.describe Webhooks::WhatsappEventsJob, type: :job do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when whatsapp business params' do
|
context 'when whatsapp business params' do
|
||||||
it 'enques Whatsapp::IncomingMessageWhatsappCloudService based on the number in payload' do
|
it 'enqueue Whatsapp::IncomingMessageWhatsappCloudService based on the number in payload' do
|
||||||
other_channel = create(:channel_whatsapp, phone_number: '+1987654', provider: 'whatsapp_cloud', sync_templates: false,
|
other_channel = create(:channel_whatsapp, phone_number: '+1987654', provider: 'whatsapp_cloud', sync_templates: false,
|
||||||
validate_provider_config: false)
|
validate_provider_config: false)
|
||||||
wb_params = {
|
wb_params = {
|
||||||
|
|||||||
Reference in New Issue
Block a user