feat(conversations): add message creation limit lock

This commit is contained in:
Sony Mathew
2026-07-02 00:28:31 +05:30
parent 926a9d8a69
commit 48e7985b0f
73 changed files with 1691 additions and 58 deletions
@@ -73,6 +73,18 @@ RSpec.describe AutomationRules::ActionService do
expect(message_builder).not_to receive(:perform)
described_class.new(rule, account, conversation).perform
end
it 'drops locked message actions without reporting an exception' do
rule.update!(actions: [{ action_name: 'send_message', action_params: ['Hello'] }])
with_modified_env 'CONVERSATION_MESSAGE_LIMIT': '1' do
create(:message, conversation: conversation, account: account, inbox: conversation.inbox)
allow(Messages::MessageBuilder).to receive(:new).and_call_original
expect(ChatwootExceptionTracker).not_to receive(:new)
expect { described_class.new(rule.reload, account, conversation).perform }.not_to change(Message, :count)
end
end
end
describe '#perform with send_email_to_team action' do
+40
View File
@@ -28,6 +28,33 @@ describe CsatSurveyService do
expect(MessageTemplates::Template::CsatSurvey).to have_received(:new).with(conversation: conversation)
expect(csat_template).to have_received(:perform)
end
it 'drops CSAT survey creation when the conversation is locked' do
create(:message, conversation: conversation, account: account, inbox: inbox)
with_modified_env CONVERSATION_MESSAGE_LIMIT: '1' do
service.perform
end
expect(MessageTemplates::Template::CsatSurvey).not_to have_received(:new)
expect(Conversations::ActivityMessageJob).not_to have_received(:perform_later)
expect(conversation.reload.messages.count).to eq(1)
end
it 'drops Twilio WhatsApp template survey before checking template status when the conversation is locked' do
create(:message, conversation: conversation, account: account, inbox: inbox)
inbox.update(csat_config: { 'template' => { 'content_sid' => 'HX123' } })
allow(conversation).to receive(:inbox).and_return(inbox)
allow(inbox).to receive(:twilio_whatsapp?).and_return(true)
expect(Twilio::CsatTemplateService).not_to receive(:new)
with_modified_env CONVERSATION_MESSAGE_LIMIT: '1' do
service.perform
end
expect(MessageTemplates::Template::CsatSurvey).not_to have_received(:new)
expect(conversation.reload.messages.count).to eq(1)
end
end
context 'when outside messaging window' do
@@ -217,6 +244,19 @@ describe CsatSurveyService do
csat_message = whatsapp_conversation.messages.where(content_type: :input_csat).last
expect(csat_message.content).to eq('Please rate this conversation')
end
it 'drops WhatsApp template survey before checking template status when the conversation is locked' do
create(:message, conversation: whatsapp_conversation, account: account, inbox: whatsapp_inbox)
expect(mock_provider_service).not_to receive(:get_template_status)
expect(mock_provider_service).not_to receive(:send_template)
with_modified_env CONVERSATION_MESSAGE_LIMIT: '1' do
whatsapp_service.perform
end
expect(MessageTemplates::Template::CsatSurvey).not_to have_received(:new)
expect(whatsapp_conversation.reload.messages.count).to eq(1)
end
end
context 'when template is not available or not approved' do
@@ -45,6 +45,19 @@ RSpec.describe Macros::ExecutionService, type: :service do
service.perform
end
it 'drops locked message actions without reporting an exception' do
allow(macro).to receive(:actions).and_return([
{ action_name: 'send_message', action_params: ['Locked message'] }
])
with_modified_env 'CONVERSATION_MESSAGE_LIMIT': '1' do
create(:message, conversation: conversation, account: account, inbox: conversation.inbox)
expect(ChatwootExceptionTracker).not_to receive(:new)
expect { service.perform }.not_to change(Message, :count)
end
end
end
end
end
@@ -29,5 +29,14 @@ describe MessageTemplates::Template::Greeting do
expect(conversation.messages.count).to eq(1)
expect(conversation.messages.last.content).to eq('Hello welcome to our board.')
end
it 'drops locked greeting messages without reporting an exception' do
with_modified_env 'CONVERSATION_MESSAGE_LIMIT': '1' do
create(:message, conversation: conversation, account: conversation.account, inbox: conversation.inbox)
expect(ChatwootExceptionTracker).not_to receive(:new)
expect { described_class.new(conversation: conversation).perform }.not_to change(Message, :count)
end
end
end
end