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

266 lines
9.0 KiB
Go

package v1
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/service"
applogger "github.com/gochat/gochat/pkg/logger"
)
// WebWidgetThemeHandler handles admin CRUD for widget theme configuration.
// These endpoints require agent JWT authentication and account scope.
// Reference: Chatwoot api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget_config
// extended with theme_config sub-resource
type WebWidgetThemeHandler struct {
widgetSvc *service.WidgetService
inboxSvc *service.InboxService
}
// NewWebWidgetThemeHandler creates a new admin widget theme handler.
func NewWebWidgetThemeHandler(widgetSvc *service.WidgetService, inboxSvc *service.InboxService) *WebWidgetThemeHandler {
return &WebWidgetThemeHandler{widgetSvc: widgetSvc, inboxSvc: inboxSvc}
}
// GetThemeConfig retrieves the custom theme configuration for a web_widget inbox.
// GET /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget/theme_config
func (h *WebWidgetThemeHandler) GetThemeConfig(c *gin.Context) {
inboxIDStr := c.Param("inbox_id")
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
// Verify account scope
accountIDStr := c.Param("id")
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
return
}
// Verify inbox belongs to account
inbox, err := h.inboxSvc.GetByAccountAndID(c.Request.Context(), uint(accountID), uint(inboxID))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "inbox not found"})
return
}
if inbox.ChannelType != "web_widget" {
c.JSON(http.StatusBadRequest, gin.H{"error": "theme config only supported for web_widget channel"})
return
}
// Use WidgetService to fetch theme config — needs inbox.ID (uint)
themeConfig, err := h.widgetSvc.GetThemeConfigByInboxID(c.Request.Context(), uint(inboxID))
if err != nil {
applogger.L().Errorf("Failed to get theme config for inbox=%d: %v", inboxID, err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"theme_config": themeConfig})
}
// UpdateThemeConfig creates or updates the theme configuration for a web_widget inbox.
// PUT /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget/theme_config
func (h *WebWidgetThemeHandler) UpdateThemeConfig(c *gin.Context) {
inboxIDStr := c.Param("inbox_id")
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
accountIDStr := c.Param("id")
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
return
}
// Verify inbox belongs to account
_, err = h.inboxSvc.GetByAccountAndID(c.Request.Context(), uint(accountID), uint(inboxID))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "inbox not found"})
return
}
var config model.WidgetThemeConfig
if err := c.ShouldBindJSON(&config); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body", "details": err.Error()})
return
}
updated, err := h.widgetSvc.UpdateThemeConfig(c.Request.Context(), uint(inboxID), &config)
if err != nil {
applogger.L().Errorf("Failed to update theme config for inbox=%d: %v", inboxID, err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"theme_config": updated})
}
// DeleteThemeConfig removes the custom theme configuration for a web_widget inbox.
// DELETE /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget/theme_config
func (h *WebWidgetThemeHandler) DeleteThemeConfig(c *gin.Context) {
inboxIDStr := c.Param("inbox_id")
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
accountIDStr := c.Param("id")
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
return
}
// Verify inbox belongs to account
_, err = h.inboxSvc.GetByAccountAndID(c.Request.Context(), uint(accountID), uint(inboxID))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "inbox not found"})
return
}
err = h.widgetSvc.DeleteThemeConfig(c.Request.Context(), uint(inboxID))
if err != nil {
applogger.L().Errorf("Failed to delete theme config for inbox=%d: %v", inboxID, err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "theme config deleted"})
}
// --- Admin Pre-Chat Form Handlers ---
// WebWidgetPreChatHandler handles admin CRUD for widget pre-chat form configuration.
// These endpoints require agent JWT authentication and account scope.
type WebWidgetPreChatHandler struct {
widgetSvc *service.WidgetService
inboxSvc *service.InboxService
}
// NewWebWidgetPreChatHandler creates a new admin widget pre-chat form handler.
func NewWebWidgetPreChatHandler(widgetSvc *service.WidgetService, inboxSvc *service.InboxService) *WebWidgetPreChatHandler {
return &WebWidgetPreChatHandler{widgetSvc: widgetSvc, inboxSvc: inboxSvc}
}
// GetPreChatForm retrieves the pre-chat form configuration for a web_widget inbox.
// GET /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget/pre_chat_form
func (h *WebWidgetPreChatHandler) GetPreChatForm(c *gin.Context) {
inboxIDStr := c.Param("inbox_id")
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
accountIDStr := c.Param("id")
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
return
}
// Verify inbox belongs to account
inbox, err := h.inboxSvc.GetByAccountAndID(c.Request.Context(), uint(accountID), uint(inboxID))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "inbox not found"})
return
}
if inbox.ChannelType != "web_widget" {
c.JSON(http.StatusBadRequest, gin.H{"error": "pre-chat form only supported for web_widget channel"})
return
}
form, err := h.widgetSvc.GetPreChatFormByInboxID(c.Request.Context(), uint(inboxID))
if err != nil {
applogger.L().Errorf("Failed to get pre-chat form for inbox=%d: %v", inboxID, err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"pre_chat_form": form})
}
// UpdatePreChatForm creates or updates the pre-chat form configuration for a web_widget inbox.
// PUT /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget/pre_chat_form
func (h *WebWidgetPreChatHandler) UpdatePreChatForm(c *gin.Context) {
inboxIDStr := c.Param("inbox_id")
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
accountIDStr := c.Param("id")
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
return
}
// Verify inbox belongs to account
_, err = h.inboxSvc.GetByAccountAndID(c.Request.Context(), uint(accountID), uint(inboxID))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "inbox not found"})
return
}
var form model.PreChatForm
if err := c.ShouldBindJSON(&form); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body", "details": err.Error()})
return
}
updated, err := h.widgetSvc.UpdatePreChatForm(c.Request.Context(), uint(inboxID), &form)
if err != nil {
applogger.L().Errorf("Failed to update pre-chat form for inbox=%d: %v", inboxID, err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"pre_chat_form": updated})
}
// DeletePreChatForm removes the pre-chat form configuration for a web_widget inbox.
// DELETE /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget/pre_chat_form
func (h *WebWidgetPreChatHandler) DeletePreChatForm(c *gin.Context) {
inboxIDStr := c.Param("inbox_id")
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
return
}
accountIDStr := c.Param("id")
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
return
}
// Verify inbox belongs to account
_, err = h.inboxSvc.GetByAccountAndID(c.Request.Context(), uint(accountID), uint(inboxID))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "inbox not found"})
return
}
err = h.widgetSvc.DeletePreChatForm(c.Request.Context(), uint(inboxID))
if err != nil {
applogger.L().Errorf("Failed to delete pre-chat form for inbox=%d: %v", inboxID, err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "pre-chat form deleted"})
}