package v1 import ( "net/http" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/service" "github.com/gochat/gochat/pkg/response" ) // AgentBulkHandler handles bulk assignment/unassignment of conversations to agents. // Reference: Chatwoot app/controllers/api/v1/accounts/agents_controller.rb #bulk_assign, #bulk_unassign type AgentBulkHandler struct { conversationSvc *service.ConversationService } // NewAgentBulkHandler creates a new AgentBulkHandler. func NewAgentBulkHandler(conversationSvc *service.ConversationService) *AgentBulkHandler { return &AgentBulkHandler{conversationSvc: conversationSvc} } // BulkAssignRequest is the DTO for bulk assigning conversations to an agent. type BulkAssignRequest struct { ConversationIDs []uint `json:"conversation_ids" binding:"required,min=1"` AgentID uint `json:"agent_id" binding:"required"` } // BulkAssign assigns multiple conversations to a single agent. // POST /api/v1/accounts/:account_id/agents/bulk_assign func (h *AgentBulkHandler) BulkAssign(c *gin.Context) { accountID, err := parseUintParam(c, "account_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } var req BulkAssignRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } results := make([]map[string]interface{}, 0, len(req.ConversationIDs)) for _, convID := range req.ConversationIDs { _, svcErr := h.conversationSvc.AssignAgent(c.Request.Context(), accountID, convID, req.AgentID) if svcErr != nil { results = append(results, map[string]interface{}{ "conversation_id": convID, "error": svcErr.Error(), }) } else { results = append(results, map[string]interface{}{ "conversation_id": convID, "status": "assigned", }) } } response.OK(c, results) } // BulkUnassignRequest is the DTO for bulk unassigning conversations from agents. type BulkUnassignRequest struct { ConversationIDs []uint `json:"conversation_ids" binding:"required,min=1"` } // BulkUnassign removes agent assignment from multiple conversations. // POST /api/v1/accounts/:account_id/agents/bulk_unassign func (h *AgentBulkHandler) BulkUnassign(c *gin.Context) { accountID, err := parseUintParam(c, "account_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } var req BulkUnassignRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } results := make([]map[string]interface{}, 0, len(req.ConversationIDs)) for _, convID := range req.ConversationIDs { _, svcErr := h.conversationSvc.UnassignAgent(c.Request.Context(), accountID, convID) if svcErr != nil { results = append(results, map[string]interface{}{ "conversation_id": convID, "error": svcErr.Error(), }) } else { results = append(results, map[string]interface{}{ "conversation_id": convID, "status": "unassigned", }) } } response.OK(c, results) }