Align GoChat with Chatwoot frontend contracts

This commit is contained in:
2026-06-13 22:13:32 +08:00
parent 71abf58636
commit d884fdda0a
162 changed files with 9825 additions and 528 deletions
+27 -1
View File
@@ -16,6 +16,7 @@ import (
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/search"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/internal/ws"
"github.com/gochat/gochat/pkg/response"
)
@@ -28,6 +29,7 @@ type ContactHandler struct {
contactNoteSvc *service.ContactNoteService
conversationSvc *service.ConversationService
presence contactPresenceReader
eventPublisher *ws.EventPublisher
}
const chatwootContactResultsPerPage = 15
@@ -41,6 +43,11 @@ func NewContactHandler(svc *service.ContactService, contactInboxSvc *service.Con
return h
}
func (h *ContactHandler) WithEventPublisher(publisher *ws.EventPublisher) *ContactHandler {
h.eventPublisher = publisher
return h
}
func (h *ContactHandler) WithContactPresence(presence contactPresenceReader) *ContactHandler {
h.presence = presence
return h
@@ -273,7 +280,16 @@ func (h *ContactHandler) Update(c *gin.Context) {
return
}
c.JSON(http.StatusOK, contactPayloadResponse(h.requestContext(c), h.svc.DB(), contact, includeContactInboxes(c)))
payload := contactPayloadResponse(h.requestContext(c), h.svc.DB(), contact, includeContactInboxes(c))
h.publishContactEvent(accountID, ws.EventContactUpdated, contact)
c.JSON(http.StatusOK, payload)
}
func (h *ContactHandler) publishContactEvent(accountID uint, eventType string, contact *model.Contact) {
if h.eventPublisher == nil || contact == nil {
return
}
h.eventPublisher.PublishEvent(accountID, eventType, serializeCRMContact(context.Background(), h.svc.DB(), contact, true))
}
// @Summary Delete a contact
@@ -304,11 +320,17 @@ func (h *ContactHandler) Delete(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid contact id"})
return
}
contact, svcErr := h.svc.GetByAccountAndID(c.Request.Context(), accountID, contactID)
if svcErr != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "failed to delete contact"})
return
}
if svcErr := h.svc.Delete(c.Request.Context(), accountID, contactID); svcErr != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "failed to delete contact"})
return
}
h.publishContactEvent(accountID, ws.EventContactDeleted, contact)
c.Status(http.StatusOK)
}
@@ -1081,6 +1103,10 @@ func (h *ContactHandler) Merge(c *gin.Context) {
handleServiceError(c, svcErr)
return
}
h.publishContactEvent(accountID, ws.EventContactUpdated, result)
if req.BaseContactID != req.MergeeContactID {
h.publishContactEvent(accountID, ws.EventContactDeleted, &model.Contact{Base: model.Base{ID: req.MergeeContactID}, AccountID: accountID})
}
c.JSON(http.StatusOK, serializeCRMContact(h.requestContext(c), h.svc.DB(), result, false))
}