Files
gochat/backend/internal/handler/api/v1/captain_preference_handler.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

117 lines
3.7 KiB
Go

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"
}