feat(integrations): align app hook payloads

This commit is contained in:
2026-06-06 06:46:36 +08:00
parent 02ea135e0c
commit d241925b6a
12 changed files with 504 additions and 132 deletions
@@ -1,11 +1,13 @@
package v1
import (
"encoding/json"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/pagination"
"github.com/gochat/gochat/pkg/response"
@@ -37,24 +39,19 @@ func (h *IntegrationHookHandler) ListApps(c *gin.Context) {
handleServiceError(c, err)
return
}
response.OK(c, apps)
accountID := getAccountID(c)
c.JSON(http.StatusOK, gin.H{"payload": h.serializeIntegrationApps(c, accountID, apps)})
}
// GetApp retrieves a single integration app by ID.
// GET /api/v1/accounts/:account_id/integrations/apps/:id
func (h *IntegrationHookHandler) GetApp(c *gin.Context) {
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
app, svcErr := h.svc.GetApp(c.Request.Context(), id)
app, svcErr := h.svc.GetAppByID(c.Request.Context(), c.Param("id"))
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, app)
c.JSON(http.StatusOK, h.serializeIntegrationApp(c, getAccountID(c), *app))
}
// --- Integration Hooks CRUD ---
@@ -86,12 +83,12 @@ func (h *IntegrationHookHandler) GetHook(c *gin.Context) {
return
}
hook, svcErr := h.svc.Get(c.Request.Context(), id)
hook, svcErr := h.svc.GetScoped(c.Request.Context(), getAccountID(c), id)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, hook)
c.JSON(http.StatusOK, serializeIntegrationHook(*hook))
}
// CreateHook creates a new integration hook.
@@ -103,22 +100,18 @@ func (h *IntegrationHookHandler) CreateHook(c *gin.Context) {
return
}
// Chatwoot: params.require(:hook) → {"hook": {...}}
var wrapper struct {
Hook service.CreateHookRequest `json:"hook"`
}
if err := c.ShouldBindJSON(&wrapper); err != nil {
var req service.CreateHookRequest
if err := bindJSONWrappedOrRaw(c, "hook", &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
req := wrapper.Hook
hook, svcErr := h.svc.Create(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, hook)
c.JSON(http.StatusOK, serializeIntegrationHook(*hook))
}
// UpdateHook updates an existing integration hook.
@@ -130,22 +123,19 @@ func (h *IntegrationHookHandler) UpdateHook(c *gin.Context) {
return
}
// Chatwoot: params.require(:hook) → {"hook": {...}}
var wrapper struct {
Hook service.UpdateHookRequest `json:"hook"`
}
if err := c.ShouldBindJSON(&wrapper); err != nil {
accountID := getAccountID(c)
var req service.UpdateHookRequest
if err := bindJSONWrappedOrRaw(c, "hook", &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
req := wrapper.Hook
hook, svcErr := h.svc.Update(c.Request.Context(), id, req)
hook, svcErr := h.svc.UpdateScoped(c.Request.Context(), accountID, id, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, hook)
c.JSON(http.StatusOK, serializeIntegrationHook(*hook))
}
// DeleteHook deletes an integration hook.
@@ -157,11 +147,11 @@ func (h *IntegrationHookHandler) DeleteHook(c *gin.Context) {
return
}
if svcErr := h.svc.Delete(c.Request.Context(), id); svcErr != nil {
if svcErr := h.svc.DeleteScoped(c.Request.Context(), getAccountID(c), id); svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{"id": id})
c.Status(http.StatusOK)
}
// ProcessHookEvent processes an incoming event for a hook.
@@ -187,7 +177,143 @@ func (h *IntegrationHookHandler) ProcessHookEvent(c *gin.Context) {
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, response.ErrInternal, svcErr.Error())
return
}
response.OK(c, gin.H{"message": "event processed"})
c.JSON(http.StatusOK, gin.H{"message": "event processed"})
}
func (h *IntegrationHookHandler) serializeIntegrationApps(c *gin.Context, accountID uint, apps []model.IntegrationApp) []gin.H {
payload := make([]gin.H, 0, len(apps))
for _, app := range apps {
payload = append(payload, h.serializeIntegrationApp(c, accountID, app))
}
return payload
}
func (h *IntegrationHookHandler) serializeIntegrationApp(c *gin.Context, accountID uint, app model.IntegrationApp) gin.H {
appID := integrationAppID(app)
hooks, _ := h.svc.ListHooksForApp(c.Request.Context(), accountID, appID)
serializedHooks := make([]gin.H, 0, len(hooks))
for _, hook := range hooks {
serializedHooks = append(serializedHooks, serializeIntegrationHook(hook))
}
return gin.H{
"id": appID,
"name": app.Name,
"description": app.Description,
"short_description": app.Description,
"enabled": len(serializedHooks) > 0,
"action": app.ActionURL,
"button": app.ActionURL,
"hook_type": integrationAppHookType(appID),
"allow_multiple_hooks": integrationAppAllowsMultipleHooks(appID),
"settings_form_schema": integrationAppSettingsFormSchema(appID),
"visible_properties": integrationAppVisibleProperties(appID),
"hooks": serializedHooks,
}
}
func serializeIntegrationHook(hook model.IntegrationHook) gin.H {
settings := gin.H{}
if len(hook.Settings) > 0 {
_ = json.Unmarshal(hook.Settings, &settings)
}
payload := gin.H{
"id": hook.ID,
"app_id": integrationHookAppID(hook),
"status": hook.Status != model.HookStatusInactive,
"account_id": hook.AccountID,
"hook_type": integrationHookKind(hook),
"settings": settings,
}
if hook.ReferenceID != "" {
payload["reference_id"] = hook.ReferenceID
}
if hook.InboxID != nil && *hook.InboxID != 0 {
inbox := gin.H{"id": *hook.InboxID}
if hook.Inbox != nil && hook.Inbox.ID != 0 {
inbox["name"] = hook.Inbox.Name
}
payload["inbox"] = inbox
}
return payload
}
func integrationAppID(app model.IntegrationApp) string {
if app.HookType != "" {
return string(app.HookType)
}
return strings.ToLower(strings.ReplaceAll(app.Name, " ", "_"))
}
func integrationHookAppID(hook model.IntegrationHook) string {
if hook.AppID != "" {
return hook.AppID
}
return string(hook.HookType)
}
func integrationAppHookType(appID string) string {
if appID == "dialogflow" {
return "inbox"
}
return "account"
}
func integrationAppAllowsMultipleHooks(appID string) bool {
return appID == "webhook" || appID == "dashboard_apps" || appID == "dialogflow"
}
func integrationAppSettingsFormSchema(appID string) []gin.H {
schemas := map[string][]gin.H{
"openai": {
{"label": "API Key", "type": "text", "name": "api_key", "validation": "required"},
{"label": "Show label suggestions", "type": "checkbox", "name": "label_suggestion", "validation": ""},
},
"dialogflow": {
{"label": "Dialogflow Project ID", "type": "text", "name": "project_id", "validation": "required", "validationName": "Project Id"},
{"label": "Dialogflow Project Key File", "type": "textarea", "name": "credentials", "validation": "required|JSON", "validationName": "Credentials", "validation-messages": gin.H{"JSON": "Invalid JSON", "required": "Credentials is required"}},
{"label": "Dialogflow Region", "type": "select", "name": "region", "default": "global", "options": []gin.H{{"label": "Global - Default", "value": "global"}, {"label": "AS-NE1 - Tokyo, Japan", "value": "asia-northeast1"}, {"label": "AU-SE1 - Sydney, Australia", "value": "australia-southeast1"}, {"label": "EU-W1 - St. Ghislain, Belgium", "value": "europe-west1"}, {"label": "EU-W2 - London, England", "value": "europe-west2"}}},
{"label": "Language Code", "type": "select", "name": "language_code", "default": "en-US", "help": "Language code for Dialogflow agent. Use \"auto\" to detect from contact language.", "options": []gin.H{{"label": "Auto-detect from contact", "value": "auto"}, {"label": "English (US)", "value": "en-US"}, {"label": "English (UK)", "value": "en-GB"}, {"label": "Spanish (Spain)", "value": "es-ES"}, {"label": "Spanish (Latin America)", "value": "es-419"}, {"label": "French", "value": "fr-FR"}, {"label": "German", "value": "de-DE"}, {"label": "Portuguese (Brazil)", "value": "pt-BR"}, {"label": "Portuguese (Portugal)", "value": "pt-PT"}, {"label": "Italian", "value": "it-IT"}, {"label": "Japanese", "value": "ja-JP"}, {"label": "Korean", "value": "ko-KR"}, {"label": "Chinese (Simplified)", "value": "zh-CN"}, {"label": "Chinese (Traditional)", "value": "zh-TW"}, {"label": "Hindi", "value": "hi-IN"}, {"label": "Arabic", "value": "ar"}, {"label": "Russian", "value": "ru-RU"}, {"label": "Dutch", "value": "nl-NL"}, {"label": "Polish", "value": "pl-PL"}, {"label": "Turkish", "value": "tr-TR"}, {"label": "Thai", "value": "th-TH"}, {"label": "Vietnamese", "value": "vi-VN"}, {"label": "Indonesian", "value": "id-ID"}}},
},
"google_translate": {
{"label": "Google Cloud Project ID", "type": "text", "name": "project_id", "validation": "required", "validationName": "Project Id"},
{"label": "Google Cloud Project Key File", "type": "textarea", "name": "credentials", "validation": "required|JSON", "validationName": "Credentials", "validation-messages": gin.H{"JSON": "Invalid JSON", "required": "Credentials is required"}},
},
"dyte": {
{"label": "Organization ID", "type": "text", "name": "organization_id", "validation": "required"},
{"label": "API Key", "type": "text", "name": "api_key", "validation": "required"},
},
"leadsquared": {
{"label": "Access Key", "type": "text", "name": "access_key", "validation": "required"},
{"label": "Secret Key", "type": "text", "name": "secret_key", "validation": "required"},
{"label": "Endpoint URL", "type": "text", "name": "endpoint_url", "validation": "required|url"},
{"label": "App URL", "type": "text", "name": "app_url", "validation": "required|url"},
},
}
if schema, ok := schemas[appID]; ok {
return schema
}
return []gin.H{}
}
func integrationAppVisibleProperties(appID string) []string {
properties := map[string][]string{
"openai": {"api_key", "label_suggestion"},
"dialogflow": {"project_id", "region", "language_code"},
"google_translate": {"project_id"},
"dyte": {"organization_id"},
"leadsquared": {"access_key", "endpoint_url", "app_url"},
}
if visibleProperties, ok := properties[appID]; ok {
return visibleProperties
}
return []string{}
}
func integrationHookKind(hook model.IntegrationHook) string {
if hook.InboxID != nil && *hook.InboxID != 0 {
return "inbox"
}
return "account"
}
// RegisterIntegrationHookRoutes registers integration hook routes on a router group.
@@ -195,6 +321,7 @@ func RegisterIntegrationHookRoutes(g *gin.RouterGroup, h *IntegrationHookHandler
// Integration apps catalog
apps := g.Group("/apps")
{
apps.GET("", h.ListApps)
apps.GET("/", h.ListApps)
apps.GET("/:id", h.GetApp)
}
@@ -202,10 +329,13 @@ func RegisterIntegrationHookRoutes(g *gin.RouterGroup, h *IntegrationHookHandler
// Integration hooks CRUD + process event
hooks := g.Group("/hooks")
{
hooks.GET("", h.ListHooks)
hooks.GET("/", h.ListHooks)
hooks.GET("/:id", h.GetHook)
hooks.POST("", h.CreateHook)
hooks.POST("/", h.CreateHook)
hooks.PUT("/:id", h.UpdateHook)
hooks.PATCH("/:id", h.UpdateHook)
hooks.DELETE("/:id", h.DeleteHook)
hooks.POST("/:id/process_event", h.ProcessHookEvent)
}
@@ -53,20 +53,7 @@ func (s *IntegrationHookHandlerSuite) SetupSuite() {
r := gin.New()
r.Use(gin.Recovery(), mockAuthMiddlewareForHook())
g := r.Group("/api/v1/accounts/:account_id/integrations")
apps := g.Group("/apps")
{
apps.GET("/", s.handler.ListApps)
apps.GET("/:id", s.handler.GetApp)
}
hooks := g.Group("/hooks")
{
hooks.GET("/", s.handler.ListHooks)
hooks.GET("/:id", s.handler.GetHook)
hooks.POST("/", s.handler.CreateHook)
hooks.PUT("/:id", s.handler.UpdateHook)
hooks.DELETE("/:id", s.handler.DeleteHook)
hooks.POST("/:id/process_event", s.handler.ProcessHookEvent)
}
RegisterIntegrationHookRoutes(g, s.handler)
s.router = r
}
@@ -105,6 +92,7 @@ func (s *IntegrationHookHandlerSuite) createHook(accountID uint, hookType model.
token := "test_token_" + strconv.Itoa(hookTokenCounter)
hook := &model.IntegrationHook{
AccountID: accountID,
AppID: string(hookType),
HookType: hookType,
Status: model.HookStatusActive,
URL: url,
@@ -129,10 +117,10 @@ func (s *IntegrationHookHandlerSuite) TestListApps_Success() {
var body map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
s.Equal(true, body["success"])
data := body["data"].([]interface{})
data := body["payload"].([]interface{})
s.Len(data, 2)
app := data[0].(map[string]interface{})
s.Contains(app, "hooks")
}
func (s *IntegrationHookHandlerSuite) TestListApps_Empty() {
@@ -143,9 +131,7 @@ func (s *IntegrationHookHandlerSuite) TestListApps_Empty() {
var body map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
s.Equal(true, body["success"])
data := body["data"].([]interface{})
data := body["payload"].([]interface{})
s.Len(data, 0)
}
@@ -154,26 +140,24 @@ func (s *IntegrationHookHandlerSuite) TestListApps_Empty() {
// =====================
func (s *IntegrationHookHandlerSuite) TestGetApp_Success() {
app := s.createApp("Slack App", model.HookTypeSlack)
s.createApp("Slack App", model.HookTypeSlack)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/integrations/apps/"+s.uid(app.ID), nil)
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/integrations/apps/slack", nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
var body map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
s.Equal(true, body["success"])
data := body["data"].(map[string]interface{})
s.Equal("Slack App", data["name"])
s.Equal("slack", body["id"])
s.Equal("Slack App", body["name"])
}
func (s *IntegrationHookHandlerSuite) TestGetApp_InvalidID() {
func (s *IntegrationHookHandlerSuite) TestGetApp_UnknownID() {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/integrations/apps/abc", nil)
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/integrations/apps/unknown", nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
s.Equal(http.StatusNotFound, w.Code)
}
func (s *IntegrationHookHandlerSuite) TestGetApp_NotFound() {
@@ -232,11 +216,9 @@ func (s *IntegrationHookHandlerSuite) TestGetHook_Success() {
var body map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
s.Equal(true, body["success"])
data := body["data"].(map[string]interface{})
s.Equal("webhook", data["hook_type"])
s.Equal("https://example.com/hook", data["url"])
s.Equal("webhook", body["app_id"])
s.Equal("account", body["hook_type"])
s.Equal(true, body["status"])
}
func (s *IntegrationHookHandlerSuite) TestGetHook_NotFound() {
@@ -251,10 +233,12 @@ func (s *IntegrationHookHandlerSuite) TestGetHook_NotFound() {
// =====================
func (s *IntegrationHookHandlerSuite) TestCreateHook_Success() {
body, _ := json.Marshal(map[string]interface{}{"hook": map[string]string{
"hook_type": "webhook",
"url": "https://example.com/new_hook",
}})
body, _ := json.Marshal(map[string]interface{}{
"app_id": "webhook",
"settings": map[string]interface{}{
"project_id": "project-1",
},
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/integrations/hooks/", bytes.NewBuffer(body))
@@ -264,12 +248,12 @@ func (s *IntegrationHookHandlerSuite) TestCreateHook_Success() {
var resp map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
s.Equal(true, resp["success"])
data := resp["data"].(map[string]interface{})
s.Equal("webhook", data["hook_type"])
s.Equal("https://example.com/new_hook", data["url"])
s.NotNil(data["id"])
s.Equal("webhook", resp["app_id"])
s.Equal("account", resp["hook_type"])
s.Equal(true, resp["status"])
s.NotNil(resp["id"])
settings := resp["settings"].(map[string]interface{})
s.Equal("project-1", settings["project_id"])
}
func (s *IntegrationHookHandlerSuite) TestCreateHook_InvalidJSON() {
@@ -295,9 +279,12 @@ func (s *IntegrationHookHandlerSuite) TestCreateHook_InvalidAccountID() {
func (s *IntegrationHookHandlerSuite) TestUpdateHook_Success() {
hook := s.createHook(1, model.HookTypeWebhook, "https://example.com/hook")
body, _ := json.Marshal(map[string]interface{}{"hook": map[string]string{
"url": "https://example.com/updated_hook",
"status": "inactive",
body, _ := json.Marshal(map[string]interface{}{"hook": map[string]interface{}{
"status": "disabled",
"reference_id": "ref-123",
"settings": map[string]interface{}{
"channel": "support",
},
}})
w := httptest.NewRecorder()
@@ -308,11 +295,10 @@ func (s *IntegrationHookHandlerSuite) TestUpdateHook_Success() {
var resp map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
s.Equal(true, resp["success"])
data := resp["data"].(map[string]interface{})
s.Equal("https://example.com/updated_hook", data["url"])
s.Equal("inactive", data["status"])
s.Equal(false, resp["status"])
s.Equal("ref-123", resp["reference_id"])
settings := resp["settings"].(map[string]interface{})
s.Equal("support", settings["channel"])
}
func (s *IntegrationHookHandlerSuite) TestUpdateHook_NotFound() {
@@ -357,21 +343,14 @@ func (s *IntegrationHookHandlerSuite) TestDeleteHook_Success() {
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/integrations/hooks/"+s.uid(hook.ID), nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusOK, w.Code)
var resp map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
s.Equal(true, resp["success"])
data := resp["data"].(map[string]interface{})
s.Equal(float64(hook.ID), data["id"])
s.Empty(w.Body.String())
}
func (s *IntegrationHookHandlerSuite) TestDeleteHook_NotFound() {
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/integrations/hooks/9999", nil)
s.router.ServeHTTP(w, req)
// GORM soft-delete on non-existent ID returns nil error → handler returns 200 with {"id": 9999}
s.Equal(http.StatusOK, w.Code)
s.Equal(http.StatusNotFound, w.Code)
}
func (s *IntegrationHookHandlerSuite) TestDeleteHook_InvalidID() {
@@ -401,10 +380,7 @@ func (s *IntegrationHookHandlerSuite) TestProcessHookEvent_Success() {
var resp map[string]interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
s.Equal(true, resp["success"])
data := resp["data"].(map[string]interface{})
s.Equal("event processed", data["message"])
s.Equal("event processed", resp["message"])
}
func (s *IntegrationHookHandlerSuite) TestProcessHookEvent_NotFound() {
@@ -442,6 +418,7 @@ func (s *IntegrationHookHandlerSuite) TestProcessHookEvent_InactiveHook() {
hookTokenCounter++
hook := &model.IntegrationHook{
AccountID: 1,
AppID: "webhook",
HookType: model.HookTypeWebhook,
Status: model.HookStatusInactive,
URL: "https://example.com/hook",
@@ -462,4 +439,4 @@ func (s *IntegrationHookHandlerSuite) TestProcessHookEvent_InactiveHook() {
// TestIntegrationHookHandlerSuite runs the suite.
func TestIntegrationHookHandlerSuite(t *testing.T) {
suite.Run(t, new(IntegrationHookHandlerSuite))
}
}