feat(reports): filter grouped live metrics by team

This commit is contained in:
2026-06-07 01:01:27 +08:00
parent 64d5e2420e
commit 18f6403128
5 changed files with 69 additions and 22 deletions
+21 -9
View File
@@ -31,14 +31,9 @@ func (h *LiveReportHandler) ConversationMetrics(c *gin.Context) {
return
}
teamID := uint(0)
if teamIDStr := c.Query("team_id"); teamIDStr != "" {
parsed, err := strconv.ParseUint(teamIDStr, 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid team_id")
return
}
teamID = uint(parsed)
teamID, ok := parseLiveReportTeamID(c)
if !ok {
return
}
result, err := h.svc.GetConversationMetricsForTeam(c.Request.Context(), accountID, teamID)
@@ -68,8 +63,12 @@ func (h *LiveReportHandler) GroupedConversationMetrics(c *gin.Context) {
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid group_by"})
return
}
teamID, ok := parseLiveReportTeamID(c)
if !ok {
return
}
result, err := h.svc.GetGroupedConversationMetrics(c.Request.Context(), accountID, groupBy)
result, err := h.svc.GetGroupedConversationMetricsForTeam(c.Request.Context(), accountID, groupBy, teamID)
if err != nil {
applogger.L().Errorf("Grouped conversation metrics: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to get grouped conversation metrics")
@@ -78,3 +77,16 @@ func (h *LiveReportHandler) GroupedConversationMetrics(c *gin.Context) {
c.JSON(http.StatusOK, result)
}
func parseLiveReportTeamID(c *gin.Context) (uint, bool) {
teamIDStr := c.Query("team_id")
if teamIDStr == "" {
return 0, true
}
parsed, err := strconv.ParseUint(teamIDStr, 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid team_id")
return 0, false
}
return uint(parsed), true
}
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gin-gonic/gin"
@@ -25,7 +26,7 @@ type LiveReportHandlerTestSuite struct {
func (s *LiveReportHandlerTestSuite) SetupSuite() {
s.db, _ = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
s.db.AutoMigrate(&model.Account{}, &model.Conversation{}, &model.ReportingEventsRollup{})
s.db.AutoMigrate(&model.Account{}, &model.Team{}, &model.Conversation{}, &model.ReportingEventsRollup{})
anSvc := service.NewAnalyticsService(
repository.NewReportingEventRepo(s.db),
@@ -42,6 +43,7 @@ func (s *LiveReportHandlerTestSuite) SetupSuite() {
func (s *LiveReportHandlerTestSuite) SetupTest() {
s.db.Exec("DELETE FROM conversations")
s.db.Exec("DELETE FROM teams")
}
func TestLiveReportHandlerTestSuite(t *testing.T) {
@@ -106,3 +108,27 @@ func (s *LiveReportHandlerTestSuite) TestGroupedConversationMetrics_ByAssignee()
s.Equal(float64(1), body[1]["unattended"])
s.Equal(float64(0), body[1]["unassigned"])
}
func (s *LiveReportHandlerTestSuite) TestGroupedConversationMetrics_FiltersByTeamID() {
team := model.Team{AccountID: 1, Name: "Support"}
otherTeam := model.Team{AccountID: 1, Name: "Other"}
s.Require().NoError(s.db.Create(&team).Error)
s.Require().NoError(s.db.Create(&otherTeam).Error)
agentID := uint(7)
teamConversation := model.Conversation{AccountID: 1, TeamID: &team.ID, AssigneeID: &agentID, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget"}
otherConversation := model.Conversation{AccountID: 1, TeamID: &otherTeam.ID, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget"}
s.Require().NoError(s.db.Create(&teamConversation).Error)
s.Require().NoError(s.db.Create(&otherConversation).Error)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/live_reports/grouped_conversation_metrics?group_by=assignee_id&team_id="+strconv.FormatUint(uint64(team.ID), 10), nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
var body []map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
s.Len(body, 1)
s.Equal(float64(agentID), body[0]["assignee_id"])
s.Equal(float64(1), body[0]["open"])
s.Equal(float64(1), body[0]["unattended"])
s.Equal(float64(0), body[0]["unassigned"])
}