155 lines
4.9 KiB
Go
155 lines
4.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/gochat/gochat/pkg/pagination"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// CustomAttributeDefinitionHandler handles CRUD for custom attribute definitions.
|
|
// Reference: Chatwoot app/controllers/api/v1/accounts/custom_attribute_definitions_controller.rb
|
|
type CustomAttributeDefinitionHandler struct {
|
|
svc *service.CustomAttributeDefinitionService
|
|
}
|
|
|
|
// NewCustomAttributeDefinitionHandler creates a new handler with service injection.
|
|
func NewCustomAttributeDefinitionHandler(svc *service.CustomAttributeDefinitionService) *CustomAttributeDefinitionHandler {
|
|
return &CustomAttributeDefinitionHandler{svc: svc}
|
|
}
|
|
|
|
// List retrieves all custom attribute definitions for an account.
|
|
// GET /api/v1/accounts/:account_id/custom_attribute_definitions
|
|
// Optional query param: attribute_model (conversation/contact) to filter by model type.
|
|
func (h *CustomAttributeDefinitionHandler) List(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
attributeModel := c.Query("attribute_model")
|
|
page := pagination.Parse(c)
|
|
|
|
defs, total, svcErr := h.svc.List(c.Request.Context(), accountID, attributeModel, page.Offset, page.PerPage)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OKWithMeta(c, defs, page.Page, page.PerPage, total)
|
|
}
|
|
|
|
// Get retrieves a single custom attribute definition by ID.
|
|
// GET /api/v1/accounts/:account_id/custom_attribute_definitions/:id
|
|
func (h *CustomAttributeDefinitionHandler) Get(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
id, err := parseUintParam(c, "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
def, svcErr := h.svc.Get(c.Request.Context(), accountID, id)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, def)
|
|
}
|
|
|
|
// Create creates a new custom attribute definition for an account.
|
|
// POST /api/v1/accounts/:account_id/custom_attribute_definitions
|
|
func (h *CustomAttributeDefinitionHandler) Create(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
// Chatwoot: params.require(:custom_attribute_definition) → {"custom_attribute_definition": {...}}
|
|
var wrapper struct {
|
|
CustomAttributeDefinition service.CreateCustomAttributeDefinitionRequest `json:"custom_attribute_definition"`
|
|
}
|
|
if err := c.ShouldBindJSON(&wrapper); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
req := wrapper.CustomAttributeDefinition
|
|
|
|
def, svcErr := h.svc.Create(c.Request.Context(), accountID, &req)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.Created(c, def)
|
|
}
|
|
|
|
// Update modifies a custom attribute definition.
|
|
// PUT /api/v1/accounts/:account_id/custom_attribute_definitions/:id
|
|
func (h *CustomAttributeDefinitionHandler) Update(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
id, err := parseUintParam(c, "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
// Chatwoot: params.require(:custom_attribute_definition) → {"custom_attribute_definition": {...}}
|
|
var wrapper struct {
|
|
CustomAttributeDefinition service.UpdateCustomAttributeDefinitionRequest `json:"custom_attribute_definition"`
|
|
}
|
|
if err := c.ShouldBindJSON(&wrapper); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
req := wrapper.CustomAttributeDefinition
|
|
|
|
def, svcErr := h.svc.Update(c.Request.Context(), accountID, id, &req)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, def)
|
|
}
|
|
|
|
// Delete soft-deletes a custom attribute definition.
|
|
// DELETE /api/v1/accounts/:account_id/custom_attribute_definitions/:id
|
|
func (h *CustomAttributeDefinitionHandler) Delete(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
id, err := parseUintParam(c, "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
svcErr := h.svc.Delete(c.Request.Context(), accountID, id)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.NoContent(c)
|
|
}
|