diff --git a/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb b/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb index bddac7581..1dfcc8ec3 100644 --- a/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb +++ b/enterprise/app/services/enterprise/message_templates/hook_execution_service.rb @@ -82,7 +82,9 @@ module Enterprise::MessageTemplates::HookExecutionService ::MessageTemplates::Template::OutOfOffice.perform_if_applicable(conversation) end + # Only let Captain suppress the fallback templates when it can actually reply; otherwise a revoked + # feature would leave the contact with neither a Captain response nor a greeting/OOO/email-collect. def captain_handling_conversation? - conversation.pending? && inbox.respond_to?(:captain_assistant) && inbox.captain_assistant.present? + conversation.pending? && inbox.respond_to?(:captain_assistant) && inbox.captain_assistant.present? && captain_feature_enabled? end end diff --git a/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb b/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb index cdaccc606..714e29983 100644 --- a/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb +++ b/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb @@ -203,6 +203,29 @@ RSpec.describe MessageTemplates::HookExecutionService do end end + context 'when the Captain feature is disabled but an assistant is still linked' do + before do + account.disable_features!('captain_integration', 'captain_integration_v2') + conversation.update!(status: :pending) + end + + it 'does not schedule captain response job' do + expect(Captain::Conversation::ResponseBuilderJob).not_to receive(:perform_later) + + create(:message, conversation: conversation, message_type: :incoming, account: account) + end + + it 'falls back to the greeting message instead of leaving the contact without a reply' do + inbox.update!(greeting_enabled: true, greeting_message: 'Hello! How can we help you?', enable_email_collect: false) + + expect do + create(:message, conversation: conversation, message_type: :incoming, account: account) + end.to change { conversation.reload.messages.template.count }.by(1) + + expect(conversation.reload.messages.template.last.content).to eq('Hello! How can we help you?') + end + end + context 'when Captain is not configured' do before do CaptainInbox.where(inbox: inbox).destroy_all