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

147 lines
4.7 KiB
Go

package v1
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"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.ShouldBind(&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
}
c.JSON(http.StatusOK, h.slackAppPayload(c, accountID, []model.IntegrationHook{*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.ShouldBind(&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 {
if errors.Is(svcErr, service.ErrSlackInvalidChannel) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "Invalid slack channel. Please try again"})
return
}
handleServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, h.slackAppPayload(c, accountID, []model.IntegrationHook{*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
}
c.Status(http.StatusOK)
}
// 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
}
c.JSON(http.StatusOK, channels)
}
// RegisterSlackIntegrationRoutes registers Slack integration routes.
func RegisterSlackIntegrationRoutes(g *gin.RouterGroup, h *SlackIntegrationHandler) {
g.POST("/slack", h.Create)
g.PATCH("/slack", h.Update)
g.PUT("/slack", h.Update)
g.DELETE("/slack", h.Delete)
slack := g.Group("/slack")
{
slack.POST("/", h.Create)
slack.PATCH("/", h.Update)
slack.PUT("/", h.Update)
slack.DELETE("/", h.Delete)
slack.GET("/list_all_channels", h.ListAllChannels)
}
}
func (h *SlackIntegrationHandler) slackAppPayload(c *gin.Context, accountID uint, fallback []model.IntegrationHook) gin.H {
hooks, err := h.svc.ListHooks(c.Request.Context(), accountID)
if err != nil || len(hooks) == 0 {
hooks = fallback
}
serializedHooks := make([]gin.H, 0, len(hooks))
for _, hook := range hooks {
serializedHooks = append(serializedHooks, serializeIntegrationHook(hook))
}
return gin.H{
"id": "slack",
"name": "Slack",
"description": "Connect Slack channels for real-time notifications",
"short_description": "Connect Slack channels for real-time notifications",
"enabled": len(serializedHooks) > 0,
"hooks": serializedHooks,
"hook_type": integrationAppHookType("slack"),
"allow_multiple_hooks": integrationAppAllowsMultipleHooks("slack"),
"settings_form_schema": integrationAppSettingsFormSchema("slack"),
"visible_properties": integrationAppVisibleProperties("slack"),
}
}