feat(conversations): align search sender names

This commit is contained in:
2026-06-07 03:51:32 +08:00
parent f4ad2fb3c6
commit 41bdec333b
3 changed files with 40 additions and 13 deletions
@@ -181,6 +181,7 @@ func (s *ConversationCrudTestSuite) TearDownTest() {
s.db.Exec("DELETE FROM inboxes")
s.db.Exec("DELETE FROM messages")
s.db.Exec("DELETE FROM account_users")
s.db.Exec("DELETE FROM users")
s.db.Exec("DELETE FROM accounts")
s.db.Exec("DELETE FROM teams")
s.db.Exec("DELETE FROM team_members")
@@ -897,10 +898,12 @@ 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, Content: "needle open", MessageType: string(model.MessageTypeIncoming)}).Error)
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)
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"}
s.Require().NoError(s.db.Create(resolved).Error)
s.Require().NoError(s.db.Create(&model.Message{AccountID: s.testAccount.ID, InboxID: s.testInbox.ID, ConversationID: resolved.ID, Content: "needle resolved", MessageType: string(model.MessageTypeOutgoing)}).Error)
s.Require().NoError(s.db.Create(&model.Message{AccountID: s.testAccount.ID, InboxID: s.testInbox.ID, ConversationID: resolved.ID, SenderID: &agent.ID, SenderType: string(model.SenderTypeUser), Content: "needle resolved", MessageType: string(model.MessageTypeOutgoing)}).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", s.accountURL()+"/conversations/search?q=needle&status=open", nil)
@@ -936,6 +939,16 @@ func (s *ConversationCrudTestSuite) TestSearch_Success() {
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{}
for _, conversation := range resp.Payload {
for _, message := range conversation.Messages {
if senderName, ok := message["sender_name"].(string); ok {
senderNamesByContent[message["content"].(string)] = senderName
}
}
}
assert.Equal(s.T(), s.testContact.Name, senderNamesByContent["needle open"])
assert.Equal(s.T(), agent.Name, senderNamesByContent["needle resolved"])
}
func (s *ConversationCrudTestSuite) TestSearch_InvalidAccountID() {
@@ -91,11 +91,11 @@ type chatwootConversationSearchPayload struct {
}
type chatwootConversationSearchMessage struct {
Content string `json:"content"`
ID uint `json:"id"`
SenderName string `json:"sender_name,omitempty"`
MessageType int `json:"message_type"`
CreatedAt int64 `json:"created_at"`
Content string `json:"content"`
ID uint `json:"id"`
SenderName *string `json:"sender_name,omitempty"`
MessageType int `json:"message_type"`
CreatedAt int64 `json:"created_at"`
}
type chatwootMessageIndexResponse struct {
@@ -189,6 +189,7 @@ func serializeConversationSearch(ctx context.Context, db *gorm.DB, conversation
payload.Messages = append(payload.Messages, chatwootConversationSearchMessage{
Content: messages[i].Content,
ID: messages[i].ID,
SenderName: messageSenderName(ctx, db, &messages[i]),
MessageType: messageTypeValue(messages[i].MessageType),
CreatedAt: messages[i].CreatedAt.Unix(),
})
@@ -463,6 +464,15 @@ func serializeMessageSender(ctx context.Context, db *gorm.DB, message *model.Mes
return nil
}
func messageSenderName(ctx context.Context, db *gorm.DB, message *model.Message) *string {
sender := serializeMessageSender(ctx, db, message)
if sender == nil {
return nil
}
name, _ := sender["name"].(string)
return &name
}
func serializeContact(contact *model.Contact) map[string]any {
return map[string]any{
"additional_attributes": jsonObject(contact.AdditionalAttributes),