package handler import ( "log" "net/http" "github.com/gin-gonic/gin" "github.com/muyuqingfeng/iloom/shared/pkg/response" "github.com/muyuqingfeng/iloom/shared/pkg/websocket" "github.com/muyuqingfeng/iloom/washing-service/internal/service" ) type CompletionHandler struct { svc *service.CompletionService wsHub *websocket.Hub } func NewCompletionHandler(svc *service.CompletionService, wsHub *websocket.Hub) *CompletionHandler { return &CompletionHandler{svc: svc, wsHub: wsHub} } func (h *CompletionHandler) Complete(c *gin.Context) { companyID := c.GetHeader("X-Company-ID") userID := c.GetHeader("X-User-ID") if companyID == "" { response.BadRequest(c, "missing company id") return } washingPlanID := c.Param("id") var req service.CompleteRequest if err := c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, err.Error()) return } completion, err := h.svc.Complete(c.Request.Context(), washingPlanID, companyID, userID, req) if err != nil { log.Printf("complete washing plan error: %v", err) response.InternalError(c, "failed to complete washing plan") return } h.wsHub.BroadcastToCompany(companyID, &websocket.Event{ Table: "washing_plan_completions", Action: "INSERT", }) c.JSON(http.StatusCreated, response.Response{ Code: 0, Message: "success", Data: completion, }) }