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/response" ) // CaptainPreferenceHandler handles CaptainPreference REST API endpoints. // Reference: Chatwoot enterprise/app/controllers/api/v1/captain/preferences_controller.rb type CaptainPreferenceHandler struct { svc *service.CaptainPreferenceService } func NewCaptainPreferenceHandler(svc *service.CaptainPreferenceService) *CaptainPreferenceHandler { return &CaptainPreferenceHandler{svc: svc} } // Create creates a new captain preference for an account. // POST /api/v1/accounts/:id/captain/preferences func (h *CaptainPreferenceHandler) Create(c *gin.Context) { accountID := parseAccountIDParam(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } var req service.CreatePreferenceRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } pref, err := h.svc.Create(c.Request.Context(), accountID, &req) if err != nil { applogger.L().Errorf("Create captain preference: %v", err) response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create preference") return } response.OK(c, pref) } // Get retrieves the captain preference for an account. // GET /api/v1/accounts/:id/captain/preferences func (h *CaptainPreferenceHandler) Get(c *gin.Context) { accountID := parseAccountIDParam(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } pref, err := h.svc.GetConfig(c.Request.Context(), accountID) if err != nil { applogger.L().Errorf("Get captain preference: %v", err) response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "preference not found") return } c.JSON(http.StatusOK, pref) } // Update updates the captain preference for an account. // PUT /api/v1/accounts/:id/captain/preferences func (h *CaptainPreferenceHandler) Update(c *gin.Context) { accountID := parseAccountIDParam(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } if !captainPreferencesCanUpdate(c) { response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "administrator role required") return } var req service.UpdateCaptainConfigRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error()) return } pref, err := h.svc.UpdateConfig(c.Request.Context(), accountID, &req) if err != nil { applogger.L().Errorf("Update captain preference: %v", err) c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, pref) } // Delete removes the captain preference for an account. // DELETE /api/v1/accounts/:id/captain/preferences func (h *CaptainPreferenceHandler) Delete(c *gin.Context) { accountID := parseAccountIDParam(c) if accountID == 0 { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } if err := h.svc.Delete(c.Request.Context(), accountID); err != nil { applogger.L().Errorf("Delete captain preference: %v", err) response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete preference") return } response.OK(c, gin.H{"message": "preference deleted"}) } func captainPreferencesCanUpdate(c *gin.Context) bool { role := getRole(c) return role == "administrator" || role == "super_admin" }