feat(crm): complete contact label avatar gaps

This commit is contained in:
2026-06-05 02:53:24 +08:00
parent 5cf735076d
commit 7a033e28a0
16 changed files with 512 additions and 31 deletions
@@ -49,6 +49,8 @@ func (s *ContactHandlerCRUDTestSuite) SetupSuite() {
&model.AccountUser{},
&model.Inbox{},
&model.Contact{},
&model.Tag{},
&model.ContactLabel{},
&model.Conversation{},
&model.ContactInbox{},
&model.InboxMember{},
@@ -83,7 +85,10 @@ func (s *ContactHandlerCRUDTestSuite) SetupSuite() {
s.router.POST("/api/v1/accounts/:id/contacts", s.handler.Create)
s.router.PUT("/api/v1/accounts/:id/contacts/:contact_id", s.handler.Update)
s.router.DELETE("/api/v1/accounts/:id/contacts/:contact_id", s.handler.Delete)
s.router.DELETE("/api/v1/accounts/:id/contacts/:contact_id/avatar", s.handler.DeleteAvatar)
s.router.GET("/api/v1/accounts/:id/contacts/:contact_id/conversations", s.handler.ListConversations)
s.router.GET("/api/v1/accounts/:id/contacts/:contact_id/labels", s.handler.ListLabels)
s.router.POST("/api/v1/accounts/:id/contacts/:contact_id/labels", s.handler.UpdateLabels)
s.router.GET("/api/v1/accounts/:id/contacts/:contact_id/contact_inboxes", s.handler.ListContactInboxes)
s.router.GET("/api/v1/accounts/:id/contacts/:contact_id/notes", s.handler.ListNotes)
s.router.POST("/api/v1/accounts/:id/contacts/:contact_id/notes", s.handler.CreateNote)
@@ -111,6 +116,8 @@ func (s *ContactHandlerCRUDTestSuite) SetupSuite() {
// SetupTest re-creates core test data before each test so tests don't leak state.
func (s *ContactHandlerCRUDTestSuite) SetupTest() {
s.db.Exec("DELETE FROM contact_notes")
s.db.Exec("DELETE FROM contact_labels")
s.db.Exec("DELETE FROM tags")
s.db.Exec("DELETE FROM notes")
s.db.Exec("DELETE FROM contact_inboxes")
s.db.Exec("DELETE FROM conversations")
@@ -532,6 +539,63 @@ func (s *ContactHandlerCRUDTestSuite) TestDelete_Success() {
s.Error(err, "contact should be soft-deleted")
}
func (s *ContactHandlerCRUDTestSuite) TestDeleteAvatar_Success() {
s.Require().NoError(s.db.Model(s.contact).Update("avatar_url", "https://example.com/avatar.png").Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE",
fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/avatar", s.account.ID, s.contact.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"].(map[string]interface{})
s.Equal("", payload["thumbnail"])
var found model.Contact
s.Require().NoError(s.db.First(&found, s.contact.ID).Error)
s.Equal("", found.AvatarURL)
}
func (s *ContactHandlerCRUDTestSuite) TestLabels_UpdateListAndFilter() {
bodyBytes, _ := json.Marshal(map[string]interface{}{"labels": []string{"vip", "trial"}})
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST",
fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/labels", s.account.ID, s.contact.ID),
bytes.NewReader(bodyBytes))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
var resp map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
s.ElementsMatch([]interface{}{"vip", "trial"}, resp["payload"])
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET",
fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/labels", s.account.ID, s.contact.ID), nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
s.ElementsMatch([]interface{}{"vip", "trial"}, resp["payload"])
other := &model.Contact{AccountID: s.account.ID, Name: "Other Contact"}
s.Require().NoError(s.db.Create(other).Error)
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET",
fmt.Sprintf("/api/v1/accounts/%d/contacts?labels%%5B%%5D=vip&page=1&page_size=25", s.account.ID), nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
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"])
}
func (s *ContactHandlerCRUDTestSuite) TestDelete_InvalidAccountID() {
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE",