feat(conversations): align meta query status
This commit is contained in:
@@ -261,6 +261,26 @@ func (s *ConversationHandlerTestSuite) TestMeta_QueryAndSourceIDFilters() {
|
||||
assert.Equal(s.T(), int64(1), resp.Meta.MineCount)
|
||||
}
|
||||
|
||||
func (s *ConversationHandlerTestSuite) TestMeta_QuerySkipsStatusFilter() {
|
||||
resolved := &model.Conversation{AccountID: s.testAccount.ID, InboxID: s.testConv.InboxID, ContactID: s.testConv.ContactID, Status: "resolved", ChannelType: "web_widget", Channel: "web_widget"}
|
||||
s.Require().NoError(s.db.Create(resolved).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Message{AccountID: s.testAccount.ID, InboxID: s.testConv.InboxID, ConversationID: s.testConv.ID, Content: "needle open", MessageType: string(model.MessageTypeIncoming)}).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Message{AccountID: s.testAccount.ID, InboxID: s.testConv.InboxID, ConversationID: resolved.ID, Content: "needle resolved", MessageType: string(model.MessageTypeOutgoing)}).Error)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", s.accountURL()+"/conversations/meta?q=needle&status=open", nil)
|
||||
s.router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var resp struct {
|
||||
Meta struct {
|
||||
AllCount int64 `json:"all_count"`
|
||||
} `json:"meta"`
|
||||
}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
assert.Equal(s.T(), int64(2), resp.Meta.AllCount)
|
||||
}
|
||||
|
||||
func (s *ConversationHandlerTestSuite) TestMeta_InvalidAccountID() {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/invalid/conversations/meta", nil)
|
||||
|
||||
@@ -405,6 +405,7 @@ type ConversationMetaParams struct {
|
||||
|
||||
func (r *ConversationRepo) conversationMetaBaseQuery(ctx context.Context, accountID, userID uint, params ConversationMetaParams) *gorm.DB {
|
||||
query := r.db.WithContext(ctx).Model(&model.Conversation{}).Where("conversations.account_id = ?", accountID)
|
||||
trimmedQuery := strings.TrimSpace(params.Query)
|
||||
|
||||
if params.RestrictToInboxes {
|
||||
if len(params.RestrictedInboxIDs) == 0 {
|
||||
@@ -414,7 +415,9 @@ func (r *ConversationRepo) conversationMetaBaseQuery(ctx context.Context, accoun
|
||||
}
|
||||
}
|
||||
|
||||
if params.Status == "all" {
|
||||
if trimmedQuery != "" {
|
||||
// Chatwoot ConversationFinder#set_up skips filter_by_status whenever q is present.
|
||||
} else if params.Status == "all" {
|
||||
// Chatwoot skips status filtering for status=all.
|
||||
} else if params.Status == "" {
|
||||
query = query.Where("conversations.status = ?", model.ConversationStatusOpen)
|
||||
@@ -448,8 +451,8 @@ func (r *ConversationRepo) conversationMetaBaseQuery(ctx context.Context, accoun
|
||||
query = query.Where(r.db.Where(strings.Join(conditions, " OR "), args...).Or("conversations.id IN (?)", tagSubquery))
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(params.Query) != "" {
|
||||
searchTerm := "%" + strings.TrimSpace(params.Query) + "%"
|
||||
if trimmedQuery != "" {
|
||||
searchTerm := "%" + trimmedQuery + "%"
|
||||
query = query.Joins("JOIN messages ON messages.conversation_id = conversations.id").
|
||||
Where("messages.content LIKE ?", searchTerm).
|
||||
Where("messages.message_type IN ?", []string{string(model.MessageTypeIncoming), string(model.MessageTypeOutgoing)})
|
||||
|
||||
@@ -556,6 +556,33 @@ func TestConversationRepo_GetMeta_FiltersQueryAndSourceID(t *testing.T) {
|
||||
assert.Equal(t, int64(1), meta.MineCount)
|
||||
}
|
||||
|
||||
func TestConversationRepo_GetMeta_QuerySkipsStatusFilter(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
repo := NewConversationRepo(db)
|
||||
|
||||
account := &model.Account{Name: "MetaQueryStatusOrg", Locale: "en", Active: true}
|
||||
require.NoError(t, db.Create(account).Error)
|
||||
inbox := &model.Inbox{AccountID: account.ID, Name: "MetaQueryStatusInbox", ChannelType: "web_widget", ChannelID: 1}
|
||||
require.NoError(t, db.Create(inbox).Error)
|
||||
contact := &model.Contact{AccountID: account.ID, Name: "MetaQueryStatusContact"}
|
||||
require.NoError(t, db.Create(contact).Error)
|
||||
|
||||
openConv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "open")
|
||||
resolvedConv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "resolved")
|
||||
pendingConv := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "pending")
|
||||
closedNoMatch := createTestConversation(t, db, account.ID, inbox.ID, contact.ID, "resolved")
|
||||
|
||||
require.NoError(t, db.Create(&model.Message{AccountID: account.ID, InboxID: inbox.ID, ConversationID: openConv.ID, Content: "needle open", MessageType: string(model.MessageTypeIncoming)}).Error)
|
||||
require.NoError(t, db.Create(&model.Message{AccountID: account.ID, InboxID: inbox.ID, ConversationID: resolvedConv.ID, Content: "needle resolved", MessageType: string(model.MessageTypeOutgoing)}).Error)
|
||||
require.NoError(t, db.Create(&model.Message{AccountID: account.ID, InboxID: inbox.ID, ConversationID: pendingConv.ID, Content: "needle pending", MessageType: string(model.MessageTypeIncoming)}).Error)
|
||||
require.NoError(t, db.Create(&model.Message{AccountID: account.ID, InboxID: inbox.ID, ConversationID: closedNoMatch.ID, Content: "other resolved", MessageType: string(model.MessageTypeIncoming)}).Error)
|
||||
|
||||
meta, err := repo.GetMeta(context.Background(), account.ID, 0, ConversationMetaParams{Query: "needle", Status: "open"})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(3), meta.AllCount)
|
||||
assert.Equal(t, int64(3), meta.UnassignedCount)
|
||||
}
|
||||
|
||||
// ========== MarkUnread Tests ==========
|
||||
|
||||
func TestConversationRepo_MarkUnread(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user