102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"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 := parseAccountIDParam(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
body, err := c.GetRawData()
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
var chatwootReq service.ChatwootBulkActionRequest
|
|
if err := json.Unmarshal(body, &chatwootReq); err == nil && chatwootReq.Type != "" {
|
|
h.executeChatwoot(c, accountID, &chatwootReq)
|
|
return
|
|
}
|
|
|
|
var req service.BulkActionRequest
|
|
if err := json.Unmarshal(body, &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(), 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)
|
|
}
|
|
|
|
func (h *CaptainBulkActionHandler) executeChatwoot(c *gin.Context, accountID uint, req *service.ChatwootBulkActionRequest) {
|
|
result, err := h.svc.ExecuteChatwoot(c.Request.Context(), accountID, req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Chatwoot bulk action: %v", err)
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"success": false})
|
|
return
|
|
}
|
|
|
|
if result.Empty {
|
|
c.JSON(http.StatusOK, []gin.H{})
|
|
return
|
|
}
|
|
if result.AssistantResponses != nil {
|
|
payload := make([]gin.H, 0, len(result.AssistantResponses))
|
|
for i := range result.AssistantResponses {
|
|
payload = append(payload, captainAssistantResponsePayload(&result.AssistantResponses[i]))
|
|
}
|
|
c.JSON(http.StatusOK, payload)
|
|
return
|
|
}
|
|
if result.IDs != nil {
|
|
c.JSON(http.StatusOK, gin.H{"ids": result.IDs, "count": result.Count})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"count": result.Count})
|
|
}
|