package v1 import ( "net/http" "time" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/service" "github.com/gochat/gochat/pkg/response" ) type chatwootCustomAttributeDefinitionResponse struct { ID uint `json:"id"` AttributeDisplayName string `json:"attribute_display_name"` AttributeDisplayType string `json:"attribute_display_type"` AttributeDescription string `json:"attribute_description"` AttributeKey string `json:"attribute_key"` RegexPattern string `json:"regex_pattern"` RegexCue string `json:"regex_cue"` AttributeValues interface{} `json:"attribute_values"` AttributeModel string `json:"attribute_model"` DefaultValue interface{} `json:"default_value"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } func serializeCustomAttributeDefinition(def *model.CustomAttributeDefinition) chatwootCustomAttributeDefinitionResponse { return chatwootCustomAttributeDefinitionResponse{ ID: def.ID, AttributeDisplayName: def.AttributeDisplayName, AttributeDisplayType: def.AttributeType, AttributeDescription: def.Description, AttributeKey: def.AttributeName, RegexPattern: def.RegexPattern, RegexCue: def.RegexCue, AttributeValues: def.AttributeValues, AttributeModel: def.AttributeModel, DefaultValue: def.DefaultValue, CreatedAt: def.CreatedAt, UpdatedAt: def.UpdatedAt, } } func serializeCustomAttributeDefinitions(defs []model.CustomAttributeDefinition) []chatwootCustomAttributeDefinitionResponse { items := make([]chatwootCustomAttributeDefinitionResponse, 0, len(defs)) for i := range defs { items = append(items, serializeCustomAttributeDefinition(&defs[i])) } return items } // 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") defs, _, svcErr := h.svc.List(c.Request.Context(), accountID, attributeModel, 0, 0) if svcErr != nil { handleServiceError(c, svcErr) return } c.JSON(http.StatusOK, serializeCustomAttributeDefinitions(defs)) } // 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 } c.JSON(http.StatusOK, serializeCustomAttributeDefinition(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 } var req service.CreateCustomAttributeDefinitionRequest if err := bindChatwootPayload(c, "custom_attribute_definition", &req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } def, svcErr := h.svc.Create(c.Request.Context(), accountID, &req) if svcErr != nil { handleServiceError(c, svcErr) return } c.JSON(http.StatusOK, serializeCustomAttributeDefinition(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 } var req service.UpdateCustomAttributeDefinitionRequest if err := bindChatwootPayload(c, "custom_attribute_definition", &req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } def, svcErr := h.svc.Update(c.Request.Context(), accountID, id, &req) if svcErr != nil { handleServiceError(c, svcErr) return } c.JSON(http.StatusOK, serializeCustomAttributeDefinition(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) }