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.
171 lines
5.9 KiB
Go
171 lines
5.9 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/pagination"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// AutoReplyRuleHandler handles Captain auto-reply rule REST API endpoints.
|
|
// Reference: M12 PRD §Captain AI — Auto-Reply Rules
|
|
type AutoReplyRuleHandler struct {
|
|
svc *service.AutoReplyRuleService
|
|
}
|
|
|
|
// NewAutoReplyRuleHandler creates a new AutoReplyRuleHandler.
|
|
func NewAutoReplyRuleHandler(svc *service.AutoReplyRuleService) *AutoReplyRuleHandler {
|
|
return &AutoReplyRuleHandler{svc: svc}
|
|
}
|
|
|
|
// Create creates a new auto-reply rule for an assistant.
|
|
// POST /api/v1/accounts/:account_id/captain/assistants/:assistant_id/auto_reply_rules
|
|
func (h *AutoReplyRuleHandler) Create(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
var req service.CreateAutoReplyRuleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_request", err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.CreateRule(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Create failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, "create_failed", "Failed to create auto-reply rule")
|
|
return
|
|
}
|
|
|
|
response.Created(c, result)
|
|
}
|
|
|
|
// Get retrieves a single auto-reply rule.
|
|
// GET /api/v1/accounts/:account_id/captain/auto_reply_rules/:rule_id
|
|
func (h *AutoReplyRuleHandler) Get(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
ruleID, err := strconv.ParseUint(c.Param("rule_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_rule_id", "Invalid rule ID")
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.GetRule(c.Request.Context(), uint(accountID), uint(ruleID))
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Get failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusNotFound, "not_found", "Auto-reply rule not found")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// Update updates an existing auto-reply rule.
|
|
// PUT /api/v1/accounts/:account_id/captain/auto_reply_rules/:rule_id
|
|
func (h *AutoReplyRuleHandler) Update(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
ruleID, err := strconv.ParseUint(c.Param("rule_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_rule_id", "Invalid rule ID")
|
|
return
|
|
}
|
|
|
|
var req service.UpdateAutoReplyRuleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_request", err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.UpdateRule(c.Request.Context(), uint(accountID), uint(ruleID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Update failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, "update_failed", "Failed to update auto-reply rule")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// Delete removes an auto-reply rule.
|
|
// DELETE /api/v1/accounts/:account_id/captain/auto_reply_rules/:rule_id
|
|
func (h *AutoReplyRuleHandler) Delete(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
ruleID, err := strconv.ParseUint(c.Param("rule_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_rule_id", "Invalid rule ID")
|
|
return
|
|
}
|
|
|
|
if err := h.svc.DeleteRule(c.Request.Context(), uint(accountID), uint(ruleID)); err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Delete failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, "delete_failed", "Failed to delete auto-reply rule")
|
|
return
|
|
}
|
|
|
|
response.NoContent(c)
|
|
}
|
|
|
|
// List returns auto-reply rules for an account, optionally filtered by assistant.
|
|
// GET /api/v1/accounts/:account_id/captain/auto_reply_rules
|
|
func (h *AutoReplyRuleHandler) List(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
p := pagination.Parse(c)
|
|
results, total, err := h.svc.ListRules(c.Request.Context(), uint(accountID), p.Offset, p.PerPage)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.List failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, "list_failed", "Failed to list auto-reply rules")
|
|
return
|
|
}
|
|
|
|
response.OKWithMeta(c, results, p.Page, p.PerPage, total)
|
|
}
|
|
|
|
// Evaluate checks rules against a conversation context and returns the best match.
|
|
// POST /api/v1/accounts/:account_id/captain/auto_reply_rules/evaluate
|
|
func (h *AutoReplyRuleHandler) Evaluate(c *gin.Context) {
|
|
accountID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
_ = uint(accountID) // scoped by account_id path param; will be used when EvaluateRules gains account-scoping
|
|
|
|
var evalCtx service.AutoReplyEvaluationContext
|
|
if err := c.ShouldBindJSON(&evalCtx); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_request", err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.EvaluateRules(c.Request.Context(), &evalCtx)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Evaluate failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, "evaluate_failed", "Failed to evaluate auto-reply rules")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|