feat(reporting): add conversation_created event to ReportingEventListener

Add a new reporting event that fires when a conversation is created.
This event serves as a denominator for calculating rates like deflection
rate and escalation rate directly from the reporting_events table.
This commit is contained in:
Shivam Mishra
2026-01-21 17:40:04 +05:30
parent f84e95ed6c
commit fb81a4fc89
2 changed files with 50 additions and 0 deletions
+15
View File
@@ -1,6 +1,21 @@
class ReportingEventListener < BaseListener
include ReportingEventHelper
def conversation_created(event)
conversation = extract_conversation_and_account(event)[0]
ReportingEvent.create!(
name: 'conversation_created',
value: 0,
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
user_id: conversation.assignee_id,
conversation_id: conversation.id,
event_start_time: conversation.created_at,
event_end_time: conversation.created_at
)
end
def conversation_resolved(event)
conversation = extract_conversation_and_account(event)[0]
time_to_resolve = conversation.updated_at.to_i - conversation.created_at.to_i
@@ -10,6 +10,41 @@ describe ReportingEventListener do
account: account, inbox: inbox, conversation: conversation)
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
end
it 'sets correct attributes for conversation_created event' do
event = Events::Base.new('conversation.created', Time.zone.now, conversation: conversation)
listener.conversation_created(event)
created_event = account.reporting_events.find_by(name: 'conversation_created')
expect(created_event.value).to eq 0
expect(created_event.account_id).to eq(account.id)
expect(created_event.inbox_id).to eq(inbox.id)
expect(created_event.conversation_id).to eq(conversation.id)
expect(created_event.user_id).to eq(user.id)
expect(created_event.event_start_time).to be_within(1.second).of(conversation.created_at)
expect(created_event.event_end_time).to be_within(1.second).of(conversation.created_at)
end
context 'when conversation has no assignee' do
let(:unassigned_conversation) { create(:conversation, account: account, inbox: inbox, assignee: nil) }
it 'creates conversation_created event with nil user_id' do
event = Events::Base.new('conversation.created', Time.zone.now, conversation: unassigned_conversation)
listener.conversation_created(event)
created_event = account.reporting_events.find_by(name: 'conversation_created', conversation_id: unassigned_conversation.id)
expect(created_event.user_id).to be_nil
end
end
end
describe '#conversation_resolved' do
it 'creates conversation_resolved event' do
expect(account.reporting_events.where(name: 'conversation_resolved').count).to be 0