Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

fix(campaigns): show campaigns sent before the first reply (#16272)

Muhsin Keloth96b13cc2060d
diff --git a/enterprise/app/controllers/api/v1/accounts/conversations/campaign_history_controller.rb b/enterprise/app/controllers/api/v1/accounts/conversations/campaign_history_controller.rb
index b1980790e82..f76bdb74f82 100644
--- a/enterprise/app/controllers/api/v1/accounts/conversations/campaign_history_controller.rb
+++ b/enterprise/app/controllers/api/v1/accounts/conversations/campaign_history_controller.rb
@@ -40,11 +40,13 @@ class Api::V1::Accounts::Conversations::CampaignHistoryController < Api::V1::Acc
end
def campaign_recipients
- next_conversation = Current.account.conversations.where(contact_id: @conversation.contact_id, inbox_id: @conversation.inbox_id)
- .where('(created_at, id) > (?, ?)', @conversation.created_at, @conversation.id)
- .order(:created_at, :id).first
+ conversations = Current.account.conversations.where(contact_id: @conversation.contact_id, inbox_id: @conversation.inbox_id)
+ has_previous_conversation = conversations.exists?(['(created_at, id) < (?, ?)', @conversation.created_at, @conversation.id])
+ next_conversation = conversations.where('(created_at, id) > (?, ?)', @conversation.created_at, @conversation.id)
+ .order(:created_at, :id).first
recipients = @conversation.contact.campaign_recipients.where(account_id: Current.account.id, inbox_id: @conversation.inbox_id)
- .where(sent_at: @conversation.created_at..)
+ .where.not(sent_at: nil)
+ recipients = recipients.where(sent_at: @conversation.created_at..) if has_previous_conversation
next_conversation ? recipients.where('sent_at < ?', next_conversation.created_at) : recipients
end
diff --git a/spec/enterprise/controllers/api/v1/accounts/conversations/campaign_history_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/conversations/campaign_history_controller_spec.rb
index dc2919c4e68..a22fdf6cf66 100644
--- a/spec/enterprise/controllers/api/v1/accounts/conversations/campaign_history_controller_spec.rb
+++ b/spec/enterprise/controllers/api/v1/accounts/conversations/campaign_history_controller_spec.rb
@@ -139,7 +139,7 @@ RSpec.describe 'Conversation campaign history API', type: :request do
created_at: next_start)
end
- it 'includes the start boundary and excludes campaigns before it and at or after the next conversation' do
+ it 'includes campaigns before the first conversation and keeps later conversations separate' do
times = [conversation.created_at - 1.second, conversation.created_at, sent_at, next_start, next_start + 1.second]
records = times.map do |time|
CampaignRecipient.create!(account: account, campaign: create(:campaign, account: account, inbox: inbox), inbox: inbox,
@@ -147,7 +147,7 @@ RSpec.describe 'Conversation campaign history API', type: :request do
end
get url, headers: headers
- expect(response.parsed_body['payload'].pluck('id')).to eq([records[2].id, records[1].id])
+ expect(response.parsed_body['payload'].pluck('id')).to eq([records[2].id, records[1].id, records[0].id])
expect(response.parsed_body['meta']['next_before']).to be_nil
get "/api/v1/accounts/#{account.id}/conversations/#{next_conversation.display_id}/campaign_history", headers: headers