feat(notifications): align chatwoot notification payloads
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -59,6 +60,7 @@ func setupNotificationRouter(handler *NotificationHandler) *gin.Engine {
|
||||
// G8 extension routes
|
||||
router.POST("/api/v1/accounts/:account_id/notifications/:notification_id/snooze", handler.Snooze)
|
||||
router.POST("/api/v1/accounts/:account_id/notifications/:notification_id/unread", handler.Unread)
|
||||
router.POST("/api/v1/accounts/:account_id/notifications/destroy_all", handler.DestroyAll)
|
||||
router.DELETE("/api/v1/accounts/:account_id/notifications/destroy_all", handler.DestroyAll)
|
||||
return router
|
||||
}
|
||||
@@ -91,6 +93,59 @@ func TestNotificationListEmpty(t *testing.T) {
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationHandler_List_ChatwootEnvelopeAndIncludes(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
user := &model.User{Name: "Envelope User", Email: "envelope@example.com", Password: "pass", AccountID: 1}
|
||||
require.NoError(t, db.Create(user).Error)
|
||||
accountID := uint(1)
|
||||
readAt := time.Now()
|
||||
snoozedUntil := time.Now().Add(time.Hour)
|
||||
unread := &model.Notification{UserID: user.ID, AccountID: &accountID, NotificationType: "message_created", PrimaryActorType: "Conversation", PrimaryActorID: 101, AdditionalAttributes: []byte(`{"push_message_title":"New message","push_message_body":"Hello"}`)}
|
||||
read := &model.Notification{UserID: user.ID, AccountID: &accountID, NotificationType: "conversation_assignment", PrimaryActorType: "Conversation", PrimaryActorID: 102, ReadAt: &readAt}
|
||||
snoozed := &model.Notification{UserID: user.ID, AccountID: &accountID, NotificationType: "conversation_mention", PrimaryActorType: "Conversation", PrimaryActorID: 103, SnoozedUntil: &snoozedUntil}
|
||||
require.NoError(t, db.Create(unread).Error)
|
||||
require.NoError(t, db.Create(read).Error)
|
||||
require.NoError(t, db.Create(snoozed).Error)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/notifications", nil)
|
||||
req.Header.Set("X-User-ID", strconv.FormatUint(uint64(user.ID), 10))
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
var body map[string]any
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
data := body["data"].(map[string]any)
|
||||
meta := data["meta"].(map[string]any)
|
||||
payload := data["payload"].([]any)
|
||||
assert.Equal(t, float64(1), meta["count"])
|
||||
assert.Equal(t, float64(1), meta["unread_count"])
|
||||
require.Len(t, payload, 1)
|
||||
item := payload[0].(map[string]any)
|
||||
assert.Equal(t, "New message", item["push_message_title"])
|
||||
assert.Equal(t, "Hello", item["push_message_body"])
|
||||
assert.Equal(t, float64(101), item["primary_actor_id"])
|
||||
assert.NotContains(t, body, "success")
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("GET", "/api/v1/accounts/1/notifications?includes[]=read&includes[]=snoozed", nil)
|
||||
req.Header.Set("X-User-ID", strconv.FormatUint(uint64(user.ID), 10))
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
data = body["data"].(map[string]any)
|
||||
meta = data["meta"].(map[string]any)
|
||||
payload = data["payload"].([]any)
|
||||
assert.Equal(t, float64(3), meta["count"])
|
||||
assert.Equal(t, float64(2), meta["unread_count"])
|
||||
assert.Len(t, payload, 3)
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationGet(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
@@ -155,11 +210,11 @@ func TestNotificationCRUDWithDB(t *testing.T) {
|
||||
|
||||
// Create notification
|
||||
notif := &model.Notification{
|
||||
UserID: user.ID,
|
||||
AccountID: uintPtr(1),
|
||||
NotificationType: "conversation_assignment",
|
||||
PrimaryActorType: "conversation",
|
||||
PrimaryActorID: 5,
|
||||
UserID: user.ID,
|
||||
AccountID: uintPtr(1),
|
||||
NotificationType: "conversation_assignment",
|
||||
PrimaryActorType: "conversation",
|
||||
PrimaryActorID: 5,
|
||||
SecondaryActorType: "user",
|
||||
SecondaryActorID: 3,
|
||||
}
|
||||
@@ -201,11 +256,11 @@ func TestNotificationReadAllWithDB(t *testing.T) {
|
||||
// Create multiple unread notifications
|
||||
for i := 0; i < 5; i++ {
|
||||
notif := &model.Notification{
|
||||
UserID: user.ID,
|
||||
AccountID: uintPtr(1),
|
||||
NotificationType: "message_created",
|
||||
PrimaryActorType: "message",
|
||||
PrimaryActorID: uint(i + 1),
|
||||
UserID: user.ID,
|
||||
AccountID: uintPtr(1),
|
||||
NotificationType: "message_created",
|
||||
PrimaryActorType: "message",
|
||||
PrimaryActorID: uint(i + 1),
|
||||
}
|
||||
err := db.Create(notif).Error
|
||||
assert.NoError(t, err)
|
||||
@@ -244,11 +299,11 @@ func TestNotificationTypes(t *testing.T) {
|
||||
|
||||
func TestNotificationModelFields(t *testing.T) {
|
||||
notif := model.Notification{
|
||||
UserID: 1,
|
||||
AccountID: uintPtr(2),
|
||||
NotificationType: "conversation_assignment",
|
||||
PrimaryActorType: "conversation",
|
||||
PrimaryActorID: 5,
|
||||
UserID: 1,
|
||||
AccountID: uintPtr(2),
|
||||
NotificationType: "conversation_assignment",
|
||||
PrimaryActorType: "conversation",
|
||||
PrimaryActorID: 5,
|
||||
SecondaryActorType: "user",
|
||||
SecondaryActorID: 3,
|
||||
}
|
||||
@@ -547,4 +602,66 @@ func TestNotificationHandler_DestroyAllWithDB(t *testing.T) {
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationHandler_DestroyAll_ReadOnly(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
user := &model.User{Name: "DestroyRead User", Email: "destroy-read@example.com", Password: "pass", AccountID: 1}
|
||||
require.NoError(t, db.Create(user).Error)
|
||||
accountID := uint(1)
|
||||
readAt := time.Now()
|
||||
readNotification := &model.Notification{UserID: user.ID, AccountID: &accountID, NotificationType: "read", PrimaryActorType: "conversation", PrimaryActorID: 1, ReadAt: &readAt}
|
||||
unreadNotification := &model.Notification{UserID: user.ID, AccountID: &accountID, NotificationType: "unread", PrimaryActorType: "conversation", PrimaryActorID: 2}
|
||||
require.NoError(t, db.Create(readNotification).Error)
|
||||
require.NoError(t, db.Create(unreadNotification).Error)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/notifications/destroy_all", strings.NewReader(`{"type":"read"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-User-ID", strconv.FormatUint(uint64(user.ID), 10))
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var count int64
|
||||
require.NoError(t, db.Model(&model.Notification{}).Where("id = ?", readNotification.ID).Count(&count).Error)
|
||||
assert.Equal(t, int64(0), count)
|
||||
require.NoError(t, db.Model(&model.Notification{}).Where("id = ?", unreadNotification.ID).Count(&count).Error)
|
||||
assert.Equal(t, int64(1), count)
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationHandler_MarkAllRead_PrimaryActorOnly(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
user := &model.User{Name: "Actor Read User", Email: "actor-read@example.com", Password: "pass", AccountID: 1}
|
||||
require.NoError(t, db.Create(user).Error)
|
||||
accountID := uint(1)
|
||||
matching := &model.Notification{UserID: user.ID, AccountID: &accountID, NotificationType: "message_created", PrimaryActorType: "Conversation", PrimaryActorID: 1}
|
||||
other := &model.Notification{UserID: user.ID, AccountID: &accountID, NotificationType: "message_created", PrimaryActorType: "Conversation", PrimaryActorID: 2}
|
||||
require.NoError(t, db.Create(matching).Error)
|
||||
require.NoError(t, db.Create(other).Error)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/notifications/read_all", strings.NewReader(`{"primary_actor_type":"Conversation","primary_actor_id":1}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-User-ID", strconv.FormatUint(uint64(user.ID), 10))
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var matchingReloaded model.Notification
|
||||
require.NoError(t, db.First(&matchingReloaded, matching.ID).Error)
|
||||
assert.NotNil(t, matchingReloaded.ReadAt)
|
||||
var otherReloaded model.Notification
|
||||
require.NoError(t, db.First(&otherReloaded, other.ID).Error)
|
||||
assert.Nil(t, otherReloaded.ReadAt)
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user