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.
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/canned"
|
||||
applogger "github.com/gochat/gochat/pkg/logger"
|
||||
"github.com/gochat/gochat/pkg/response"
|
||||
)
|
||||
|
||||
// CannedResponseHandler handles CannedResponse CRUD + search endpoints.
|
||||
// Reference: Chatwoot CannedResponsesController — CRUD + search by short_code/content
|
||||
type CannedResponseHandler struct {
|
||||
svc *canned.CannedResponseService
|
||||
}
|
||||
|
||||
type cannedResponseInput struct {
|
||||
ShortCode *string `json:"short_code"`
|
||||
Content *string `json:"content"`
|
||||
}
|
||||
|
||||
type cannedResponsePayload struct {
|
||||
ID uint `json:"id"`
|
||||
AccountID uint `json:"account_id"`
|
||||
ShortCode string `json:"short_code"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// NewCannedResponseHandler creates a new CannedResponseHandler.
|
||||
func NewCannedResponseHandler(svc *canned.CannedResponseService) *CannedResponseHandler {
|
||||
return &CannedResponseHandler{svc: svc}
|
||||
}
|
||||
|
||||
func bindCannedResponseInput(c *gin.Context) (cannedResponseInput, error) {
|
||||
var body map[string]json.RawMessage
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
return cannedResponseInput{}, err
|
||||
}
|
||||
|
||||
if raw, ok := body["canned_response"]; ok {
|
||||
var wrapped cannedResponseInput
|
||||
if err := json.Unmarshal(raw, &wrapped); err != nil {
|
||||
return cannedResponseInput{}, err
|
||||
}
|
||||
return wrapped, nil
|
||||
}
|
||||
|
||||
var input cannedResponseInput
|
||||
if raw, ok := body["short_code"]; ok {
|
||||
if err := json.Unmarshal(raw, &input.ShortCode); err != nil {
|
||||
return cannedResponseInput{}, err
|
||||
}
|
||||
}
|
||||
if raw, ok := body["content"]; ok {
|
||||
if err := json.Unmarshal(raw, &input.Content); err != nil {
|
||||
return cannedResponseInput{}, err
|
||||
}
|
||||
}
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func serializeCannedResponse(cr canned.CannedResponse) cannedResponsePayload {
|
||||
return cannedResponsePayload{
|
||||
ID: cr.ID,
|
||||
AccountID: cr.AccountID,
|
||||
ShortCode: cr.ShortCode,
|
||||
Content: cr.Content,
|
||||
CreatedAt: cr.CreatedAt,
|
||||
UpdatedAt: cr.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func serializeCannedResponses(responses []canned.CannedResponse) []cannedResponsePayload {
|
||||
payload := make([]cannedResponsePayload, 0, len(responses))
|
||||
for _, cr := range responses {
|
||||
payload = append(payload, serializeCannedResponse(cr))
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
// Create creates a new canned response.
|
||||
// POST /api/v1/accounts/:account_id/canned_responses
|
||||
// Reference: Chatwoot CannedResponsesController#create
|
||||
func (h *CannedResponseHandler) Create(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
req, err := bindCannedResponseInput(c)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
if req.ShortCode == nil || *req.ShortCode == "" || req.Content == nil || *req.Content == "" {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "short_code and content are required")
|
||||
return
|
||||
}
|
||||
|
||||
cr := &canned.CannedResponse{
|
||||
AccountID: accountID,
|
||||
ShortCode: *req.ShortCode,
|
||||
Content: *req.Content,
|
||||
}
|
||||
|
||||
if err := h.svc.Create(c.Request.Context(), cr); err != nil {
|
||||
applogger.L().Errorf("Create canned response: %v", err)
|
||||
handleServiceError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, serializeCannedResponse(*cr))
|
||||
}
|
||||
|
||||
// Get retrieves a canned response by ID.
|
||||
// GET /api/v1/accounts/:account_id/canned_responses/:id
|
||||
// Reference: Chatwoot CannedResponsesController#show
|
||||
func (h *CannedResponseHandler) Get(c *gin.Context) {
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
cr, svcErr := h.svc.GetByAccountAndID(c.Request.Context(), accountID, id)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, serializeCannedResponse(*cr))
|
||||
}
|
||||
|
||||
// Update updates a canned response.
|
||||
// PUT /api/v1/accounts/:account_id/canned_responses/:id
|
||||
// Reference: Chatwoot CannedResponsesController#update
|
||||
func (h *CannedResponseHandler) Update(c *gin.Context) {
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
req, err := bindCannedResponseInput(c)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{}
|
||||
if req.ShortCode != nil {
|
||||
updates["short_code"] = *req.ShortCode
|
||||
}
|
||||
if req.Content != nil {
|
||||
updates["content"] = *req.Content
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "no fields to update")
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.svc.UpdateByAccountAndID(c.Request.Context(), accountID, id, updates); svcErr != nil {
|
||||
applogger.L().Errorf("Update canned response: %v", svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
cr, svcErr := h.svc.GetByAccountAndID(c.Request.Context(), accountID, id)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, serializeCannedResponse(*cr))
|
||||
}
|
||||
|
||||
// Delete soft-deletes a canned response.
|
||||
// DELETE /api/v1/accounts/:account_id/canned_responses/:id
|
||||
// Reference: Chatwoot CannedResponsesController#destroy
|
||||
func (h *CannedResponseHandler) Delete(c *gin.Context) {
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.svc.DeleteByAccountAndID(c.Request.Context(), accountID, id); svcErr != nil {
|
||||
applogger.L().Errorf("Delete canned response: %v", svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// List returns all canned responses for an account.
|
||||
// GET /api/v1/accounts/:account_id/canned_responses
|
||||
// Reference: Chatwoot CannedResponsesController#index
|
||||
func (h *CannedResponseHandler) List(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
searchQuery := c.Query("search")
|
||||
if searchQuery == "" {
|
||||
searchQuery = c.Query("q")
|
||||
}
|
||||
if searchQuery != "" {
|
||||
responses, svcErr := h.svc.Search(c.Request.Context(), accountID, searchQuery)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, serializeCannedResponses(responses))
|
||||
return
|
||||
}
|
||||
|
||||
responses, svcErr := h.svc.ListByAccount(c.Request.Context(), accountID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, serializeCannedResponses(responses))
|
||||
}
|
||||
|
||||
// Search performs a ranked search over canned responses.
|
||||
// GET /api/v1/accounts/:account_id/canned_responses/search?q=...
|
||||
// Reference: Chatwoot CannedResponsesController#index with search parameter (order_by_search scope)
|
||||
func (h *CannedResponseHandler) Search(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
searchQuery := c.Query("search")
|
||||
if searchQuery == "" {
|
||||
searchQuery = c.Query("q")
|
||||
}
|
||||
responses, svcErr := h.svc.Search(c.Request.Context(), accountID, searchQuery)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, serializeCannedResponses(responses))
|
||||
}
|
||||
Reference in New Issue
Block a user