feat(conversations): align meta query status
This commit is contained in:
@@ -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