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.
93 lines
3.0 KiB
Go
93 lines
3.0 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"
|
|
)
|
|
|
|
// LiveReportHandler handles live/real-time reporting endpoints.
|
|
// Reference: Chatwoot app/controllers/api/v2/accounts/live_reports_controller.rb
|
|
type LiveReportHandler struct {
|
|
svc *service.AnalyticsService
|
|
}
|
|
|
|
func NewLiveReportHandler(svc *service.AnalyticsService) *LiveReportHandler {
|
|
return &LiveReportHandler{svc: svc}
|
|
}
|
|
|
|
// ConversationMetrics returns real-time conversation counts (open, unattended, unassigned, pending).
|
|
// GET /api/v1/accounts/:account_id/live_reports/conversation_metrics
|
|
// Reference: Chatwoot live_reports#conversation_metrics — { open, unattended, unassigned, pending }
|
|
func (h *LiveReportHandler) ConversationMetrics(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
teamID, ok := parseLiveReportTeamID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.GetConversationMetricsForTeam(c.Request.Context(), accountID, teamID)
|
|
if err != nil {
|
|
applogger.L().Errorf("Live conversation metrics: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to get conversation metrics")
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GroupedConversationMetrics returns conversation metrics grouped by team_id or assignee_id.
|
|
// GET /api/v1/accounts/:account_id/live_reports/grouped_conversation_metrics
|
|
// Reference: Chatwoot live_reports#grouped_conversation_metrics — group_by=team_id|assignee_id
|
|
// Returns array of { group_by_field, open, unattended, unassigned }
|
|
func (h *LiveReportHandler) GroupedConversationMetrics(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
groupBy := c.Query("group_by")
|
|
if groupBy != "team_id" && groupBy != "assignee_id" {
|
|
// Reference: Chatwoot returns 422 with a raw { error } body for invalid group_by.
|
|
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{"error": "invalid group_by"})
|
|
return
|
|
}
|
|
teamID, ok := parseLiveReportTeamID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.GetGroupedConversationMetricsForTeam(c.Request.Context(), accountID, groupBy, teamID)
|
|
if err != nil {
|
|
applogger.L().Errorf("Grouped conversation metrics: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to get grouped conversation metrics")
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
func parseLiveReportTeamID(c *gin.Context) (uint, bool) {
|
|
teamIDStr := c.Query("team_id")
|
|
if teamIDStr == "" {
|
|
return 0, true
|
|
}
|
|
parsed, err := strconv.ParseUint(teamIDStr, 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid team_id")
|
|
return 0, false
|
|
}
|
|
return uint(parsed), true
|
|
}
|