feat(notifications): align subscription payloads

This commit is contained in:
2026-06-06 13:58:35 +08:00
parent 218eec92ac
commit 0ed1cfdee5
9 changed files with 265 additions and 44 deletions
@@ -1,9 +1,12 @@
package v1
import (
"encoding/json"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/service"
)
@@ -22,14 +25,14 @@ func NewNotificationSubscriptionHandler(svc *service.NotificationSubscriptionSer
// POST /api/v1/notification_subscriptions
// Chatwoot: requires identifier, subscription_attributes, subscription_type
func (h *NotificationSubscriptionHandler) Create(c *gin.Context) {
userID, exists := c.Get("current_user_id")
if !exists {
userID := getUserID(c)
if userID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
var req service.CreateSubscriptionRequest
if err := c.ShouldBindJSON(&req); err != nil {
if err := bindJSONWrappedOrRaw(c, "notification_subscription", &req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
@@ -40,35 +43,62 @@ func (h *NotificationSubscriptionHandler) Create(c *gin.Context) {
return
}
sub, err := h.svc.Create(c.Request.Context(), userID.(uint), &req)
sub, err := h.svc.Create(c.Request.Context(), userID, &req)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, sub)
c.JSON(http.StatusOK, notificationSubscriptionPayloadFromModel(sub))
}
// Destroy removes a notification subscription.
// DELETE /api/v1/notification_subscriptions/:identifier
// Chatwoot: finds by identifier and deletes
func (h *NotificationSubscriptionHandler) Destroy(c *gin.Context) {
userID, exists := c.Get("current_user_id")
if !exists {
userID := getUserID(c)
if userID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
identifier := c.Param("identifier")
if identifier == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "identifier is required"})
return
pushToken := c.Query("push_token")
if pushToken == "" {
pushToken = c.Param("identifier")
}
if pushToken == "" {
pushToken = c.PostForm("push_token")
}
if pushToken == "" && c.Request.Body != nil {
var body struct {
PushToken string `json:"push_token"`
}
_ = c.ShouldBindJSON(&body)
pushToken = body.PushToken
}
if err := h.svc.Destroy(c.Request.Context(), userID.(uint), identifier); err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
_ = h.svc.Destroy(c.Request.Context(), userID, pushToken)
c.Status(http.StatusOK)
}
c.JSON(http.StatusOK, gin.H{})
}
type notificationSubscriptionDTO struct {
ID uint `json:"id"`
Identifier string `json:"identifier"`
SubscriptionAttributes json.RawMessage `json:"subscription_attributes"`
SubscriptionType string `json:"subscription_type"`
UserID uint `json:"user_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func notificationSubscriptionPayloadFromModel(sub *model.NotificationSubscription) notificationSubscriptionDTO {
return notificationSubscriptionDTO{
ID: sub.ID,
Identifier: sub.Identifier,
SubscriptionAttributes: sub.SubscriptionAttributes,
SubscriptionType: sub.SubscriptionType.String(),
UserID: sub.UserID,
CreatedAt: sub.CreatedAt,
UpdatedAt: sub.UpdatedAt,
}
}