fix: Disable IMAP inboxes that requires authorization (#12092)

This PR disables queueing IMAP sync jobs for emails channels that 
- are in free plan if on Chatwoot cloud.
- requires authorization
This commit is contained in:
Pranav
2025-08-01 16:32:29 -07:00
committed by GitHub
parent 4dc7a653eb
commit 51b9fd8eca
3 changed files with 55 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
class Inboxes::FetchImapEmailInboxesJob < ApplicationJob
queue_as :scheduled_jobs
include BillingHelper
def perform
email_inboxes = Inbox.where(channel_type: 'Channel::Email')
@@ -11,6 +12,13 @@ class Inboxes::FetchImapEmailInboxesJob < ApplicationJob
private
def should_fetch_emails?(inbox)
inbox.channel.imap_enabled && !inbox.account.suspended?
return false if inbox.account.suspended?
return false unless inbox.channel.imap_enabled
return false if inbox.channel.reauthorization_required?
return true unless ChatwootApp.chatwoot_cloud?
return false if default_plan?(inbox.account)
true
end
end

View File

@@ -0,0 +1,26 @@
require 'rails_helper'
RSpec.describe Inboxes::FetchImapEmailInboxesJob do
context 'when chatwoot_cloud is enabled' do
let(:account) { create(:account) }
let(:premium_account) { create(:account, custom_attributes: { plan_name: 'Startups' }) }
let(:imap_email_channel) { create(:channel_email, imap_enabled: true, account: account) }
let(:premium_imap_channel) { create(:channel_email, imap_enabled: true, account: premium_account) }
before do
premium_account.custom_attributes['plan_name'] = 'Startups'
InstallationConfig.where(name: 'DEPLOYMENT_ENV').first_or_create!(value: 'cloud')
InstallationConfig.where(name: 'CHATWOOT_CLOUD_PLANS').first_or_create!(value: [{ 'name' => 'Hacker' }])
end
it 'skips inboxes with default plan' do
expect(Inboxes::FetchImapEmailsJob).not_to receive(:perform_later).with(imap_email_channel)
described_class.perform_now
end
it 'processes inboxes with premium plan' do
expect(Inboxes::FetchImapEmailsJob).to receive(:perform_later).with(premium_imap_channel)
described_class.perform_now
end
end
end

View File

@@ -3,6 +3,7 @@ require 'rails_helper'
RSpec.describe Inboxes::FetchImapEmailInboxesJob do
let(:account) { create(:account) }
let(:suspended_account) { create(:account, status: 'suspended') }
let(:premium_account) { create(:account, custom_attributes: { plan_name: 'Startups' }) }
let(:imap_email_channel) do
create(:channel_email, imap_enabled: true, account: account)
@@ -16,6 +17,19 @@ RSpec.describe Inboxes::FetchImapEmailInboxesJob do
create(:channel_email, imap_enabled: false, account: account)
end
let(:reauth_required_channel) do
create(:channel_email, imap_enabled: true, account: account)
end
let(:premium_imap_channel) do
create(:channel_email, imap_enabled: true, account: premium_account)
end
before do
reauth_required_channel.prompt_reauthorization!
premium_account.custom_attributes['plan_name'] = 'Startups'
end
it 'enqueues the job' do
expect { described_class.perform_later }.to have_enqueued_job(described_class)
.on_queue('scheduled_jobs')
@@ -44,5 +58,11 @@ RSpec.describe Inboxes::FetchImapEmailInboxesJob do
described_class.perform_now
end
it 'skips channels requiring reauthorization' do
expect(Inboxes::FetchImapEmailsJob).not_to receive(:perform_later).with(reauth_required_channel)
described_class.perform_now
end
end
end