feat(shangwutong): sync customer names back to channel (#3)
Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/gochat/gochat/internal/channel"
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/repository"
|
||||
"github.com/gochat/gochat/internal/search"
|
||||
@@ -23,13 +24,14 @@ import (
|
||||
// ContactHandler handles contact-related API endpoints.
|
||||
// Reference: Chatwoot app/controllers/api/v1/contacts_controller.rb
|
||||
type ContactHandler struct {
|
||||
svc *service.ContactService
|
||||
contactInboxSvc *service.ContactInboxService
|
||||
mergeSvc *service.ContactMergeService
|
||||
contactNoteSvc *service.ContactNoteService
|
||||
conversationSvc *service.ConversationService
|
||||
presence contactPresenceReader
|
||||
eventPublisher *ws.EventPublisher
|
||||
svc *service.ContactService
|
||||
contactInboxSvc *service.ContactInboxService
|
||||
mergeSvc *service.ContactMergeService
|
||||
contactNoteSvc *service.ContactNoteService
|
||||
conversationSvc *service.ConversationService
|
||||
presence contactPresenceReader
|
||||
eventPublisher *ws.EventPublisher
|
||||
channelDispatcher *channel.Dispatcher
|
||||
}
|
||||
|
||||
const chatwootContactResultsPerPage = 15
|
||||
@@ -48,6 +50,11 @@ func (h *ContactHandler) WithEventPublisher(publisher *ws.EventPublisher) *Conta
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *ContactHandler) WithChannelDispatcher(dispatcher *channel.Dispatcher) *ContactHandler {
|
||||
h.channelDispatcher = dispatcher
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *ContactHandler) WithContactPresence(presence contactPresenceReader) *ContactHandler {
|
||||
h.presence = presence
|
||||
return h
|
||||
@@ -269,6 +276,7 @@ func (h *ContactHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
before, _ := h.svc.GetByAccountAndID(c.Request.Context(), accountID, contactID)
|
||||
contact, svcErr := h.svc.Update(c.Request.Context(), accountID, contactID, req)
|
||||
if svcErr != nil {
|
||||
var validationErr *service.ContactValidationError
|
||||
@@ -282,6 +290,12 @@ func (h *ContactHandler) Update(c *gin.Context) {
|
||||
|
||||
payload := contactPayloadResponse(h.requestContext(c), h.svc.DB(), contact, includeContactInboxes(c))
|
||||
h.publishContactEvent(accountID, ws.EventContactUpdated, contact)
|
||||
if h.channelDispatcher != nil && req.Name != "" && (before == nil || before.Name != contact.Name) {
|
||||
event := channel.NewChannelEvent(channel.EventContactUpdated, channel.ChannelAPI, accountID, 0)
|
||||
event.ContactID, event.Data["contact"] = contact.ID, contact
|
||||
event.Data["changed_attributes"] = map[string]any{"name": contact.Name}
|
||||
_ = h.channelDispatcher.Dispatch(c.Request.Context(), event)
|
||||
}
|
||||
c.JSON(http.StatusOK, payload)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user