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

86 lines
2.8 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
)
// WorkingHourHandler handles API requests for working hours / out-of-office configuration.
// Reference: Chatwoot — working_hours managed via inbox update endpoint
// - GET /api/v1/accounts/{account_id}/inboxes/{inbox_id}/working_hours
// - PUT /api/v1/accounts/{account_id}/inboxes/{inbox_id}/working_hours
type WorkingHourHandler struct {
service *service.WorkingHourService
}
func NewWorkingHourHandler(service *service.WorkingHourService) *WorkingHourHandler {
return &WorkingHourHandler{service: service}
}
// GetWeeklySchedule returns the 7-day working hours schedule for an inbox.
// GET /api/v1/accounts/:account_id/inboxes/:inbox_id/working_hours
// Reference: Chatwoot out_of_offisable.rb — weekly_schedule
func (h *WorkingHourHandler) GetWeeklySchedule(c *gin.Context) {
inboxID, err := parseUintParam(c, "inbox_id")
if err != nil || inboxID == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
schedule, err := h.service.GetWeeklySchedule(c.Request.Context(), inboxID)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "failed to fetch working hours"})
return
}
c.JSON(http.StatusOK, schedule)
}
// UpdateWeeklySchedule bulk-updates the working hours schedule for an inbox.
// PUT /api/v1/accounts/:account_id/inboxes/:inbox_id/working_hours
// Reference: Chatwoot out_of_offisable.rb — update_working_hours(params)
func (h *WorkingHourHandler) UpdateWeeklySchedule(c *gin.Context) {
inboxID, err := parseUintParam(c, "inbox_id")
if err != nil || inboxID == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
var params []repository.WorkingHourUpdateParam
if err := c.ShouldBindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body: " + err.Error()})
return
}
schedule, err := h.service.UpdateWeeklySchedule(c.Request.Context(), inboxID, params)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, schedule)
}
// IsOutOfOffice checks whether an inbox is currently out of office.
// GET /api/v1/accounts/:account_id/inboxes/:inbox_id/out_of_office
func (h *WorkingHourHandler) IsOutOfOffice(c *gin.Context) {
inboxID, err := parseUintParam(c, "inbox_id")
if err != nil || inboxID == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
outOfOffice, err := h.service.IsOutOfOffice(c.Request.Context(), inboxID)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "failed to check out-of-office status"})
return
}
c.JSON(http.StatusOK, gin.H{
"out_of_office": outOfOffice,
"working_now": !outOfOffice,
})
}