feat(conversations): align search message order
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -898,7 +899,10 @@ func (s *ConversationCrudTestSuite) TestUpdateLabels_NotFound() {
|
||||
// ========== Search Handler Tests ==========
|
||||
|
||||
func (s *ConversationCrudTestSuite) TestSearch_Success() {
|
||||
s.Require().NoError(s.db.Create(&model.Message{AccountID: s.testAccount.ID, InboxID: s.testInbox.ID, ConversationID: s.testConv.ID, SenderID: &s.testContact.ID, SenderType: string(model.SenderTypeContact), Content: "needle open", MessageType: string(model.MessageTypeIncoming)}).Error)
|
||||
newer := time.Date(2026, 6, 7, 12, 0, 0, 0, time.UTC)
|
||||
older := newer.Add(-time.Hour)
|
||||
s.Require().NoError(s.db.Create(&model.Message{Base: model.Base{CreatedAt: newer}, AccountID: s.testAccount.ID, InboxID: s.testInbox.ID, ConversationID: s.testConv.ID, SenderID: &s.testContact.ID, SenderType: string(model.SenderTypeContact), Content: "needle open", MessageType: string(model.MessageTypeIncoming)}).Error)
|
||||
s.Require().NoError(s.db.Create(&model.Message{Base: model.Base{CreatedAt: older}, AccountID: s.testAccount.ID, InboxID: s.testInbox.ID, ConversationID: s.testConv.ID, SenderID: &s.testContact.ID, SenderType: string(model.SenderTypeContact), Content: "older context", MessageType: string(model.MessageTypeIncoming)}).Error)
|
||||
agent := &model.User{AccountID: s.testAccount.ID, Name: "Search Agent", Email: "search-agent-" + strconv.FormatUint(uint64(s.testAccount.ID), 10) + "@example.com", Password: "secret", Role: "agent"}
|
||||
s.Require().NoError(s.db.Create(agent).Error)
|
||||
resolved := &model.Conversation{AccountID: s.testAccount.ID, InboxID: s.testInbox.ID, ContactID: s.testContact.ID, Status: "resolved", ChannelType: "web_widget", Channel: "web_widget"}
|
||||
@@ -942,15 +946,22 @@ func (s *ConversationCrudTestSuite) TestSearch_Success() {
|
||||
assert.Equal(s.T(), s.testContact.Name, resp.Payload[0].Contact["name"])
|
||||
assert.Equal(s.T(), s.testInbox.Name, resp.Payload[0].Inbox["name"])
|
||||
assert.NotEmpty(s.T(), resp.Payload[0].Messages)
|
||||
assert.Equal(s.T(), float64(0), resp.Payload[0].Messages[0]["message_type"])
|
||||
senderNamesByContent := map[string]string{}
|
||||
messageTypesByContent := map[string]float64{}
|
||||
contentsByConversation := map[uint][]string{}
|
||||
for _, conversation := range resp.Payload {
|
||||
for _, message := range conversation.Messages {
|
||||
content := message["content"].(string)
|
||||
contentsByConversation[conversation.ID] = append(contentsByConversation[conversation.ID], content)
|
||||
messageTypesByContent[content] = message["message_type"].(float64)
|
||||
if senderName, ok := message["sender_name"].(string); ok {
|
||||
senderNamesByContent[message["content"].(string)] = senderName
|
||||
senderNamesByContent[content] = senderName
|
||||
}
|
||||
}
|
||||
}
|
||||
assert.Equal(s.T(), []string{"older context", "needle open"}, contentsByConversation[s.testConv.ID])
|
||||
assert.Equal(s.T(), float64(0), messageTypesByContent["needle open"])
|
||||
assert.Equal(s.T(), float64(1), messageTypesByContent["needle resolved"])
|
||||
assert.Equal(s.T(), s.testContact.Name, senderNamesByContent["needle open"])
|
||||
assert.Equal(s.T(), agent.Name, senderNamesByContent["needle resolved"])
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ func serializeConversationSearch(ctx context.Context, db *gorm.DB, conversation
|
||||
var messages []model.Message
|
||||
if err := db.WithContext(ctx).
|
||||
Where("account_id = ? AND conversation_id = ?", conversation.AccountID, conversation.ID).
|
||||
Order("id ASC").
|
||||
Order("created_at ASC, id ASC").
|
||||
Find(&messages).Error; err == nil {
|
||||
payload.Messages = make([]chatwootConversationSearchMessage, 0, len(messages))
|
||||
for i := range messages {
|
||||
|
||||
Reference in New Issue
Block a user