feat(conversations): align meta counts

This commit is contained in:
2026-06-07 02:54:41 +08:00
parent 8591fe1d33
commit cdc0978476
7 changed files with 273 additions and 95 deletions
@@ -635,13 +635,25 @@ func (h *ConversationHandler) Meta(c *gin.Context) {
return
}
meta, svcErr := h.conversationSvc.GetMeta(c.Request.Context(), accountID)
var params service.FilterParams
if err := c.ShouldBindQuery(&params); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
if params.Labels == "" {
labels := append(c.QueryArray("labels"), c.QueryArray("labels[]")...)
if len(labels) > 0 {
params.Labels = strings.Join(labels, ",")
}
}
meta, svcErr := h.conversationSvc.GetMeta(c.Request.Context(), accountID, currentUserID(c), params)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, meta)
c.JSON(http.StatusOK, gin.H{"meta": meta})
}
// Unread marks a conversation as unread by resetting agent_last_seen_at.
@@ -158,17 +158,20 @@ func (s *ConversationHandlerTestSuite) TearDownTest() {
s.db.Exec("DELETE FROM contact_inboxes")
s.db.Exec("DELETE FROM contacts")
s.db.Exec("DELETE FROM inbox_members")
s.db.Exec("DELETE FROM account_users")
s.db.Exec("DELETE FROM inboxes")
s.db.Exec("DELETE FROM messages")
s.db.Exec("DELETE FROM accounts")
// Re-seed base data
account := &model.Account{Name: "ConvHandlerTestOrg", Locale: "en", Active: true}
account := &model.Account{Name: "ConvHandlerTestOrg", Locale: "en", Active: true, FeatureFlags: `{"conversation_unread_counts":true}`}
s.Require().NoError(s.db.Create(account).Error)
s.testAccount = account
s.Require().NoError(s.db.Create(&model.AccountUser{AccountID: account.ID, UserID: s.testUser.ID, Role: "administrator"}).Error)
inbox := &model.Inbox{AccountID: account.ID, Name: "ConvHandlerTestInbox", ChannelType: "web_widget", ChannelID: 1}
s.Require().NoError(s.db.Create(inbox).Error)
s.Require().NoError(s.db.Create(&model.InboxMember{InboxID: inbox.ID, UserID: s.testUser.ID}).Error)
contact := &model.Contact{AccountID: account.ID, Name: "ConvHandlerTestContact"}
s.Require().NoError(s.db.Create(contact).Error)
@@ -193,17 +196,44 @@ func (s *ConversationHandlerTestSuite) TestMeta_Success() {
assert.Equal(s.T(), http.StatusOK, w.Code)
var resp struct {
Success bool `json:"success"`
Data struct {
TotalCount int64 `json:"total_count"`
StatusCounts map[string]int64 `json:"status_counts"`
LabelCounts map[string]int64 `json:"label_counts"`
} `json:"data"`
Meta struct {
MineCount int64 `json:"mine_count"`
AssignedCount int64 `json:"assigned_count"`
UnassignedCount int64 `json:"unassigned_count"`
AllCount int64 `json:"all_count"`
} `json:"meta"`
}
err := json.Unmarshal(w.Body.Bytes(), &resp)
assert.NoError(s.T(), err)
assert.True(s.T(), resp.Success)
assert.Equal(s.T(), int64(1), resp.Data.TotalCount)
assert.NotContains(s.T(), w.Body.String(), "success")
assert.Equal(s.T(), int64(1), resp.Meta.AllCount)
assert.Equal(s.T(), int64(1), resp.Meta.UnassignedCount)
}
func (s *ConversationHandlerTestSuite) TestMeta_FiltersStatusAndIgnoresAssigneeTypeForCounts() {
assigned := &model.Conversation{AccountID: s.testAccount.ID, InboxID: s.testConv.InboxID, ContactID: s.testConv.ContactID, AssigneeID: &s.testUser.ID, Status: "open", ChannelType: "web_widget", Channel: "web_widget"}
s.Require().NoError(s.db.Create(assigned).Error)
resolved := &model.Conversation{AccountID: s.testAccount.ID, InboxID: s.testConv.InboxID, ContactID: s.testConv.ContactID, AssigneeID: &s.testUser.ID, Status: "resolved", ChannelType: "web_widget", Channel: "web_widget"}
s.Require().NoError(s.db.Create(resolved).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", s.accountURL()+"/conversations/meta?assignee_type=assigned", nil)
s.router.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusOK, w.Code)
var resp struct {
Meta struct {
MineCount int64 `json:"mine_count"`
AssignedCount int64 `json:"assigned_count"`
UnassignedCount int64 `json:"unassigned_count"`
AllCount int64 `json:"all_count"`
} `json:"meta"`
}
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
assert.Equal(s.T(), int64(1), resp.Meta.MineCount)
assert.Equal(s.T(), int64(1), resp.Meta.AssignedCount)
assert.Equal(s.T(), int64(1), resp.Meta.UnassignedCount)
assert.Equal(s.T(), int64(2), resp.Meta.AllCount)
}
func (s *ConversationHandlerTestSuite) TestMeta_InvalidAccountID() {