feat: improve captain overview loading and reuse stats for summary [CW-7610] (#15105)

This commit is contained in:
Shivam Mishra
2026-07-21 15:14:18 +05:30
committed by GitHub
parent 920a98ccf4
commit 7a5385cc32
8 changed files with 112 additions and 24 deletions
@@ -257,10 +257,20 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
let(:alice) { create(:user, account: account, role: :administrator, name: 'Alice Adams') }
let(:bob) { create(:user, account: account, role: :administrator, name: 'Bob Brown') }
let(:summary_service) { instance_double(Captain::OverviewSummaryService) }
let(:summary_stats) do
{
conversations_handled: { current: 42 },
hours_saved: { current: 12 },
auto_resolution_rate: { current: 65.0, trend: 5.0 },
handoff_rate: { current: 20.0, trend: -2.0 },
reopen_rate: { current: 5.0, trend: -1.0 },
knowledge: { coverage: 80, approved: 8, documents: 3 }
}
end
def get_summary(user)
get "/api/v1/accounts/#{account.id}/captain/assistants/#{assistant.id}/summary",
params: { range: '30' },
params: { range: '30', stats: summary_stats },
headers: user.create_new_auth_token,
as: :json
end
@@ -273,6 +283,7 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
it 'caches the summary per viewer so one user never receives another user\'s greeting' do
allow(summary_service).to receive(:perform).and_return({ message: 'Hi Alice' })
expect(Captain::AssistantStatsBuilder).not_to receive(:new)
get_summary(alice)
get_summary(alice) # served from Alice's cache, no regeneration
@@ -280,6 +291,7 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
expect(response).to have_http_status(:success)
expect(Captain::OverviewSummaryService).to have_received(:new).twice
expect(Captain::OverviewSummaryService).to have_received(:new).with(hash_including(stats: summary_stats)).twice
end
it 'does not cache failures so a transient error is retried' do