63 lines
1.9 KiB
Go
63 lines
1.9 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/response"
|
|
)
|
|
|
|
// CaptainBulkActionHandler handles bulk AI operations on multiple conversations.
|
|
// Reference: Chatwoot Captain::BulkActionsController
|
|
type CaptainBulkActionHandler struct {
|
|
svc *service.CaptainBulkActionService
|
|
}
|
|
|
|
func NewCaptainBulkActionHandler(svc *service.CaptainBulkActionService) *CaptainBulkActionHandler {
|
|
return &CaptainBulkActionHandler{svc: svc}
|
|
}
|
|
|
|
// Execute performs a bulk AI action on multiple conversations.
|
|
// POST /api/v1/accounts/:id/captain/bulk_actions
|
|
func (h *CaptainBulkActionHandler) Execute(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.BulkActionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
// Validate action type
|
|
validActions := map[service.BulkActionType]bool{
|
|
service.BulkActionLabelSuggestion: true,
|
|
service.BulkActionReplySuggestion: true,
|
|
service.BulkActionFollowUp: true,
|
|
}
|
|
if !validActions[req.Action] {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "unsupported action type: "+string(req.Action))
|
|
return
|
|
}
|
|
|
|
if len(req.ConversationIDs) == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "conversation_ids required")
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.Execute(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Bulk action: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to execute bulk action")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|