feat(reports): align live grouped errors
This commit is contained in:
@@ -64,8 +64,8 @@ func (h *LiveReportHandler) GroupedConversationMetrics(c *gin.Context) {
|
||||
|
||||
groupBy := c.Query("group_by")
|
||||
if groupBy != "team_id" && groupBy != "assignee_id" {
|
||||
// Reference: Chatwoot returns 422 for invalid group_by
|
||||
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, response.ErrBadRequest, "invalid group_by, must be team_id or assignee_id")
|
||||
// Reference: Chatwoot returns 422 with a raw { error } body for invalid group_by.
|
||||
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid group_by"})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -36,9 +36,14 @@ func (s *LiveReportHandlerTestSuite) SetupSuite() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/live_reports/conversation_metrics", s.handler.ConversationMetrics)
|
||||
r.GET("/api/v1/accounts/:account_id/live_reports/grouped_conversation_metrics", s.handler.GroupedConversationMetrics)
|
||||
s.router = r
|
||||
}
|
||||
|
||||
func (s *LiveReportHandlerTestSuite) SetupTest() {
|
||||
s.db.Exec("DELETE FROM conversations")
|
||||
}
|
||||
|
||||
func TestLiveReportHandlerTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(LiveReportHandlerTestSuite))
|
||||
}
|
||||
@@ -63,3 +68,41 @@ func (s *LiveReportHandlerTestSuite) TestConversationMetrics_Success() {
|
||||
s.Equal(float64(0), body["unassigned"])
|
||||
s.Equal(float64(0), body["pending"])
|
||||
}
|
||||
|
||||
func (s *LiveReportHandlerTestSuite) TestGroupedConversationMetrics_InvalidGroupByReturnsChatwootError() {
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/live_reports/grouped_conversation_metrics?group_by=invalid_param", nil)
|
||||
s.router.ServeHTTP(w, req)
|
||||
s.Equal(http.StatusUnprocessableEntity, w.Code)
|
||||
var body map[string]interface{}
|
||||
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
|
||||
s.Equal("invalid group_by", body["error"])
|
||||
s.NotContains(body, "success")
|
||||
}
|
||||
|
||||
func (s *LiveReportHandlerTestSuite) TestGroupedConversationMetrics_ByAssignee() {
|
||||
agentID := uint(42)
|
||||
firstReply := int64(1760000000)
|
||||
assigned := model.Conversation{AccountID: 1, AssigneeID: &agentID, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget"}
|
||||
replied := model.Conversation{AccountID: 1, AssigneeID: &agentID, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget", FirstReplyCreatedAt: &firstReply}
|
||||
unassigned := model.Conversation{AccountID: 1, Status: string(model.ConversationStatusOpen), ChannelType: "web_widget", Channel: "web_widget"}
|
||||
s.Require().NoError(s.db.Create(&assigned).Error)
|
||||
s.Require().NoError(s.db.Create(&replied).Error)
|
||||
s.Require().NoError(s.db.Create(&unassigned).Error)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/live_reports/grouped_conversation_metrics?group_by=assignee_id", 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, 2)
|
||||
s.Nil(body[0]["assignee_id"])
|
||||
s.Equal(float64(1), body[0]["open"])
|
||||
s.Equal(float64(1), body[0]["unattended"])
|
||||
s.Equal(float64(1), body[0]["unassigned"])
|
||||
s.Equal(float64(agentID), body[1]["assignee_id"])
|
||||
s.Equal(float64(2), body[1]["open"])
|
||||
s.Equal(float64(1), body[1]["unattended"])
|
||||
s.Equal(float64(0), body[1]["unassigned"])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user