Files
gochat/internal/handler/api/v1/working_hour_handler.go
T
2026-06-04 15:44:48 +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,
})
}