Compare commits

...
Author SHA1 Message Date
Shivam Mishra a55e951206 test: conversation assigned event 2025-08-11 20:23:58 +05:30
Shivam Mishra 2231640580 feat: add conversation assigned event 2025-08-11 20:17:32 +05:30
Shivam Mishra da930af312 feat: add conversation created event 2025-08-11 19:34:45 +05:30
Shivam MishraandGitHub ad42fffcf0 Merge branch 'develop' into feat/reporting-reopen 2025-08-11 14:37:02 +05:30
Shivam MishraandGitHub 86b8d6392e Merge branch 'develop' into feat/reporting-reopen 2025-08-11 12:42:03 +05:30
Shivam MishraandGitHub f3eb7fb283 Merge branch 'develop' into feat/reporting-reopen 2025-08-08 19:04:38 +05:30
Shivam Mishra 2f37b4979f test: fix timestamp precision 2025-08-08 18:11:17 +05:30
Shivam MishraandGitHub a580dfe1e2 Merge branch 'develop' into feat/reporting-reopen 2025-08-08 09:18:13 +05:30
Shivam MishraandGitHub 2e4e620c6a Merge branch 'develop' into feat/reporting-reopen 2025-08-04 12:14:37 +05:30
Shivam Mishra 9e899040f8 Merge branch 'feat/reporting-reopen' of github.com:chatwoot/chatwoot into feat/reporting-reopen 2025-08-01 09:23:17 +05:30
Shivam Mishra 1e6734fcc5 feat: log all conversation open events 2025-07-31 14:55:04 +05:30
Muhsin KelothandGitHub 27bca5dfe5 Merge branch 'develop' into feat/reporting-reopen 2025-07-29 14:34:19 +04:00
Shivam Mishra 0dfe68b7f8 Merge branch 'feat/reporting-reopen' of github.com:chatwoot/chatwoot into feat/reporting-reopen 2025-07-29 15:05:58 +05:30
Shivam Mishra c10602f551 fix: typo 2025-07-29 15:03:33 +05:30
Shivam MishraandGitHub 6827b0fa0c Merge branch 'develop' into feat/reporting-reopen 2025-07-29 13:30:58 +04:00
Shivam Mishra d8a3e1a4fe fix: first_response time for re-opened conversations 2025-07-29 14:52:12 +05:30
4 changed files with 583 additions and 5 deletions
+43 -5
View File
@@ -18,12 +18,25 @@ module ReportingEventHelper
end
def last_non_human_activity(conversation)
# check if a handoff event already exists
handoff_event = ReportingEvent.where(conversation_id: conversation.id, name: 'conversation_bot_handoff').last
# Try to get either a handoff or reopened event first
# These will always take precedence over any other activity
# Also, any of these events can happen at any time in the course of a conversation lifecycle.
# So we pick the latest event
event = ReportingEvent.where(
conversation_id: conversation.id,
name: %w[conversation_bot_handoff conversation_opened]
).order(event_end_time: :desc).first
# if a handoff exists, last non human activity is when the handoff ended,
# otherwise it's when the conversation was created
handoff_event&.event_end_time || conversation.created_at
return event.event_end_time if event&.event_end_time
# Fallback to bot resolved event
# Because this will be closest to the most accurate activity instead of conversation.created_at
bot_event = ReportingEvent.where(conversation_id: conversation.id, name: 'conversation_bot_resolved').last
return bot_event.event_end_time if bot_event&.event_end_time
# If no events found, return conversation creation time
conversation.created_at
end
private
@@ -51,6 +64,31 @@ module ReportingEventHelper
{ format_time(working_hour.open_hour, working_hour.open_minutes) => format_time(working_hour.close_hour, working_hour.close_minutes) }
end
def assignee_changed_to_assigned?(previous_changes)
return false unless previous_changes['assignee_id']
previous_assignee, current_assignee = previous_changes['assignee_id']
previous_assignee.nil? && current_assignee.present?
end
def assignee_changed_to_unassigned?(previous_changes)
return false unless previous_changes['assignee_id']
previous_assignee, current_assignee = previous_changes['assignee_id']
previous_assignee.present? && current_assignee.nil?
end
def find_last_unassignment_event(conversation)
last_assignment_event = ReportingEvent.where(
conversation_id: conversation.id,
name: 'conversation_assigned'
).order(event_end_time: :desc).first
return last_assignment_event if last_assignment_event&.user_id.nil?
nil
end
def format_time(hour, minute)
hour = hour < 10 ? "0#{hour}" : hour
minute = minute < 10 ? "0#{minute}" : minute
+131
View File
@@ -67,6 +67,23 @@ class ReportingEventListener < BaseListener
reporting_event.save!
end
def conversation_created(event)
conversation = extract_conversation_and_account(event)[0]
reporting_event = ReportingEvent.new(
name: 'conversation_created',
value: 0,
value_in_business_hours: 0,
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
user_id: nil,
conversation_id: conversation.id,
event_start_time: conversation.created_at,
event_end_time: conversation.created_at
)
reporting_event.save!
end
def conversation_bot_handoff(event)
conversation = extract_conversation_and_account(event)[0]
@@ -90,8 +107,122 @@ class ReportingEventListener < BaseListener
reporting_event.save!
end
def conversation_assigned(event)
conversation = extract_conversation_and_account(event)[0]
previous_changes = event.data[:changed_attributes] || {}
return unless previous_changes['assignee_id']
if assignee_changed_to_assigned?(previous_changes)
assignment_time_data = calculate_assignment_time(conversation, previous_changes)
create_assignment_event(conversation, assignment_time_data)
elsif assignee_changed_to_unassigned?(previous_changes)
create_unassignment_event(conversation)
else
# Re-assignment: user to different user
reassignment_data = { value: 0, business_hours_value: 0, start_time: conversation.updated_at }
create_assignment_event(conversation, reassignment_data)
end
end
def conversation_opened(event)
conversation = extract_conversation_and_account(event)[0]
# Find the most recent resolved event for this conversation
last_resolved_event = ReportingEvent.where(
conversation_id: conversation.id,
name: 'conversation_resolved'
).order(event_end_time: :desc).first
# For first-time openings, value is 0
# For reopenings, calculate time since resolution
if last_resolved_event
time_since_resolved = conversation.updated_at.to_i - last_resolved_event.event_end_time.to_i
business_hours_value = business_hours(conversation.inbox, last_resolved_event.event_end_time, conversation.updated_at)
start_time = last_resolved_event.event_end_time
else
time_since_resolved = 0
business_hours_value = 0
start_time = conversation.created_at
end
create_conversation_opened_event(conversation, time_since_resolved, business_hours_value, start_time)
end
private
def create_conversation_opened_event(conversation, time_since_resolved, business_hours_value, start_time)
reporting_event = ReportingEvent.new(
name: 'conversation_opened',
value: time_since_resolved,
value_in_business_hours: business_hours_value,
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
user_id: conversation.assignee_id,
conversation_id: conversation.id,
event_start_time: start_time,
event_end_time: conversation.updated_at
)
reporting_event.save!
end
def calculate_assignment_time(conversation, previous_changes)
previous_assignee = previous_changes['assignee_id'][0]
if previous_assignee.nil?
calculate_time_to_assignment(conversation)
else
# Re-assignment - value is 0
{ value: 0, business_hours_value: 0, start_time: conversation.updated_at }
end
end
def calculate_time_to_assignment(conversation)
last_unassignment = find_last_unassignment_event(conversation)
start_time = if last_unassignment
# Assignment after unassignment
last_unassignment.event_end_time
else
# First assignment
conversation.created_at
end
time_to_assignment = conversation.updated_at.to_i - start_time.to_i
business_hours_value = business_hours(conversation.inbox, start_time, conversation.updated_at)
{ value: time_to_assignment, business_hours_value: business_hours_value, start_time: start_time }
end
def create_assignment_event(conversation, time_data)
reporting_event = ReportingEvent.new(
name: 'conversation_assigned',
value: time_data[:value],
value_in_business_hours: time_data[:business_hours_value],
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
user_id: conversation.assignee_id,
conversation_id: conversation.id,
event_start_time: time_data[:start_time],
event_end_time: conversation.updated_at
)
reporting_event.save!
end
def create_unassignment_event(conversation)
reporting_event = ReportingEvent.new(
name: 'conversation_assigned',
value: 0,
value_in_business_hours: 0,
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
user_id: nil,
conversation_id: conversation.id,
event_start_time: conversation.updated_at,
event_end_time: conversation.updated_at
)
reporting_event.save!
end
def create_bot_resolved_event(conversation, reporting_event)
return unless conversation.inbox.active_bot?
# We don't want to create a bot_resolved event if there is user interaction on the conversation
+177
View File
@@ -0,0 +1,177 @@
require 'rails_helper'
RSpec.describe ReportingEventHelper, type: :helper do
describe '#last_non_human_activity' do
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
let(:user) { create(:user, account: account) }
let(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: user) }
context 'when conversation has no events' do
it 'returns conversation created_at' do
expect(helper.last_non_human_activity(conversation)).to eq(conversation.created_at)
end
end
context 'when conversation has bot handoff event' do
let!(:handoff_event) do
create(:reporting_event,
name: 'conversation_bot_handoff',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
event_end_time: 2.hours.ago)
end
it 'returns handoff event end time' do
expect(helper.last_non_human_activity(conversation).to_i).to eq(handoff_event.event_end_time.to_i)
end
end
context 'when conversation has bot resolved event' do
let!(:bot_resolved_event) do
create(:reporting_event,
name: 'conversation_bot_resolved',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
event_end_time: 3.hours.ago)
end
it 'returns bot resolved event end time' do
expect(helper.last_non_human_activity(conversation).to_i).to eq(bot_resolved_event.event_end_time.to_i)
end
end
context 'when conversation is reopened after bot resolution' do
let(:creation_time) { 5.days.ago }
let(:bot_resolution_time) { 5.days.ago + 5.minutes }
let(:reopening_time) { 1.hour.ago }
let!(:conversation) do
create(:conversation,
account: account,
inbox: inbox,
assignee: user,
created_at: creation_time)
end
before do
# First opened event
create(:reporting_event,
name: 'conversation_opened',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
value: 0,
event_start_time: creation_time,
event_end_time: creation_time)
# Bot resolved event
create(:reporting_event,
name: 'conversation_bot_resolved',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
event_start_time: creation_time,
event_end_time: bot_resolution_time)
# Resolved event
create(:reporting_event,
name: 'conversation_resolved',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
event_start_time: creation_time,
event_end_time: bot_resolution_time)
# Reopened event
create(:reporting_event,
name: 'conversation_opened',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
value: (reopening_time - bot_resolution_time).to_i,
event_start_time: bot_resolution_time,
event_end_time: reopening_time)
end
it 'returns the reopening event time, not the creation time' do
# This is the key test: last_non_human_activity should return the reopening time
# so that first response time is calculated from when the conversation was reopened,
# not from when it was originally created
expect(helper.last_non_human_activity(conversation).to_i).to eq(reopening_time.to_i)
# Verify it's not returning the creation time or bot resolution time
expect(helper.last_non_human_activity(conversation).to_i).not_to eq(creation_time.to_i)
expect(helper.last_non_human_activity(conversation).to_i).not_to eq(bot_resolution_time.to_i)
end
end
context 'when conversation has multiple types of events' do
let(:opened_event_time) { 1.hour.ago }
before do
create(:reporting_event,
name: 'conversation_bot_resolved',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
event_end_time: 4.hours.ago)
create(:reporting_event,
name: 'conversation_bot_handoff',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
event_end_time: 3.hours.ago)
create(:reporting_event,
name: 'conversation_opened',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
event_end_time: opened_event_time)
end
it 'returns the most recent handoff or opened event' do
# opened_event is more recent than handoff_event
expect(helper.last_non_human_activity(conversation).to_i).to eq(opened_event_time.to_i)
end
end
context 'when conversation has multiple reopenings' do
let(:third_opened_time) { 30.minutes.ago }
before do
create(:reporting_event,
name: 'conversation_opened',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
value: 0,
event_end_time: 5.days.ago)
create(:reporting_event,
name: 'conversation_opened',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
value: 3600,
event_end_time: 2.days.ago)
create(:reporting_event,
name: 'conversation_opened',
conversation_id: conversation.id,
account_id: account.id,
inbox_id: inbox.id,
value: 7200,
event_end_time: third_opened_time)
end
it 'returns the most recent opened event' do
expect(helper.last_non_human_activity(conversation).to_i).to eq(third_opened_time.to_i)
end
end
end
end
@@ -10,6 +10,99 @@ describe ReportingEventListener do
account: account, inbox: inbox, conversation: conversation)
end
describe '#conversation_assigned' do
it 'creates conversation_assigned event on first assignment' do
# Create conversation without assignee
test_conversation = create(:conversation, account: account, inbox: inbox, assignee: nil)
expect(account.reporting_events.where(name: 'conversation_assigned').count).to be 0
# Simulate assignment 10 seconds later
travel_to 10.seconds.from_now do
test_conversation.update!(assignee: user)
event = Events::Base.new('conversation.updated', Time.zone.now, conversation: test_conversation,
changed_attributes: { 'assignee_id' => [nil, user.id] })
listener.conversation_assigned(event)
end
expect(account.reporting_events.where(name: 'conversation_assigned').count).to be 1
assigned_event = account.reporting_events.where(name: 'conversation_assigned').first
expect(assigned_event.user_id).to eq user.id
expect(assigned_event.value).to be_within(1).of(10)
end
it 'creates conversation_assigned event with value 0 on re-assignment' do
conversation.update!(assignee: user)
other_user = create(:user, account: account)
# Update conversation to new assignee
conversation.update!(assignee: other_user)
event = Events::Base.new('conversation.updated', Time.zone.now, conversation: conversation,
changed_attributes: { 'assignee_id' => [user.id, other_user.id] })
listener.conversation_assigned(event)
reassigned_event = account.reporting_events.where(name: 'conversation_assigned').first
expect(reassigned_event.user_id).to eq other_user.id
expect(reassigned_event.value).to eq 0
end
it 'creates conversation_assigned event with nil user_id on unassignment' do
conversation.update!(assignee: user)
conversation.update!(assignee: nil)
event = Events::Base.new('conversation.updated', Time.zone.now, conversation: conversation,
changed_attributes: { 'assignee_id' => [user.id, nil] })
listener.conversation_assigned(event)
unassigned_event = account.reporting_events.where(name: 'conversation_assigned').first
expect(unassigned_event.user_id).to be_nil
expect(unassigned_event.value).to eq 0
end
it 'calculates correct time for assignment after unassignment' do
conversation.update!(assignee: user)
# Create unassignment event first
event = Events::Base.new('conversation.updated', Time.zone.now, conversation: conversation,
changed_attributes: { 'assignee_id' => [user.id, nil] })
listener.conversation_assigned(event)
travel_to 5.minutes.from_now do
conversation.update!(assignee: user, updated_at: Time.zone.now)
event = Events::Base.new('conversation.updated', Time.zone.now, conversation: conversation,
changed_attributes: { 'assignee_id' => [nil, user.id] })
listener.conversation_assigned(event)
reassigned_event = account.reporting_events.where(name: 'conversation_assigned').last
expect(reassigned_event.user_id).to eq user.id
expect(reassigned_event.value).to be_within(5).of(300) # 5 minutes
end
end
end
describe '#conversation_created' do
it 'creates conversation_created event' do
expect(account.reporting_events.where(name: 'conversation_created').count).to be 0
event = Events::Base.new('conversation.created', Time.zone.now, conversation: conversation)
listener.conversation_created(event)
expect(account.reporting_events.where(name: 'conversation_created').count).to be 1
created_event = account.reporting_events.where(name: 'conversation_created').first
expect(created_event.value).to eq 0
expect(created_event.conversation_id).to eq conversation.id
end
it 'creates conversation_created event when conversation is created' do
# ensure gravatar job does not send a request
stub_request(:get, /gravatar\.com/).to_return(status: 404, body: '', headers: {})
expect do
perform_enqueued_jobs do
create(:conversation, account: account, inbox: inbox)
end
end.to change { account.reporting_events.where(name: 'conversation_created').count }.by(1)
end
end
describe '#conversation_resolved' do
it 'creates conversation_resolved event' do
expect(account.reporting_events.where(name: 'conversation_resolved').count).to be 0
@@ -267,4 +360,143 @@ describe ReportingEventListener do
end
end
end
describe '#conversation_opened' do
context 'when conversation is opened for the first time' do
let(:new_conversation) { create(:conversation, account: account, inbox: inbox, assignee: user) }
it 'creates conversation_opened event with value 0' do
expect(account.reporting_events.where(name: 'conversation_opened').count).to be 0
event = Events::Base.new('conversation.opened', Time.zone.now, conversation: new_conversation)
listener.conversation_opened(event)
expect(account.reporting_events.where(name: 'conversation_opened').count).to be 1
opened_event = account.reporting_events.where(name: 'conversation_opened').first
expect(opened_event.value).to eq 0
expect(opened_event.value_in_business_hours).to eq 0
expect(opened_event.event_start_time).to be_within(1.second).of(new_conversation.created_at)
expect(opened_event.event_end_time).to be_within(1.second).of(new_conversation.updated_at)
end
end
context 'when conversation is reopened after being resolved' do
let(:resolved_time) { 2.hours.ago }
let(:reopened_time) { 1.hour.ago }
let(:reopened_conversation) do
create(:conversation, account: account, inbox: inbox, assignee: user, updated_at: reopened_time)
end
before do
# Create a resolved event first
create(:reporting_event,
name: 'conversation_resolved',
account_id: account.id,
inbox_id: inbox.id,
conversation_id: reopened_conversation.id,
user_id: user.id,
value: 3600,
event_start_time: reopened_conversation.created_at,
event_end_time: resolved_time)
end
it 'creates conversation_opened event' do
expect(account.reporting_events.where(name: 'conversation_opened').count).to be 0
event = Events::Base.new('conversation.opened', reopened_time, conversation: reopened_conversation)
listener.conversation_opened(event)
expect(account.reporting_events.where(name: 'conversation_opened').count).to be 1
end
it 'calculates correct time since resolution' do
event = Events::Base.new('conversation.opened', reopened_time, conversation: reopened_conversation)
listener.conversation_opened(event)
reopened_event = account.reporting_events.where(name: 'conversation_opened').first
expect(reopened_event.value).to be_within(1).of(3600) # 1 hour = 3600 seconds
expect(reopened_event.event_start_time).to be_within(1.second).of(resolved_time)
expect(reopened_event.event_end_time).to be_within(1.second).of(reopened_time)
end
it 'sets correct attributes for conversation_opened event' do
event = Events::Base.new('conversation.opened', reopened_time, conversation: reopened_conversation)
listener.conversation_opened(event)
reopened_event = account.reporting_events.where(name: 'conversation_opened').first
expect(reopened_event.account_id).to eq(account.id)
expect(reopened_event.inbox_id).to eq(inbox.id)
expect(reopened_event.conversation_id).to eq(reopened_conversation.id)
expect(reopened_event.user_id).to eq(user.id)
end
context 'when business hours enabled for inbox' do
let(:resolved_time) { Time.zone.parse('March 20, 2022 12:00') }
let(:reopened_time) { Time.zone.parse('March 21, 2022 14:00') }
let!(:business_hours_inbox) { create(:inbox, working_hours_enabled: true, account: account) }
let!(:business_hours_conversation) do
create(:conversation, account: account, inbox: business_hours_inbox, assignee: user, updated_at: reopened_time)
end
before do
create(:reporting_event,
name: 'conversation_resolved',
account_id: account.id,
inbox_id: business_hours_inbox.id,
conversation_id: business_hours_conversation.id,
user_id: user.id,
value: 3600,
event_start_time: business_hours_conversation.created_at,
event_end_time: resolved_time)
end
it 'creates conversation_opened event with business hour value' do
event = Events::Base.new('conversation.opened', reopened_time, conversation: business_hours_conversation)
listener.conversation_opened(event)
reopened_event = account.reporting_events.where(name: 'conversation_opened').first
expect(reopened_event.value_in_business_hours).to be 18_000.0 # 5 business hours (26 hours total - 21 non-business hours)
end
end
end
context 'when conversation has multiple resolutions' do
let(:first_resolved_time) { 3.hours.ago }
let(:second_resolved_time) { 1.hour.ago }
let(:reopened_time) { 30.minutes.ago }
let(:multiple_resolution_conversation) do
create(:conversation, account: account, inbox: inbox, assignee: user, updated_at: reopened_time)
end
before do
# Create first resolved event
create(:reporting_event,
name: 'conversation_resolved',
account_id: account.id,
inbox_id: inbox.id,
conversation_id: multiple_resolution_conversation.id,
user_id: user.id,
value: 3600,
event_start_time: multiple_resolution_conversation.created_at,
event_end_time: first_resolved_time)
# Create second resolved event (more recent)
create(:reporting_event,
name: 'conversation_resolved',
account_id: account.id,
inbox_id: inbox.id,
conversation_id: multiple_resolution_conversation.id,
user_id: user.id,
value: 1800,
event_start_time: first_resolved_time,
event_end_time: second_resolved_time)
end
it 'uses the most recent resolved event for calculation' do
event = Events::Base.new('conversation.opened', reopened_time, conversation: multiple_resolution_conversation)
listener.conversation_opened(event)
reopened_event = account.reporting_events.where(name: 'conversation_opened').first
expect(reopened_event.value).to be_within(1).of(1800) # 30 minutes from second resolution
expect(reopened_event.event_start_time).to be_within(1.second).of(second_resolved_time)
end
end
end
end