feat(labels): align chatwoot label payloads

This commit is contained in:
2026-06-06 00:33:00 +08:00
parent 8b378c79e4
commit 171cbd16d2
10 changed files with 474 additions and 60 deletions
+66 -21
View File
@@ -5,6 +5,7 @@ import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/service"
applogger "github.com/gochat/gochat/pkg/logger"
"github.com/gochat/gochat/pkg/response"
@@ -31,15 +32,11 @@ func (h *LabelHandler) CreateTag(c *gin.Context) {
return
}
// Chatwoot: params.require(:label) → {"label": {...}}
var wrapper struct {
Label service.CreateTagRequest `json:"label"`
}
if err := c.ShouldBindJSON(&wrapper); err != nil {
var req service.CreateTagRequest
if err := bindJSONWrappedOrRaw(c, "label", &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
req := wrapper.Label
tag, err := h.tagSvc.Create(c.Request.Context(), accountID, &req)
if err != nil {
@@ -48,26 +45,31 @@ func (h *LabelHandler) CreateTag(c *gin.Context) {
return
}
response.Created(c, tag)
c.JSON(http.StatusOK, serializeLabel(tag))
}
// GetTag retrieves a tag by ID.
// GET /api/v1/accounts/:account_id/tags/:tag_id
func (h *LabelHandler) GetTag(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
tagID, err := parseUintParam(c, "tag_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid tag_id")
return
}
tag, err := h.tagSvc.GetByID(c.Request.Context(), tagID)
tag, err := h.tagSvc.GetByIDAndAccountID(c.Request.Context(), accountID, tagID)
if err != nil {
applogger.L().Errorf("Get tag: %v", err)
handleServiceError(c, err)
return
}
response.OK(c, tag)
c.JSON(http.StatusOK, serializeLabel(tag))
}
// ListTags returns all tags in an account.
@@ -86,27 +88,34 @@ func (h *LabelHandler) ListTags(c *gin.Context) {
return
}
response.OK(c, tags)
c.JSON(http.StatusOK, gin.H{"payload": serializeLabels(tags)})
}
// UpdateTag modifies an existing tag.
// PUT /api/v1/accounts/:account_id/tags/:tag_id
func (h *LabelHandler) UpdateTag(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
tagID, err := parseUintParam(c, "tag_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid tag_id")
return
}
// Chatwoot: params.require(:label) → {"label": {...}}
var wrapper struct {
Label service.UpdateTagRequest `json:"label"`
if _, err := h.tagSvc.GetByIDAndAccountID(c.Request.Context(), accountID, tagID); err != nil {
applogger.L().Errorf("Get tag for update: %v", err)
handleServiceError(c, err)
return
}
if err := c.ShouldBindJSON(&wrapper); err != nil {
var req service.UpdateTagRequest
if err := bindJSONWrappedOrRaw(c, "label", &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
req := wrapper.Label
tag, err := h.tagSvc.Update(c.Request.Context(), tagID, &req)
if err != nil {
@@ -115,25 +124,61 @@ func (h *LabelHandler) UpdateTag(c *gin.Context) {
return
}
response.OK(c, tag)
c.JSON(http.StatusOK, serializeLabel(tag))
}
// DeleteTag soft-deletes a tag.
// DELETE /api/v1/accounts/:account_id/tags/:tag_id
func (h *LabelHandler) DeleteTag(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
tagID, err := parseUintParam(c, "tag_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid tag_id")
return
}
if _, err := h.tagSvc.GetByIDAndAccountID(c.Request.Context(), accountID, tagID); err != nil {
applogger.L().Errorf("Get tag for delete: %v", err)
handleServiceError(c, err)
return
}
if err := h.tagSvc.Delete(c.Request.Context(), tagID); err != nil {
applogger.L().Errorf("Delete tag: %v", err)
handleServiceError(c, err)
return
}
response.NoContent(c)
c.Status(http.StatusOK)
}
func serializeLabels(tags []model.Tag) []gin.H {
payload := make([]gin.H, 0, len(tags))
for i := range tags {
payload = append(payload, serializeLabel(&tags[i]))
}
return payload
}
func serializeLabel(tag *model.Tag) gin.H {
return gin.H{
"id": tag.ID,
"title": tag.Name,
"description": tag.Description,
"color": tag.Color,
"show_on_sidebar": labelShowOnSidebar(tag),
}
}
func labelShowOnSidebar(tag *model.Tag) bool {
if tag.ShowOnSidebar == nil {
return false
}
return *tag.ShowOnSidebar
}
// ========== Conversation-Label Association ==========
@@ -327,9 +372,9 @@ func (h *LabelHandler) GetConversationsByTag(c *gin.Context) {
}
response.OK(c, gin.H{
"labels": labels,
"count": count,
"page": page,
"labels": labels,
"count": count,
"page": page,
"per_page": perPage,
})
}
}