Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
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})
|
|
}
|