Files
gochat/backend/internal/handler/api/v1/assignment_policy_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

410 lines
13 KiB
Go

package v1
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
applogger "github.com/gochat/gochat/pkg/logger"
"github.com/gochat/gochat/pkg/response"
)
// AssignmentPolicyHandler handles AssignmentPolicy CRUD + auto-assignment actions.
// Reference: Chatwoot app/controllers/api/v1/assignment_policies_controller.rb
type AssignmentPolicyHandler struct {
svc *service.AssignmentPolicyService
}
// ListAccountPolicies returns all assignment policies for the account.
// GET /api/v1/accounts/:id/assignment_policies
func (h *AssignmentPolicyHandler) ListAccountPolicies(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
policies, svcErr := h.svc.ListAccountPolicies(c.Request.Context(), accountID)
if svcErr != nil {
applogger.L().Errorf("List assignment policies for account %d: %v", accountID, svcErr)
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, policies)
}
// NewAssignmentPolicyHandler creates a new AssignmentPolicy handler.
func NewAssignmentPolicyHandler(svc *service.AssignmentPolicyService) *AssignmentPolicyHandler {
return &AssignmentPolicyHandler{svc: svc}
}
// GetAccountPolicy returns the account-level assignment policy.
// GET /api/v1/accounts/:id/assignment_policy
func (h *AssignmentPolicyHandler) GetAccountPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
var policyID uint
if raw := c.Param("policy_id"); raw != "" {
id, err := strconv.ParseUint(raw, 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
return
}
policyID = uint(id)
}
policy, svcErr := h.svc.GetAccountPolicy(c.Request.Context(), accountID, policyID)
if svcErr != nil {
applogger.L().Errorf("Get assignment policy for account %d: %v", accountID, svcErr)
handleServiceError(c, svcErr)
return
}
payload, svcErr := h.svc.SerializePolicy(c.Request.Context(), policy)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, payload)
}
// CreateAccountPolicy creates the account-level assignment policy.
// POST /api/v1/accounts/:id/assignment_policy
func (h *AssignmentPolicyHandler) CreateAccountPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
req, err := bindAssignmentPolicyCreate(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
policy, svcErr := h.svc.CreateAccountPolicy(c.Request.Context(), accountID, *req)
if svcErr != nil {
applogger.L().Errorf("Create assignment policy for account %d: %v", accountID, svcErr)
handleServiceError(c, svcErr)
return
}
payload, svcErr := h.svc.SerializePolicy(c.Request.Context(), policy)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, payload)
}
// UpdateAccountPolicy updates the account-level assignment policy.
// PUT /api/v1/accounts/:id/assignment_policy
func (h *AssignmentPolicyHandler) UpdateAccountPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
return
}
req, err := bindAssignmentPolicyUpdate(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
policy, svcErr := h.svc.UpdateAccountPolicy(c.Request.Context(), uint(id), accountID, *req)
if svcErr != nil {
applogger.L().Errorf("Update assignment policy %d for account %d: %v", id, accountID, svcErr)
handleServiceError(c, svcErr)
return
}
payload, svcErr := h.svc.SerializePolicy(c.Request.Context(), policy)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, payload)
}
// DeleteAccountPolicy deletes the account-level assignment policy.
// DELETE /api/v1/accounts/:id/assignment_policy/:policy_id
func (h *AssignmentPolicyHandler) DeleteAccountPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
return
}
if svcErr := h.svc.DeleteAccountPolicy(c.Request.Context(), uint(id), accountID); svcErr != nil {
applogger.L().Errorf("Delete assignment policy %d for account %d: %v", id, accountID, svcErr)
handleServiceError(c, svcErr)
return
}
c.Status(http.StatusOK)
}
// GetInboxPolicy returns the inbox-level assignment policy override.
// GET /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy
func (h *AssignmentPolicyHandler) GetInboxPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
inboxID, err := strconv.ParseUint(c.Param("inbox_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox ID")
return
}
policy, svcErr := h.svc.GetInboxPolicy(c.Request.Context(), accountID, uint(inboxID))
if svcErr != nil {
applogger.L().Errorf("Get inbox assignment policy for inbox %d: %v", inboxID, svcErr)
handleServiceError(c, svcErr)
return
}
payload, svcErr := h.svc.SerializePolicy(c.Request.Context(), policy)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, payload)
}
// ListPolicyInboxes returns Chatwoot `{ inboxes: [...] }` payload for a policy.
// GET /api/v1/accounts/:id/assignment_policies/:policy_id/inboxes
func (h *AssignmentPolicyHandler) ListPolicyInboxes(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
policyID, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
return
}
inboxes, svcErr := h.svc.ListPolicyInboxes(c.Request.Context(), accountID, uint(policyID))
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, gin.H{"inboxes": inboxes})
}
// AddPolicyInbox associates an inbox with a policy through the nested policy route.
// POST /api/v1/accounts/:id/assignment_policies/:policy_id/inboxes
func (h *AssignmentPolicyHandler) AddPolicyInbox(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
policyID, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
return
}
var req service.CreateInboxPolicyRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
req.AssignmentPolicyID = uint(policyID)
policy, svcErr := h.svc.CreateInboxPolicy(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
payload, svcErr := h.svc.SerializePolicy(c.Request.Context(), policy)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, payload)
}
// RemovePolicyInbox removes the current policy association from an inbox.
// DELETE /api/v1/accounts/:id/assignment_policies/:policy_id/inboxes/:inbox_id
func (h *AssignmentPolicyHandler) RemovePolicyInbox(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
inboxID, err := strconv.ParseUint(c.Param("inbox_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox ID")
return
}
if svcErr := h.svc.DeleteInboxPolicy(c.Request.Context(), uint(inboxID), accountID); svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.Status(http.StatusOK)
}
// CreateInboxPolicy creates an inbox-level assignment policy override.
// POST /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy
func (h *AssignmentPolicyHandler) CreateInboxPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
inboxID, err := strconv.ParseUint(c.Param("inbox_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox ID")
return
}
var req service.CreateInboxPolicyRequest
req.InboxID = uint(inboxID) // Set from URL param
if err := bindAssignmentPolicyBody(c, &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
policy, svcErr := h.svc.CreateInboxPolicy(c.Request.Context(), accountID, req)
if svcErr != nil {
applogger.L().Errorf("Create inbox assignment policy for inbox %d: %v", inboxID, svcErr)
handleServiceError(c, svcErr)
return
}
payload, svcErr := h.svc.SerializePolicy(c.Request.Context(), policy)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, payload)
}
// UpdateInboxPolicy updates an inbox-level assignment policy override.
// PUT /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy/:policy_id
func (h *AssignmentPolicyHandler) UpdateInboxPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
return
}
var req service.UpdateInboxPolicyRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
policy, svcErr := h.svc.UpdateInboxPolicy(c.Request.Context(), uint(id), accountID, req)
if svcErr != nil {
applogger.L().Errorf("Update inbox assignment policy %d: %v", id, svcErr)
handleServiceError(c, svcErr)
return
}
response.OK(c, policy)
}
// DeleteInboxPolicy deletes an inbox-level assignment policy override.
// DELETE /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy/:policy_id
func (h *AssignmentPolicyHandler) DeleteInboxPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
return
}
if svcErr := h.svc.DeleteInboxPolicy(c.Request.Context(), uint(id), accountID); svcErr != nil {
applogger.L().Errorf("Delete inbox assignment policy %d: %v", id, svcErr)
handleServiceError(c, svcErr)
return
}
c.Status(http.StatusOK)
}
// DeleteCurrentInboxPolicy removes the policy association for the URL inbox.
// DELETE /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy
func (h *AssignmentPolicyHandler) DeleteCurrentInboxPolicy(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
return
}
inboxID, err := strconv.ParseUint(c.Param("inbox_id"), 10, 32)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox ID")
return
}
if svcErr := h.svc.DeleteInboxPolicy(c.Request.Context(), uint(inboxID), accountID); svcErr != nil {
handleServiceError(c, svcErr)
return
}
c.Status(http.StatusOK)
}
func bindAssignmentPolicyCreate(c *gin.Context) (*service.CreatePolicyRequest, error) {
var req service.CreatePolicyRequest
if err := bindChatwootPayload(c, "assignment_policy", &req); err != nil {
return nil, err
}
return &req, nil
}
func bindAssignmentPolicyUpdate(c *gin.Context) (*service.UpdatePolicyRequest, error) {
var req service.UpdatePolicyRequest
if err := bindChatwootPayload(c, "assignment_policy", &req); err != nil {
return nil, err
}
return &req, nil
}
func bindAssignmentPolicyBody(c *gin.Context, req *service.CreateInboxPolicyRequest) error {
var body struct {
AssignmentPolicyID uint `json:"assignment_policy_id"`
}
if err := c.ShouldBindJSON(&body); err != nil {
return err
}
req.AssignmentPolicyID = body.AssignmentPolicyID
return nil
}