second commit
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"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
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"webhook_subscriptions": 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
|
||||
}
|
||||
webhookID, err := parseUintParam(c, "webhook_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid webhook id")
|
||||
return
|
||||
}
|
||||
|
||||
subscription, svcErr := h.webhookSubscriptionService.GetSubscription(c.Request.Context(), webhookID)
|
||||
if svcErr != nil {
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to fetch webhook subscription")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"webhook_subscription": 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 struct {
|
||||
URL string `json:"url" binding:"required"`
|
||||
Events []string `json:"events" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid request: url and events are required")
|
||||
return
|
||||
}
|
||||
|
||||
subscription, err := h.webhookSubscriptionService.CreateSubscription(c.Request.Context(), accountID, req.URL, req.Events)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to create webhook subscription")
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, 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
|
||||
}
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid webhook subscription ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
URL string `json:"url"`
|
||||
Events []string `json:"events"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
subscription, err := h.webhookSubscriptionService.UpdateSubscription(c.Request.Context(), id, req.URL, req.Events, req.Active)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to update webhook subscription")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, 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
|
||||
}
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid webhook subscription ID")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.webhookSubscriptionService.DeleteSubscription(c.Request.Context(), id); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to delete webhook subscription")
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(c)
|
||||
}
|
||||
|
||||
// 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})
|
||||
}
|
||||
Reference in New Issue
Block a user