package v1 import ( "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/pagination" "github.com/gochat/gochat/pkg/response" ) // AgentCapacityHandler handles AgentCapacityPolicy CRUD operations. // Reference: Chatwoot enterprise/app/controllers/api/v1/agent_capacity_policies_controller.rb type AgentCapacityHandler struct { svc *service.AgentCapacityPolicyService } // NewAgentCapacityHandler creates a new AgentCapacityPolicy handler. func NewAgentCapacityHandler(svc *service.AgentCapacityPolicyService) *AgentCapacityHandler { return &AgentCapacityHandler{svc: svc} } // List returns all agent capacity policies for an account. // GET /api/v1/accounts/:account_id/agent_capacity_policies func (h *AgentCapacityHandler) List(c *gin.Context) { accountID := getAccountID(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified") return } pg := pagination.Parse(c) policies, total, err := h.svc.List(c.Request.Context(), accountID, pg.Page, pg.PerPage) if err != nil { applogger.L().Errorf("List agent capacity policies for account %d: %v", accountID, err) handleServiceError(c, err) return } response.OKWithMeta(c, policies, pg.Page, pg.PerPage, total) } // Create creates a new agent capacity policy for an account. // POST /api/v1/accounts/:account_id/agent_capacity_policies // Request body uses Chatwoot-style wrapper: { "agent_capacity_policy": { "name": "...", ... } } func (h *AgentCapacityHandler) Create(c *gin.Context) { accountID := getAccountID(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified") return } var wrapper struct { AgentCapacityPolicy service.CreateAgentCapacityPolicyRequest `json:"agent_capacity_policy"` } if err := c.ShouldBindJSON(&wrapper); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } policy, err := h.svc.Create(c.Request.Context(), accountID, wrapper.AgentCapacityPolicy) if err != nil { applogger.L().Errorf("Create agent capacity policy for account %d: %v", accountID, err) handleServiceError(c, err) return } response.Created(c, policy) } // Get returns a single agent capacity policy by ID. // GET /api/v1/accounts/:account_id/agent_capacity_policies/:id func (h *AgentCapacityHandler) Get(c *gin.Context) { accountID := getAccountID(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified") return } id, err := parseUintParam(c, "id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id") return } policy, svcErr := h.svc.GetByID(c.Request.Context(), id, accountID) if svcErr != nil { applogger.L().Errorf("Get agent capacity policy %d for account %d: %v", id, accountID, svcErr) handleServiceError(c, svcErr) return } response.OK(c, policy) } // Update updates an existing agent capacity policy. // PUT /api/v1/accounts/:account_id/agent_capacity_policies/:id func (h *AgentCapacityHandler) Update(c *gin.Context) { accountID := getAccountID(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified") return } id, err := parseUintParam(c, "id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id") return } var req service.UpdateAgentCapacityPolicyRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } policy, svcErr := h.svc.Update(c.Request.Context(), id, accountID, req) if svcErr != nil { applogger.L().Errorf("Update agent capacity policy %d for account %d: %v", id, accountID, svcErr) handleServiceError(c, svcErr) return } response.OK(c, policy) } // Delete deletes an agent capacity policy. // DELETE /api/v1/accounts/:account_id/agent_capacity_policies/:id func (h *AgentCapacityHandler) Delete(c *gin.Context) { accountID := getAccountID(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified") return } 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, accountID); svcErr != nil { applogger.L().Errorf("Delete agent capacity policy %d for account %d: %v", id, accountID, svcErr) handleServiceError(c, svcErr) return } response.NoContent(c) } // RegisterAgentCapacityRoutes registers agent capacity policy routes on a gin.RouterGroup. func RegisterAgentCapacityRoutes(rg *gin.RouterGroup, h *AgentCapacityHandler) { policies := rg.Group("/agent_capacity_policies") { policies.GET("/", h.List) policies.POST("/", h.Create) policies.GET("/:id", h.Get) policies.PUT("/:id", h.Update) policies.DELETE("/:id", h.Delete) } }