feat(contacts): align resolved scope

This commit is contained in:
2026-06-07 05:25:38 +08:00
parent 781ed352dd
commit 0baee8e47a
4 changed files with 174 additions and 8 deletions
@@ -229,6 +229,52 @@ func (s *ContactHandlerCRUDTestSuite) TestList_Success() {
s.Equal(float64(1), metaMap["current_page"])
}
func (s *ContactHandlerCRUDTestSuite) TestList_ChatwootResolvedContactsScope() {
anonymous := &model.Contact{AccountID: s.account.ID, Name: "Anonymous Visitor"}
s.Require().NoError(s.db.Create(anonymous).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET",
fmt.Sprintf("/api/v1/accounts/%d/contacts?page=1&page_size=25", s.account.ID), nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
var resp map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
payload := resp["payload"].([]interface{})
s.Len(payload, 1)
s.Equal(float64(s.contact.ID), payload[0].(map[string]interface{})["id"])
meta := resp["meta"].(map[string]interface{})
s.Equal(float64(1), meta["count"])
}
func (s *ContactHandlerCRUDTestSuite) TestList_ChatwootResolvedContactsCRMV2Scope() {
s.account.FeatureFlags = `{"crm_v2":true}`
s.Require().NoError(s.db.Save(s.account).Error)
s.contact.ContactType = "lead"
s.Require().NoError(s.db.Save(s.contact).Error)
leadOnly := &model.Contact{AccountID: s.account.ID, Name: "Lead Only", ContactType: "lead"}
s.Require().NoError(s.db.Create(leadOnly).Error)
customer := &model.Contact{AccountID: s.account.ID, Name: "Customer", Email: "customer@example.com", ContactType: "customer"}
s.Require().NoError(s.db.Create(customer).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET",
fmt.Sprintf("/api/v1/accounts/%d/contacts?page=1&page_size=25", s.account.ID), nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
var resp map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
payload := resp["payload"].([]interface{})
s.Len(payload, 2)
ids := []float64{payload[0].(map[string]interface{})["id"].(float64), payload[1].(map[string]interface{})["id"].(float64)}
s.ElementsMatch([]float64{float64(s.contact.ID), float64(leadOnly.ID)}, ids)
meta := resp["meta"].(map[string]interface{})
s.Equal(float64(2), meta["count"])
}
func (s *ContactHandlerCRUDTestSuite) TestList_InvalidAccountID() {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts?page=1&page_size=25", nil)
@@ -247,6 +293,7 @@ func (s *ContactHandlerCRUDTestSuite) TestList_Pagination() {
c := &model.Contact{
AccountID: s.account.ID,
Name: fmt.Sprintf("Contact %d", i),
Email: fmt.Sprintf("contact-%d@example.com", i),
}
s.Require().NoError(s.db.Create(c).Error)
}
@@ -930,9 +977,9 @@ func (s *ContactHandlerCRUDTestSuite) TestFilter_ChatwootPayloadCustomAttributeN
s.contact.CustomAttributes = datatypes.JSON(`{"customer_type":"platinum"}`)
s.Require().NoError(s.db.Save(s.contact).Error)
regular := &model.Contact{AccountID: s.account.ID, Name: "Regular User", CustomAttributes: datatypes.JSON(`{"customer_type":"regular"}`)}
regular := &model.Contact{AccountID: s.account.ID, Name: "Regular User", Email: "regular@example.com", CustomAttributes: datatypes.JSON(`{"customer_type":"regular"}`)}
s.Require().NoError(s.db.Create(regular).Error)
missing := &model.Contact{AccountID: s.account.ID, Name: "Missing User", CustomAttributes: datatypes.JSON(`{}`)}
missing := &model.Contact{AccountID: s.account.ID, Name: "Missing User", Email: "missing@example.com", CustomAttributes: datatypes.JSON(`{}`)}
s.Require().NoError(s.db.Create(missing).Error)
body, _ := json.Marshal(map[string]interface{}{