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

112 lines
3.3 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
)
// SlackIntegrationHandler handles Slack integration endpoints.
// Reference: Chatwoot Integrations::SlackController
type SlackIntegrationHandler struct {
svc *service.SlackIntegrationService
}
// NewSlackIntegrationHandler creates a new SlackIntegrationHandler.
func NewSlackIntegrationHandler(svc *service.SlackIntegrationService) *SlackIntegrationHandler {
return &SlackIntegrationHandler{svc: svc}
}
// Create creates a Slack integration for an account.
// POST /api/v1/accounts/:account_id/integrations/slack
func (h *SlackIntegrationHandler) Create(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req service.CreateSlackRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
hook, svcErr := h.svc.Create(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, hook)
}
// Update updates a Slack integration for an account.
// PATCH /api/v1/accounts/:account_id/integrations/slack
func (h *SlackIntegrationHandler) Update(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req service.UpdateSlackRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
hook, svcErr := h.svc.Update(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, hook)
}
// Delete removes a Slack integration for an account.
// DELETE /api/v1/accounts/:account_id/integrations/slack
func (h *SlackIntegrationHandler) Delete(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
if svcErr := h.svc.Delete(c.Request.Context(), accountID); svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{"message": "Slack integration deleted"})
}
// ListAllChannels lists available Slack channels.
// GET /api/v1/accounts/:account_id/integrations/slack/list_all_channels
func (h *SlackIntegrationHandler) ListAllChannels(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
channels, svcErr := h.svc.ListAllChannels(c.Request.Context(), accountID)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, channels)
}
// RegisterSlackIntegrationRoutes registers Slack integration routes.
func RegisterSlackIntegrationRoutes(g *gin.RouterGroup, h *SlackIntegrationHandler) {
slack := g.Group("/slack")
{
slack.POST("/", h.Create)
slack.PATCH("/", h.Update)
slack.DELETE("/", h.Delete)
slack.GET("/list_all_channels", h.ListAllChannels)
}
}