second commit
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
applogger "github.com/gochat/gochat/pkg/logger"
|
||||
)
|
||||
|
||||
// WebWidgetHandler handles WebWidget-specific configuration CRUD.
|
||||
// Reference: Chatwoot app/controllers/api/v1/inboxes/web_widget_config_controller.rb
|
||||
// WebWidget is a channel type managed through the Inbox model, where channel_type="web_widget"
|
||||
// and widget-specific settings (website_token, hmac_token, widget_color, etc.) are stored
|
||||
// in the Inbox's channel_config JSONB field.
|
||||
type WebWidgetHandler struct {
|
||||
inboxSvc *service.InboxService
|
||||
}
|
||||
|
||||
// NewWebWidgetHandler creates a new WebWidget handler.
|
||||
func NewWebWidgetHandler(inboxSvc *service.InboxService) *WebWidgetHandler {
|
||||
return &WebWidgetHandler{inboxSvc: inboxSvc}
|
||||
}
|
||||
|
||||
// CreateWebWidgetInbox creates a new inbox with channel_type=web_widget.
|
||||
// Reference: Chatwoot inboxes#create — when channel_type is web_widget,
|
||||
// it auto-generates website_token and hmac_token and stores them in channel_config.
|
||||
//
|
||||
// POST /api/v1/accounts/:account_id/inboxes/web_widget
|
||||
func (h *WebWidgetHandler) CreateWebWidgetInbox(c *gin.Context) {
|
||||
accountIDStr := c.Param("id")
|
||||
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Invalid account_id: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
|
||||
return
|
||||
}
|
||||
|
||||
var req service.CreateWebWidgetInboxRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
applogger.L().Errorf("Failed to bind request: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
inbox, err := h.inboxSvc.CreateWebWidgetInbox(c.Request.Context(), uint(accountID), req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Failed to create web_widget inbox: %v", err)
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, inbox)
|
||||
}
|
||||
|
||||
// GetWebWidgetConfig retrieves the web_widget channel_config for an inbox.
|
||||
// Reference: Chatwoot api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget_config#show
|
||||
//
|
||||
// GET /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget_config
|
||||
func (h *WebWidgetHandler) GetWebWidgetConfig(c *gin.Context) {
|
||||
accountIDStr := c.Param("id")
|
||||
inboxIDStr := c.Param("inbox_id")
|
||||
|
||||
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Invalid account_id: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
|
||||
return
|
||||
}
|
||||
|
||||
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Invalid inbox_id: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
|
||||
return
|
||||
}
|
||||
|
||||
config, err := h.inboxSvc.GetWebWidgetConfig(c.Request.Context(), uint(accountID), uint(inboxID))
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Failed to get web_widget config: %v", err)
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, config)
|
||||
}
|
||||
|
||||
// UpdateWebWidgetConfig updates the web_widget channel_config for an inbox.
|
||||
// Reference: Chatwoot api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget_config#update
|
||||
// Allows updating widget_color, welcome_title, welcome_tagline, greeting_enabled,
|
||||
// greeting_message, reply_time, pre_chat_message, pre_chat_fields_enabled, etc.
|
||||
//
|
||||
// PUT /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget_config
|
||||
func (h *WebWidgetHandler) UpdateWebWidgetConfig(c *gin.Context) {
|
||||
accountIDStr := c.Param("id")
|
||||
inboxIDStr := c.Param("inbox_id")
|
||||
|
||||
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Invalid account_id: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
|
||||
return
|
||||
}
|
||||
|
||||
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Invalid inbox_id: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
|
||||
return
|
||||
}
|
||||
|
||||
var req service.UpdateWebWidgetConfigRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
applogger.L().Errorf("Failed to bind request: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
inbox, err := h.inboxSvc.UpdateWebWidgetConfig(c.Request.Context(), uint(accountID), uint(inboxID), req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Failed to update web_widget config: %v", err)
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, inbox)
|
||||
}
|
||||
|
||||
// DeleteWebWidgetInbox deletes a web_widget inbox (soft-delete via Inbox.Delete).
|
||||
// Reference: Chatwoot inboxes#destroy
|
||||
//
|
||||
// DELETE /api/v1/accounts/:account_id/inboxes/:inbox_id/web_widget
|
||||
func (h *WebWidgetHandler) DeleteWebWidgetInbox(c *gin.Context) {
|
||||
accountIDStr := c.Param("id")
|
||||
inboxIDStr := c.Param("inbox_id")
|
||||
|
||||
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Invalid account_id: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid account_id"})
|
||||
return
|
||||
}
|
||||
|
||||
inboxID, err := strconv.ParseUint(inboxIDStr, 10, 64)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Invalid inbox_id: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid inbox_id"})
|
||||
return
|
||||
}
|
||||
|
||||
err = h.inboxSvc.DeleteByAccount(c.Request.Context(), uint(accountID), uint(inboxID))
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Failed to delete web_widget inbox: %v", err)
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"id": inboxID, "deleted": true})
|
||||
}
|
||||
Reference in New Issue
Block a user