Files
X-Agents/server/internal/handler/approval_handler.go

81 lines
1.8 KiB
Go
Raw Normal View History

package handler
import (
"net/http"
"x-agents/server/internal/model"
"x-agents/server/internal/service"
"github.com/gin-gonic/gin"
)
type ApprovalHandler struct {
approvalService *service.ApprovalService
}
func NewApprovalHandler(approvalService *service.ApprovalService) *ApprovalHandler {
return &ApprovalHandler{approvalService: approvalService}
}
// Approve 处理审批请求
func (h *ApprovalHandler) Approve(c *gin.Context) {
var req struct {
RequestID string `json:"request_id" binding:"required"`
Approved bool `json:"approved"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
userID, exists := c.Get("user_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
var result interface{}
var err error
if req.Approved {
result, err = h.approvalService.Approve(req.RequestID, userID.(string))
} else {
result, err = h.approvalService.Reject(req.RequestID, userID.(string))
}
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
}
// GetStatus 获取审批状态
func (h *ApprovalHandler) GetStatus(c *gin.Context) {
requestID := c.Param("id")
result, err := h.approvalService.GetApproval(requestID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "request not found"})
return
}
c.JSON(http.StatusOK, result)
}
// ListPending 获取待审批列表
func (h *ApprovalHandler) ListPending(c *gin.Context) {
result, err := h.approvalService.GetPendingApprovals()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if result == nil {
result = []model.ToolApprovalRequest{}
}
c.JSON(http.StatusOK, gin.H{"pending": result})
}