Files
gochat/backend/internal/handler/api/v1/custom_attribute_value_handler.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

143 lines
4.7 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
)
// CustomAttributeValueHandler handles setting/removing custom attribute values
// on conversations and contacts.
// Reference: Chatwoot app/controllers/api/v1/accounts/custom_attribute_definitions_controller.rb
type CustomAttributeValueHandler struct {
svc *service.CustomAttributeValueService
}
// NewCustomAttributeValueHandler creates a new handler with service injection.
func NewCustomAttributeValueHandler(svc *service.CustomAttributeValueService) *CustomAttributeValueHandler {
return &CustomAttributeValueHandler{svc: svc}
}
// SetConversationAttribute sets a custom attribute value on a conversation.
// POST /api/v1/accounts/:account_id/conversations/:conversation_id/custom_attributes
func (h *CustomAttributeValueHandler) SetConversationAttribute(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
conversationID, err := parseUintParam(c, "conversation_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
return
}
var req service.SetAttributeValueRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
conversation, svcErr := h.svc.SetConversationAttributeValue(c.Request.Context(), accountID, conversationID, &req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, conversation)
}
// RemoveConversationAttribute removes a custom attribute from a conversation.
// DELETE /api/v1/accounts/:account_id/conversations/:conversation_id/custom_attributes/:attribute_name
func (h *CustomAttributeValueHandler) RemoveConversationAttribute(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
conversationID, err := parseUintParam(c, "conversation_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
return
}
attributeName := c.Param("attribute_name")
if attributeName == "" {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "attribute_name is required")
return
}
conversation, svcErr := h.svc.RemoveConversationAttributeValue(c.Request.Context(), accountID, conversationID, attributeName)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, conversation)
}
// SetContactAttribute sets a custom attribute value on a contact.
// POST /api/v1/accounts/:account_id/contacts/:contact_id/custom_attributes
func (h *CustomAttributeValueHandler) SetContactAttribute(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
contactID, err := parseUintParam(c, "contact_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid contact_id")
return
}
var req service.SetAttributeValueRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
contact, svcErr := h.svc.SetContactAttributeValue(c.Request.Context(), accountID, contactID, &req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, contact)
}
// RemoveContactAttribute removes a custom attribute from a contact.
// DELETE /api/v1/accounts/:account_id/contacts/:contact_id/custom_attributes/:attribute_name
func (h *CustomAttributeValueHandler) RemoveContactAttribute(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
contactID, err := parseUintParam(c, "contact_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid contact_id")
return
}
attributeName := c.Param("attribute_name")
if attributeName == "" {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "attribute_name is required")
return
}
contact, svcErr := h.svc.RemoveContactAttributeValue(c.Request.Context(), accountID, contactID, attributeName)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, contact)
}