Files
gochat/internal/handler/api/v1/captain_custom_tool_handler.go
T
2026-06-04 15:44:48 +08:00

180 lines
6.0 KiB
Go

package v1
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
applogger "github.com/gochat/gochat/pkg/logger"
"github.com/gochat/gochat/pkg/pagination"
"github.com/gochat/gochat/pkg/response"
)
// CaptainCustomToolHandler handles CaptainCustomTool REST API endpoints.
// Reference: Chatwoot enterprise/app/controllers/api/v1/captain/custom_tools_controller.rb
type CaptainCustomToolHandler struct {
svc *service.CaptainCustomToolService
}
// NewCaptainCustomToolHandler creates a new CaptainCustomToolHandler.
func NewCaptainCustomToolHandler(svc *service.CaptainCustomToolService) *CaptainCustomToolHandler {
return &CaptainCustomToolHandler{svc: svc}
}
// Create creates a new custom tool.
// POST /api/v1/accounts/:account_id/captain_custom_tools
func (h *CaptainCustomToolHandler) Create(c *gin.Context) {
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req service.CreateCustomToolRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
tool, err := h.svc.Create(c.Request.Context(), uint(accountID), &req)
if err != nil {
applogger.L().Errorf("Create captain custom tool: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create custom tool")
return
}
response.Created(c, tool)
}
// Get retrieves a custom tool by ID.
// GET /api/v1/accounts/:account_id/captain_custom_tools/:id
func (h *CaptainCustomToolHandler) Get(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("tool_id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
tool, err := h.svc.Get(c.Request.Context(), uint(id))
if err != nil {
applogger.L().Errorf("Get captain custom tool: %v", err)
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "custom tool not found")
return
}
response.OK(c, tool)
}
// Update updates an existing custom tool.
// PUT /api/v1/accounts/:account_id/captain_custom_tools/:id
func (h *CaptainCustomToolHandler) Update(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("tool_id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
var req service.UpdateCustomToolRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
tool, err := h.svc.Update(c.Request.Context(), uint(id), &req)
if err != nil {
applogger.L().Errorf("Update captain custom tool: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update custom tool")
return
}
response.OK(c, tool)
}
// Delete deletes a custom tool.
// DELETE /api/v1/accounts/:account_id/captain_custom_tools/:id
func (h *CaptainCustomToolHandler) Delete(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("tool_id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
if err := h.svc.Delete(c.Request.Context(), uint(id)); err != nil {
applogger.L().Errorf("Delete captain custom tool: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete custom tool")
return
}
response.NoContent(c)
}
// List retrieves custom tools for an account.
// GET /api/v1/accounts/:account_id/captain_custom_tools
func (h *CaptainCustomToolHandler) List(c *gin.Context) {
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
p := pagination.Parse(c)
tools, count, err := h.svc.List(c.Request.Context(), uint(accountID), p.Offset, p.PerPage)
if err != nil {
applogger.L().Errorf("List captain custom tools: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list custom tools")
return
}
response.OKWithMeta(c, tools, p.Page, p.PerPage, count)
}
// ExecuteTool calls the external HTTP endpoint of a custom tool.
// POST /api/v1/accounts/:account_id/captain_custom_tools/:id/execute
func (h *CaptainCustomToolHandler) ExecuteTool(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("tool_id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
var params map[string]interface{}
if err := c.ShouldBindJSON(&params); err != nil {
// Empty body is acceptable (use nil params)
params = nil
}
result, err := h.svc.ExecuteTool(c.Request.Context(), uint(id), params)
if err != nil {
applogger.L().Errorf("ExecuteTool: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to execute tool")
return
}
response.OK(c, result)
}
// TestTool tests a custom tool with given parameters.
// POST /api/v1/accounts/:account_id/captain/custom_tools/test
func (h *CaptainCustomToolHandler) TestTool(c *gin.Context) {
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req service.TestToolRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
result, err := h.svc.TestTool(c.Request.Context(), uint(accountID), &req)
if err != nil {
applogger.L().Errorf("TestTool: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to test tool")
return
}
response.OK(c, result)
}