fix(shangwutong): 展示会话来源信息

This commit is contained in:
Rogee
2026-08-06 16:54:57 +08:00
parent 5c47d1d5fe
commit d2aeea5f44
7 changed files with 231 additions and 8 deletions
@@ -836,7 +836,8 @@ func (h *ConversationHandler) UpdateCustomAttributes(c *gin.Context) {
if !requireConnectorShangwutongConversation(c, h.conversationSvc.DB(), conversation) {
return
}
conversation, svcErr := h.conversationSvc.UpdateCustomAttributes(c.Request.Context(), accountID, conversation.ID, req.CustomAttributes)
requestContext := service.WithShangwutongRequestMetadata(c.Request.Context(), middleware.IsConnectorService(c), currentUserID(c))
conversation, svcErr := h.conversationSvc.UpdateCustomAttributes(requestContext, accountID, conversation.ID, req.CustomAttributes)
if svcErr != nil {
handleServiceError(c, svcErr)
return
@@ -0,0 +1,38 @@
package service
import (
"context"
"encoding/json"
"testing"
"github.com/gochat/gochat/internal/channel"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/stretchr/testify/require"
"gorm.io/datatypes"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestUpdateCustomAttributesMergesConnectorPayload(t *testing.T) {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, db.AutoMigrate(&model.Conversation{}))
conversation := model.Conversation{
AccountID: 1,
CustomAttributes: datatypes.JSON([]byte(`{"swt_sid":"sid","swt_state":"chatting"}`)),
}
require.NoError(t, db.Create(&conversation).Error)
svc := NewConversationService(repository.NewConversationRepo(db), repository.NewMessageRepo(db), channel.NewDispatcher(), nil, nil, nil, nil)
ctx := WithShangwutongRequestMetadata(context.Background(), true, 0)
updated, err := svc.UpdateCustomAttributes(ctx, 1, conversation.ID, datatypes.JSON([]byte(`{"swt_source_channel":"百度搜索推广"}`)))
require.NoError(t, err)
attributes := map[string]any{}
require.NoError(t, json.Unmarshal(updated.CustomAttributes, &attributes))
require.Equal(t, "sid", attributes["swt_sid"])
require.Equal(t, "chatting", attributes["swt_state"])
require.Equal(t, "百度搜索推广", attributes["swt_source_channel"])
}
@@ -2,6 +2,7 @@ package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
@@ -1646,6 +1647,28 @@ func (s *ConversationService) UpdateCustomAttributes(ctx context.Context, accoun
return nil, err
}
metadata, _ := ctx.Value(shangwutongRequestMetadataKey{}).(shangwutongRequestMetadata)
if metadata.ConnectorOrigin {
merged := map[string]any{}
if len(conversation.CustomAttributes) > 0 {
if err := json.Unmarshal(conversation.CustomAttributes, &merged); err != nil {
return nil, fmt.Errorf("decode existing conversation custom attributes: %w", err)
}
}
incoming := map[string]any{}
if err := json.Unmarshal(attrs, &incoming); err != nil {
return nil, fmt.Errorf("decode incoming conversation custom attributes: %w", err)
}
for key, value := range incoming {
merged[key] = value
}
encoded, err := json.Marshal(merged)
if err != nil {
return nil, fmt.Errorf("encode merged conversation custom attributes: %w", err)
}
attrs = datatypes.JSON(encoded)
}
if err := s.repo.UpdateCustomAttributes(ctx, id, attrs); err != nil {
return nil, err
}