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.
201 lines
7.2 KiB
Go
201 lines
7.2 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// WebhookSubscriptionHandler handles webhook subscription CRUD + delivery history.
|
|
// Reference: Chatwoot webhook integration API + P2B M8 spec
|
|
type WebhookSubscriptionHandler struct {
|
|
webhookSubscriptionService *service.WebhookSubscriptionService
|
|
}
|
|
|
|
// NewWebhookSubscriptionHandler creates a new WebhookSubscription handler with injected service.
|
|
func NewWebhookSubscriptionHandler(webhookSubscriptionService *service.WebhookSubscriptionService) *WebhookSubscriptionHandler {
|
|
return &WebhookSubscriptionHandler{webhookSubscriptionService: webhookSubscriptionService}
|
|
}
|
|
|
|
// List returns all webhook subscriptions for an account.
|
|
// GET /api/v1/accounts/:account_id/webhook_subscriptions
|
|
func (h *WebhookSubscriptionHandler) List(c *gin.Context) {
|
|
if h.webhookSubscriptionService == nil {
|
|
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Webhook subscription service not available")
|
|
return
|
|
}
|
|
accountID := getAccountID(c)
|
|
|
|
subscriptions, err := h.webhookSubscriptionService.ListSubscriptions(c.Request.Context(), accountID)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to fetch webhook subscriptions")
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"payload": gin.H{"webhooks": serializeWebhookSubscriptions(subscriptions)}})
|
|
}
|
|
|
|
// Get returns a single webhook subscription by ID.
|
|
// GET /api/v1/accounts/:account_id/webhooks/:webhook_id
|
|
func (h *WebhookSubscriptionHandler) Get(c *gin.Context) {
|
|
if h.webhookSubscriptionService == nil {
|
|
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Webhook subscription service not available")
|
|
return
|
|
}
|
|
accountID := getAccountID(c)
|
|
webhookID, err := parseUintParam(c, "webhook_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid webhook id")
|
|
return
|
|
}
|
|
|
|
subscription, svcErr := h.webhookSubscriptionService.GetWebhook(c.Request.Context(), accountID, webhookID)
|
|
if svcErr != nil {
|
|
abortWebhookSubscriptionError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"payload": gin.H{"webhook": serializeWebhookSubscription(*subscription)}})
|
|
}
|
|
|
|
// Create adds a new webhook subscription for an account.
|
|
// POST /api/v1/accounts/:account_id/webhook_subscriptions
|
|
func (h *WebhookSubscriptionHandler) Create(c *gin.Context) {
|
|
if h.webhookSubscriptionService == nil {
|
|
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Webhook subscription service not available")
|
|
return
|
|
}
|
|
accountID := getAccountID(c)
|
|
|
|
var req service.WebhookSubscriptionMutation
|
|
if err := bindJSONWrappedOrRaw(c, "webhook", &req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid request body")
|
|
return
|
|
}
|
|
|
|
subscription, err := h.webhookSubscriptionService.CreateWebhook(c.Request.Context(), accountID, req)
|
|
if err != nil {
|
|
abortWebhookSubscriptionError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"payload": gin.H{"webhook": serializeWebhookSubscription(*subscription)}})
|
|
}
|
|
|
|
// Update modifies a webhook subscription.
|
|
// PUT /api/v1/accounts/:account_id/webhook_subscriptions/:id
|
|
func (h *WebhookSubscriptionHandler) Update(c *gin.Context) {
|
|
if h.webhookSubscriptionService == nil {
|
|
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Webhook subscription service not available")
|
|
return
|
|
}
|
|
accountID := getAccountID(c)
|
|
id, err := parseUintAnyParam(c, "webhook_id", "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid webhook subscription ID")
|
|
return
|
|
}
|
|
|
|
var req service.WebhookSubscriptionMutation
|
|
if err := bindJSONWrappedOrRaw(c, "webhook", &req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid request body")
|
|
return
|
|
}
|
|
|
|
subscription, err := h.webhookSubscriptionService.UpdateWebhook(c.Request.Context(), accountID, id, req)
|
|
if err != nil {
|
|
abortWebhookSubscriptionError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"payload": gin.H{"webhook": serializeWebhookSubscription(*subscription)}})
|
|
}
|
|
|
|
// Delete removes a webhook subscription.
|
|
// DELETE /api/v1/accounts/:account_id/webhook_subscriptions/:id
|
|
func (h *WebhookSubscriptionHandler) Delete(c *gin.Context) {
|
|
if h.webhookSubscriptionService == nil {
|
|
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Webhook subscription service not available")
|
|
return
|
|
}
|
|
accountID := getAccountID(c)
|
|
id, err := parseUintAnyParam(c, "webhook_id", "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid webhook subscription ID")
|
|
return
|
|
}
|
|
|
|
if err := h.webhookSubscriptionService.DeleteWebhook(c.Request.Context(), accountID, id); err != nil {
|
|
abortWebhookSubscriptionError(c, err)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|
|
|
|
// ListDeliveries returns recent webhook delivery records for a subscription.
|
|
// GET /api/v1/accounts/:account_id/webhook_subscriptions/:id/deliveries
|
|
func (h *WebhookSubscriptionHandler) ListDeliveries(c *gin.Context) {
|
|
if h.webhookSubscriptionService == nil {
|
|
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Webhook subscription service not available")
|
|
return
|
|
}
|
|
id, err := parseUintParam(c, "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid webhook subscription ID")
|
|
return
|
|
}
|
|
|
|
deliveries, err := h.webhookSubscriptionService.ListDeliveries(c.Request.Context(), id, 50)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to fetch webhook deliveries")
|
|
return
|
|
}
|
|
|
|
response.OK(c, gin.H{"deliveries": deliveries})
|
|
}
|
|
|
|
func abortWebhookSubscriptionError(c *gin.Context, err error) {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "webhook not found")
|
|
return
|
|
}
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, response.ErrValidation, err.Error())
|
|
}
|
|
|
|
func serializeWebhookSubscriptions(subscriptions []model.WebhookSubscription) []gin.H {
|
|
items := make([]gin.H, 0, len(subscriptions))
|
|
for _, subscription := range subscriptions {
|
|
items = append(items, serializeWebhookSubscription(subscription))
|
|
}
|
|
return items
|
|
}
|
|
|
|
func serializeWebhookSubscription(subscription model.WebhookSubscription) gin.H {
|
|
var subscriptions []string
|
|
_ = json.Unmarshal(subscription.Events, &subscriptions)
|
|
payload := gin.H{
|
|
"id": subscription.ID,
|
|
"name": subscription.Name,
|
|
"url": subscription.URL,
|
|
"account_id": subscription.AccountID,
|
|
"subscriptions": subscriptions,
|
|
"secret": subscription.Secret,
|
|
}
|
|
if subscription.InboxID != nil && *subscription.InboxID != 0 {
|
|
inbox := gin.H{"id": *subscription.InboxID}
|
|
if subscription.Inbox.ID != 0 {
|
|
inbox["name"] = subscription.Inbox.Name
|
|
}
|
|
payload["inbox"] = inbox
|
|
}
|
|
return payload
|
|
}
|