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

207 lines
5.9 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/canned"
applogger "github.com/gochat/gochat/pkg/logger"
"github.com/gochat/gochat/pkg/response"
)
// CannedResponseHandler handles CannedResponse CRUD + search endpoints.
// Reference: Chatwoot CannedResponsesController — CRUD + search by short_code/content
type CannedResponseHandler struct {
svc *canned.CannedResponseService
}
// NewCannedResponseHandler creates a new CannedResponseHandler.
func NewCannedResponseHandler(svc *canned.CannedResponseService) *CannedResponseHandler {
return &CannedResponseHandler{svc: svc}
}
// Create creates a new canned response.
// POST /api/v1/accounts/:account_id/canned_responses
// Reference: Chatwoot CannedResponsesController#create
func (h *CannedResponseHandler) 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(:canned_response) → {"canned_response": {...}}
var wrapper struct {
CannedResponse struct {
ShortCode string `json:"short_code" binding:"required"`
Content string `json:"content" binding:"required"`
} `json:"canned_response"`
}
if err := c.ShouldBindJSON(&wrapper); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
req := wrapper.CannedResponse
cr := &canned.CannedResponse{
AccountID: accountID,
ShortCode: req.ShortCode,
Content: req.Content,
}
if err := h.svc.Create(c.Request.Context(), cr); err != nil {
applogger.L().Errorf("Create canned response: %v", err)
handleServiceError(c, err)
return
}
response.Created(c, cr)
}
// Get retrieves a canned response by ID.
// GET /api/v1/accounts/:account_id/canned_responses/:id
// Reference: Chatwoot CannedResponsesController#show
func (h *CannedResponseHandler) Get(c *gin.Context) {
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
cr, svcErr := h.svc.GetByID(c.Request.Context(), id)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, cr)
}
// Update updates a canned response.
// PUT /api/v1/accounts/:account_id/canned_responses/:id
// Reference: Chatwoot CannedResponsesController#update
func (h *CannedResponseHandler) Update(c *gin.Context) {
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
// Chatwoot: params.require(:canned_response) → {"canned_response": {...}}
var wrapper struct {
CannedResponse struct {
ShortCode string `json:"short_code"`
Content string `json:"content"`
} `json:"canned_response"`
}
if err := c.ShouldBindJSON(&wrapper); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
req := wrapper.CannedResponse
updates := map[string]interface{}{}
if req.ShortCode != "" {
updates["short_code"] = req.ShortCode
}
if req.Content != "" {
updates["content"] = req.Content
}
if len(updates) == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "no fields to update")
return
}
if svcErr := h.svc.Update(c.Request.Context(), id, updates); svcErr != nil {
applogger.L().Errorf("Update canned response: %v", svcErr)
handleServiceError(c, svcErr)
return
}
// Fetch updated record to return
cr, svcErr := h.svc.GetByID(c.Request.Context(), id)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, cr)
}
// Delete soft-deletes a canned response.
// DELETE /api/v1/accounts/:account_id/canned_responses/:id
// Reference: Chatwoot CannedResponsesController#destroy
func (h *CannedResponseHandler) Delete(c *gin.Context) {
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
if svcErr := h.svc.Delete(c.Request.Context(), id); svcErr != nil {
applogger.L().Errorf("Delete canned response: %v", svcErr)
handleServiceError(c, svcErr)
return
}
response.NoContent(c)
}
// List returns all canned responses for an account.
// GET /api/v1/accounts/:account_id/canned_responses
// Reference: Chatwoot CannedResponsesController#index
func (h *CannedResponseHandler) List(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
// If a search query is provided, use Search instead of ListByAccount
searchQuery := c.Query("q")
if searchQuery != "" {
responses, svcErr := h.svc.Search(c.Request.Context(), accountID, searchQuery)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{
"canned_responses": responses,
})
return
}
responses, svcErr := h.svc.ListByAccount(c.Request.Context(), accountID)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{
"canned_responses": responses,
})
}
// Search performs a ranked search over canned responses.
// GET /api/v1/accounts/:account_id/canned_responses/search?q=...
// Reference: Chatwoot CannedResponsesController#index with search parameter (order_by_search scope)
func (h *CannedResponseHandler) Search(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
searchQuery := c.Query("q")
responses, svcErr := h.svc.Search(c.Request.Context(), accountID, searchQuery)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{
"canned_responses": responses,
})
}