fix: Remove fallback phone_number search in WhatsApp event processing (#6904)
This commit is contained in:
@@ -2,7 +2,7 @@ class Webhooks::WhatsappEventsJob < ApplicationJob
|
|||||||
queue_as :low
|
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)
|
||||||
return if channel_is_inactive?(channel)
|
return if channel_is_inactive?(channel)
|
||||||
|
|
||||||
case channel.provider
|
case channel.provider
|
||||||
@@ -23,7 +23,7 @@ class Webhooks::WhatsappEventsJob < ApplicationJob
|
|||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_channel(params)
|
def find_channel_by_url_param(params)
|
||||||
return unless params[:phone_number]
|
return unless params[:phone_number]
|
||||||
|
|
||||||
Channel::Whatsapp.find_by(phone_number: params[:phone_number])
|
Channel::Whatsapp.find_by(phone_number: params[:phone_number])
|
||||||
@@ -33,9 +33,9 @@ class Webhooks::WhatsappEventsJob < ApplicationJob
|
|||||||
# for the case where facebook cloud api support multiple numbers for a single app
|
# for the case where facebook cloud api support multiple numbers for a single app
|
||||||
# https://github.com/chatwoot/chatwoot/issues/4712#issuecomment-1173838350
|
# https://github.com/chatwoot/chatwoot/issues/4712#issuecomment-1173838350
|
||||||
# we will give priority to the phone_number in the payload
|
# we will give priority to the phone_number in the payload
|
||||||
return unless params[:object] == 'whatsapp_business_account'
|
return get_channel_from_wb_payload(params) if params[:object] == 'whatsapp_business_account'
|
||||||
|
|
||||||
get_channel_from_wb_payload(params)
|
find_channel_by_url_param(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_channel_from_wb_payload(wb_params)
|
def get_channel_from_wb_payload(wb_params)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ FactoryBot.define do
|
|||||||
factory :channel_whatsapp, class: 'Channel::Whatsapp' do
|
factory :channel_whatsapp, class: 'Channel::Whatsapp' do
|
||||||
sequence(:phone_number) { |n| "+123456789#{n}1" }
|
sequence(:phone_number) { |n| "+123456789#{n}1" }
|
||||||
account
|
account
|
||||||
provider_config { { 'api_key' => 'test_key' } }
|
provider_config { { 'api_key' => 'test_key', 'phone_number_id' => 'random_id' } }
|
||||||
message_templates do
|
message_templates do
|
||||||
[{ 'name' => 'sample_shipping_confirmation',
|
[{ 'name' => 'sample_shipping_confirmation',
|
||||||
'status' => 'approved',
|
'status' => 'approved',
|
||||||
|
|||||||
@@ -4,7 +4,24 @@ RSpec.describe Webhooks::WhatsappEventsJob, type: :job do
|
|||||||
subject(:job) { described_class }
|
subject(:job) { described_class }
|
||||||
|
|
||||||
let(:channel) { create(:channel_whatsapp, provider: 'whatsapp_cloud', sync_templates: false, validate_provider_config: false) }
|
let(:channel) { create(:channel_whatsapp, provider: 'whatsapp_cloud', sync_templates: false, validate_provider_config: false) }
|
||||||
let(:params) { { phone_number: channel.phone_number } }
|
let(:params) do
|
||||||
|
{
|
||||||
|
object: 'whatsapp_business_account',
|
||||||
|
phone_number: channel.phone_number,
|
||||||
|
entry: [{
|
||||||
|
changes: [
|
||||||
|
{
|
||||||
|
value: {
|
||||||
|
metadata: {
|
||||||
|
phone_number_id: channel.provider_config['phone_number_id'],
|
||||||
|
display_phone_number: channel.phone_number.delete('+')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
end
|
||||||
let(:process_service) { double }
|
let(:process_service) { double }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@@ -24,6 +41,20 @@ RSpec.describe Webhooks::WhatsappEventsJob, type: :job do
|
|||||||
job.perform_now(params)
|
job.perform_now(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'will not enqueue message jobs based on phone number in the URL if the entry payload is not present' do
|
||||||
|
params = {
|
||||||
|
object: 'whatsapp_business_account',
|
||||||
|
phone_number: channel.phone_number,
|
||||||
|
entry: [{ changes: [{}] }]
|
||||||
|
}
|
||||||
|
allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new)
|
||||||
|
allow(Whatsapp::IncomingMessageService).to receive(:new)
|
||||||
|
|
||||||
|
expect(Whatsapp::IncomingMessageWhatsappCloudService).not_to receive(:new)
|
||||||
|
expect(Whatsapp::IncomingMessageService).not_to receive(:new)
|
||||||
|
job.perform_now(params)
|
||||||
|
end
|
||||||
|
|
||||||
it 'will not enqueue 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user