Files
gochat/internal/handler/api/v1/reporting_event_handler.go
T
2026-06-04 15:44:48 +08:00

57 lines
1.6 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
)
// ReportingEventHandler handles reporting event API endpoints.
// Reference: Chatwoot app/controllers/api/v1/accounts/reporting_events_controller.rb
type ReportingEventHandler struct {
svc *service.ReportingEventService
}
// NewReportingEventHandler creates a new ReportingEventHandler.
func NewReportingEventHandler(svc *service.ReportingEventService) *ReportingEventHandler {
return &ReportingEventHandler{svc: svc}
}
// List retrieves reporting events for an account with optional date range filtering.
// GET /api/v1/accounts/:account_id/reporting_events?since=2024-01-01T00:00:00Z&until=2024-12-31T23:59:59Z&metric=message_created
func (h *ReportingEventHandler) List(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
since, until, ok := parseDateRange(c)
if !ok {
return
}
// Check for optional metric filter
metric := c.Query("metric")
if metric != "" {
events, svcErr := h.svc.GetByMetric(c.Request.Context(), accountID, metric, since, until)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{"reporting_events": events})
return
}
events, svcErr := h.svc.ListByAccount(c.Request.Context(), accountID, since, until)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{"reporting_events": events})
}