diff --git a/app/jobs/webhooks/whatsapp_events_job.rb b/app/jobs/webhooks/whatsapp_events_job.rb index f904b3723..14429e61c 100644 --- a/app/jobs/webhooks/whatsapp_events_job.rb +++ b/app/jobs/webhooks/whatsapp_events_job.rb @@ -126,12 +126,17 @@ class Webhooks::WhatsappEventsJob < MutexApplicationJob def channel_is_inactive?(channel) return true if channel.blank? - return true if channel.reauthorization_required? + # Only skip for embedded signup when reauth is required; manual flow uses API keys and should still receive webhooks + return true if channel.reauthorization_required? && embedded_signup_channel?(channel) return true unless channel.account.active? false end + def embedded_signup_channel?(channel) + (channel.provider_config || {}).to_h['source'] == 'embedded_signup' + end + def find_channel_by_url_param(params) return unless params[:phone_number] diff --git a/app/views/api/v1/models/_inbox.json.jbuilder b/app/views/api/v1/models/_inbox.json.jbuilder index 0ae0745cd..b34bbe95b 100644 --- a/app/views/api/v1/models/_inbox.json.jbuilder +++ b/app/views/api/v1/models/_inbox.json.jbuilder @@ -130,7 +130,11 @@ json.bot_name resource.channel.try(:bot_name) if resource.telegram? if resource.whatsapp? json.message_templates resource.channel.try(:message_templates) json.provider_config resource.channel.try(:provider_config) if Current.account_user&.administrator? - json.reauthorization_required resource.channel.try(:reauthorization_required?) + # Only show reauthorization for embedded signup; manual flow uses API keys, not OAuth + json.reauthorization_required( + (resource.channel.try(:provider_config) || {}).to_h['source'] == 'embedded_signup' && + resource.channel.try(:reauthorization_required?) + ) end ## Voice attributes for TwilioSms diff --git a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb index 9e03e2587..0fd6ad7bf 100644 --- a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb @@ -100,6 +100,35 @@ RSpec.describe 'Inboxes API', type: :request do expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(inbox.id) end + it 'returns reauthorization_required for embedded signup whatsapp channel when reauth required' do + whatsapp_channel = create(:channel_whatsapp, account: account, provider: 'whatsapp_cloud', sync_templates: false, + validate_provider_config: false) + whatsapp_inbox = create(:inbox, channel: whatsapp_channel, account: account) + whatsapp_channel.prompt_reauthorization! + + get "/api/v1/accounts/#{account.id}/inboxes/#{whatsapp_inbox.id}", + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.parsed_body['reauthorization_required']).to be(true) + end + + it 'does not flag reauthorization_required for manual whatsapp channel even when reauth required' do + whatsapp_channel = create(:channel_whatsapp, account: account, provider: 'whatsapp_cloud', sync_templates: false, + validate_provider_config: false) + whatsapp_channel.update!(provider_config: whatsapp_channel.provider_config.merge('source' => 'manual')) + whatsapp_inbox = create(:inbox, channel: whatsapp_channel, account: account) + whatsapp_channel.prompt_reauthorization! + + get "/api/v1/accounts/#{account.id}/inboxes/#{whatsapp_inbox.id}", + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.parsed_body['reauthorization_required']).to be(false) + end + it 'returns the inbox if assigned inbox is assigned as agent' do create(:inbox_member, user: agent, inbox: inbox) get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}", diff --git a/spec/jobs/webhooks/whatsapp_events_job_spec.rb b/spec/jobs/webhooks/whatsapp_events_job_spec.rb index d82658102..8d1b24b52 100644 --- a/spec/jobs/webhooks/whatsapp_events_job_spec.rb +++ b/spec/jobs/webhooks/whatsapp_events_job_spec.rb @@ -62,6 +62,14 @@ RSpec.describe Webhooks::WhatsappEventsJob do job.perform_now(params) end + it 'still enqueues for manual channels even when reauthorization required' do + channel.update!(provider_config: channel.provider_config.merge('source' => 'manual')) + channel.prompt_reauthorization! + allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service) + expect(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new) + job.perform_now(params) + 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)