+
+
diff --git a/app/models/conversation.rb b/app/models/conversation.rb
index 4ec63acc2..eb97a22e8 100644
--- a/app/models/conversation.rb
+++ b/app/models/conversation.rb
@@ -228,14 +228,17 @@ class Conversation < ApplicationRecord
def determine_conversation_status
self.status = :resolved and return if contact.blocked?
- # Message template hooks aren't executed for conversations from campaigns
- # So making these conversations open for agent visibility
- return if campaign.present?
+ return handle_campaign_status if campaign.present?
# TODO: make this an inbox config instead of assuming bot conversations should start as pending
self.status = :pending if inbox.active_bot?
end
+ def handle_campaign_status
+ # If campaign has no sender (bot-initiated) and inbox has active bot, let bot handle it
+ self.status = :pending if campaign.sender_id.nil? && inbox.active_bot?
+ end
+
def notify_conversation_creation
dispatcher_dispatch(CONVERSATION_CREATED)
end
diff --git a/app/presenters/messages/search_data_presenter.rb b/app/presenters/messages/search_data_presenter.rb
index dba0b9499..7d0638add 100644
--- a/app/presenters/messages/search_data_presenter.rb
+++ b/app/presenters/messages/search_data_presenter.rb
@@ -3,7 +3,7 @@ class Messages::SearchDataPresenter < SimpleDelegator
{
**searchable_content,
**message_attributes,
- **search_additional_data,
+ additional_attributes: additional_attributes_data,
conversation: conversation_data
}
end
@@ -49,7 +49,7 @@ class Messages::SearchDataPresenter < SimpleDelegator
{ id: conversation.display_id }
end
- def search_additional_data
+ def additional_attributes_data
{
campaign_id: additional_attributes&.dig('campaign_id'),
automation_rule_id: content_attributes&.dig('automation_rule_id')
diff --git a/app/services/contacts/bulk_action_service.rb b/app/services/contacts/bulk_action_service.rb
index c759e95a5..a0e11ad9a 100644
--- a/app/services/contacts/bulk_action_service.rb
+++ b/app/services/contacts/bulk_action_service.rb
@@ -6,6 +6,7 @@ class Contacts::BulkActionService
end
def perform
+ return delete_contacts if delete_requested?
return assign_labels if labels_to_add.any?
Rails.logger.warn("Unknown contact bulk operation payload: #{@params.keys}")
@@ -22,6 +23,13 @@ class Contacts::BulkActionService
).perform
end
+ def delete_contacts
+ Contacts::BulkDeleteService.new(
+ account: @account,
+ contact_ids: ids
+ ).perform
+ end
+
def ids
Array(@params[:ids]).compact
end
@@ -29,4 +37,8 @@ class Contacts::BulkActionService
def labels_to_add
@labels_to_add ||= Array(@params.dig(:labels, :add)).reject(&:blank?)
end
+
+ def delete_requested?
+ @params[:action_name] == 'delete'
+ end
end
diff --git a/app/services/contacts/bulk_delete_service.rb b/app/services/contacts/bulk_delete_service.rb
new file mode 100644
index 000000000..d197f17f6
--- /dev/null
+++ b/app/services/contacts/bulk_delete_service.rb
@@ -0,0 +1,18 @@
+class Contacts::BulkDeleteService
+ def initialize(account:, contact_ids: [])
+ @account = account
+ @contact_ids = Array(contact_ids).compact
+ end
+
+ def perform
+ return if @contact_ids.blank?
+
+ contacts.find_each(&:destroy!)
+ end
+
+ private
+
+ def contacts
+ @account.contacts.where(id: @contact_ids)
+ end
+end
diff --git a/app/services/llm_formatter/conversation_llm_formatter.rb b/app/services/llm_formatter/conversation_llm_formatter.rb
index 8654e0adf..4e0bd7013 100644
--- a/app/services/llm_formatter/conversation_llm_formatter.rb
+++ b/app/services/llm_formatter/conversation_llm_formatter.rb
@@ -39,7 +39,14 @@ class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter
end
def format_message(message)
- sender = message.message_type == 'incoming' ? 'User' : 'Support agent'
+ sender = case message.sender_type
+ when 'User'
+ 'Support Agent'
+ when 'Contact'
+ 'User'
+ else
+ 'Bot'
+ end
sender = "[Private Note] #{sender}" if message.private?
"#{sender}: #{message.content}\n"
end
diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb
index 04d605c2e..dd5c71a4d 100644
--- a/config/initializers/sidekiq.rb
+++ b/config/initializers/sidekiq.rb
@@ -18,8 +18,10 @@ end
Sidekiq.configure_server do |config|
config.redis = Redis::Config.app
- config.server_middleware do |chain|
- chain.add ChatwootDequeuedLogger
+ if ActiveModel::Type::Boolean.new.cast(ENV.fetch('ENABLE_SIDEKIQ_DEQUEUE_LOGGER', false))
+ config.server_middleware do |chain|
+ chain.add ChatwootDequeuedLogger
+ end
end
# skip the default start stop logging
diff --git a/package.json b/package.json
index 6fb7156cb..4660b44fa 100644
--- a/package.json
+++ b/package.json
@@ -33,7 +33,7 @@
"dependencies": {
"@breezystack/lamejs": "^1.2.7",
"@chatwoot/ninja-keys": "1.2.3",
- "@chatwoot/prosemirror-schema": "1.2.1",
+ "@chatwoot/prosemirror-schema": "1.2.3",
"@chatwoot/utils": "^0.0.51",
"@formkit/core": "^1.6.7",
"@formkit/vue": "^1.6.7",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 22c662d89..e5629af3b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -20,8 +20,8 @@ importers:
specifier: 1.2.3
version: 1.2.3
'@chatwoot/prosemirror-schema':
- specifier: 1.2.1
- version: 1.2.1
+ specifier: 1.2.3
+ version: 1.2.3
'@chatwoot/utils':
specifier: ^0.0.51
version: 0.0.51
@@ -406,8 +406,8 @@ packages:
'@chatwoot/ninja-keys@1.2.3':
resolution: {integrity: sha512-xM8d9P5ikDMZm2WbaCTk/TW5HFauylrU3cJ75fq5je6ixKwyhl/0kZbVN/vbbZN4+AUX/OaSIn6IJbtCgIF67g==}
- '@chatwoot/prosemirror-schema@1.2.1':
- resolution: {integrity: sha512-UbiEvG5tgi1d0lMbkaqxgTh7vHfywEYKLQo1sxqp4Q7aLZh4QFtbLzJ2zyBtu4Nhipe+guFfEJdic7i43MP/XQ==}
+ '@chatwoot/prosemirror-schema@1.2.3':
+ resolution: {integrity: sha512-q/EfirVK9jt8FJAx3Gf6y3LoVadmYVLknbYvPrkUe81WO0f2mkZ/kY2UQgpUISVvOGEkCH4bkfYMp5UQ+Buz3g==}
'@chatwoot/utils@0.0.51':
resolution: {integrity: sha512-WlEmWfOTzR7YZRUWzn5Wpm15/BRudpwqoNckph8TohyDbiim1CP4UZGa+qjajxTbNGLLhtKlm0Xl+X16+5Wceg==}
@@ -4768,7 +4768,7 @@ snapshots:
hotkeys-js: 3.8.7
lit: 2.2.6
- '@chatwoot/prosemirror-schema@1.2.1':
+ '@chatwoot/prosemirror-schema@1.2.3':
dependencies:
markdown-it-sup: 2.0.0
prosemirror-commands: 1.6.0
diff --git a/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb b/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb
index 9c53099fd..1ab490a96 100644
--- a/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb
+++ b/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb
@@ -195,37 +195,6 @@ RSpec.describe 'Api::V1::Accounts::BulkActionsController', type: :request do
expect(Conversation.first.label_list).to contain_exactly('support', 'priority_customer')
expect(Conversation.second.label_list).to contain_exactly('support', 'priority_customer')
end
-
- it 'enqueues contact bulk action job with permitted params' do
- contact_one = create(:contact, account: account)
- contact_two = create(:contact, account: account)
-
- previous_adapter = ActiveJob::Base.queue_adapter
- ActiveJob::Base.queue_adapter = :test
-
- expect do
- post "/api/v1/accounts/#{account.id}/bulk_actions",
- headers: agent.create_new_auth_token,
- params: {
- type: 'Contact',
- ids: [contact_one.id, contact_two.id],
- labels: { add: %w[vip support] },
- extra: 'ignored'
- }
- end.to have_enqueued_job(Contacts::BulkActionJob).with(
- account.id,
- agent.id,
- hash_including(
- 'ids' => [contact_one.id, contact_two.id],
- 'labels' => hash_including('add' => %w[vip support])
- )
- )
-
- expect(response).to have_http_status(:success)
- ensure
- ActiveJob::Base.queue_adapter = previous_adapter
- clear_enqueued_jobs
- end
end
end
@@ -256,4 +225,49 @@ RSpec.describe 'Api::V1::Accounts::BulkActionsController', type: :request do
end
end
end
+
+ describe 'POST /api/v1/accounts/{account.id}/bulk_actions (contacts)' do
+ context 'when it is an authenticated user' do
+ let!(:agent) { create(:user, account: account, role: :agent) }
+
+ it 'enqueues Contacts::BulkActionJob with permitted params' do
+ contact_one = create(:contact, account: account)
+ contact_two = create(:contact, account: account)
+
+ expect do
+ post "/api/v1/accounts/#{account.id}/bulk_actions",
+ headers: agent.create_new_auth_token,
+ params: {
+ type: 'Contact',
+ ids: [contact_one.id, contact_two.id],
+ labels: { add: %w[vip support] },
+ extra: 'ignored'
+ }
+ end.to have_enqueued_job(Contacts::BulkActionJob).with(
+ account.id,
+ agent.id,
+ hash_including(
+ 'ids' => [contact_one.id.to_s, contact_two.id.to_s],
+ 'labels' => hash_including('add' => %w[vip support])
+ )
+ )
+
+ expect(response).to have_http_status(:success)
+ end
+
+ it 'returns unauthorized for delete action when user is not admin' do
+ contact = create(:contact, account: account)
+
+ post "/api/v1/accounts/#{account.id}/bulk_actions",
+ headers: agent.create_new_auth_token,
+ params: {
+ type: 'Contact',
+ ids: [contact.id],
+ action_name: 'delete'
+ }
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+ end
end
diff --git a/spec/factories/messages.rb b/spec/factories/messages.rb
index b2ae41c5e..99a2c7cd8 100644
--- a/spec/factories/messages.rb
+++ b/spec/factories/messages.rb
@@ -27,6 +27,13 @@ FactoryBot.define do
end
end
+ trait :bot_message do
+ message_type { 'outgoing' }
+ after(:build) do |message|
+ message.sender = nil
+ end
+ end
+
after(:build) do |message|
message.sender ||= message.outgoing? ? create(:user, account: message.account) : create(:contact, account: message.account)
message.inbox ||= message.conversation&.inbox || create(:inbox, account: message.account)
diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb
index 72962ba54..51bb43384 100644
--- a/spec/models/conversation_spec.rb
+++ b/spec/models/conversation_spec.rb
@@ -576,9 +576,38 @@ RSpec.describe Conversation do
expect(conversation.status).to eq('pending')
end
- it 'returns conversation as open if campaign is present' do
- conversation = create(:conversation, inbox: bot_inbox.inbox, campaign: create(:campaign))
- expect(conversation.status).to eq('open')
+ context 'with campaigns' do
+ let(:user) { create(:user, account: bot_inbox.inbox.account) }
+
+ it 'returns conversation as open if campaign has a sender' do
+ campaign = create(:campaign, inbox: bot_inbox.inbox, account: bot_inbox.inbox.account, sender: user)
+ conversation = create(:conversation, inbox: bot_inbox.inbox, campaign: campaign)
+ expect(conversation.status).to eq('open')
+ end
+
+ it 'returns conversation as pending if campaign has no sender (bot-initiated) and bot is active' do
+ campaign = create(:campaign, inbox: bot_inbox.inbox, account: bot_inbox.inbox.account, sender: nil)
+ conversation = create(:conversation, inbox: bot_inbox.inbox, campaign: campaign)
+ expect(conversation.status).to eq('pending')
+ end
+ end
+
+ context 'with campaigns in inbox without bot' do
+ let(:account) { create(:account) }
+ let(:inbox) { create(:inbox, account: account) }
+ let(:user) { create(:user, account: account) }
+
+ it 'returns conversation as open if campaign has no sender but no bot is active' do
+ campaign = create(:campaign, inbox: inbox, account: account, sender: nil)
+ conversation = create(:conversation, inbox: inbox, campaign: campaign)
+ expect(conversation.status).to eq('open')
+ end
+
+ it 'returns conversation as open if campaign has a sender' do
+ campaign = create(:campaign, inbox: inbox, account: account, sender: user)
+ conversation = create(:conversation, inbox: inbox, campaign: campaign)
+ expect(conversation.status).to eq('open')
+ end
end
end
diff --git a/spec/presenters/messages/search_data_presenter_spec.rb b/spec/presenters/messages/search_data_presenter_spec.rb
index a5062086a..0bbe1a812 100644
--- a/spec/presenters/messages/search_data_presenter_spec.rb
+++ b/spec/presenters/messages/search_data_presenter_spec.rb
@@ -67,11 +67,11 @@ RSpec.describe Messages::SearchDataPresenter do
end
it 'includes campaign_id' do
- expect(presenter.search_data[:campaign_id]).to eq('123')
+ expect(presenter.search_data[:additional_attributes][:campaign_id]).to eq('123')
end
it 'includes automation_rule_id' do
- expect(presenter.search_data[:automation_rule_id]).to eq('456')
+ expect(presenter.search_data[:additional_attributes][:automation_rule_id]).to eq('456')
end
end
end
diff --git a/spec/services/contacts/bulk_action_service_spec.rb b/spec/services/contacts/bulk_action_service_spec.rb
new file mode 100644
index 000000000..0411c8455
--- /dev/null
+++ b/spec/services/contacts/bulk_action_service_spec.rb
@@ -0,0 +1,38 @@
+require 'rails_helper'
+
+RSpec.describe Contacts::BulkActionService do
+ subject(:service) { described_class.new(account: account, user: user, params: params) }
+
+ let(:account) { create(:account) }
+ let(:user) { create(:user, account: account) }
+
+ describe '#perform' do
+ context 'when delete action is requested via action_name' do
+ let(:params) { { ids: [1, 2], action_name: 'delete' } }
+
+ it 'delegates to the bulk delete service' do
+ bulk_delete_service = instance_double(Contacts::BulkDeleteService, perform: true)
+
+ expect(Contacts::BulkDeleteService).to receive(:new)
+ .with(account: account, contact_ids: [1, 2])
+ .and_return(bulk_delete_service)
+
+ service.perform
+ end
+ end
+
+ context 'when labels are provided' do
+ let(:params) { { ids: [10, 20], labels: { add: %w[vip support] }, extra: 'ignored' } }
+
+ it 'delegates to the bulk assign labels service with permitted params' do
+ bulk_assign_service = instance_double(Contacts::BulkAssignLabelsService, perform: true)
+
+ expect(Contacts::BulkAssignLabelsService).to receive(:new)
+ .with(account: account, contact_ids: [10, 20], labels: %w[vip support])
+ .and_return(bulk_assign_service)
+
+ service.perform
+ end
+ end
+ end
+end
diff --git a/spec/services/contacts/bulk_delete_service_spec.rb b/spec/services/contacts/bulk_delete_service_spec.rb
new file mode 100644
index 000000000..4eaecaa0f
--- /dev/null
+++ b/spec/services/contacts/bulk_delete_service_spec.rb
@@ -0,0 +1,24 @@
+require 'rails_helper'
+
+RSpec.describe Contacts::BulkDeleteService do
+ subject(:service) { described_class.new(account: account, contact_ids: contact_ids) }
+
+ let(:account) { create(:account) }
+ let!(:contact_one) { create(:contact, account: account) }
+ let!(:contact_two) { create(:contact, account: account) }
+ let(:contact_ids) { [contact_one.id, contact_two.id] }
+
+ describe '#perform' do
+ it 'deletes the provided contacts' do
+ expect { service.perform }
+ .to change { account.contacts.exists?(contact_one.id) }.from(true).to(false)
+ .and change { account.contacts.exists?(contact_two.id) }.from(true).to(false)
+ end
+
+ it 'returns when no contact ids are provided' do
+ empty_service = described_class.new(account: account, contact_ids: [])
+
+ expect { empty_service.perform }.not_to change(Contact, :count)
+ end
+ end
+end
diff --git a/spec/services/llm_formatter/conversation_llm_formatter_spec.rb b/spec/services/llm_formatter/conversation_llm_formatter_spec.rb
index 49fcc1a18..b79ee6d79 100644
--- a/spec/services/llm_formatter/conversation_llm_formatter_spec.rb
+++ b/spec/services/llm_formatter/conversation_llm_formatter_spec.rb
@@ -28,6 +28,14 @@ RSpec.describe LlmFormatter::ConversationLlmFormatter do
content: 'Hello, I need help'
)
+ create(
+ :message,
+ :bot_message,
+ conversation: conversation,
+ message_type: 'outgoing',
+ content: 'Thanks for reaching out, an agent will reach out to you soon'
+ )
+
create(
:message,
conversation: conversation,
@@ -40,7 +48,8 @@ RSpec.describe LlmFormatter::ConversationLlmFormatter do
"Channel: #{conversation.inbox.channel.name}",
'Message History:',
'User: Hello, I need help',
- 'Support agent: How can I assist you today?',
+ 'Bot: Thanks for reaching out, an agent will reach out to you soon',
+ 'Support Agent: How can I assist you today?',
''
].join("\n")
diff --git a/spec/services/widget/token_service_expiry_spec.rb b/spec/services/widget/token_service_expiry_spec.rb
index 051a757a5..50e104c65 100644
--- a/spec/services/widget/token_service_expiry_spec.rb
+++ b/spec/services/widget/token_service_expiry_spec.rb
@@ -15,11 +15,11 @@ RSpec.describe Widget::TokenService, type: :service do
end
it 'uses the configured value for token expiry' do
- freeze_time do
+ travel_to '2025-01-01' do
token = service.generate_token
decoded = JWT.decode(token, Rails.application.secret_key_base, true, algorithm: 'HS256').first
- expect(decoded['iat']).to eq(Time.now.to_i)
- expect(decoded['exp']).to eq(30.days.from_now.to_i)
+ expect(decoded['iat']).to eq(Time.zone.now.to_i)
+ expect(decoded['exp']).to eq(Time.zone.now.to_i + 30.days.to_i)
end
end
end
@@ -30,11 +30,11 @@ RSpec.describe Widget::TokenService, type: :service do
end
it 'uses the default expiry' do
- freeze_time do
+ travel_to '2025-01-01' do
token = service.generate_token
decoded = JWT.decode(token, Rails.application.secret_key_base, true, algorithm: 'HS256').first
- expect(decoded['iat']).to eq(Time.now.to_i)
- expect(decoded['exp']).to eq(180.days.from_now.to_i)
+ expect(decoded['iat']).to eq(Time.zone.now.to_i)
+ expect(decoded['exp']).to eq(Time.zone.now.to_i + 180.days.to_i)
end
end
end