second commit
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user