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

105 lines
3.3 KiB
Go

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"
)
// NotificationSubscriptionHandler handles notification subscription API endpoints.
// Reference: Chatwoot app/controllers/api/v1/notification_subscriptions_controller.rb
// Routes: resource :notification_subscriptions, only: [:create, :destroy]
type NotificationSubscriptionHandler struct {
svc *service.NotificationSubscriptionService
}
func NewNotificationSubscriptionHandler(svc *service.NotificationSubscriptionService) *NotificationSubscriptionHandler {
return &NotificationSubscriptionHandler{svc: svc}
}
// Create adds a new notification subscription.
// POST /api/v1/notification_subscriptions
// Chatwoot: requires identifier, subscription_attributes, subscription_type
func (h *NotificationSubscriptionHandler) Create(c *gin.Context) {
userID := getUserID(c)
if userID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
var req service.CreateSubscriptionRequest
if err := bindJSONWrappedOrRaw(c, "notification_subscription", &req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Validate subscription_attributes based on type
if err := service.ValidateSubscriptionAttributes(req.SubscriptionType, req.SubscriptionAttributes); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
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, 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 := getUserID(c)
if userID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
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
}
_ = h.svc.Destroy(c.Request.Context(), userID, pushToken)
c.Status(http.StatusOK)
}
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,
}
}