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

59 lines
1.6 KiB
Go

package v1
import (
"errors"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
"gorm.io/gorm"
)
// YearInReviewHandler serves Chatwoot API v2 year_in_review.
// Reference: app/controllers/api/v2/accounts/year_in_reviews_controller.rb.
type YearInReviewHandler struct {
svc *service.YearInReviewService
}
func NewYearInReviewHandler(svc *service.YearInReviewService) *YearInReviewHandler {
return &YearInReviewHandler{svc: svc}
}
// Show returns the current user's cached or freshly built yearly review payload.
// GET /api/v2/accounts/:account_id/year_in_review?year=YYYY
func (h *YearInReviewHandler) Show(c *gin.Context) {
accountID, ok := parseAccountID(c)
if !ok {
return
}
userID := getUserID(c)
if userID == 0 {
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "user not authenticated")
return
}
year := service.DefaultYearInReviewYear()
if rawYear := c.Query("year"); rawYear != "" {
parsed, err := strconv.Atoi(rawYear)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid year")
return
}
year = parsed
}
data, err := h.svc.Show(c.Request.Context(), accountID, userID, year)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "record not found")
return
}
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to build year in review")
return
}
c.JSON(http.StatusOK, data)
}