feat(custom-fields): align chatwoot saved view payloads

This commit is contained in:
2026-06-06 00:53:33 +08:00
parent 171cbd16d2
commit 328d16c363
12 changed files with 461 additions and 147 deletions
@@ -82,7 +82,7 @@ func seedCustomAttrDef(t *testing.T, db *gorm.DB, accountID uint, name, displayN
repo := repository.NewCustomAttributeDefinitionRepo(db)
svc := service.NewCustomAttributeDefinitionService(repo)
def, err := svc.Create(context.Background(), accountID, &service.CreateCustomAttributeDefinitionRequest{
AttributeKey: name,
AttributeKey: name,
AttributeDisplayName: displayName,
AttributeDisplayType: attrType,
AttributeModel: attrModel,
@@ -111,12 +111,10 @@ func TestCustomAttributeDefinitionHandler_List(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)
var resp map[string]interface{}
var resp []map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &resp)
require.NoError(t, err)
assert.True(t, resp["success"].(bool))
data := resp["data"].([]interface{})
assert.Len(t, data, 2)
assert.Len(t, resp, 2)
}
func TestCustomAttributeDefinitionHandler_List_FilterByModel(t *testing.T) {
@@ -128,16 +126,15 @@ func TestCustomAttributeDefinitionHandler_List_FilterByModel(t *testing.T) {
r.GET("/api/v1/accounts/:account_id/custom_attribute_definitions", handler.List)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/custom_attribute_definitions?attribute_model=conversation", accountID), nil)
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/custom_attribute_definitions?attribute_model=conversation_attribute", accountID), nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var resp map[string]interface{}
var resp []map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &resp)
require.NoError(t, err)
data := resp["data"].([]interface{})
assert.Len(t, data, 1)
assert.Len(t, resp, 1)
}
func TestCustomAttributeDefinitionHandler_List_InvalidAccountID(t *testing.T) {
@@ -170,7 +167,7 @@ func TestCustomAttributeDefinitionHandler_Get(t *testing.T) {
var resp map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &resp)
require.NoError(t, err)
assert.True(t, resp["success"].(bool))
assert.Equal(t, "custom_priority_score", resp["attribute_key"])
}
func TestCustomAttributeDefinitionHandler_Get_NotFound(t *testing.T) {
@@ -204,27 +201,21 @@ func TestCustomAttributeDefinitionHandler_Create(t *testing.T) {
r.POST("/api/v1/accounts/:account_id/custom_attribute_definitions", handler.Create)
body := service.CreateCustomAttributeDefinitionRequest{
AttributeKey: "custom_priority_score",
AttributeDisplayName: "Custom Priority Score",
AttributeDisplayType: "number",
AttributeModel: "conversation",
}
bodyBytes := marshalNestedDef(body)
bodyBytes := []byte(`{"attribute_key":"custom_priority_score","attribute_display_name":"Custom Priority Score","attribute_description":"Score","attribute_display_type":1,"attribute_model":0}`)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/custom_attribute_definitions", accountID), bytes.NewReader(bodyBytes))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
assert.Equal(t, http.StatusOK, w.Code)
var resp map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &resp)
require.NoError(t, err)
assert.True(t, resp["success"].(bool))
data := resp["data"].(map[string]interface{})
assert.Equal(t, "custom_priority_score", data["attribute_key"])
assert.Equal(t, "custom_priority_score", resp["attribute_key"])
assert.Equal(t, "number", resp["attribute_display_type"])
assert.Equal(t, "conversation_attribute", resp["attribute_model"])
}
func TestCustomAttributeDefinitionHandler_Create_InvalidType(t *testing.T) {
@@ -233,9 +224,9 @@ func TestCustomAttributeDefinitionHandler_Create_InvalidType(t *testing.T) {
r.POST("/api/v1/accounts/:account_id/custom_attribute_definitions", handler.Create)
body := service.CreateCustomAttributeDefinitionRequest{
AttributeKey: "custom_priority_score",
AttributeKey: "custom_priority_score",
AttributeDisplayName: "Custom Priority Score",
AttributeDisplayType: "invalid_type",
AttributeDisplayType: "invalid_type",
AttributeModel: "conversation",
}
bodyBytes := marshalNestedDef(body)
@@ -254,9 +245,9 @@ func TestCustomAttributeDefinitionHandler_Create_InvalidAccountID(t *testing.T)
r.POST("/api/v1/accounts/:account_id/custom_attribute_definitions", handler.Create)
body := service.CreateCustomAttributeDefinitionRequest{
AttributeKey: "custom_priority_score",
AttributeKey: "custom_priority_score",
AttributeDisplayName: "Custom Priority Score",
AttributeDisplayType: "number",
AttributeDisplayType: "number",
AttributeModel: "conversation",
}
bodyBytes := marshalNestedDef(body)
@@ -293,7 +284,7 @@ func TestCustomAttributeDefinitionHandler_Update(t *testing.T) {
body := service.UpdateCustomAttributeDefinitionRequest{
AttributeDisplayName: "Priority Level",
AttributeDescription: "Updated description",
AttributeDescription: "Updated description",
}
bodyBytes := marshalNestedDef(body)
@@ -307,8 +298,7 @@ func TestCustomAttributeDefinitionHandler_Update(t *testing.T) {
var resp map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &resp)
require.NoError(t, err)
data := resp["data"].(map[string]interface{})
assert.Equal(t, "Priority Level", data["attribute_display_name"])
assert.Equal(t, "Priority Level", resp["attribute_display_name"])
}
func TestCustomAttributeDefinitionHandler_Update_NotFound(t *testing.T) {
@@ -385,4 +375,4 @@ func TestCustomAttributeDefinitionHandler_Delete_InvalidAccountID(t *testing.T)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
}