88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
|
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/muyuqingfeng/iloom/shared/pkg/response"
|
||
|
|
ws "github.com/muyuqingfeng/iloom/shared/pkg/websocket"
|
||
|
|
"github.com/muyuqingfeng/iloom/textile-service/internal/service"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ProcessStepHandler struct {
|
||
|
|
svc *service.ProcessService
|
||
|
|
wsHub *ws.Hub
|
||
|
|
}
|
||
|
|
|
||
|
|
type CompleteStepRequest struct {
|
||
|
|
Notes string `json:"notes"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type RejectStepRequest struct {
|
||
|
|
Notes string `json:"notes" binding:"required"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewProcessStepHandler(svc *service.ProcessService, wsHub *ws.Hub) *ProcessStepHandler {
|
||
|
|
return &ProcessStepHandler{svc: svc, wsHub: wsHub}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *ProcessStepHandler) Complete(c *gin.Context) {
|
||
|
|
companyID := c.GetHeader("X-Company-ID")
|
||
|
|
userID := c.GetHeader("X-User-ID")
|
||
|
|
if companyID == "" || userID == "" {
|
||
|
|
response.BadRequest(c, "missing required headers")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
planID := c.Param("id")
|
||
|
|
stepID := c.Param("stepId")
|
||
|
|
|
||
|
|
var req CompleteStepRequest
|
||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
|
// Notes is optional, allow empty body
|
||
|
|
req = CompleteStepRequest{}
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := h.svc.CompleteStep(c.Request.Context(), planID, stepID, companyID, userID, req.Notes); err != nil {
|
||
|
|
response.Error(c, http.StatusBadRequest, 40000, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
h.wsHub.BroadcastToCompany(companyID, &ws.Event{
|
||
|
|
Table: "plan_process_steps",
|
||
|
|
Action: "UPDATE",
|
||
|
|
})
|
||
|
|
|
||
|
|
response.OK(c, gin.H{"message": "step completed"})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *ProcessStepHandler) Reject(c *gin.Context) {
|
||
|
|
companyID := c.GetHeader("X-Company-ID")
|
||
|
|
userID := c.GetHeader("X-User-ID")
|
||
|
|
if companyID == "" || userID == "" {
|
||
|
|
response.BadRequest(c, "missing required headers")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
planID := c.Param("id")
|
||
|
|
stepID := c.Param("stepId")
|
||
|
|
|
||
|
|
var req RejectStepRequest
|
||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
|
response.BadRequest(c, "notes is required for rejection")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := h.svc.RejectStep(c.Request.Context(), planID, stepID, companyID, userID, req.Notes); err != nil {
|
||
|
|
response.Error(c, http.StatusBadRequest, 40000, err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
h.wsHub.BroadcastToCompany(companyID, &ws.Event{
|
||
|
|
Table: "plan_process_steps",
|
||
|
|
Action: "UPDATE",
|
||
|
|
})
|
||
|
|
|
||
|
|
response.OK(c, gin.H{"message": "step rejected"})
|
||
|
|
}
|