fix: Include waiting on agent conversations to unattended view (#7667)
Updating the `unattended` tab to include conversations where the customer responded and is awaiting an agent's response. Previously it showed only the conversations where the first response was pending. Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -25,10 +25,11 @@ export const filterByLabel = (shouldFilter, labels, chatLabels) => {
|
|||||||
export const filterByUnattended = (
|
export const filterByUnattended = (
|
||||||
shouldFilter,
|
shouldFilter,
|
||||||
conversationType,
|
conversationType,
|
||||||
firstReplyOn
|
firstReplyOn,
|
||||||
|
waitingSince
|
||||||
) => {
|
) => {
|
||||||
return conversationType === 'unattended'
|
return conversationType === 'unattended'
|
||||||
? !firstReplyOn && shouldFilter
|
? (!firstReplyOn || !!waitingSince) && shouldFilter
|
||||||
: shouldFilter;
|
: shouldFilter;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ export const applyPageFilters = (conversation, filters) => {
|
|||||||
labels: chatLabels = [],
|
labels: chatLabels = [],
|
||||||
meta = {},
|
meta = {},
|
||||||
first_reply_created_at: firstReplyOn,
|
first_reply_created_at: firstReplyOn,
|
||||||
|
waiting_since: waitingSince,
|
||||||
} = conversation;
|
} = conversation;
|
||||||
const team = meta.team || {};
|
const team = meta.team || {};
|
||||||
const { id: chatTeamId } = team;
|
const { id: chatTeamId } = team;
|
||||||
@@ -51,7 +53,8 @@ export const applyPageFilters = (conversation, filters) => {
|
|||||||
shouldFilter = filterByUnattended(
|
shouldFilter = filterByUnattended(
|
||||||
shouldFilter,
|
shouldFilter,
|
||||||
conversationType,
|
conversationType,
|
||||||
firstReplyOn
|
firstReplyOn,
|
||||||
|
waitingSince
|
||||||
);
|
);
|
||||||
|
|
||||||
return shouldFilter;
|
return shouldFilter;
|
||||||
|
|||||||
@@ -38,8 +38,6 @@ class ReportingEventListener < BaseListener
|
|||||||
event_end_time: message.created_at
|
event_end_time: message.created_at
|
||||||
)
|
)
|
||||||
|
|
||||||
conversation.update(first_reply_created_at: message.created_at)
|
|
||||||
|
|
||||||
reporting_event.save!
|
reporting_event.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class Conversation < ApplicationRecord
|
|||||||
scope :unassigned, -> { where(assignee_id: nil) }
|
scope :unassigned, -> { where(assignee_id: nil) }
|
||||||
scope :assigned, -> { where.not(assignee_id: nil) }
|
scope :assigned, -> { where.not(assignee_id: nil) }
|
||||||
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
|
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
|
||||||
scope :unattended, -> { where(first_reply_created_at: nil) }
|
scope :unattended, -> { where(first_reply_created_at: nil).or(where.not(waiting_since: nil)) }
|
||||||
scope :resolvable, lambda { |auto_resolve_duration|
|
scope :resolvable, lambda { |auto_resolve_duration|
|
||||||
return none if auto_resolve_duration.to_i.zero?
|
return none if auto_resolve_duration.to_i.zero?
|
||||||
|
|
||||||
@@ -218,7 +218,7 @@ class Conversation < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def ensure_waiting_since
|
def ensure_waiting_since
|
||||||
self.waiting_since = Time.now.utc
|
self.waiting_since = created_at
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_additional_attributes
|
def validate_additional_attributes
|
||||||
@@ -242,7 +242,8 @@ class Conversation < ApplicationRecord
|
|||||||
|
|
||||||
def allowed_keys?
|
def allowed_keys?
|
||||||
(
|
(
|
||||||
previous_changes.keys.intersect?(%w[team_id assignee_id status snoozed_until custom_attributes label_list first_reply_created_at priority]) ||
|
previous_changes.keys.intersect?(%w[team_id assignee_id status snoozed_until custom_attributes label_list waiting_since first_reply_created_at
|
||||||
|
priority]) ||
|
||||||
(previous_changes['additional_attributes'].present? && previous_changes['additional_attributes'][1].keys.intersect?(%w[conversation_language]))
|
(previous_changes['additional_attributes'].present? && previous_changes['additional_attributes'][1].keys.intersect?(%w[conversation_language]))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -247,7 +247,6 @@ class Message < ApplicationRecord
|
|||||||
send_reply
|
send_reply
|
||||||
execute_message_template_hooks
|
execute_message_template_hooks
|
||||||
update_contact_activity
|
update_contact_activity
|
||||||
update_waiting_since
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_contact_activity
|
def update_contact_activity
|
||||||
@@ -261,7 +260,7 @@ class Message < ApplicationRecord
|
|||||||
)
|
)
|
||||||
conversation.update(waiting_since: nil)
|
conversation.update(waiting_since: nil)
|
||||||
end
|
end
|
||||||
conversation.update(waiting_since: Time.now.utc) if incoming? && conversation.waiting_since.blank?
|
conversation.update(waiting_since: created_at) if incoming? && conversation.waiting_since.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
def human_response?
|
def human_response?
|
||||||
@@ -276,8 +275,12 @@ class Message < ApplicationRecord
|
|||||||
|
|
||||||
def dispatch_create_events
|
def dispatch_create_events
|
||||||
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
|
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
|
||||||
|
|
||||||
if valid_first_reply?
|
if valid_first_reply?
|
||||||
Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
|
Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
|
||||||
|
conversation.update(first_reply_created_at: created_at, waiting_since: nil)
|
||||||
|
else
|
||||||
|
update_waiting_since
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ RSpec.describe 'Conversations API', type: :request do
|
|||||||
context 'when it is an authenticated user' do
|
context 'when it is an authenticated user' do
|
||||||
let(:agent) { create(:user, account: account, role: :agent) }
|
let(:agent) { create(:user, account: account, role: :agent) }
|
||||||
let(:conversation) { create(:conversation, account: account) }
|
let(:conversation) { create(:conversation, account: account) }
|
||||||
let(:attended_conversation) { create(:conversation, account: account, first_reply_created_at: Time.now.utc) }
|
|
||||||
let(:unattended_conversation) { create(:conversation, account: account, first_reply_created_at: nil) }
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
||||||
@@ -48,9 +46,16 @@ RSpec.describe 'Conversations API', type: :request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'returns unattended conversations' do
|
it 'returns unattended conversations' do
|
||||||
|
attended_conversation = create(:conversation, account: account, first_reply_created_at: Time.now.utc)
|
||||||
|
# to ensure that waiting since value is populated
|
||||||
|
create(:message, message_type: :outgoing, conversation: attended_conversation, account: account)
|
||||||
|
unattended_conversation_no_first_reply = create(:conversation, account: account, first_reply_created_at: nil)
|
||||||
|
unattended_conversation_waiting_since = create(:conversation, account: account, first_reply_created_at: Time.now.utc)
|
||||||
|
|
||||||
agent_1 = create(:user, account: account, role: :agent)
|
agent_1 = create(:user, account: account, role: :agent)
|
||||||
create(:inbox_member, user: agent_1, inbox: attended_conversation.inbox)
|
create(:inbox_member, user: agent_1, inbox: attended_conversation.inbox)
|
||||||
create(:inbox_member, user: agent_1, inbox: unattended_conversation.inbox)
|
create(:inbox_member, user: agent_1, inbox: unattended_conversation_no_first_reply.inbox)
|
||||||
|
create(:inbox_member, user: agent_1, inbox: unattended_conversation_waiting_since.inbox)
|
||||||
|
|
||||||
get "/api/v1/accounts/#{account.id}/conversations",
|
get "/api/v1/accounts/#{account.id}/conversations",
|
||||||
headers: agent_1.create_new_auth_token,
|
headers: agent_1.create_new_auth_token,
|
||||||
@@ -59,8 +64,8 @@ RSpec.describe 'Conversations API', type: :request do
|
|||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
expect(response).to have_http_status(:success)
|
||||||
body = JSON.parse(response.body, symbolize_names: true)
|
body = JSON.parse(response.body, symbolize_names: true)
|
||||||
expect(body[:data][:meta][:all_count]).to eq(1)
|
expect(body[:data][:meta][:all_count]).to eq(2)
|
||||||
expect(body[:data][:payload].count).to eq(1)
|
expect(body[:data][:payload].count).to eq(2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -160,9 +160,13 @@ describe ConversationFinder do
|
|||||||
let(:params) { { status: 'open', assignee_type: 'me', conversation_type: 'unattended' } }
|
let(:params) { { status: 'open', assignee_type: 'me', conversation_type: 'unattended' } }
|
||||||
|
|
||||||
it 'returns unattended conversations' do
|
it 'returns unattended conversations' do
|
||||||
create_list(:conversation, 25, account: account, inbox: inbox, assignee: user_1)
|
create(:conversation, account: account, first_reply_created_at: Time.now.utc, assignee: user_1) # attended_conversation
|
||||||
|
create(:conversation, account: account, first_reply_created_at: nil, assignee: user_1) # unattended_conversation_no_first_reply
|
||||||
|
create(:conversation, account: account, first_reply_created_at: Time.now.utc,
|
||||||
|
assignee: user_1, waiting_since: Time.now.utc) # unattended_conversation_waiting_since
|
||||||
|
|
||||||
result = conversation_finder.perform
|
result = conversation_finder.perform
|
||||||
expect(result[:conversations].length).to be 25
|
expect(result[:conversations].length).to be 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -95,56 +95,81 @@ RSpec.describe Message do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Check if message is a valid first reply' do
|
describe 'message create event' do
|
||||||
it 'is valid if it is outgoing' do
|
let(:conversation) { create(:conversation) }
|
||||||
outgoing_message = create(:message, message_type: :outgoing)
|
|
||||||
expect(outgoing_message.valid_first_reply?).to be true
|
it 'updates the conversation first reply created at if it is the first outgoing message' do
|
||||||
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
|
||||||
|
outgoing_message = create(:message, message_type: :outgoing, conversation: conversation)
|
||||||
|
|
||||||
|
expect(conversation.first_reply_created_at).to eq outgoing_message.created_at
|
||||||
|
expect(conversation.waiting_since).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'is invalid if it is not outgoing' do
|
it 'does not update the conversation first reply created at if the message is incoming' do
|
||||||
incoming_message = create(:message, message_type: :incoming)
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
expect(incoming_message.valid_first_reply?).to be false
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
|
||||||
activity_message = create(:message, message_type: :activity)
|
create(:message, message_type: :incoming, conversation: conversation)
|
||||||
expect(activity_message.valid_first_reply?).to be false
|
|
||||||
|
|
||||||
template_message = create(:message, message_type: :template)
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
expect(template_message.valid_first_reply?).to be false
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'is invalid if it is outgoing but private' do
|
it 'does not update the conversation first reply created at if the message is template' do
|
||||||
conversation = create(:conversation)
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
|
||||||
outgoing_message = create(:message, message_type: :outgoing, conversation: conversation, private: true)
|
create(:message, message_type: :template, conversation: conversation)
|
||||||
expect(outgoing_message.valid_first_reply?).to be false
|
|
||||||
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not update the conversation first reply created at if the message is activity' do
|
||||||
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
|
||||||
|
create(:message, message_type: :activity, conversation: conversation)
|
||||||
|
|
||||||
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not update the conversation first reply created at if the message is a private message' do
|
||||||
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
|
||||||
|
create(:message, message_type: :outgoing, conversation: conversation, private: true)
|
||||||
|
|
||||||
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
|
|
||||||
# next message should be a valid reply
|
|
||||||
next_message = create(:message, message_type: :outgoing, conversation: conversation)
|
next_message = create(:message, message_type: :outgoing, conversation: conversation)
|
||||||
expect(next_message.valid_first_reply?).to be true
|
expect(conversation.first_reply_created_at).to eq next_message.created_at
|
||||||
|
expect(conversation.waiting_since).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'is invalid if it is not the first reply' do
|
it 'does not update first reply if the message is sent as campaign' do
|
||||||
conversation = create(:conversation)
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
first_message = create(:message, message_type: :outgoing, conversation: conversation)
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
expect(first_message.valid_first_reply?).to be true
|
|
||||||
|
|
||||||
second_message = create(:message, message_type: :outgoing, conversation: conversation)
|
create(:message, message_type: :outgoing, conversation: conversation, additional_attributes: { campaign_id: 1 })
|
||||||
expect(second_message.valid_first_reply?).to be false
|
|
||||||
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'is invalid if it is sent as campaign' do
|
it 'does not update first reply if the message is sent by automation' do
|
||||||
conversation = create(:conversation)
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
campaign_message = create(:message, message_type: :outgoing, conversation: conversation, additional_attributes: { campaign_id: 1 })
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
expect(campaign_message.valid_first_reply?).to be false
|
|
||||||
|
|
||||||
second_message = create(:message, message_type: :outgoing, conversation: conversation)
|
create(:message, message_type: :outgoing, conversation: conversation, content_attributes: { automation_rule_id: 1 })
|
||||||
expect(second_message.valid_first_reply?).to be true
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is invalid if it is sent by automation' do
|
expect(conversation.first_reply_created_at).to be_nil
|
||||||
conversation = create(:conversation)
|
expect(conversation.waiting_since).to eq conversation.created_at
|
||||||
automation_message = create(:message, message_type: :outgoing, conversation: conversation, content_attributes: { automation_rule_id: 1 })
|
|
||||||
expect(automation_message.valid_first_reply?).to be false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user