Align GoChat with Chatwoot frontend contracts
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
"github.com/gochat/gochat/internal/ws"
|
||||
applogger "github.com/gochat/gochat/pkg/logger"
|
||||
"github.com/gochat/gochat/pkg/pagination"
|
||||
"github.com/gochat/gochat/pkg/response"
|
||||
@@ -23,6 +24,7 @@ import (
|
||||
// Reference: Chatwoot app/controllers/api/v1/accounts/notifications_controller.rb
|
||||
type NotificationHandler struct {
|
||||
notificationService *service.NotificationService
|
||||
eventPublisher *ws.EventPublisher
|
||||
}
|
||||
|
||||
// NewNotificationHandler creates a new Notification handler with injected service.
|
||||
@@ -30,6 +32,11 @@ func NewNotificationHandler(notificationService *service.NotificationService) *N
|
||||
return &NotificationHandler{notificationService: notificationService}
|
||||
}
|
||||
|
||||
func (h *NotificationHandler) WithEventPublisher(publisher *ws.EventPublisher) *NotificationHandler {
|
||||
h.eventPublisher = publisher
|
||||
return h
|
||||
}
|
||||
|
||||
// List returns all notifications for the current user in an account.
|
||||
// GET /api/v1/accounts/:account_id/notifications
|
||||
// Reference: Chatwoot index — NotificationFinder with pagination
|
||||
@@ -113,6 +120,7 @@ func (h *NotificationHandler) Update(c *gin.Context) {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
h.publishNotificationEvent(c.Request.Context(), accountID, userID, ws.EventNotificationUpdated, notification)
|
||||
|
||||
c.JSON(http.StatusOK, h.serializeNotification(c.Request.Context(), notification))
|
||||
}
|
||||
@@ -202,6 +210,7 @@ func (h *NotificationHandler) Snooze(c *gin.Context) {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
h.publishNotificationEvent(c.Request.Context(), accountID, userID, ws.EventNotificationUpdated, notification)
|
||||
|
||||
c.JSON(http.StatusOK, h.serializeNotification(c.Request.Context(), notification))
|
||||
}
|
||||
@@ -224,6 +233,7 @@ func (h *NotificationHandler) Unread(c *gin.Context) {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
h.publishNotificationEvent(c.Request.Context(), accountID, userID, ws.EventNotificationUpdated, notification)
|
||||
|
||||
c.JSON(http.StatusOK, h.serializeNotification(c.Request.Context(), notification))
|
||||
}
|
||||
@@ -240,10 +250,16 @@ func (h *NotificationHandler) Destroy(c *gin.Context) {
|
||||
|
||||
accountID := getAccountID(c)
|
||||
userID := getUserID(c)
|
||||
notification, svcErr := h.notificationService.GetNotificationByAccount(c.Request.Context(), notificationID, userID, accountID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
if svcErr := h.notificationService.DeleteNotificationByAccount(c.Request.Context(), notificationID, userID, accountID); svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
h.publishNotificationEvent(c.Request.Context(), accountID, userID, ws.EventNotificationDeleted, notification)
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
@@ -290,6 +306,27 @@ func notificationIncludes(c *gin.Context, value string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *NotificationHandler) publishNotificationEvent(ctx context.Context, accountID, userID uint, eventType string, notification *model.Notification) {
|
||||
if h.eventPublisher == nil || notification == nil || accountID == 0 || userID == 0 {
|
||||
return
|
||||
}
|
||||
unreadCount, err := h.notificationService.GetUnreadCountByAccount(ctx, userID, accountID)
|
||||
if err != nil {
|
||||
applogger.L().Warnf("notification event %s unread count: %v", eventType, err)
|
||||
return
|
||||
}
|
||||
total, err := h.notificationService.CountNotificationsByAccount(ctx, userID, accountID)
|
||||
if err != nil {
|
||||
applogger.L().Warnf("notification event %s total count: %v", eventType, err)
|
||||
return
|
||||
}
|
||||
h.eventPublisher.PublishEvent(accountID, eventType, gin.H{
|
||||
"notification": h.serializeNotification(ctx, notification),
|
||||
"unread_count": unreadCount,
|
||||
"count": total,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *NotificationHandler) serializeNotification(ctx context.Context, notification *model.Notification) gin.H {
|
||||
var db *gorm.DB
|
||||
if h != nil && h.notificationService != nil {
|
||||
|
||||
Reference in New Issue
Block a user